From ae409bb00c3d12573da8124833644aaa3d6cf962 Mon Sep 17 00:00:00 2001 From: John Yao Date: Fri, 21 Aug 2020 14:21:45 +1000 Subject: [PATCH] Fix #490 add admin setting for admin to control the fields for "Identify student by" --- classes/structure.php | 2 +- export.php | 62 +++++++++++++++++------------- export_form.php | 38 +++++++++++++----- lang/en/attendance.php | 5 +++ settings.php | 18 +++++++++ tests/behat/attendance_mod.feature | 52 +++++++++++++++++++++++-- 6 files changed, 135 insertions(+), 42 deletions(-) diff --git a/classes/structure.php b/classes/structure.php index 87e0e86..43ab8a2 100644 --- a/classes/structure.php +++ b/classes/structure.php @@ -801,7 +801,7 @@ class mod_attendance_structure { public function get_users($groupid = 0, $page = 1) : array { global $DB; - $fields = array('username' , 'idnumber' , 'institution' , 'department'); + $fields = array('username' , 'idnumber' , 'institution' , 'department', 'city', 'country'); // Get user identity fields if required - doesn't return original $fields array. $extrafields = get_extra_user_fields($this->context, $fields); $fields = array_merge($fields, $extrafields); diff --git a/export.php b/export.php index df6eaa3..661c976 100644 --- a/export.php +++ b/export.php @@ -89,26 +89,30 @@ if ($formdata = $mform->get_data()) { $data->course = $att->course->fullname; $data->group = $group ? $group->name : get_string('allparticipants'); - if (isset($formdata->ident['id'])) { - $data->tabhead[] = get_string('studentid', 'attendance'); - } - if (isset($formdata->ident['uname'])) { - $data->tabhead[] = get_string('username'); - } - - $optional = array('idnumber', 'institution', 'department'); - foreach ($optional as $opt) { - if (isset($formdata->ident[$opt])) { - $data->tabhead[] = get_string($opt); - } - } - $data->tabhead[] = get_string('lastname'); $data->tabhead[] = get_string('firstname'); $groupmode = groups_get_activity_groupmode($cm, $course); if (!empty($groupmode)) { $data->tabhead[] = get_string('groups'); } + require_once($CFG->dirroot . '/user/profile/lib.php'); + $customfields = profile_get_custom_fields(false); + + if (isset($formdata->ident)) { + foreach (array_keys($formdata->ident) as $opt) { + if ($opt == 'id') { + $data->tabhead[] = get_string('studentid', 'attendance'); + } else if (in_array($opt, array_column($customfields, 'shortname'))) { + foreach ($customfields as $customfield) { + if ($opt == $customfield->shortname) { + $data->tabhead[] = $customfield->name; + } + } + } else { + $data->tabhead[] = get_string($opt); + } + } + } if (count($reportdata->sessions) > 0) { foreach ($reportdata->sessions as $sess) { @@ -144,19 +148,7 @@ if ($formdata = $mform->get_data()) { $i = 0; $data->table = array(); foreach ($reportdata->users as $user) { - if (isset($formdata->ident['id'])) { - $data->table[$i][] = $user->id; - } - if (isset($formdata->ident['uname'])) { - $data->table[$i][] = $user->username; - } - - $optionalrow = array('idnumber', 'institution', 'department'); - foreach ($optionalrow as $opt) { - if (isset($formdata->ident[$opt])) { - $data->table[$i][] = $user->$opt; - } - } + profile_load_custom_fields($user); $data->table[$i][] = $user->lastname; $data->table[$i][] = $user->firstname; @@ -169,6 +161,22 @@ if ($formdata = $mform->get_data()) { } $data->table[$i][] = implode(', ', $groups); } + + if (isset($formdata->ident)) { + foreach (array_keys($formdata->ident) as $opt) { + if (in_array($opt, array_column($customfields, 'shortname'))) { + if (isset($user->profile[$opt])) { + $data->table[$i][] = $user->profile[$opt]; + } else { + $data->table[$i][] = ''; + } + continue; + } + + $data->table[$i][] = $user->$opt; + } + } + $cellsgenerator = new user_sessions_cells_text_generator($reportdata, $user); $data->table[$i] = array_merge($data->table[$i], $cellsgenerator->get_cells(isset($formdata->includeremarks))); diff --git a/export_form.php b/export_form.php index 2a7b280..a9adc5e 100644 --- a/export_form.php +++ b/export_form.php @@ -39,7 +39,7 @@ class mod_attendance_export_form extends moodleform { * @return void */ public function definition() { - global $USER, $DB, $PAGE; + global $USER, $DB, $PAGE, $CFG; $mform =& $this->_form; $course = $this->_customdata['course']; $cm = $this->_customdata['cm']; @@ -102,19 +102,37 @@ class mod_attendance_export_form extends moodleform { $PAGE->requires->yui_module('moodle-mod_attendance-groupfilter', 'M.mod_attendance.groupfilter.init', array($opts)); $ident = array(); - $ident[] =& $mform->createElement('checkbox', 'id', '', get_string('studentid', 'attendance')); - $ident[] =& $mform->createElement('checkbox', 'uname', '', get_string('username')); + $checkedfields = array(); + $adminsetfields = get_config('attendance', 'defaultexportfields'); + if (in_array('id', explode(',', $adminsetfields))) { + $ident[] =& $mform->createElement('checkbox', 'id', '', get_string('studentid', 'attendance')); + $checkedfields['ident[id]'] = true; + } + + $extrafields = get_extra_user_fields($modcontext); + foreach ($extrafields as $field) { + $ident[] =& $mform->createElement('checkbox', $field, '', get_string( $field)); + $mform->setType($field, PARAM_NOTAGS); + $checkedfields['ident['. $field .']'] = true; + } - $optional = array('idnumber', 'institution', 'department'); - foreach ($optional as $opt) { - $ident[] =& $mform->createElement('checkbox', $opt, '', get_string($opt)); - $mform->setType($opt, PARAM_NOTAGS); + require_once($CFG->dirroot . '/user/profile/lib.php'); + $customfields = profile_get_custom_fields(); + + foreach ($customfields as $field) { + if ((is_siteadmin($USER) || $field->visible == PROFILE_VISIBLE_ALL) + && in_array($field->shortname, explode(',', $adminsetfields))) { + $ident[] =& $mform->createElement('checkbox', $field->shortname, '', $field->name); + $mform->setType($field->shortname, PARAM_NOTAGS); + $checkedfields['ident['. $field->shortname .']'] = true; + } } - $mform->addGroup($ident, 'ident', get_string('identifyby', 'attendance'), array('
'), true); - $mform->setDefaults(array('ident[id]' => true, 'ident[uname]' => true)); + if (count($ident) > 0) { + $mform->addGroup($ident, 'ident', get_string('identifyby', 'attendance'), array('
'), true); + $mform->setDefaults($checkedfields); + } $mform->setType('id', PARAM_INT); - $mform->setType('uname', PARAM_INT); $mform->addElement('checkbox', 'includeallsessions', get_string('includeall', 'attendance'), get_string('yes')); $mform->setDefault('includeallsessions', true); diff --git a/lang/en/attendance.php b/lang/en/attendance.php index ad984e5..8e34e76 100644 --- a/lang/en/attendance.php +++ b/lang/en/attendance.php @@ -615,3 +615,8 @@ $string['warningupdated'] = 'Updated warnings'; $string['week'] = 'week(s)'; $string['weeks'] = 'Weeks'; $string['youcantdo'] = 'You can\'t do anything'; + +$string['defaultexportsettings'] = 'Default export settings'; +$string['defaultexportsettings_help'] = 'These settings define the defaults for sessions export'; +$string['defaultexportfields'] = 'Default export fields'; +$string['defaultexportfields_help'] = 'Fields that can be used to identify student in export report (If you are seeing less options than expected, please check show user identity in User policies).'; diff --git a/settings.php b/settings.php index 534f865..7e5b4c8 100644 --- a/settings.php +++ b/settings.php @@ -192,4 +192,22 @@ if ($ADMIN->fulltree) { get_string('emailcontent', 'attendance'), get_string('emailcontent_help', 'attendance'), get_string('emailcontent_default', 'attendance'), PARAM_RAW)); + $name = new lang_string('defaultexportsettings', 'mod_attendance'); + $description = new lang_string('defaultexportsettings_help', 'mod_attendance'); + $settings->add(new admin_setting_heading('defaultexportsettings', $name, $description)); + + $fields = array('id' => get_string('studentid', 'attendance')); + + require_once($CFG->dirroot . '/user/profile/lib.php'); + $customfields = profile_get_custom_fields(); + foreach ($customfields as $field) { + $fields[$field->shortname] = $field->name; + } + + $settings->add(new admin_setting_configmultiselect('attendance/defaultexportfields', + new lang_string('defaultexportfields', 'attendance'), + new lang_string('defaultexportfields_help', 'attendance'), + array('id'), $fields) + ); + } diff --git a/tests/behat/attendance_mod.feature b/tests/behat/attendance_mod.feature index d42db80..894287f 100644 --- a/tests/behat/attendance_mod.feature +++ b/tests/behat/attendance_mod.feature @@ -100,7 +100,53 @@ Feature: Teachers and Students can record session attendance Then "Attendance report viewed" "link" should exist Scenario: Export report includes id number, department and institution - Given I log in as "teacher1" + Given I log in as "admin" + And I navigate to "Users > Permissions > User policies" in site administration + And the following config values are set as admin: + | showuseridentity | idnumber,email,phone1,phone2,department,institution | + + And I log out + And I log in as "teacher1" + And I am on "Course 1" course homepage + And I follow "Attendance" + And I follow "Add" + And I set the following fields to these values: + | id_sestime_starthour | 01 | + | id_sestime_endhour | 02 | + And I click on "id_submitbutton" "button" + And I follow "Export" + Then the field "id_ident_idnumber" matches value "1" + And the field "id_ident_institution" matches value "1" + And the field "id_ident_department" matches value "1" + + Scenario: Test enabling custom user profile field + # Add custom field. + Given I log in as "admin" + And I navigate to "Users > Accounts > User profile fields" in site administration + And I set the field "datatype" to "Text input" + And I set the following fields to these values: + | Short name | superfield | + | Name | Super field | + And I click on "Save changes" "button" + + And I navigate to "Plugins > Activity modules > Attendance" in site administration + And the "Default export fields" select box should contain "Super field" + + Scenario: Test adding custom user profile + # Add custom field. + Given I log in as "admin" + And I navigate to "Users > Accounts > User profile fields" in site administration + And I set the field "datatype" to "Text input" + And I set the following fields to these values: + | Short name | superfield | + | Name | Super field | + And I click on "Save changes" "button" + + And the following config values are set as admin: + | defaultexportfields | superfield | attendance | + + And I log out + And I log in as "teacher1" And I am on "Course 1" course homepage And I follow "Attendance" And I follow "Add" @@ -109,9 +155,7 @@ Feature: Teachers and Students can record session attendance | id_sestime_endhour | 02 | And I click on "id_submitbutton" "button" And I follow "Export" - Then the field "id_ident_idnumber" matches value "" - And the field "id_ident_institution" matches value "" - And the field "id_ident_department" matches value "" + Then the field "id_ident_superfield" matches value "1" # Removed dependency on behat_download to allow automated Travis CI tests to pass. # It would be good to add these back at some point.