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
......@@ -91,7 +91,8 @@ switch ($action) {
}
if ($errorstr) {
$user = $DB->get_record('user', array('id' => $userid), 'id, ' . get_all_user_name_fields(true));
$userfieldsapi = \core\user_fields::for_name();
$user = $DB->get_record('user', array('id' => $userid), 'id' . $userfieldsapi->get_sql()->selects);
$gradestr = new stdClass();
$gradestr->username = fullname($user);
$gradestr->itemname = $grade_item->get_name();
......
......@@ -293,7 +293,8 @@ class grade_report_grader extends grade_report {
}
if ($errorstr) {
$userfields = 'id, ' . get_all_user_name_fields(true);
$userfieldsapi = \core\user_fields::for_name();
$userfields = 'id, ' . $userfieldsapi->get_sql('', false, '', '', false)->selects;
$user = $DB->get_record('user', array('id' => $userid), $userfields);
$gradestr = new stdClass();
$gradestr->username = fullname($user, $viewfullnames);
......@@ -437,7 +438,9 @@ class grade_report_grader extends grade_report {
list($enrolledsql, $enrolledparams) = get_enrolled_sql($this->context, '', 0, $showonlyactiveenrol);
// Fields we need from the user table.
$userfields = user_picture::fields('u', get_extra_user_fields($this->context));
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->context, false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
// We want to query both the current context and parent contexts.
list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx');
......@@ -657,7 +660,8 @@ class grade_report_grader extends grade_report {
$strfeedback = $this->get_lang_string("feedback");
$strgrade = $this->get_lang_string('grade');
$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);
$arrows = $this->get_sort_arrows($extrafields);
......@@ -1942,7 +1946,7 @@ class grade_report_grader extends grade_report {
}
$arrows['studentname'] = '';
$requirednames = order_in_string(get_all_user_name_fields(), $nameformat);
$requirednames = order_in_string(\core\user_fields::get_name_fields(), $nameformat);
if (!empty($requirednames)) {
foreach ($requirednames as $name) {
$arrows['studentname'] .= html_writer::link(
......@@ -1959,7 +1963,7 @@ class grade_report_grader extends grade_report {
foreach ($extrafields as $field) {
$fieldlink = html_writer::link(new moodle_url($this->baseurl,
array('sortitemid'=>$field)), get_user_field_name($field));
array('sortitemid'=>$field)), \core\user_fields::get_display_name($field));
$arrows[$field] = $fieldlink;
if ($field == $this->sortitemid) {
......
......@@ -131,7 +131,8 @@ class helper {
global $DB, $USER;
// Fields we need from the user table.
$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);
$params = array();
if (!empty($search)) {
list($filtersql, $params) = users_search_sql($search, 'u', true, $extrafields);
......@@ -140,7 +141,8 @@ class helper {
$filtersql = '';
}
$ufields = \user_picture::fields('u', $extrafields).',u.username';
$userfieldsapi = \core\user_fields::for_userpic()->including(...(array_merge($extrafields, ['username'])));
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
if ($count) {
$select = "SELECT COUNT(DISTINCT u.id) ";
$orderby = "";
......@@ -201,7 +203,8 @@ class helper {
$groupwheresql = " AND gm.groupid $insql ";
}
$ufields = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT u.id, $ufields
FROM {user} u
JOIN {grade_grades_history} ggh ON ggh.usermodified = u.id
......
......@@ -140,7 +140,8 @@ class tablelog extends \table_sql implements \renderable {
* Setup the headers for the html 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(
......@@ -394,17 +395,19 @@ class tablelog extends \table_sql implements \renderable {
gi.itemtype, gi.itemmodule, gi.iteminstance, gi.itemnumber, ';
// 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).
$extrafields = \core\user_fields::get_identity_fields($this->context, false);
foreach ($extrafields as $field) {
$fields .= 'u.' . $field . ', ';
}
$gradeduserfields = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$gradeduserfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$fields .= $gradeduserfields . ', ';
$groupby = $fields;
// Add extra user fields that we need for the grader user.
$fields .= get_all_user_name_fields(true, 'ug', '', 'grader');
$groupby .= get_all_user_name_fields(true, 'ug');
$fields .= $userfieldsapi->get_sql('ug', false, 'grader', '', false)->selects;
$groupby .= $userfieldsapi->get_sql('ug', false, '', '', false)->selects;
// Filtering on revised grades only.
$revisedonly = !empty($this->filters->revisedonly);
......
......@@ -51,7 +51,8 @@ $users = \gradereport_history\helper::get_users($context, $search, $page, 25);
$outcome->response = array('users' => array());
$outcome->response['totalusers'] = \gradereport_history\helper::get_users_count($context, $search);;
$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);
$useroptions = array('link' => false, 'visibletoscreenreaders' => false);
// Format the user record.
......
......@@ -99,7 +99,8 @@ if ($editform->is_cancelled()) {
// Display only active users if the option was selected or they do not have the capability to view suspended users.
$onlyactive = !empty($data->includeonlyactiveenrol) || !has_capability('moodle/course:viewsuspendedusers', $context);
$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);
$users = groups_get_potential_members($data->courseid, $data->roleid, $source, $orderby, !empty($data->notingroup),
$onlyactive, $extrafields);
$usercnt = count($users);
......
......@@ -81,9 +81,12 @@ switch ($action) {
case 'ajax_getmembersingroup':
$roles = array();
$extrafields = get_extra_user_fields($context);
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($context, false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$extrafields = $userfieldsapi->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]);
if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid,
'u.id, ' . user_picture::fields('u', $extrafields))) {
'u.id, ' . $userfields)) {
$viewfullnames = has_capability('moodle/site:viewfullnames', $context);
......@@ -202,9 +205,12 @@ if ($groups) {
// Get list of group members to render if there is a single selected group.
$members = array();
if ($singlegroup) {
$extrafields = get_extra_user_fields($context);
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($context, false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$extrafields = $userfieldsapi->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]);
if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid,
'u.id, ' . user_picture::fields('u', $extrafields))) {
'u.id, ' . $userfields)) {
$viewfullnames = has_capability('moodle/site:viewfullnames', $context);
......
......@@ -848,7 +848,8 @@ function groups_get_potential_members($courseid, $roleid = null, $source = null,
}
}
$allusernamefields = user_picture::fields('u', $extrafields);
$userfieldsapi = \core\user_fields::for_userpic()->including(...$extrafields);
$allusernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT DISTINCT u.id, u.username, $allusernamefields, u.idnumber
FROM {user} u
JOIN ($esql) e ON e.id = u.id
......
......@@ -110,8 +110,11 @@ if ($groupingid) {
list($sort, $sortparams) = users_order_by_sql('u');
$extrafields = get_extra_user_fields($context);
$allnames = 'u.id, ' . user_picture::fields('u', $extrafields);
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($context, false)->with_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$extrafields = $userfieldsapi->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]);
$allnames = 'u.id, ' . $userfields;
$sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
FROM {groups} g
......
......@@ -3923,7 +3923,8 @@ function get_role_users($roleid, context $context, $parent = false, $fields = ''
global $DB;
if (empty($fields)) {
$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, '.
......
......@@ -4311,7 +4311,8 @@ class admin_setting_users_with_capability extends admin_setting_configmultiselec
'This is unexpected, and a problem because there is no way to pass these ' .
'parameters to get_users_by_capability. See MDL-34657.');
}
$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', false, '', '', false)->selects;
$users = get_users_by_capability(context_system::instance(), $this->capability, $userfields, $sort);
$this->choices = array(
'$@NONE@$' => get_string('nobody'),
......
......@@ -1102,7 +1102,7 @@ function signup_setup_new_user($user) {
$user->secret = random_string(15);
$user->auth = $CFG->registerauth;
// Initialize alternate name fields to empty strings.
$namefields = array_diff(get_all_user_name_fields(), useredit_get_required_name_fields());
$namefields = array_diff(\core\user_fields::get_name_fields(), useredit_get_required_name_fields());
foreach ($namefields as $namefield) {
$user->$namefield = '';
}
......
......@@ -145,7 +145,7 @@ function badges_notify_badge_award(badge $badge, $userid, $issued, $filepathhash
$userfrom = new stdClass();
$userfrom->id = $admin->id;
$userfrom->email = !empty($CFG->badges_defaultissuercontact) ? $CFG->badges_defaultissuercontact : $admin->email;
foreach (get_all_user_name_fields() as $addname) {
foreach (\core\user_fields::get_name_fields() as $addname) {
$userfrom->$addname = !empty($CFG->badges_defaultissuername) ? '' : $admin->$addname;
}
$userfrom->firstname = !empty($CFG->badges_defaultissuername) ? $CFG->badges_defaultissuername : $admin->firstname;
......
......@@ -66,7 +66,8 @@ class riskadmin extends check {
*/
public function get_result(): result {
global $DB, $CFG;
$userfields = \user_picture::fields('u');
$userfieldsapi = \core\user_fields::for_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT $userfields
FROM {user} u
WHERE u.id IN ($CFG->siteadmins)";
......
......@@ -165,7 +165,8 @@ class riskbackup_result extends \core\check\result {
'context1' => CONTEXT_COURSE,
'context2' => CONTEXT_COURSE,
];
$userfields = \user_picture::fields('u');
$userfieldsapi = \core\user_fields::for_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$rs = $DB->get_recordset_sql("
SELECT DISTINCT $userfields,
ra.contextid,
......
......@@ -90,7 +90,8 @@ class riskxss_result extends \core\check\result {
global $CFG, $DB;
$userfields = \user_picture::fields('u');
$userfieldsapi = \core\user_fields::for_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$users = $DB->get_records_sql("SELECT DISTINCT $userfields $this->sqlfrom", $this->params);
foreach ($users as $uid => $user) {
$url = "$CFG->wwwroot/user/view.php?id=$user->id";
......
......@@ -115,7 +115,8 @@ class send_failed_login_notifications_task extends scheduled_task {
// Now, select all the login error logged records belonging to the ips and infos
// since lastnotifyfailure, that we have stored in the cache_flags table.
$namefields = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT * FROM (
SELECT l.*, u.username, $namefields
FROM {" . $logtable . "} l
......
......@@ -47,7 +47,8 @@ class send_new_user_passwords_task extends scheduled_task {
// Generate new password emails for users - ppl expect these generated asap.
if ($DB->count_records('user_preferences', array('name' => 'create_password', 'value' => '1'))) {
mtrace('Creating passwords for new users...');
$usernamefields = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$newusers = $DB->get_recordset_sql("SELECT u.id as id, u.email, u.auth, u.deleted,
u.suspended, u.emailstop, u.mnethostid, u.mailformat,
$usernamefields, u.username, u.lang,
......
......@@ -252,20 +252,10 @@ class core_user {
$extraparams = [];
// TODO Does not support custom user profile fields (MDL-70456).
$extra = \core\user_fields::get_identity_fields(null, false);
// We need the username just to skip guests.
$extrafieldlist = $extra;
if (!in_array('username', $extra)) {
$extrafieldlist[] = 'username';
}
// The deleted flag will always be false because users_search_sql excludes deleted users,
// but it must be present or it causes PHP warnings in some functions below.
if (!in_array('deleted', $extra)) {
$extrafieldlist[] = 'deleted';
}
$selectfields = \user_picture::fields('u',
array_merge(get_all_user_name_fields(), $extrafieldlist));
$userfieldsapi = \core\user_fields::for_identity(null, false)->with_userpic()->with_name()
->including('username', 'deleted');
$selectfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$extra = $userfieldsapi->get_required_fields([\core\user_fields::PURPOSE_IDENTITY]);
$index = 1;
foreach ($extra as $fieldname) {
......
......@@ -1190,11 +1190,10 @@ class completion_info {
context_course::instance($this->course->id),
'moodle/course:isincompletionreports', $groupid, true);
$allusernames = get_all_user_name_fields(true, 'u');
$sql = 'SELECT u.id, u.idnumber, ' . $allusernames;
if ($extracontext) {
$sql .= get_extra_user_fields_sql($extracontext, 'u', '', array('idnumber'));
}
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($extracontext, false)->with_name();
$allusernames = $userfieldsapi->get_sql('u')->selects;
$sql = 'SELECT u.id, u.idnumber ' . $allusernames;
$sql .= ' FROM (' . $enrolledsql . ') eu JOIN {user} u ON u.id = eu.id';
if ($where) {
......
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