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 {
$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();
......
......@@ -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 = [
......
......@@ -879,8 +879,9 @@ class external extends external_api {
$USER->id, SQL_PARAMS_NAMED);
// TODO Does not support custom user profile fields (MDL-70456).
$extrasearchfields = \core\user_fields::get_identity_fields($context, false);
$fields = \user_picture::fields('u', $extrasearchfields);
$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,15 @@
// 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);
// Get all user name fields as an array.
$allusernamefields = get_all_user_name_fields(false, null, null, null, true);
// TODO Does not support custom user profile fields (MDL-70456).
$userfields = \core\user_fields::for_identity($context, false)->excluding(...$requiredcolumns);
$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);
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") {
......
......@@ -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();
......
......@@ -44,23 +44,23 @@ class frontend extends \core_availability\frontend {
\section_info $section = null) {
// Standard user fields.
$standardfields = array(
'firstname' => get_user_field_name('firstname'),
'lastname' => get_user_field_name('lastname'),
'email' => get_user_field_name('email'),
'city' => get_user_field_name('city'),
'country' => get_user_field_name('country'),
'url' => get_user_field_name('url'),
'icq' => get_user_field_name('icq'),
'skype' => get_user_field_name('skype'),
'aim' => get_user_field_name('aim'),
'yahoo' => get_user_field_name('yahoo'),
'msn' => get_user_field_name('msn'),
'idnumber' => get_user_field_name('idnumber'),
'institution' => get_user_field_name('institution'),
'department' => get_user_field_name('department'),
'phone1' => get_user_field_name('phone1'),
'phone2' => get_user_field_name('phone2'),
'address' => get_user_field_name('address')
'firstname' => \core\user_fields::get_display_name('firstname'),
'lastname' => \core\user_fields::get_display_name('lastname'),
'email' => \core\user_fields::get_display_name('email'),
'city' => \core\user_fields::get_display_name('city'),
'country' => \core\user_fields::get_display_name('country'),
'url' => \core\user_fields::get_display_name('url'),
'icq' => \core\user_fields::get_display_name('icq'),
'skype' => \core\user_fields::get_display_name('skype'),
'aim' => \core\user_fields::get_display_name('aim'),
'yahoo' => \core\user_fields::get_display_name('yahoo'),
'msn' => \core\user_fields::get_display_name('msn'),
'idnumber' => \core\user_fields::get_display_name('idnumber'),
'institution' => \core\user_fields::get_display_name('institution'),
'department' => \core\user_fields::get_display_name('department'),
'phone1' => \core\user_fields::get_display_name('phone1'),
'phone2' => \core\user_fields::get_display_name('phone2'),
'address' => \core\user_fields::get_display_name('address')
);
\core_collator::asort($standardfields);
......
......@@ -1368,7 +1368,7 @@ class backup_users_structure_step extends backup_structure_step {
'phone2', 'institution', 'department', 'address',
'city', 'country', 'lastip', 'picture',
'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
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