Commit 5a7c629f authored by jun's avatar jun
Browse files

Merge branch 'MDL-45242-master' of https://github.com/sammarshallou/moodle

parents 644f3278 987e5545
......@@ -124,8 +124,9 @@ class task_log_table extends \table_sql {
$sort = "ORDER BY $sort";
}
$extrafields = get_extra_user_fields(\context_system::instance());
$userfields = \user_picture::fields('u', $extrafields, 'userid2', 'user');
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity(\context_system::instance(), false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, 'user', 'userid2', false)->selects;
$where = '';
if (!empty($this->sql->where)) {
......
......@@ -271,7 +271,8 @@ if ($roleid) {
foreach ($assignableroles as $roleid => $notused) {
$roleusers = '';
if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE) {
$userfields = 'u.id, u.username, ' . get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$userfields = 'u.id, u.username' . $userfieldsapi->get_sql('u')->selects;
$roleusers = get_role_users($roleid, $context, false, $userfields);
if (!empty($roleusers)) {
$strroleusers = array();
......
......@@ -214,21 +214,38 @@ if ($hassiteconfig
// with moodle/site:viewuseridentity).
// Options include fields from the user table that might be helpful to
// distinguish when adding or listing users ('I want to add the John
// Smith from Science faculty').
// Custom user profile fields are not currently supported.
// Smith from Science faculty') and any custom profile fields.
$temp->add(new admin_setting_configmulticheckbox('showuseridentity',
new lang_string('showuseridentity', 'admin'),
new lang_string('showuseridentity_desc', 'admin'), array('email' => 1), array(
'username' => new lang_string('username'),
'idnumber' => new lang_string('idnumber'),
'email' => new lang_string('email'),
'phone1' => new lang_string('phone1'),
'phone2' => new lang_string('phone2'),
'department' => new lang_string('department'),
'institution' => new lang_string('institution'),
'city' => new lang_string('city'),
'country' => new lang_string('country'),
)));
new lang_string('showuseridentity_desc', 'admin'), ['email' => 1],
function() {
global $DB;
// Basic fields available in user table.
$fields = [
'username' => new lang_string('username'),
'idnumber' => new lang_string('idnumber'),
'email' => new lang_string('email'),
'phone1' => new lang_string('phone1'),
'phone2' => new lang_string('phone2'),
'department' => new lang_string('department'),
'institution' => new lang_string('institution'),
'city' => new lang_string('city'),
'country' => new lang_string('country'),
];
// Custom profile fields.
$profilefields = $DB->get_records('user_info_field', ['datatype' => 'text'], 'sortorder ASC');
foreach ($profilefields as $key => $field) {
// Only reasonable-length fields can be used as identity fields.
if ($field->param2 > 255) {
continue;
}
$fields['profile_field_' . $field->shortname] = $field->name . ' *';
}
return $fields;
}));
$setting = new admin_setting_configtext('fullnamedisplay', new lang_string('fullnamedisplay', 'admin'),
new lang_string('configfullnamedisplay', 'admin'), 'language', PARAM_TEXT, 50);
$setting->set_force_ltr(true);
......
......@@ -5,12 +5,16 @@ Feature: An administrator can filter user accounts by role, cohort and other pro
I need to filter the users account list using different filter
Background:
Given the following "users" exist:
| username | firstname | lastname | email | auth | confirmed | lastip | institution | department |
| user1 | User | One | one@example.com | manual | 0 | 127.0.1.1 | moodle | red |
| user2 | User | Two | two@example.com | ldap | 1 | 0.0.0.0 | moodle | blue |
| user3 | User | Three | three@example.com | manual | 1 | 0.0.0.0 | | |
| user4 | User | Four | four@example.com | ldap | 0 | 127.0.1.2 | | |
Given the following "custom profile fields" exist:
| datatype | shortname | name |
| text | frog | Favourite frog |
| text | undead | Type of undead |
And the following "users" exist:
| username | firstname | lastname | email | auth | confirmed | lastip | institution | department | profile_field_frog | profile_field_undead |
| user1 | User | One | one@example.com | manual | 0 | 127.0.1.1 | moodle | red | Kermit | |
| user2 | User | Two | two@example.com | ldap | 1 | 0.0.0.0 | moodle | blue | Mr Toad | Zombie |
| user3 | User | Three | three@example.com | manual | 1 | 0.0.0.0 | | | | |
| user4 | User | Four | four@example.com | ldap | 0 | 127.0.1.2 | | | | |
And the following "cohorts" exist:
| name | idnumber |
| Cohort 1 | CH1 |
......@@ -116,3 +120,20 @@ Feature: An administrator can filter user accounts by role, cohort and other pro
And I press "Add filter"
And I should see "User One"
And I should not see "User Two"
Scenario: Filter users by custom profile field (specific or any)
When I set the field "id_profile_fld" to "Favourite frog"
And I set the field "id_profile" to "Kermit"
And I press "Add filter"
Then I should see "User One"
And I should not see "User Two"
And I should not see "User Three"
And I should not see "User Four"
And I press "Remove all filters"
And I set the field "id_profile_fld" to "any field"
And I set the field "id_profile" to "Zombie"
And I press "Add filter"
And I should see "User Two"
And I should not see "User One"
And I should not see "User Three"
And I should not see "User Four"
......@@ -126,7 +126,8 @@ class cohort_role_assignments_table extends table_sql {
* Setup the headers for the table.
*/
protected function define_table_columns() {
$extrafields = get_extra_user_fields($this->context);
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->context, false);
// Define headers and columns.
$cols = array(
......@@ -170,14 +171,12 @@ class cohort_role_assignments_table extends table_sql {
protected function get_sql_and_params($count = false) {
$fields = 'uca.id, uca.cohortid, uca.userid, uca.roleid, ';
$fields .= 'c.name as cohortname, c.idnumber as cohortidnumber, c.contextid as cohortcontextid, ';
$fields .= 'c.visible as cohortvisible, c.description as cohortdescription, c.theme as cohorttheme, ';
$fields .= 'c.visible as cohortvisible, c.description as cohortdescription, c.theme as cohorttheme';
// Add extra user fields that we need for the graded user.
$extrafields = get_extra_user_fields($this->context);
foreach ($extrafields as $field) {
$fields .= 'u.' . $field . ', ';
}
$fields .= get_all_user_name_fields(true, 'u');
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->context, false)->with_name();
$fields .= $userfieldsapi->get_sql('u')->selects;
if ($count) {
$select = "COUNT(1)";
......
......@@ -189,7 +189,8 @@ class api {
$dpos = [];
$context = context_system::instance();
foreach ($dporoles as $roleid) {
$allnames = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$fields = 'u.id, u.confirmed, u.username, '. $allnames . ', ' .
'u.maildisplay, u.mailformat, u.maildigest, u.email, u.emailstop, u.city, '.
'u.country, u.picture, u.idnumber, u.department, u.institution, '.
......
......@@ -700,13 +700,15 @@ class external extends external_api {
self::validate_context($context);
require_capability('tool/dataprivacy:managedatarequests', $context);
$allusernames = get_all_user_name_fields(true);
$userfieldsapi = \core\user_fields::for_name();
$allusernames = $userfieldsapi->get_sql('', false, '', '', false)->selects;
// Exclude admins and guest user.
$excludedusers = array_keys(get_admins()) + [guest_user()->id];
$sort = 'lastname ASC, firstname ASC';
$fields = 'id,' . $allusernames;
$extrafields = get_extra_user_fields($context);
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($context, false);
if (!empty($extrafields)) {
$fields .= ',' . implode(',', $extrafields);
}
......
......@@ -187,7 +187,8 @@ class helper {
global $DB;
// Get users that the user has role assignments to.
$allusernames = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$allusernames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT u.id, $allusernames
FROM {role_assignments} ra, {context} c, {user} u
WHERE ra.userid = :userid
......
......@@ -62,7 +62,8 @@ class tool_dataprivacy_data_request_form extends \core\form\persistent {
'valuehtmlcallback' => function($value) {
global $OUTPUT;
$allusernames = get_all_user_name_fields(true);
$userfieldsapi = \core\user_fields::for_name();
$allusernames = $userfieldsapi->get_sql('', false, '', '', false)->selects;
$fields = 'id, email, ' . $allusernames;
$user = \core_user::get_user($value, $fields);
$useroptiondata = [
......
......@@ -878,11 +878,10 @@ class external extends external_api {
list($filtercapsql, $filtercapparams) = api::filter_users_with_capability_on_user_context_sql($cap,
$USER->id, SQL_PARAMS_NAMED);
$extrasearchfields = array();
if (!empty($CFG->showuseridentity) && has_capability('moodle/site:viewuseridentity', $context)) {
$extrasearchfields = explode(',', $CFG->showuseridentity);
}
$fields = \user_picture::fields('u', $extrasearchfields);
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($context, false)->with_userpic();
$fields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$extrasearchfields = $userfieldsapi->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]);
list($wheresql, $whereparams) = users_search_sql($query, 'u', true, $extrasearchfields);
list($sortsql, $sortparams) = users_order_by_sql('u', $query, $context);
......
......@@ -92,7 +92,8 @@ class template_plans_table extends table_sql {
* Setup the headers for the table.
*/
protected function define_table_columns() {
$extrafields = get_extra_user_fields($this->context);
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->context, false);
// Define headers and columns.
$cols = array(
......@@ -132,14 +133,12 @@ class template_plans_table extends table_sql {
* @return array containing sql to use and an array of params.
*/
protected function get_sql_and_params($count = false) {
$fields = 'p.id, p.userid, p.name, ';
$fields = 'p.id, p.userid, p.name';
// Add extra user fields that we need for the graded user.
$extrafields = get_extra_user_fields($this->context);
foreach ($extrafields as $field) {
$fields .= 'u.' . $field . ', ';
}
$fields .= get_all_user_name_fields(true, 'u');
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->context, false)->with_name();
$fields .= $userfieldsapi->get_sql('u')->selects;
if ($count) {
$select = "COUNT(1)";
......
......@@ -91,8 +91,10 @@ class acceptances_table extends \table_sql {
}
}
$extrafields = get_extra_user_fields(\context_system::instance());
$userfields = \user_picture::fields('u', $extrafields);
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity(\context_system::instance(), false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$extrafields = $userfieldsapi->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]);
$this->set_sql("$userfields",
"{user} u",
......@@ -103,7 +105,7 @@ class acceptances_table extends \table_sql {
}
$this->add_column_header('fullname', get_string('fullnameuser', 'core'));
foreach ($extrafields as $field) {
$this->add_column_header($field, get_user_field_name($field));
$this->add_column_header($field, \core\user_fields::get_display_name($field));
}
if (!$this->is_downloading() && !has_capability('tool/policy:acceptbehalf', \context_system::instance())) {
......@@ -168,7 +170,8 @@ class acceptances_table extends \table_sql {
* Helper configuration method.
*/
protected function configure_for_single_version() {
$userfieldsmod = get_all_user_name_fields(true, 'm', null, 'mod');
$userfieldsapi = \core\user_fields::for_name();
$userfieldsmod = $userfieldsapi->get_sql('m', false, 'mod', '', false)->selects;
$v = key($this->versionids);
$this->sql->fields .= ", $userfieldsmod, a{$v}.status AS status{$v}, a{$v}.note, ".
"a{$v}.timemodified, a{$v}.usermodified AS usermodified{$v}";
......@@ -643,4 +646,4 @@ class acceptances_table extends \table_sql {
}
return parent::other_cols($column, $row);
}
}
\ No newline at end of file
}
......@@ -343,10 +343,10 @@ class api {
global $DB;
$ctxfields = context_helper::get_preload_record_columns_sql('c');
$namefields = get_all_user_name_fields(true, 'u');
$pixfields = user_picture::fields('u', $extrafields);
$userfieldsapi = \core\user_fields::for_name()->with_userpic()->including(...($extrafields ?? []));
$userfields = $userfieldsapi->get_sql('u')->selects;
$sql = "SELECT $ctxfields, $namefields, $pixfields
$sql = "SELECT $ctxfields $userfields
FROM {role_assignments} ra
JOIN {context} c ON c.contextlevel = ".CONTEXT_USER." AND ra.contextid = c.id
JOIN {user} u ON c.instanceid = u.id
......@@ -682,7 +682,8 @@ class api {
$vsql = ' AND a.policyversionid ' . $vsql;
}
$userfieldsmod = get_all_user_name_fields(true, 'm', null, 'mod');
$userfieldsapi = \core\user_fields::for_name();
$userfieldsmod = $userfieldsapi->get_sql('m', false, 'mod', '', false)->selects;
$sql = "SELECT u.id AS mainuserid, a.policyversionid, a.status, a.lang, a.timemodified, a.usermodified, a.note,
u.policyagreed, $userfieldsmod
FROM {user} u
......
......@@ -128,7 +128,8 @@ class accept_policy extends \moodleform {
$usernames = [];
list($sql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
$params['usercontextlevel'] = CONTEXT_USER;
$users = $DB->get_records_sql("SELECT u.id, " . get_all_user_name_fields(true, 'u') . ", " .
$userfieldsapi = \core\user_fields::for_name();
$users = $DB->get_records_sql("SELECT u.id" . $userfieldsapi->get_sql('u')->selects . ", " .
\context_helper::get_preload_record_columns_sql('ctx') .
" FROM {user} u JOIN {context} ctx ON ctx.contextlevel=:usercontextlevel AND ctx.instanceid = u.id
WHERE u.id " . $sql, $params);
......
......@@ -155,7 +155,7 @@ class process {
'interests',
);
// Include all name fields.
$this->standardfields = array_merge($this->standardfields, get_all_user_name_fields());
$this->standardfields = array_merge($this->standardfields, \core\user_fields::get_name_fields());
}
/**
......
......@@ -183,13 +183,13 @@
// These columns are always shown in the users list.
$requiredcolumns = array('city', 'country', 'lastaccess');
// Extra columns containing the extra user fields, excluding the required columns (city and country, to be specific).
$extracolumns = get_extra_user_fields($context, $requiredcolumns);
$userfields = \core\user_fields::for_identity($context, true)->with_name()->excluding(...$requiredcolumns);
$extracolumns = $userfields->get_required_fields();
// Get all user name fields as an array.
$allusernamefields = get_all_user_name_fields(false, null, null, null, true);
$columns = array_merge($allusernamefields, $extracolumns, $requiredcolumns);
$columns = array_merge($extracolumns, $requiredcolumns);
foreach ($columns as $column) {
$string[$column] = get_user_field_name($column);
$string[$column] = \core\user_fields::get_display_name($column);
if ($sort != $column) {
$columnicon = "";
if ($column == "lastaccess") {
......@@ -226,7 +226,7 @@
}
// Order in string will ensure that the name columns are in the correct order.
$usernames = order_in_string($allusernamefields, $fullnamesetting);
$usernames = order_in_string($extracolumns, $fullnamesetting);
$fullnamedisplay = array();
foreach ($usernames as $name) {
// Use the link from $$column for sorting on the user's name.
......
......@@ -64,7 +64,8 @@ if (count($cohorts) < 2) {
}
$countries = get_string_manager()->get_list_of_countries(true);
$namefields = get_all_user_name_fields(true);
$userfieldsapi = \core\user_fields::for_name();
$namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
foreach ($users as $key => $id) {
$user = $DB->get_record('user', array('id' => $id), 'id, ' . $namefields . ', username,
email, country, lastaccess, city, deleted');
......
......@@ -24,7 +24,8 @@ echo $OUTPUT->header();
$countries = get_string_manager()->get_list_of_countries(true);
$namefields = get_all_user_name_fields(true);
$userfieldsapi = \core\user_fields::for_name();
$namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
foreach ($users as $key => $id) {
$user = $DB->get_record('user', array('id'=>$id), 'id, ' . $namefields . ', username, email, country, lastaccess, city');
$user->fullname = fullname($user, true);
......
......@@ -241,7 +241,8 @@ class web_service_token_form extends moodleform {
if ($usertotal < 500) {
list($sort, $params) = users_order_by_sql('u');
// User searchable selector - return users who are confirmed, not deleted, not suspended and not a guest.
$sql = 'SELECT u.id, ' . get_all_user_name_fields(true, 'u') . '
$userfieldsapi = \core\user_fields::for_name();
$sql = 'SELECT u.id' . $userfieldsapi->get_sql('u')->selects . '
FROM {user} u
WHERE u.deleted = 0
AND u.confirmed = 1
......
......@@ -197,7 +197,7 @@ class condition extends \core_availability\condition {
$this->customfield);
}
} else {
$translatedfieldname = get_user_field_name($this->standardfield);
$translatedfieldname = \core\user_fields::get_display_name($this->standardfield);
}
$context = \context_course::instance($course->id);
$a = new \stdClass();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment