Commit 5e72715e authored by sam marshall's avatar sam marshall
Browse files

MDL-71099 Lib: Move new user_fields class from core to core_user

This class would belong more appropriately within the 'user' API
(core_user) instead of within the 'core' API, since it is
directly related to user data.

Since the class has only just been added to Moodle, now is a good
time to move it.
parent d65ed58e
...@@ -125,7 +125,7 @@ class task_log_table extends \table_sql { ...@@ -125,7 +125,7 @@ class task_log_table extends \table_sql {
} }
// TODO Does not support custom user profile fields (MDL-70456). // TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity(\context_system::instance(), false)->with_userpic(); $userfieldsapi = \core_user\fields::for_identity(\context_system::instance(), false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, 'user', 'userid2', false)->selects; $userfields = $userfieldsapi->get_sql('u', false, 'user', 'userid2', false)->selects;
$where = ''; $where = '';
......
...@@ -271,7 +271,7 @@ if ($roleid) { ...@@ -271,7 +271,7 @@ if ($roleid) {
foreach ($assignableroles as $roleid => $notused) { foreach ($assignableroles as $roleid => $notused) {
$roleusers = ''; $roleusers = '';
if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE) { if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE) {
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$userfields = 'u.id, u.username' . $userfieldsapi->get_sql('u')->selects; $userfields = 'u.id, u.username' . $userfieldsapi->get_sql('u')->selects;
$roleusers = get_role_users($roleid, $context, false, $userfields); $roleusers = get_role_users($roleid, $context, false, $userfields);
if (!empty($roleusers)) { if (!empty($roleusers)) {
......
...@@ -127,7 +127,7 @@ class cohort_role_assignments_table extends table_sql { ...@@ -127,7 +127,7 @@ class cohort_role_assignments_table extends table_sql {
*/ */
protected function define_table_columns() { protected function define_table_columns() {
// TODO Does not support custom user profile fields (MDL-70456). // TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->context, false); $extrafields = \core_user\fields::get_identity_fields($this->context, false);
// Define headers and columns. // Define headers and columns.
$cols = array( $cols = array(
...@@ -175,7 +175,7 @@ class cohort_role_assignments_table extends table_sql { ...@@ -175,7 +175,7 @@ class cohort_role_assignments_table extends table_sql {
// Add extra user fields that we need for the graded user. // Add extra user fields that we need for the graded user.
// TODO Does not support custom user profile fields (MDL-70456). // TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->context, false)->with_name(); $userfieldsapi = \core_user\fields::for_identity($this->context, false)->with_name();
$fields .= $userfieldsapi->get_sql('u')->selects; $fields .= $userfieldsapi->get_sql('u')->selects;
if ($count) { if ($count) {
......
...@@ -189,7 +189,7 @@ class api { ...@@ -189,7 +189,7 @@ class api {
$dpos = []; $dpos = [];
$context = context_system::instance(); $context = context_system::instance();
foreach ($dporoles as $roleid) { foreach ($dporoles as $roleid) {
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects; $allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$fields = 'u.id, u.confirmed, u.username, '. $allnames . ', ' . $fields = 'u.id, u.confirmed, u.username, '. $allnames . ', ' .
'u.maildisplay, u.mailformat, u.maildigest, u.email, u.emailstop, u.city, '. 'u.maildisplay, u.mailformat, u.maildigest, u.email, u.emailstop, u.city, '.
......
...@@ -700,7 +700,7 @@ class external extends external_api { ...@@ -700,7 +700,7 @@ class external extends external_api {
self::validate_context($context); self::validate_context($context);
require_capability('tool/dataprivacy:managedatarequests', $context); require_capability('tool/dataprivacy:managedatarequests', $context);
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$allusernames = $userfieldsapi->get_sql('', false, '', '', false)->selects; $allusernames = $userfieldsapi->get_sql('', false, '', '', false)->selects;
// Exclude admins and guest user. // Exclude admins and guest user.
$excludedusers = array_keys(get_admins()) + [guest_user()->id]; $excludedusers = array_keys(get_admins()) + [guest_user()->id];
...@@ -708,7 +708,7 @@ class external extends external_api { ...@@ -708,7 +708,7 @@ class external extends external_api {
$fields = 'id,' . $allusernames; $fields = 'id,' . $allusernames;
// TODO Does not support custom user profile fields (MDL-70456). // TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($context, false); $extrafields = \core_user\fields::get_identity_fields($context, false);
if (!empty($extrafields)) { if (!empty($extrafields)) {
$fields .= ',' . implode(',', $extrafields); $fields .= ',' . implode(',', $extrafields);
} }
......
...@@ -187,7 +187,7 @@ class helper { ...@@ -187,7 +187,7 @@ class helper {
global $DB; global $DB;
// Get users that the user has role assignments to. // Get users that the user has role assignments to.
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$allusernames = $userfieldsapi->get_sql('u', false, '', '', false)->selects; $allusernames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT u.id, $allusernames $sql = "SELECT u.id, $allusernames
FROM {role_assignments} ra, {context} c, {user} u FROM {role_assignments} ra, {context} c, {user} u
......
...@@ -62,7 +62,7 @@ class tool_dataprivacy_data_request_form extends \core\form\persistent { ...@@ -62,7 +62,7 @@ class tool_dataprivacy_data_request_form extends \core\form\persistent {
'valuehtmlcallback' => function($value) { 'valuehtmlcallback' => function($value) {
global $OUTPUT; global $OUTPUT;
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$allusernames = $userfieldsapi->get_sql('', false, '', '', false)->selects; $allusernames = $userfieldsapi->get_sql('', false, '', '', false)->selects;
$fields = 'id, email, ' . $allusernames; $fields = 'id, email, ' . $allusernames;
$user = \core_user::get_user($value, $fields); $user = \core_user::get_user($value, $fields);
......
...@@ -879,9 +879,9 @@ class external extends external_api { ...@@ -879,9 +879,9 @@ class external extends external_api {
$USER->id, SQL_PARAMS_NAMED); $USER->id, SQL_PARAMS_NAMED);
// TODO Does not support custom user profile fields (MDL-70456). // TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($context, false)->with_userpic(); $userfieldsapi = \core_user\fields::for_identity($context, false)->with_userpic();
$fields = $userfieldsapi->get_sql('u', false, '', '', false)->selects; $fields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$extrasearchfields = $userfieldsapi->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]); $extrasearchfields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
list($wheresql, $whereparams) = users_search_sql($query, 'u', true, $extrasearchfields); list($wheresql, $whereparams) = users_search_sql($query, 'u', true, $extrasearchfields);
list($sortsql, $sortparams) = users_order_by_sql('u', $query, $context); list($sortsql, $sortparams) = users_order_by_sql('u', $query, $context);
......
...@@ -93,7 +93,7 @@ class template_plans_table extends table_sql { ...@@ -93,7 +93,7 @@ class template_plans_table extends table_sql {
*/ */
protected function define_table_columns() { protected function define_table_columns() {
// TODO Does not support custom user profile fields (MDL-70456). // TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->context, false); $extrafields = \core_user\fields::get_identity_fields($this->context, false);
// Define headers and columns. // Define headers and columns.
$cols = array( $cols = array(
...@@ -137,7 +137,7 @@ class template_plans_table extends table_sql { ...@@ -137,7 +137,7 @@ class template_plans_table extends table_sql {
// Add extra user fields that we need for the graded user. // Add extra user fields that we need for the graded user.
// TODO Does not support custom user profile fields (MDL-70456). // TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->context, false)->with_name(); $userfieldsapi = \core_user\fields::for_identity($this->context, false)->with_name();
$fields .= $userfieldsapi->get_sql('u')->selects; $fields .= $userfieldsapi->get_sql('u')->selects;
if ($count) { if ($count) {
......
...@@ -92,9 +92,9 @@ class acceptances_table extends \table_sql { ...@@ -92,9 +92,9 @@ class acceptances_table extends \table_sql {
} }
// TODO Does not support custom user profile fields (MDL-70456). // TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity(\context_system::instance(), false)->with_userpic(); $userfieldsapi = \core_user\fields::for_identity(\context_system::instance(), false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects; $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$extrafields = $userfieldsapi->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]); $extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
$this->set_sql("$userfields", $this->set_sql("$userfields",
"{user} u", "{user} u",
...@@ -105,7 +105,7 @@ class acceptances_table extends \table_sql { ...@@ -105,7 +105,7 @@ class acceptances_table extends \table_sql {
} }
$this->add_column_header('fullname', get_string('fullnameuser', 'core')); $this->add_column_header('fullname', get_string('fullnameuser', 'core'));
foreach ($extrafields as $field) { foreach ($extrafields as $field) {
$this->add_column_header($field, \core\user_fields::get_display_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())) { if (!$this->is_downloading() && !has_capability('tool/policy:acceptbehalf', \context_system::instance())) {
...@@ -170,7 +170,7 @@ class acceptances_table extends \table_sql { ...@@ -170,7 +170,7 @@ class acceptances_table extends \table_sql {
* Helper configuration method. * Helper configuration method.
*/ */
protected function configure_for_single_version() { protected function configure_for_single_version() {
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$userfieldsmod = $userfieldsapi->get_sql('m', false, 'mod', '', false)->selects; $userfieldsmod = $userfieldsapi->get_sql('m', false, 'mod', '', false)->selects;
$v = key($this->versionids); $v = key($this->versionids);
$this->sql->fields .= ", $userfieldsmod, a{$v}.status AS status{$v}, a{$v}.note, ". $this->sql->fields .= ", $userfieldsmod, a{$v}.status AS status{$v}, a{$v}.note, ".
......
...@@ -343,7 +343,7 @@ class api { ...@@ -343,7 +343,7 @@ class api {
global $DB; global $DB;
$ctxfields = context_helper::get_preload_record_columns_sql('c'); $ctxfields = context_helper::get_preload_record_columns_sql('c');
$userfieldsapi = \core\user_fields::for_name()->with_userpic()->including(...($extrafields ?? [])); $userfieldsapi = \core_user\fields::for_name()->with_userpic()->including(...($extrafields ?? []));
$userfields = $userfieldsapi->get_sql('u')->selects; $userfields = $userfieldsapi->get_sql('u')->selects;
$sql = "SELECT $ctxfields $userfields $sql = "SELECT $ctxfields $userfields
...@@ -682,7 +682,7 @@ class api { ...@@ -682,7 +682,7 @@ class api {
$vsql = ' AND a.policyversionid ' . $vsql; $vsql = ' AND a.policyversionid ' . $vsql;
} }
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$userfieldsmod = $userfieldsapi->get_sql('m', false, 'mod', '', false)->selects; $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, $sql = "SELECT u.id AS mainuserid, a.policyversionid, a.status, a.lang, a.timemodified, a.usermodified, a.note,
u.policyagreed, $userfieldsmod u.policyagreed, $userfieldsmod
......
...@@ -128,7 +128,7 @@ class accept_policy extends \moodleform { ...@@ -128,7 +128,7 @@ class accept_policy extends \moodleform {
$usernames = []; $usernames = [];
list($sql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED); list($sql, $params) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
$params['usercontextlevel'] = CONTEXT_USER; $params['usercontextlevel'] = CONTEXT_USER;
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$users = $DB->get_records_sql("SELECT u.id" . $userfieldsapi->get_sql('u')->selects . ", " . $users = $DB->get_records_sql("SELECT u.id" . $userfieldsapi->get_sql('u')->selects . ", " .
\context_helper::get_preload_record_columns_sql('ctx') . \context_helper::get_preload_record_columns_sql('ctx') .
" FROM {user} u JOIN {context} ctx ON ctx.contextlevel=:usercontextlevel AND ctx.instanceid = u.id " FROM {user} u JOIN {context} ctx ON ctx.contextlevel=:usercontextlevel AND ctx.instanceid = u.id
......
...@@ -155,7 +155,7 @@ class process { ...@@ -155,7 +155,7 @@ class process {
'interests', 'interests',
); );
// Include all name fields. // Include all name fields.
$this->standardfields = array_merge($this->standardfields, \core\user_fields::get_name_fields()); $this->standardfields = array_merge($this->standardfields, \core_user\fields::get_name_fields());
} }
/** /**
......
...@@ -183,13 +183,13 @@ ...@@ -183,13 +183,13 @@
// These columns are always shown in the users list. // These columns are always shown in the users list.
$requiredcolumns = array('city', 'country', 'lastaccess'); $requiredcolumns = array('city', 'country', 'lastaccess');
// Extra columns containing the extra user fields, excluding the required columns (city and country, to be specific). // Extra columns containing the extra user fields, excluding the required columns (city and country, to be specific).
$userfields = \core\user_fields::for_identity($context, true)->with_name()->excluding(...$requiredcolumns); $userfields = \core_user\fields::for_identity($context, true)->with_name()->excluding(...$requiredcolumns);
$extracolumns = $userfields->get_required_fields(); $extracolumns = $userfields->get_required_fields();
// Get all user name fields as an array. // Get all user name fields as an array.
$columns = array_merge($extracolumns, $requiredcolumns); $columns = array_merge($extracolumns, $requiredcolumns);
foreach ($columns as $column) { foreach ($columns as $column) {
$string[$column] = \core\user_fields::get_display_name($column); $string[$column] = \core_user\fields::get_display_name($column);
if ($sort != $column) { if ($sort != $column) {
$columnicon = ""; $columnicon = "";
if ($column == "lastaccess") { if ($column == "lastaccess") {
......
...@@ -64,7 +64,7 @@ if (count($cohorts) < 2) { ...@@ -64,7 +64,7 @@ if (count($cohorts) < 2) {
} }
$countries = get_string_manager()->get_list_of_countries(true); $countries = get_string_manager()->get_list_of_countries(true);
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects; $namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
foreach ($users as $key => $id) { foreach ($users as $key => $id) {
$user = $DB->get_record('user', array('id' => $id), 'id, ' . $namefields . ', username, $user = $DB->get_record('user', array('id' => $id), 'id, ' . $namefields . ', username,
......
...@@ -24,7 +24,7 @@ echo $OUTPUT->header(); ...@@ -24,7 +24,7 @@ echo $OUTPUT->header();
$countries = get_string_manager()->get_list_of_countries(true); $countries = get_string_manager()->get_list_of_countries(true);
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects; $namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
foreach ($users as $key => $id) { foreach ($users as $key => $id) {
$user = $DB->get_record('user', array('id'=>$id), 'id, ' . $namefields . ', username, email, country, lastaccess, city'); $user = $DB->get_record('user', array('id'=>$id), 'id, ' . $namefields . ', username, email, country, lastaccess, city');
......
...@@ -197,7 +197,7 @@ class condition extends \core_availability\condition { ...@@ -197,7 +197,7 @@ class condition extends \core_availability\condition {
$this->customfield); $this->customfield);
} }
} else { } else {
$translatedfieldname = \core\user_fields::get_display_name($this->standardfield); $translatedfieldname = \core_user\fields::get_display_name($this->standardfield);
} }
$context = \context_course::instance($course->id); $context = \context_course::instance($course->id);
$a = new \stdClass(); $a = new \stdClass();
......
...@@ -44,23 +44,23 @@ class frontend extends \core_availability\frontend { ...@@ -44,23 +44,23 @@ class frontend extends \core_availability\frontend {
\section_info $section = null) { \section_info $section = null) {
// Standard user fields. // Standard user fields.
$standardfields = array( $standardfields = array(
'firstname' => \core\user_fields::get_display_name('firstname'), 'firstname' => \core_user\fields::get_display_name('firstname'),
'lastname' => \core\user_fields::get_display_name('lastname'), 'lastname' => \core_user\fields::get_display_name('lastname'),
'email' => \core\user_fields::get_display_name('email'), 'email' => \core_user\fields::get_display_name('email'),
'city' => \core\user_fields::get_display_name('city'), 'city' => \core_user\fields::get_display_name('city'),
'country' => \core\user_fields::get_display_name('country'), 'country' => \core_user\fields::get_display_name('country'),
'url' => \core\user_fields::get_display_name('url'), 'url' => \core_user\fields::get_display_name('url'),
'icq' => \core\user_fields::get_display_name('icq'), 'icq' => \core_user\fields::get_display_name('icq'),
'skype' => \core\user_fields::get_display_name('skype'), 'skype' => \core_user\fields::get_display_name('skype'),
'aim' => \core\user_fields::get_display_name('aim'), 'aim' => \core_user\fields::get_display_name('aim'),
'yahoo' => \core\user_fields::get_display_name('yahoo'), 'yahoo' => \core_user\fields::get_display_name('yahoo'),
'msn' => \core\user_fields::get_display_name('msn'), 'msn' => \core_user\fields::get_display_name('msn'),
'idnumber' => \core\user_fields::get_display_name('idnumber'), 'idnumber' => \core_user\fields::get_display_name('idnumber'),
'institution' => \core\user_fields::get_display_name('institution'), 'institution' => \core_user\fields::get_display_name('institution'),
'department' => \core\user_fields::get_display_name('department'), 'department' => \core_user\fields::get_display_name('department'),
'phone1' => \core\user_fields::get_display_name('phone1'), 'phone1' => \core_user\fields::get_display_name('phone1'),
'phone2' => \core\user_fields::get_display_name('phone2'), 'phone2' => \core_user\fields::get_display_name('phone2'),
'address' => \core\user_fields::get_display_name('address') 'address' => \core_user\fields::get_display_name('address')
); );
\core_collator::asort($standardfields); \core_collator::asort($standardfields);
......
...@@ -1365,7 +1365,7 @@ class backup_users_structure_step extends backup_structure_step { ...@@ -1365,7 +1365,7 @@ class backup_users_structure_step extends backup_structure_step {
'phone2', 'institution', 'department', 'address', 'phone2', 'institution', 'department', 'address',
'city', 'country', 'lastip', 'picture', 'city', 'country', 'lastip', 'picture',
'url', 'description', 'descriptionformat', 'imagealt', 'auth'); 'url', 'description', 'descriptionformat', 'imagealt', 'auth');
$anonfields = array_merge($anonfields, \core\user_fields::get_name_fields()); $anonfields = array_merge($anonfields, \core_user\fields::get_name_fields());
// Add anonymized fields to $userfields with custom final element // Add anonymized fields to $userfields with custom final element
foreach ($anonfields as $field) { foreach ($anonfields as $field) {
......
...@@ -58,7 +58,7 @@ class external_badge implements renderable { ...@@ -58,7 +58,7 @@ class external_badge implements renderable {
global $DB; global $DB;
// At this point a user has connected a backpack. So, we are going to get // At this point a user has connected a backpack. So, we are going to get
// their backpack email rather than their account email. // their backpack email rather than their account email.
$userfieldsapi = \core\user_fields::for_name(); $userfieldsapi = \core_user\fields::for_name();
$namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects; $namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$user = $DB->get_record_sql("SELECT {$namefields}, b.email $user = $DB->get_record_sql("SELECT {$namefields}, b.email
FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
......
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