Skip to content

Commit

Permalink
MDL-45242 Lib: Replace calls to deprecated functions
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
sammarshallou committed Mar 10, 2021
1 parent 3f00345 commit 558cc1b
Show file tree
Hide file tree
Showing 152 changed files with 612 additions and 394 deletions.
5 changes: 3 additions & 2 deletions admin/classes/task_log_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ public function query_db($pagesize, $useinitialsbar = true) {
$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)) {
Expand Down
3 changes: 2 additions & 1 deletion admin/roles/assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ protected function col_actions($data) {
* 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(
Expand Down Expand Up @@ -170,14 +171,12 @@ protected function define_table_configs() {
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)";
Expand Down
3 changes: 2 additions & 1 deletion admin/tool/dataprivacy/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ public static function get_site_dpos() {
$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, '.
Expand Down
6 changes: 4 additions & 2 deletions admin/tool/dataprivacy/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,13 +700,15 @@ public static function get_users($query) {
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);
}
Expand Down
3 changes: 2 additions & 1 deletion admin/tool/dataprivacy/classes/local/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public static function get_children_of_user($userid) {
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
Expand Down
3 changes: 2 additions & 1 deletion admin/tool/dataprivacy/createdatarequest_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function definition() {
'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 = [
Expand Down
5 changes: 3 additions & 2 deletions admin/tool/lp/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,9 @@ public static function search_users($query, $capability = '', $limitfrom = 0, $l
$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);
Expand Down
13 changes: 6 additions & 7 deletions admin/tool/lp/classes/output/template_plans_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ protected function col_name($row) {
* 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(
Expand Down Expand Up @@ -132,14 +133,12 @@ protected function define_table_configs() {
* @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)";
Expand Down
13 changes: 8 additions & 5 deletions admin/tool/policy/classes/acceptances_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ public function __construct($uniqueid, acceptances_filter $acceptancesfilter, re
}
}

$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",
Expand All @@ -103,7 +105,7 @@ public function __construct($uniqueid, acceptances_filter $acceptancesfilter, re
}
$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())) {
Expand Down Expand Up @@ -168,7 +170,8 @@ protected function add_column_header($key, $label, $sortable = true, $columnclas
* 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}";
Expand Down Expand Up @@ -643,4 +646,4 @@ public function other_cols($column, $row) {
}
return parent::other_cols($column, $row);
}
}
}
9 changes: 5 additions & 4 deletions admin/tool/policy/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ public static function get_user_minors($userid, array $extrafields = null) {
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
Expand Down Expand Up @@ -682,7 +682,8 @@ public static function get_user_acceptances($userid, $versions = null) {
$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
Expand Down
3 changes: 2 additions & 1 deletion admin/tool/policy/classes/form/accept_policy.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ protected function validate_and_get_users($versionids, $userids, $action) {
$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);
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/uploaduser/classes/process.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ protected function find_standard_fields(): void {
'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());
}

/**
Expand Down
10 changes: 6 additions & 4 deletions admin/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
3 changes: 2 additions & 1 deletion admin/user/user_bulk_cohortadd.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
}

$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');
Expand Down
3 changes: 2 additions & 1 deletion admin/user/user_bulk_display.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

$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);
Expand Down
3 changes: 2 additions & 1 deletion admin/webservice/forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ function definition() {
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
Expand Down
2 changes: 1 addition & 1 deletion availability/condition/profile/classes/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function get_description($full, $not, \core_availability\info $info) {
$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();
Expand Down
34 changes: 17 additions & 17 deletions availability/condition/profile/classes/frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@ protected function get_javascript_init_params($course, \cm_info $cm = null,
\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);

Expand Down
2 changes: 1 addition & 1 deletion backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ protected function define_structure() {
'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) {
Expand Down
3 changes: 2 additions & 1 deletion badges/classes/output/external_badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function __construct($badge, $recipient) {
global $DB;
// At this point a user has connected a backpack. So, we are going to get
// their backpack email rather than their account email.
$namefields = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$user = $DB->get_record_sql("SELECT {$namefields}, b.email
FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
WHERE b.userid = :userid", array('userid' => $recipient), IGNORE_MISSING);
Expand Down
3 changes: 2 additions & 1 deletion badges/classes/output/issued_badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function __construct($hash) {
array('hash' => $hash), IGNORE_MISSING);
if ($rec) {
// Get a recipient from database.
$namefields = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$user = $DB->get_record_sql("SELECT u.id, $namefields, u.deleted, u.email
FROM {user} u WHERE u.id = :userid", array('userid' => $rec->userid));
$this->recipient = $user;
Expand Down
Loading

0 comments on commit 558cc1b

Please sign in to comment.