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
......@@ -368,7 +368,8 @@ function users_order_by_sql($usertablealias = '', $search = null, context $conte
$params[$paramkey] = $search;
$paramkey++;
$fieldstocheck = array_merge(array('firstname', 'lastname'), get_extra_user_fields($context));
// TODO Does not support custom user profile fields (MDL-70456).
$fieldstocheck = array_merge(array('firstname', 'lastname'), \core\user_fields::get_identity_fields($context, false));
foreach ($fieldstocheck as $key => $field) {
$exactconditions[] = 'LOWER(' . $tableprefix . $field . ') = LOWER(:' . $paramkey . ')';
$params[$paramkey] = $search;
......@@ -512,14 +513,11 @@ function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperp
// If a context is specified, get extra user fields that the current user
// is supposed to see.
$extrafields = '';
if ($extracontext) {
$extrafields = get_extra_user_fields_sql($extracontext, '', '',
array('id', 'username', 'email', 'firstname', 'lastname', 'city', 'country',
'lastaccess', 'confirmed', 'mnethostid'));
}
$namefields = get_all_user_name_fields(true);
$extrafields = "$extrafields, $namefields";
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($extracontext, false)->with_name()
->excluding('id', 'username', 'email', 'firstname', 'lastname', 'city', 'country',
'lastaccess', 'confirmed', 'mnethostid');
$extrafields = $userfields->get_sql()->selects;
// warning: will return UNCONFIRMED USERS
return $DB->get_records_sql("SELECT id, username, email, city, country, lastaccess, confirmed, mnethostid, suspended $extrafields
......
......@@ -3083,8 +3083,10 @@ function user_get_participants_sql($courseid, $groupid = 0, $accesssince = 0, $r
$joins = array('FROM {user} u');
$wheres = array();
$userfields = get_extra_user_fields($context);
$userfieldssql = user_picture::fields('u', $userfields);
// TODO Does not support custom user profile fields (MDL-70456).
$userfields = \core\user_fields::get_identity_fields($context, false);
$userfieldsapi = \core\user_fields::for_userpic()->including(...$userfields);
$userfieldssql = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
if ($isfrontpage) {
$select = "SELECT $userfieldssql, u.lastaccess";
......
......@@ -1321,7 +1321,8 @@ function groups_user_groups_visible($course, $userid, $cm = null) {
function groups_get_groups_members($groupsids, $extrafields=null, $sort='lastname ASC') {
global $DB;
$userfields = user_picture::fields('u', $extrafields);
$userfieldsapi = \core\user_fields::for_userpic()->including(...($extrafields ?? []));
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
list($insql, $params) = $DB->get_in_or_equal($groupsids);
return $DB->get_records_sql("SELECT $userfields
......
......@@ -3540,7 +3540,7 @@ function fullname($user, $override=false) {
}
// Get all of the name fields.
$allnames = get_all_user_name_fields();
$allnames = \core\user_fields::get_name_fields();
if ($CFG->debugdeveloper) {
foreach ($allnames as $allname) {
if (!property_exists($user, $allname)) {
......@@ -3645,7 +3645,10 @@ function fullname($user, $override=false) {
* @return object User name fields.
*/
function username_load_fields_from_object($addtoobject, $secondobject, $prefix = null, $additionalfields = null) {
$fields = get_all_user_name_fields(false, null, $prefix);
$fields = \core\user_fields::get_name_fields();
foreach ($fields as &$field) {
$field = $prefix . $field;
}
if ($additionalfields) {
// Additional fields can specify their own 'alias' such as 'id' => 'userid'. This checks to see if
// the key is a number and then sets the key to the array value.
......
......@@ -230,7 +230,8 @@ class user_picture implements renderable {
}
if ($needrec) {
$this->user = $DB->get_record('user', array('id'=>$user->id), self::fields(), MUST_EXIST);
$this->user = $DB->get_record('user', array('id'=>$user->id),
implode(',', \core\user_fields::get_picture_fields()), MUST_EXIST);
} else {
$this->user = clone($user);
}
......
......@@ -608,7 +608,7 @@ class flexible_table {
if (isset($this->columns[$column])) {
continue; // This column is OK.
}
if (in_array($column, get_all_user_name_fields()) &&
if (in_array($column, \core\user_fields::get_name_fields()) &&
isset($this->columns['fullname'])) {
continue; // This column is OK.
}
......@@ -1235,7 +1235,7 @@ class flexible_table {
$nameformat = get_string('fullnamedisplay');
}
$requirednames = order_in_string(get_all_user_name_fields(), $nameformat);
$requirednames = order_in_string(\core\user_fields::get_name_fields(), $nameformat);
if (!empty($requirednames)) {
if ($this->is_sortable($column)) {
......@@ -1315,7 +1315,7 @@ class flexible_table {
$sortdata = array_merge([$sortby => $sortorder], $sortdata);
}
$usernamefields = get_all_user_name_fields();
$usernamefields = \core\user_fields::get_name_fields();
$sortdata = array_filter($sortdata, function($sortby) use ($usernamefields) {
$isvalidsort = $sortby && $this->is_sortable($sortby);
$isvalidsort = $isvalidsort && empty($this->prefs['collapse'][$sortby]);
......
......@@ -3788,7 +3788,7 @@ class core_moodlelib_testcase extends advanced_testcase {
// User information for showing a picture.
$user = new stdClass();
$additionalfields = explode(',', user_picture::fields());
$additionalfields = explode(',', implode(',', \core\user_fields::get_picture_fields()));
$user = username_load_fields_from_object($user, $userinfo, null, $additionalfields);
$user->id = $userinfo->userid;
$expectedarray = new stdClass();
......@@ -3817,7 +3817,7 @@ class core_moodlelib_testcase extends advanced_testcase {
// Return an object with user picture information.
$user = new stdClass();
$additionalfields = explode(',', user_picture::fields());
$additionalfields = explode(',', implode(',', \core\user_fields::get_picture_fields()));
$user = username_load_fields_from_object($user, $userinfo, 'author', $additionalfields);
$user->id = $userinfo->userid;
$expectedarray = new stdClass();
......
......@@ -33,6 +33,11 @@ require_once($CFG->libdir . '/outputcomponents.php');
*/
class core_outputcomponents_testcase extends advanced_testcase {
/**
* Tests user_picture::fields.
*
* @deprecated since Moodle 3.11 MDL-45242
*/
public function test_fields_aliasing() {
$fields = user_picture::fields();
$fields = array_map('trim', explode(',', $fields));
......@@ -60,10 +65,16 @@ class core_outputcomponents_testcase extends advanced_testcase {
$this->assertContains($expected, $returned, "Expected pattern '$expected' not returned");
}
$this->assertContains("custom1 AS prefixcustom1", $returned, "Expected pattern 'custom1 AS prefixcustom1' not returned");
// Deprecation warnings for user_picture::fields.
$this->assertDebuggingCalledCount(2);
}
/**
* Tests user_picture::unalias.
*/
public function test_fields_unaliasing() {
$fields = user_picture::fields();
$fields = implode(',', \core\user_fields::get_picture_fields());
$fields = array_map('trim', explode(',', $fields));
$fakerecord = new stdClass();
......@@ -86,8 +97,11 @@ class core_outputcomponents_testcase extends advanced_testcase {
$this->assertSame('Value of custom1', $returned->custom1);
}
/**
* Tests user_picture::unalias with null values.
*/
public function test_fields_unaliasing_null() {
$fields = user_picture::fields();
$fields = implode(',', \core\user_fields::get_picture_fields());
$fields = array_map('trim', explode(',', $fields));
$fakerecord = new stdClass();
......
......@@ -111,8 +111,9 @@ class api {
global $DB;
// Get the user fields we want.
$ufields = \user_picture::fields('u', array('lastaccess'), 'userfrom_id', 'userfrom_');
$ufields2 = \user_picture::fields('u2', array('lastaccess'), 'userto_id', 'userto_');
$userfieldsapi = \core\user_fields::for_userpic()->including('lastaccess');
$ufields = $userfieldsapi->get_sql('u', false, 'userfrom_', '', false)->selects;
$ufields2 = $userfieldsapi->get_sql('u2', false, 'userto_', '', false)->selects;
// Add the uniqueid column to make each row unique and avoid SQL errors.
$uniqueidsql = $DB->sql_concat('m.id', "'_'", 'm.useridfrom', "'_'", 'mcm.userid');
......@@ -1022,7 +1023,8 @@ class api {
debugging('\core_message\api::get_contacts_with_unread_message_count is deprecated and no longer used',
DEBUG_DEVELOPER);
$userfields = \user_picture::fields('u', array('lastaccess'));
$userfieldsapi = \core\user_fields::for_userpic()->including('lastaccess');
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$unreadcountssql = "SELECT $userfields, count(m.id) as messagecount
FROM {message_contacts} mc
INNER JOIN {user} u
......@@ -1063,7 +1065,8 @@ class api {
debugging('\core_message\api::get_non_contacts_with_unread_message_count is deprecated and no longer used',
DEBUG_DEVELOPER);
$userfields = \user_picture::fields('u', array('lastaccess'));
$userfieldsapi = \core\user_fields::for_userpic()->including('lastaccess');
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$unreadcountssql = "SELECT $userfields, count(m.id) as messagecount
FROM {user} u
INNER JOIN {messages} m
......@@ -1885,7 +1888,8 @@ class api {
public static function get_blocked_users($userid) {
global $DB;
$userfields = \user_picture::fields('u', array('lastaccess'));
$userfieldsapi = \core\user_fields::for_userpic()->including('lastaccess');
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$blockeduserssql = "SELECT $userfields
FROM {message_users_blocked} mub
INNER JOIN {user} u
......
......@@ -429,7 +429,8 @@ class helper {
}
list($useridsql, $usersparams) = $DB->get_in_or_equal($userids);
$userfields = \user_picture::fields('u', array('lastaccess'));
$userfieldsapi = \core\user_fields::for_userpic()->including('lastaccess');
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$userssql = "SELECT $userfields, u.deleted, mc.id AS contactid, mub.id AS blockedid
FROM {user} u
LEFT JOIN {message_contacts} mc
......
......@@ -183,7 +183,8 @@ function message_search_users($courseids, $searchtext, $sort='', $exceptions='')
}
$fullname = $DB->sql_fullname();
$ufields = user_picture::fields('u');
$userfieldsapi = \core\user_fields::for_userpic();
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
if (!empty($sort)) {
$order = ' ORDER BY '. $sort;
......@@ -549,11 +550,12 @@ function message_get_messages($useridto, $useridfrom = 0, $notifications = -1, $
global $DB;
// If the 'useridto' value is empty then we are going to retrieve messages sent by the useridfrom to any user.
$userfieldsapi = \core\user_fields::for_name();
if (empty($useridto)) {
$userfields = get_all_user_name_fields(true, 'u', '', 'userto');
$userfields = $userfieldsapi->get_sql('u', false, 'userto', '', false)->selects;
$messageuseridtosql = 'u.id as useridto';
} else {
$userfields = get_all_user_name_fields(true, 'u', '', 'userfrom');
$userfields = $userfieldsapi->get_sql('u', false, 'userfrom', '', false)->selects;
$messageuseridtosql = "$useridto as useridto";
}
......
......@@ -161,7 +161,8 @@ class send_email_task extends scheduled_task {
protected function get_users_messages_for_conversation(int $conversationid, int $userid) : moodle_recordset {
global $DB;
$usernamefields = \user_picture::fields('u');
$userfieldsapi = \core\user_fields::for_userpic();
$usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT $usernamefields, m.*
FROM {messages} m
JOIN {user} u
......
......@@ -58,7 +58,8 @@ class mod_assign_extension_form extends moodleform {
$usercount = 0;
$usershtml = '';
$extrauserfields = get_extra_user_fields($assign->get_context());
// TODO Does not support custom user profile fields (MDL-70456).
$extrauserfields = \core\user_fields::get_identity_fields($assign->get_context(), false);
foreach ($userlist as $userid) {
if ($usercount >= 5) {
$usershtml .= get_string('moreusers', 'assign', count($userlist) - 5);
......
......@@ -474,7 +474,8 @@ class assign_feedback_file extends assign_feedback_plugin {
$this->assignment->get_course_context()),
$this->assignment->is_blind_marking(),
$this->assignment->get_uniqueid_for_user($user->id),
get_extra_user_fields($this->assignment->get_context()));
// TODO Does not support custom user profile fields (MDL-70456).
\core\user_fields::get_identity_fields($this->assignment->get_context(), false));
$usershtml .= $this->assignment->get_renderer()->render($usersummary);
$usercount += 1;
}
......
......@@ -134,9 +134,11 @@ class assign_grading_table extends table_sql implements renderable {
$params['assignmentid3'] = (int)$this->assignment->get_instance()->id;
$params['newstatus'] = ASSIGN_SUBMISSION_STATUS_NEW;
$extrauserfields = get_extra_user_fields($this->assignment->get_context());
$fields = user_picture::fields('u', $extrauserfields) . ', ';
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->assignment->get_context(), false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$extrauserfields = $userfieldsapi->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]);
$fields = $userfields . ', ';
$fields .= 'u.id as userid, ';
$fields .= 's.status as status, ';
$fields .= 's.id as submissionid, ';
......@@ -406,7 +408,7 @@ class assign_grading_table extends table_sql implements renderable {
foreach ($extrauserfields as $extrafield) {
$columns[] = $extrafield;
$headers[] = get_user_field_name($extrafield);
$headers[] = \core\user_fields::get_display_name($extrafield);
}
} else {
// Record ID.
......
......@@ -589,7 +589,8 @@ function assign_print_recent_activity($course, $viewfullnames, $timestart) {
// Do not use log table if possible, it may be huge.
$dbparams = array($timestart, $course->id, 'assign', ASSIGN_SUBMISSION_STATUS_SUBMITTED);
$namefields = user_picture::fields('u', null, 'userid');
$userfieldsapi = \core\user_fields::for_userpic();
$namefields = $userfieldsapi->get_sql('u', false, '', 'userid', false)->selects;;
if (!$submissions = $DB->get_records_sql("SELECT asb.id, asb.timemodified, cm.id AS cmid, um.id as recordid,
$namefields
FROM {assign_submission} asb
......@@ -746,7 +747,8 @@ function assign_get_recent_mod_activity(&$activities,
$params['timestart'] = $timestart;
$params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
$userfields = user_picture::fields('u', null, 'userid');
$userfieldsapi = \core\user_fields::for_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', 'userid', false)->selects;
if (!$submissions = $DB->get_records_sql('SELECT asb.id, asb.timemodified, ' .
$userfields .
......@@ -833,7 +835,7 @@ function assign_get_recent_mod_activity(&$activities,
$activity->grade = $grades->items[0]->grades[$submission->userid]->str_long_grade;
}
$userfields = explode(',', user_picture::fields());
$userfields = explode(',', implode(',', \core\user_fields::get_picture_fields()));
foreach ($userfields as $userfield) {
if ($userfield == 'id') {
// Aliased in SQL above.
......
......@@ -2065,9 +2065,9 @@ class assign {
*/
private function get_grading_sort_sql() {
$usersort = flexible_table::get_sort_for_table('mod_assign_grading');
$extrauserfields = get_extra_user_fields($this->get_context());
$userfields = explode(',', user_picture::fields('', $extrauserfields));
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->context, false)->with_userpic();
$userfields = $userfieldsapi->get_required_fields();
$orderfields = explode(',', $usersort);
$validlist = [];
......@@ -4148,7 +4148,8 @@ class assign {
$viewfullnames,
$this->is_blind_marking(),
$this->get_uniqueid_for_user($user->id),
get_extra_user_fields($this->get_context()),
// TODO Does not support custom user profile fields (MDL-70456).
\core\user_fields::get_identity_fields($this->get_context(), false),
!$this->is_active_user($userid));
$o .= $this->get_renderer()->render($usersummary);
}
......@@ -4997,7 +4998,8 @@ class assign {
$usershtml = '';
$usercount = 0;
$extrauserfields = get_extra_user_fields($this->get_context());
// TODO Does not support custom user profile fields (MDL-70456).
$extrauserfields = \core\user_fields::get_identity_fields($this->get_context(), false);
$viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_context());
foreach ($userlist as $userid) {
if ($usercount >= 5) {
......@@ -5061,7 +5063,8 @@ class assign {
$usershtml = '';
$usercount = 0;
$extrauserfields = get_extra_user_fields($this->get_context());
// TODO Does not support custom user profile fields (MDL-70456).
$extrauserfields = \core\user_fields::get_identity_fields($this->get_context(), false);
$viewfullnames = has_capability('moodle/site:viewfullnames', $this->get_context());
foreach ($userlist as $userid) {
if ($usercount >= 5) {
......
......@@ -156,12 +156,13 @@ class assign_override_form extends moodleform {
list($sort) = users_order_by_sql('u');
// Get the list of appropriate users, depending on whether and how groups are used.
$userfieldsapi = \core\user_fields::for_name();
if ($accessallgroups) {
$users = get_enrolled_users($this->context, '', 0,
'u.id, u.email, ' . get_all_user_name_fields(true, 'u'), $sort);
'u.id, u.email, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects, $sort);
} else if ($groups = groups_get_activity_allowed_groups($cm)) {
$enrolledjoin = get_enrolled_join($this->context, 'u.id');
$userfields = 'u.id, u.email, ' . get_all_user_name_fields(true, 'u');
$userfields = 'u.id, u.email, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects;
list($ingroupsql, $ingroupparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
$params = $enrolledjoin->params + $ingroupparams;
$sql = "SELECT $userfields
......@@ -185,7 +186,8 @@ class assign_override_form extends moodleform {
}
$userchoices = array();
$canviewemail = in_array('email', get_extra_user_fields($this->context));
// TODO Does not support custom user profile fields (MDL-70456).
$canviewemail = in_array('email', \core\user_fields::get_identity_fields($this->context, false));
foreach ($users as $id => $user) {
if (empty($invalidusers[$id]) || (!empty($override) &&
$id == $override->userid)) {
......
......@@ -90,7 +90,8 @@ if ($override->groupid) {
$group = $DB->get_record('groups', array('id' => $override->groupid), 'id, name');
$confirmstr = get_string("overridedeletegroupsure", "assign", $group->name);
} else {
$namefields = get_all_user_name_fields(true);
$userfieldsapi = \core\user_fields::for_name();
$namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects;
$user = $DB->get_record('user', array('id' => $override->userid),
'id, ' . $namefields);
$confirmstr = get_string("overridedeleteusersure", "assign", fullname($user));
......
......@@ -121,8 +121,9 @@ if ($groupmode) {
list($sort, $params) = users_order_by_sql('u');
$params['assignid'] = $assign->id;
$userfieldsapi = \core\user_fields::for_name();
if ($accessallgroups) {
$sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
$sql = 'SELECT o.*, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects . '
FROM {assign_overrides} o
JOIN {user} u ON o.userid = u.id
WHERE o.assignid = :assignid
......@@ -133,7 +134,7 @@ if ($groupmode) {
list($insql, $inparams) = $DB->get_in_or_equal(array_keys($groups), SQL_PARAMS_NAMED);
$params += $inparams;
$sql = 'SELECT o.*, ' . get_all_user_name_fields(true, 'u') . '
$sql = 'SELECT o.*, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects . '
FROM {assign_overrides} o
JOIN {user} u ON o.userid = u.id
JOIN {groups_members} gm ON u.id = gm.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