Commit 558cc1b8 authored by sam marshall's avatar sam marshall
Browse files

MDL-45242 Lib: Replace calls to deprecated functions

In all cases changes have been kept to a minimum while not making
the code completely horrible. For example, there are many instances
where it would probably be better to rewrite a query entirely, but
I have not done that (in order to reduce the risk of changes).
parent 3f003455
...@@ -124,8 +124,9 @@ class task_log_table extends \table_sql { ...@@ -124,8 +124,9 @@ class task_log_table extends \table_sql {
$sort = "ORDER BY $sort"; $sort = "ORDER BY $sort";
} }
$extrafields = get_extra_user_fields(\context_system::instance()); // TODO Does not support custom user profile fields (MDL-70456).
$userfields = \user_picture::fields('u', $extrafields, 'userid2', 'user'); $userfieldsapi = \core\user_fields::for_identity(\context_system::instance(), false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, 'user', 'userid2', false)->selects;
$where = ''; $where = '';
if (!empty($this->sql->where)) { if (!empty($this->sql->where)) {
......
...@@ -271,7 +271,8 @@ if ($roleid) { ...@@ -271,7 +271,8 @@ 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) {
$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); $roleusers = get_role_users($roleid, $context, false, $userfields);
if (!empty($roleusers)) { if (!empty($roleusers)) {
$strroleusers = array(); $strroleusers = array();
......
...@@ -126,7 +126,8 @@ class cohort_role_assignments_table extends table_sql { ...@@ -126,7 +126,8 @@ class cohort_role_assignments_table extends table_sql {
* Setup the headers for the table. * Setup the headers for the table.
*/ */
protected function define_table_columns() { 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. // Define headers and columns.
$cols = array( $cols = array(
...@@ -170,14 +171,12 @@ class cohort_role_assignments_table extends table_sql { ...@@ -170,14 +171,12 @@ class cohort_role_assignments_table extends table_sql {
protected function get_sql_and_params($count = false) { protected function get_sql_and_params($count = false) {
$fields = 'uca.id, uca.cohortid, uca.userid, uca.roleid, '; $fields = 'uca.id, uca.cohortid, uca.userid, uca.roleid, ';
$fields .= 'c.name as cohortname, c.idnumber as cohortidnumber, c.contextid as cohortcontextid, '; $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. // Add extra user fields that we need for the graded user.
$extrafields = get_extra_user_fields($this->context); // TODO Does not support custom user profile fields (MDL-70456).
foreach ($extrafields as $field) { $userfieldsapi = \core\user_fields::for_identity($this->context, false)->with_name();
$fields .= 'u.' . $field . ', '; $fields .= $userfieldsapi->get_sql('u')->selects;
}
$fields .= get_all_user_name_fields(true, 'u');
if ($count) { if ($count) {
$select = "COUNT(1)"; $select = "COUNT(1)";
......
...@@ -189,7 +189,8 @@ class api { ...@@ -189,7 +189,8 @@ class api {
$dpos = []; $dpos = [];
$context = context_system::instance(); $context = context_system::instance();
foreach ($dporoles as $roleid) { 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 . ', ' . $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, '.
'u.country, u.picture, u.idnumber, u.department, u.institution, '. 'u.country, u.picture, u.idnumber, u.department, u.institution, '.
......
...@@ -700,13 +700,15 @@ class external extends external_api { ...@@ -700,13 +700,15 @@ 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);
$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. // Exclude admins and guest user.
$excludedusers = array_keys(get_admins()) + [guest_user()->id]; $excludedusers = array_keys(get_admins()) + [guest_user()->id];
$sort = 'lastname ASC, firstname ASC'; $sort = 'lastname ASC, firstname ASC';
$fields = 'id,' . $allusernames; $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)) { if (!empty($extrafields)) {
$fields .= ',' . implode(',', $extrafields); $fields .= ',' . implode(',', $extrafields);
} }
......
...@@ -187,7 +187,8 @@ class helper { ...@@ -187,7 +187,8 @@ class helper {
global $DB; global $DB;
// Get users that the user has role assignments to. // 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 $sql = "SELECT u.id, $allusernames
FROM {role_assignments} ra, {context} c, {user} u FROM {role_assignments} ra, {context} c, {user} u
WHERE ra.userid = :userid WHERE ra.userid = :userid
......
...@@ -62,7 +62,8 @@ class tool_dataprivacy_data_request_form extends \core\form\persistent { ...@@ -62,7 +62,8 @@ class tool_dataprivacy_data_request_form extends \core\form\persistent {
'valuehtmlcallback' => function($value) { 'valuehtmlcallback' => function($value) {
global $OUTPUT; 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; $fields = 'id, email, ' . $allusernames;
$user = \core_user::get_user($value, $fields); $user = \core_user::get_user($value, $fields);
$useroptiondata = [ $useroptiondata = [
......
...@@ -879,8 +879,9 @@ class external extends external_api { ...@@ -879,8 +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).
$extrasearchfields = \core\user_fields::get_identity_fields($context, false); $userfieldsapi = \core\user_fields::for_identity($context, false)->with_userpic();
$fields = \user_picture::fields('u', $extrasearchfields); $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($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);
......
...@@ -92,7 +92,8 @@ class template_plans_table extends table_sql { ...@@ -92,7 +92,8 @@ class template_plans_table extends table_sql {
* Setup the headers for the table. * Setup the headers for the table.
*/ */
protected function define_table_columns() { 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. // Define headers and columns.
$cols = array( $cols = array(
...@@ -132,14 +133,12 @@ class template_plans_table extends table_sql { ...@@ -132,14 +133,12 @@ class template_plans_table extends table_sql {
* @return array containing sql to use and an array of params. * @return array containing sql to use and an array of params.
*/ */
protected function get_sql_and_params($count = false) { 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. // Add extra user fields that we need for the graded user.
$extrafields = get_extra_user_fields($this->context); // TODO Does not support custom user profile fields (MDL-70456).
foreach ($extrafields as $field) { $userfieldsapi = \core\user_fields::for_identity($this->context, false)->with_name();
$fields .= 'u.' . $field . ', '; $fields .= $userfieldsapi->get_sql('u')->selects;
}
$fields .= get_all_user_name_fields(true, 'u');
if ($count) { if ($count) {
$select = "COUNT(1)"; $select = "COUNT(1)";
......
...@@ -91,8 +91,10 @@ class acceptances_table extends \table_sql { ...@@ -91,8 +91,10 @@ class acceptances_table extends \table_sql {
} }
} }
$extrafields = get_extra_user_fields(\context_system::instance()); // TODO Does not support custom user profile fields (MDL-70456).
$userfields = \user_picture::fields('u', $extrafields); $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", $this->set_sql("$userfields",
"{user} u", "{user} u",
...@@ -103,7 +105,7 @@ class acceptances_table extends \table_sql { ...@@ -103,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, 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())) { if (!$this->is_downloading() && !has_capability('tool/policy:acceptbehalf', \context_system::instance())) {
...@@ -168,7 +170,8 @@ class acceptances_table extends \table_sql { ...@@ -168,7 +170,8 @@ class acceptances_table extends \table_sql {
* Helper configuration method. * Helper configuration method.
*/ */
protected function configure_for_single_version() { 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); $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, ".
"a{$v}.timemodified, a{$v}.usermodified AS usermodified{$v}"; "a{$v}.timemodified, a{$v}.usermodified AS usermodified{$v}";
...@@ -643,4 +646,4 @@ class acceptances_table extends \table_sql { ...@@ -643,4 +646,4 @@ class acceptances_table extends \table_sql {
} }
return parent::other_cols($column, $row); return parent::other_cols($column, $row);
} }
} }
\ No newline at end of file
...@@ -343,10 +343,10 @@ class api { ...@@ -343,10 +343,10 @@ class api {
global $DB; global $DB;
$ctxfields = context_helper::get_preload_record_columns_sql('c'); $ctxfields = context_helper::get_preload_record_columns_sql('c');
$namefields = get_all_user_name_fields(true, 'u'); $userfieldsapi = \core\user_fields::for_name()->with_userpic()->including(...($extrafields ?? []));
$pixfields = user_picture::fields('u', $extrafields); $userfields = $userfieldsapi->get_sql('u')->selects;
$sql = "SELECT $ctxfields, $namefields, $pixfields $sql = "SELECT $ctxfields $userfields
FROM {role_assignments} ra FROM {role_assignments} ra
JOIN {context} c ON c.contextlevel = ".CONTEXT_USER." AND ra.contextid = c.id JOIN {context} c ON c.contextlevel = ".CONTEXT_USER." AND ra.contextid = c.id
JOIN {user} u ON c.instanceid = u.id JOIN {user} u ON c.instanceid = u.id
...@@ -682,7 +682,8 @@ class api { ...@@ -682,7 +682,8 @@ class api {
$vsql = ' AND a.policyversionid ' . $vsql; $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, $sql = "SELECT u.id AS mainuserid, a.policyversionid, a.status, a.lang, a.timemodified, a.usermodified, a.note,
u.policyagreed, $userfieldsmod u.policyagreed, $userfieldsmod
FROM {user} u FROM {user} u
......
...@@ -128,7 +128,8 @@ class accept_policy extends \moodleform { ...@@ -128,7 +128,8 @@ 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;
$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') . \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
WHERE u.id " . $sql, $params); WHERE u.id " . $sql, $params);
......
...@@ -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, get_all_user_name_fields()); $this->standardfields = array_merge($this->standardfields, \core\user_fields::get_name_fields());
} }
/** /**
......
...@@ -183,13 +183,15 @@ ...@@ -183,13 +183,15 @@
// 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).
$extracolumns = get_extra_user_fields($context, $requiredcolumns); // TODO Does not support custom user profile fields (MDL-70456).
// Get all user name fields as an array. $userfields = \core\user_fields::for_identity($context, false)->excluding(...$requiredcolumns);
$allusernamefields = get_all_user_name_fields(false, null, null, null, true); $extracolumns = $userfields->get_required_fields();
// Get all user name fields as an array, but with firstname and lastname first.
$allusernamefields = \core\user_fields::get_name_fields(true);
$columns = array_merge($allusernamefields, $extracolumns, $requiredcolumns); $columns = array_merge($allusernamefields, $extracolumns, $requiredcolumns);
foreach ($columns as $column) { foreach ($columns as $column) {
$string[$column] = get_user_field_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,8 @@ if (count($cohorts) < 2) { ...@@ -64,7 +64,8 @@ if (count($cohorts) < 2) {
} }
$countries = get_string_manager()->get_list_of_countries(true); $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) { 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,
email, country, lastaccess, city, deleted'); email, country, lastaccess, city, deleted');
......
...@@ -24,7 +24,8 @@ echo $OUTPUT->header(); ...@@ -24,7 +24,8 @@ echo $OUTPUT->header();
$countries = get_string_manager()->get_list_of_countries(true); $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) { 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');
$user->fullname = fullname($user, true); $user->fullname = fullname($user, true);
......
...@@ -241,7 +241,8 @@ class web_service_token_form extends moodleform { ...@@ -241,7 +241,8 @@ class web_service_token_form extends moodleform {
if ($usertotal < 500) { if ($usertotal < 500) {
list($sort, $params) = users_order_by_sql('u'); list($sort, $params) = users_order_by_sql('u');
// User searchable selector - return users who are confirmed, not deleted, not suspended and not a guest. // 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 FROM {user} u
WHERE u.deleted = 0 WHERE u.deleted = 0
AND u.confirmed = 1 AND u.confirmed = 1
......
...@@ -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 = get_user_field_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' => get_user_field_name('firstname'), 'firstname' => \core\user_fields::get_display_name('firstname'),
'lastname' => get_user_field_name('lastname'), 'lastname' => \core\user_fields::get_display_name('lastname'),
'email' => get_user_field_name('email'), 'email' => \core\user_fields::get_display_name('email'),
'city' => get_user_field_name('city'), 'city' => \core\user_fields::get_display_name('city'),
'country' => get_user_field_name('country'), 'country' => \core\user_fields::get_display_name('country'),
'url' => get_user_field_name('url'), 'url' => \core\user_fields::get_display_name('url'),
'icq' => get_user_field_name('icq'), 'icq' => \core\user_fields::get_display_name('icq'),
'skype' => get_user_field_name('skype'), 'skype' => \core\user_fields::get_display_name('skype'),
'aim' => get_user_field_name('aim'), 'aim' => \core\user_fields::get_display_name('aim'),
'yahoo' => get_user_field_name('yahoo'), 'yahoo' => \core\user_fields::get_display_name('yahoo'),
'msn' => get_user_field_name('msn'), 'msn' => \core\user_fields::get_display_name('msn'),
'idnumber' => get_user_field_name('idnumber'), 'idnumber' => \core\user_fields::get_display_name('idnumber'),
'institution' => get_user_field_name('institution'), 'institution' => \core\user_fields::get_display_name('institution'),
'department' => get_user_field_name('department'), 'department' => \core\user_fields::get_display_name('department'),
'phone1' => get_user_field_name('phone1'), 'phone1' => \core\user_fields::get_display_name('phone1'),
'phone2' => get_user_field_name('phone2'), 'phone2' => \core\user_fields::get_display_name('phone2'),
'address' => get_user_field_name('address') 'address' => \core\user_fields::get_display_name('address')
); );
\core_collator::asort($standardfields); \core_collator::asort($standardfields);
......
...@@ -1368,7 +1368,7 @@ class backup_users_structure_step extends backup_structure_step { ...@@ -1368,7 +1368,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, get_all_user_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) {
......
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