Commit 987e5545 authored by sam marshall's avatar sam marshall
Browse files

MDL-45242 Course: Enrol feature supports custom profile fields

parent e86ead1d
......@@ -88,7 +88,8 @@ class award_criteria_profile extends award_criteria {
if (in_array($field, $existing)) {
$checked = true;
}
$this->config_options($mform, array('id' => $field, 'checked' => $checked, 'name' => \core\user_fields::get_display_name($field), 'error' => false));
$this->config_options($mform, array('id' => $field, 'checked' => $checked,
'name' => \core\user_fields::get_display_name($field), 'error' => false));
$none = false;
}
}
......
......@@ -558,10 +558,20 @@ class core_enrol_external extends external_api {
$results = array();
// Add also extra user fields.
$identityfields = \core\user_fields::get_identity_fields($context, true);
$customprofilefields = [];
foreach ($identityfields as $key => $value) {
if ($fieldname = \core\user_fields::match_custom_field($value)) {
unset($identityfields[$key]);
$customprofilefields[$fieldname] = true;
}
}
if ($customprofilefields) {
$identityfields[] = 'customfields';
}
$requiredfields = array_merge(
['id', 'fullname', 'profileimageurl', 'profileimageurlsmall'],
// TODO Does not support custom user profile fields (MDL-70456).
\core\user_fields::get_identity_fields($context, false)
$identityfields
);
foreach ($users['users'] as $id => $user) {
// Note: We pass the course here to validate that the current user can at least view user details in this course.
......@@ -569,6 +579,15 @@ class core_enrol_external extends external_api {
// user records, and the user has been validated to have course:enrolreview in this course. Otherwise
// there is no way to find users who aren't in the course in order to enrol them.
if ($userdetails = user_get_user_details($user, $course, $requiredfields)) {
// For custom fields, only return the ones we actually need.
if ($customprofilefields && array_key_exists('customfields', $userdetails)) {
foreach ($userdetails['customfields'] as $key => $data) {
if (!array_key_exists($data['shortname'], $customprofilefields)) {
unset($userdetails['customfields'][$key]);
}
}
$userdetails['customfields'] = array_values($userdetails['customfields']);
}
$results[] = $userdetails;
}
}
......
......@@ -23,6 +23,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core\user_fields;
defined('MOODLE_INTERNAL') || die();
/**
......@@ -238,14 +240,15 @@ class course_enrolment_manager {
list($instancessql, $params, $filter) = $this->get_instance_sql();
list($filtersql, $moreparams) = $this->get_filter_sql();
$params += $moreparams;
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->get_context(), false);
$extrafields[] = 'lastaccess';
$ufields = user_picture::fields('u', $extrafields);
$sql = "SELECT DISTINCT $ufields, COALESCE(ul.timeaccess, 0) AS lastcourseaccess
$userfields = user_fields::for_identity($this->get_context())->with_userpic()->excluding('lastaccess');
['selects' => $fieldselect, 'joins' => $fieldjoin, 'params' => $fieldjoinparams] =
(array)$userfields->get_sql('u', true, '', '', false);
$params += $fieldjoinparams;
$sql = "SELECT DISTINCT $fieldselect, COALESCE(ul.timeaccess, 0) AS lastcourseaccess
FROM {user} u
JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid $instancessql)
JOIN {enrol} e ON (e.id = ue.enrolid)
$fieldjoin
LEFT JOIN {user_lastaccess} ul ON (ul.courseid = e.courseid AND ul.userid = u.id)";
if ($this->groupfilter) {
$sql .= " LEFT JOIN ({groups_members} gm JOIN {groups} g ON (g.id = gm.groupid))
......@@ -270,7 +273,7 @@ class course_enrolment_manager {
// Search condition.
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->get_context(), false);
$extrafields = user_fields::get_identity_fields($this->get_context(), false);
list($sql, $params) = users_search_sql($this->searchfilter, 'u', true, $extrafields);
// Role condition.
......@@ -343,23 +346,26 @@ class course_enrolment_manager {
list($ctxcondition, $params) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'ctx');
$params['courseid'] = $this->course->id;
$params['cid'] = $this->course->id;
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->get_context(), false);
$ufields = user_picture::fields('u', $extrafields);
$sql = "SELECT ra.id as raid, ra.contextid, ra.component, ctx.contextlevel, ra.roleid, $ufields,
coalesce(u.lastaccess,0) AS lastaccess
FROM {role_assignments} ra
JOIN {user} u ON u.id = ra.userid
JOIN {context} ctx ON ra.contextid = ctx.id
LEFT JOIN (
$userfields = user_fields::for_identity($this->get_context())->with_userpic();
['selects' => $fieldselect, 'joins' => $fieldjoin, 'params' => $fieldjoinparams] =
(array)$userfields->get_sql('u', true);
$params += $fieldjoinparams;
$sql = "SELECT ra.id as raid, ra.contextid, ra.component, ctx.contextlevel, ra.roleid,
coalesce(u.lastaccess,0) AS lastaccess
$fieldselect
FROM {role_assignments} ra
JOIN {user} u ON u.id = ra.userid
JOIN {context} ctx ON ra.contextid = ctx.id
$fieldjoin
LEFT JOIN (
SELECT ue.id, ue.userid
FROM {user_enrolments} ue
JOIN {enrol} e ON e.id = ue.enrolid
WHERE e.courseid = :courseid
) ue ON ue.userid=u.id
WHERE ctx.id $ctxcondition AND
ue.id IS NULL
ORDER BY $sort $direction, ctx.depth DESC";
WHERE ctx.id $ctxcondition AND
ue.id IS NULL
ORDER BY $sort $direction, ctx.depth DESC";
$this->otherusers[$key] = $DB->get_records_sql($sql, $params, $page*$perpage, $perpage);
}
return $this->otherusers[$key];
......@@ -372,20 +378,33 @@ class course_enrolment_manager {
* @param bool $searchanywhere Can the search term be anywhere, or must it be at the start.
* @return array with three elements:
* string list of fields to SELECT,
* string possible database joins for user fields
* string contents of SQL WHERE clause,
* array query params. Note that the SQL snippets use named parameters.
*/
protected function get_basic_search_conditions($search, $searchanywhere) {
global $DB, $CFG;
// Get custom user field SQL used for querying all the fields we need (identity, name, and
// user picture).
$userfields = user_fields::for_identity($this->context)->with_name()->with_userpic()
->excluding('username', 'lastaccess', 'maildisplay');
['selects' => $fieldselects, 'joins' => $fieldjoins, 'params' => $params, 'mappings' => $mappings] =
(array)$userfields->get_sql('u', true, '', '', false);
// Searchable fields are only the identity and name ones (not userpic).
$searchable = array_fill_keys($userfields->get_required_fields(
[user_fields::PURPOSE_IDENTITY, user_fields::PURPOSE_NAME]), true);
// Add some additional sensible conditions
$tests = array("u.id <> :guestid", 'u.deleted = 0', 'u.confirmed = 1');
$params = array('guestid' => $CFG->siteguest);
$params['guestid'] = $CFG->siteguest;
if (!empty($search)) {
// TODO Does not support custom user profile fields (MDL-70456).
$conditions = \core\user_fields::get_identity_fields($this->get_context(), false);
foreach (\core\user_fields::get_name_fields() as $field) {
$conditions[] = 'u.'.$field;
// Include identity and name fields as conditions.
foreach ($mappings as $fieldname => $fieldsql) {
if (array_key_exists($fieldname, $searchable)) {
$conditions[] = $fieldsql;
}
}
$conditions[] = $DB->sql_fullname('u.firstname', 'u.lastname');
if ($searchanywhere) {
......@@ -403,15 +422,8 @@ class course_enrolment_manager {
}
$wherecondition = implode(' AND ', $tests);
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->get_context(), false)->excluding('username', 'lastaccess');
$extrafields = $userfieldsapi->get_required_fields();
$extrafields[] = 'username';
$extrafields[] = 'lastaccess';
$extrafields[] = 'maildisplay';
$ufields = user_picture::fields('u', $extrafields);
return array($ufields, $params, $wherecondition);
$selects = $fieldselects . ', u.username, u.lastaccess, u.maildisplay';
return [$selects, $fieldjoins, $params, $wherecondition];
}
/**
......@@ -492,11 +504,12 @@ class course_enrolment_manager {
$addedenrollment = 0, $returnexactcount = false) {
global $DB;
list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere);
[$ufields, $joins, $params, $wherecondition] = $this->get_basic_search_conditions($search, $searchanywhere);
$fields = 'SELECT '.$ufields;
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
$joins
LEFT JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid)
WHERE $wherecondition
AND ue.id IS NULL";
......@@ -524,11 +537,12 @@ class course_enrolment_manager {
public function search_other_users($search = '', $searchanywhere = false, $page = 0, $perpage = 25, $returnexactcount = false) {
global $DB, $CFG;
list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere);
[$ufields, $joins, $params, $wherecondition] = $this->get_basic_search_conditions($search, $searchanywhere);
$fields = 'SELECT ' . $ufields;
$countfields = 'SELECT COUNT(u.id)';
$sql = " FROM {user} u
$joins
LEFT JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.contextid = :contextid)
WHERE $wherecondition
AND ra.id IS NULL";
......@@ -552,11 +566,12 @@ class course_enrolment_manager {
*/
public function search_users(string $search = '', bool $searchanywhere = false, int $page = 0, int $perpage = 25,
bool $returnexactcount = false) {
list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere);
[$ufields, $joins, $params, $wherecondition] = $this->get_basic_search_conditions($search, $searchanywhere);
$fields = 'SELECT ' . $ufields;
$countfields = 'SELECT COUNT(u.id)';
$sql = " FROM {user} u
$joins
JOIN {user_enrolments} ue ON ue.userid = u.id
JOIN {enrol} e ON ue.enrolid = e.id
WHERE $wherecondition
......@@ -1053,7 +1068,7 @@ class course_enrolment_manager {
$context = $this->get_context();
$now = time();
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($context, false);
$extrafields = user_fields::get_identity_fields($context, false);
$users = array();
foreach ($userroles as $userrole) {
......@@ -1132,7 +1147,7 @@ class course_enrolment_manager {
$url = new moodle_url($pageurl, $this->get_url_params());
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($context, false);
$extrafields = user_fields::get_identity_fields($context, false);
$enabledplugins = $this->get_enrolment_plugins(true);
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
......@@ -77,13 +77,27 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/str'], function($, Ajax,
if (results.length <= perpage) {
// Render the label.
const profileRegex = /^profile_field_(.*)$/;
$.each(results, function(index, user) {
var ctx = user,
identity = [];
$.each(userfields, function(i, k) {
if (typeof user[k] !== 'undefined' && user[k] !== '') {
ctx.hasidentity = true;
identity.push(user[k]);
const result = profileRegex.exec(k);
if (result) {
if (user.customfields) {
user.customfields.forEach(function(customfield) {
if (customfield.shortname === result[1]) {
ctx.hasidentity = true;
identity.push(customfield.value);
}
});
}
} else {
if (typeof user[k] !== 'undefined' && user[k] !== '') {
ctx.hasidentity = true;
identity.push(user[k]);
}
}
});
ctx.identity = identity.join(', ');
......
......@@ -93,8 +93,7 @@ class enrol_manual_enrol_users_form extends moodleform {
'courseid' => $course->id,
'enrolid' => $instance->id,
'perpage' => $CFG->maxusersperpage,
// TODO Does not support custom user profile fields (MDL-70456).
'userfields' => implode(',', \core\user_fields::get_identity_fields($context, false))
'userfields' => implode(',', \core\user_fields::get_identity_fields($context, true))
);
$mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), array(), $options);
......
......@@ -5,108 +5,111 @@ Feature: Teacher can search and enrol users one by one into the course
I can search for the students and enrol them into the course
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher001 | Teacher | 001 | teacher001@example.com |
| student001 | Student | 001 | student001@example.com |
| student002 | Student | 002 | student002@example.com |
| student003 | Student | 003 | student003@example.com |
| student004 | Student | 004 | student004@example.com |
| student005 | Student | 005 | student005@example.com |
| student006 | Student | 006 | student006@example.com |
| student007 | Student | 007 | student007@example.com |
| student008 | Student | 008 | student008@example.com |
| student009 | Student | 009 | student009@example.com |
| student010 | Student | 010 | student010@example.com |
| student011 | Student | 011 | student011@example.com |
| student012 | Student | 012 | student012@example.com |
| student013 | Student | 013 | student013@example.com |
| student014 | Student | 014 | student014@example.com |
| student015 | Student | 015 | student015@example.com |
| student016 | Student | 016 | student016@example.com |
| student017 | Student | 017 | student017@example.com |
| student018 | Student | 018 | student018@example.com |
| student019 | Student | 019 | student019@example.com |
| student020 | Student | 020 | student020@example.com |
| student021 | Student | 021 | student021@example.com |
| student022 | Student | 022 | student022@example.com |
| student023 | Student | 023 | student023@example.com |
| student024 | Student | 024 | student024@example.com |
| student025 | Student | 025 | student025@example.com |
| student026 | Student | 026 | student026@example.com |
| student027 | Student | 027 | student027@example.com |
| student028 | Student | 028 | student028@example.com |
| student029 | Student | 029 | student029@example.com |
| student030 | Student | 030 | student030@example.com |
| student031 | Student | 031 | student031@example.com |
| student032 | Student | 032 | student032@example.com |
| student033 | Student | 033 | student033@example.com |
| student034 | Student | 034 | student034@example.com |
| student035 | Student | 035 | student035@example.com |
| student036 | Student | 036 | student036@example.com |
| student037 | Student | 037 | student037@example.com |
| student038 | Student | 038 | student038@example.com |
| student039 | Student | 039 | student039@example.com |
| student040 | Student | 040 | student040@example.com |
| student041 | Student | 041 | student041@example.com |
| student042 | Student | 042 | student042@example.com |
| student043 | Student | 043 | student043@example.com |
| student044 | Student | 044 | student044@example.com |
| student045 | Student | 045 | student045@example.com |
| student046 | Student | 046 | student046@example.com |
| student047 | Student | 047 | student047@example.com |
| student048 | Student | 048 | student048@example.com |
| student049 | Student | 049 | student049@example.com |
| student050 | Student | 050 | student050@example.com |
| student051 | Student | 051 | student051@example.com |
| student052 | Student | 052 | student052@example.com |
| student053 | Student | 053 | student053@example.com |
| student054 | Student | 054 | student054@example.com |
| student055 | Student | 055 | student055@example.com |
| student056 | Student | 056 | student056@example.com |
| student057 | Student | 057 | student057@example.com |
| student058 | Student | 058 | student058@example.com |
| student059 | Student | 059 | student059@example.com |
| student060 | Student | 060 | student060@example.com |
| student061 | Student | 061 | student061@example.com |
| student062 | Student | 062 | student062@example.com |
| student063 | Student | 063 | student063@example.com |
| student064 | Student | 064 | student064@example.com |
| student065 | Student | 065 | student065@example.com |
| student066 | Student | 066 | student066@example.com |
| student067 | Student | 067 | student067@example.com |
| student068 | Student | 068 | student068@example.com |
| student069 | Student | 069 | student069@example.com |
| student070 | Student | 070 | student070@example.com |
| student071 | Student | 071 | student071@example.com |
| student072 | Student | 072 | student072@example.com |
| student073 | Student | 073 | student073@example.com |
| student074 | Student | 074 | student074@example.com |
| student075 | Student | 075 | student075@example.com |
| student076 | Student | 076 | student076@example.com |
| student077 | Student | 077 | student077@example.com |
| student078 | Student | 078 | student078@example.com |
| student079 | Student | 079 | student079@example.com |
| student080 | Student | 080 | student080@example.com |
| student081 | Student | 081 | student081@example.com |
| student082 | Student | 082 | student082@example.com |
| student083 | Student | 083 | student083@example.com |
| student084 | Student | 084 | student084@example.com |
| student085 | Student | 085 | student085@example.com |
| student086 | Student | 086 | student086@example.com |
| student087 | Student | 087 | student087@example.com |
| student088 | Student | 088 | student088@example.com |
| student089 | Student | 089 | student089@example.com |
| student090 | Student | 090 | student090@example.com |
| student091 | Student | 091 | student091@example.com |
| student092 | Student | 092 | student092@example.com |
| student093 | Student | 093 | student093@example.com |
| student094 | Student | 094 | student094@example.com |
| student095 | Student | 095 | student095@example.com |
| student096 | Student | 096 | student096@example.com |
| student097 | Student | 097 | student097@example.com |
| student098 | Student | 098 | student098@example.com |
| student099 | Student | 099 | student099@example.com |
Given the following "custom profile fields" exist:
| datatype | shortname | name |
| text | customid | Custom user id |
And the following "users" exist:
| username | firstname | lastname | email | profile_field_customid |
| teacher001 | Teacher | 001 | teacher001@example.com | |
| student001 | Student | 001 | student001@example.com | Q994 |
| student002 | Student | 002 | student002@example.com | Q008 |
| student003 | Student | 003 | student003@example.com | Z442 |
| student004 | Student | 004 | student004@example.com | |
| student005 | Student | 005 | student005@example.com | |
| student006 | Student | 006 | student006@example.com | |
| student007 | Student | 007 | student007@example.com | |
| student008 | Student | 008 | student008@example.com | |
| student009 | Student | 009 | student009@example.com | |
| student010 | Student | 010 | student010@example.com | |
| student011 | Student | 011 | student011@example.com | |
| student012 | Student | 012 | student012@example.com | |
| student013 | Student | 013 | student013@example.com | |
| student014 | Student | 014 | student014@example.com | |
| student015 | Student | 015 | student015@example.com | |
| student016 | Student | 016 | student016@example.com | |
| student017 | Student | 017 | student017@example.com | |
| student018 | Student | 018 | student018@example.com | |
| student019 | Student | 019 | student019@example.com | |
| student020 | Student | 020 | student020@example.com | |
| student021 | Student | 021 | student021@example.com | |
| student022 | Student | 022 | student022@example.com | |
| student023 | Student | 023 | student023@example.com | |
| student024 | Student | 024 | student024@example.com | |
| student025 | Student | 025 | student025@example.com | |
| student026 | Student | 026 | student026@example.com | |
| student027 | Student | 027 | student027@example.com | |
| student028 | Student | 028 | student028@example.com | |
| student029 | Student | 029 | student029@example.com | |
| student030 | Student | 030 | student030@example.com | |
| student031 | Student | 031 | student031@example.com | |
| student032 | Student | 032 | student032@example.com | |
| student033 | Student | 033 | student033@example.com | |
| student034 | Student | 034 | student034@example.com | |
| student035 | Student | 035 | student035@example.com | |
| student036 | Student | 036 | student036@example.com | |
| student037 | Student | 037 | student037@example.com | |
| student038 | Student | 038 | student038@example.com | |
| student039 | Student | 039 | student039@example.com | |
| student040 | Student | 040 | student040@example.com | |
| student041 | Student | 041 | student041@example.com | |
| student042 | Student | 042 | student042@example.com | |
| student043 | Student | 043 | student043@example.com | |
| student044 | Student | 044 | student044@example.com | |
| student045 | Student | 045 | student045@example.com | |
| student046 | Student | 046 | student046@example.com | |
| student047 | Student | 047 | student047@example.com | |
| student048 | Student | 048 | student048@example.com | |
| student049 | Student | 049 | student049@example.com | |
| student050 | Student | 050 | student050@example.com | |
| student051 | Student | 051 | student051@example.com | |
| student052 | Student | 052 | student052@example.com | |
| student053 | Student | 053 | student053@example.com | |
| student054 | Student | 054 | student054@example.com | |
| student055 | Student | 055 | student055@example.com | |
| student056 | Student | 056 | student056@example.com | |
| student057 | Student | 057 | student057@example.com | |
| student058 | Student | 058 | student058@example.com | |
| student059 | Student | 059 | student059@example.com | |
| student060 | Student | 060 | student060@example.com | |
| student061 | Student | 061 | student061@example.com | |
| student062 | Student | 062 | student062@example.com | |
| student063 | Student | 063 | student063@example.com | |
| student064 | Student | 064 | student064@example.com | |
| student065 | Student | 065 | student065@example.com | |
| student066 | Student | 066 | student066@example.com | |
| student067 | Student | 067 | student067@example.com | |
| student068 | Student | 068 | student068@example.com | |
| student069 | Student | 069 | student069@example.com | |
| student070 | Student | 070 | student070@example.com | |
| student071 | Student | 071 | student071@example.com | |
| student072 | Student | 072 | student072@example.com | |
| student073 | Student | 073 | student073@example.com | |
| student074 | Student | 074 | student074@example.com | |
| student075 | Student | 075 | student075@example.com | |
| student076 | Student | 076 | student076@example.com | |
| student077 | Student | 077 | student077@example.com | |
| student078 | Student | 078 | student078@example.com | |
| student079 | Student | 079 | student079@example.com | |
| student080 | Student | 080 | student080@example.com | |
| student081 | Student | 081 | student081@example.com | |
| student082 | Student | 082 | student082@example.com | |
| student083 | Student | 083 | student083@example.com | |
| student084 | Student | 084 | student084@example.com | |
| student085 | Student | 085 | student085@example.com | |
| student086 | Student | 086 | student086@example.com | |
| student087 | Student | 087 | student087@example.com | |
| student088 | Student | 088 | student088@example.com | |
| student089 | Student | 089 | student089@example.com | |
| student090 | Student | 090 | student090@example.com | |
| student091 | Student | 091 | student091@example.com | |
| student092 | Student | 092 | student092@example.com | |
| student093 | Student | 093 | student093@example.com | |
| student094 | Student | 094 | student094@example.com | |
| student095 | Student | 095 | student095@example.com | |
| student096 | Student | 096 | student096@example.com | |
| student097 | Student | 097 | student097@example.com | |
| student098 | Student | 098 | student098@example.com | |
| student099 | Student | 099 | student099@example.com | |
And the following "courses" exist:
| fullname | shortname | format | startdate |
| Course 001 | C001 | weeks | ##1 month ago## |
......@@ -189,6 +192,26 @@ Feature: Teacher can search and enrol users one by one into the course
And I type "student100@example.com"
And I should see "student100@example.com, 1234567892, 1234567893, ABC1, ABC2"
@javascript
Scenario: Custom user profile fields work for search and display, if user has permission
Given the following config values are set as admin:
| showuseridentity | email,profile_field_customid |
And I navigate to course participants
And I press "Enrol users"
When I set the field "Select users" to "Q994"
Then I should see "student001@example.com, Q994"
And I click on "Cancel" "button" in the "Enrol users" "dialogue"
And the following "permission overrides" exist:
| capability | permission | role | contextlevel | reference |
| moodle/site:viewuseridentity | Prevent | editingteacher | Course | C001 |
And I press "Enrol users"
# Do this by keyboard because the 'I set the field' step doesn't let you set it to a missing value.
And I press tab
And I press tab
And I press tab
And I type "Q994"
And I should see "No suggestions"
# The following tests are commented out as a result of MDL-66339.
# @javascript
# Scenario: Enrol user from participants page
......
......@@ -254,6 +254,127 @@ class core_course_enrolment_manager_testcase extends advanced_testcase {
$this->assertArrayHasKey($this->users['user22']->id, $users);
}
/**
* Sets up a custom profile field and the showuseridentity option, and creates a test user
* with suitable values set.
*
* @return stdClass Test user
*/
protected function setup_for_user_identity_tests(): stdClass {
// Configure extra fields to include one normal user field and one profile field, and
// set the values for a new test user.
$generator = $this->getDataGenerator();
$generator->create_custom_profile_field(['datatype' => 'text',
'shortname' => 'researchtopic', 'name' => 'Research topic']);
set_config('showuseridentity', 'email,department,profile_field_researchtopic');
return $generator->create_user(
['username' => 'newuser', 'department' => 'Amphibian studies', 'email' => 'x@x.org',
'profile_field_researchtopic' => 'Frogs', 'imagealt' => 'Smart suit']);
}
/**
* Checks that the get_users function returns the correct user fields.
*/
public function test_get_users_fields() {
global $PAGE;
$this->resetAfterTest();
$newuser = $this->setup_for_user_identity_tests();
// Enrol the user in test course.
$this->getDataGenerator()->enrol_user($newuser->id, $this->course->id, 'student');
// Get all users and fish out the one we're interested in.
$manager = new course_enrolment_manager($PAGE, $this->course);
$users = $manager->get_users('id');
$user = $users[$newuser->id];
// Should include core required fields...
$this->assertEquals($newuser->id, $user->id);
// ...And the ones specified in showuseridentity (one of which is also needed for user pics).
$this->assertEquals('Amphibian studies', $user->department);
$this->assertEquals('Frogs', $user->profile_field_researchtopic);
$this->assertEquals('x@x.org', $user->email);
// And the ones necessary for user pics.