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
......@@ -58,7 +58,8 @@ class external_badge implements renderable {
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);
......
......@@ -76,7 +76,8 @@ class issued_badge implements renderable {
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;
......
......@@ -88,7 +88,7 @@ class award_criteria_profile extends award_criteria {
if (in_array($field, $existing)) {
$checked = true;
}
$this->config_options($mform, array('id' => $field, 'checked' => $checked, 'name' => get_user_field_name($field), 'error' => false));
$this->config_options($mform, array('id' => $field, 'checked' => $checked, 'name' => \core\user_fields::get_display_name($field), 'error' => false));
$none = false;
}
}
......@@ -138,7 +138,7 @@ class award_criteria_profile extends award_criteria {
if (is_numeric($p['field'])) {
$str = $DB->get_field('user_info_field', 'name', array('id' => $p['field']));
} else {
$str = get_user_field_name($p['field']);
$str = \core\user_fields::get_display_name($p['field']);
}
if (!$str) {
$output[] = $OUTPUT->error_text(get_string('error:nosuchfield', 'badges'));
......
......@@ -87,7 +87,8 @@ if ($badge->has_manual_award_criteria() && has_capability('moodle/badges:awardba
echo $OUTPUT->box($OUTPUT->single_button($url, get_string('award', 'badges')), 'clearfix mdl-align');
}
$namefields = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$namefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT b.userid, b.dateissued, b.uniquehash, $namefields
FROM {badge_issued} b INNER JOIN {user} u
ON b.userid = u.id
......
......@@ -508,12 +508,12 @@ class block_activity_results extends block_base {
// Now grab all the users from the database.
$userids = array_merge(array_keys($best), array_keys($worst));
$fields = array_merge(array('id', 'idnumber'), get_all_user_name_fields());
$fields = array_merge(array('id', 'idnumber'), \core\user_fields::get_name_fields());
$fields = implode(',', $fields);
$users = $DB->get_records_list('user', 'id', $userids, '', $fields);
// If configured to view user idnumber, ensure current user can see it.
$extrafields = get_extra_user_fields($this->context);
$extrafields = \core\user_fields::for_identity($this->context)->get_required_fields();
$canviewidnumber = (array_search('idnumber', $extrafields) !== false);
// Ready for output!
......
......@@ -50,7 +50,8 @@ class block_mentees extends block_base {
$this->content = new stdClass();
// get all the mentees, i.e. users you have a direct assignment to
$allusernames = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$allusernames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
if ($usercontexts = $DB->get_records_sql("SELECT c.instanceid, c.instanceid, $allusernames
FROM {role_assignments} ra, {context} c, {user} u
WHERE ra.userid = ?
......
......@@ -86,7 +86,8 @@ class fetcher {
}
$params = array();
$userfields = \user_picture::fields('u', array('username', 'deleted'));
$userfieldsapi = \core\user_fields::for_userpic()->including('username', 'deleted');
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
// Add this to the SQL to show only group users.
if ($currentgroup !== null) {
......
......@@ -646,8 +646,8 @@ class blog_listing {
if (!$userid) {
$userid = $USER->id;
}
$allnamefields = \user_picture::fields('u', null, 'useridalias');
$userfieldsapi = \core\user_fields::for_userpic();
$allnamefields = $userfieldsapi->get_sql('u', false, '', 'useridalias', false)->selects;
// The query used to locate blog entries is complicated. It will be built from the following components:
$requiredfields = "p.*, $allnamefields"; // The SELECT clause.
$tables = array('p' => 'post', 'u' => 'user'); // Components of the FROM clause (table_id => table_name).
......
......@@ -234,7 +234,9 @@ function blog_rss_get_feed($context, $args) {
switch ($type) {
case 'user':
$info = fullname($DB->get_record('user', array('id' => $id), get_all_user_name_fields(true)));
$userfieldsapi = \core\user_fields::for_name();
$info = fullname($DB->get_record('user', array('id' => $id),
$userfieldsapi->get_sql('', false, '', '', false)->selects));
break;
case 'course':
$info = $DB->get_field('course', 'fullname', array('id' => $id));
......
......@@ -551,7 +551,8 @@ class comment {
$params = array();
$perpage = (!empty($CFG->commentsperpage))?$CFG->commentsperpage:15;
$start = $page * $perpage;
$ufields = user_picture::fields('u');
$userfieldsapi = \core\user_fields::for_userpic();
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
list($componentwhere, $component) = $this->get_component_select_sql('c');
if ($component) {
......
......@@ -61,7 +61,8 @@ class comment_manager {
}
$comments = array();
$usernamefields = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT c.id, c.contextid, c.itemid, c.component, c.commentarea, c.userid, c.content, $usernamefields, c.timecreated
FROM {comments} c
JOIN {user} u
......@@ -75,7 +76,7 @@ class comment_manager {
$item->time = userdate($item->timecreated);
$item->content = format_text($item->content, FORMAT_MOODLE, $formatoptions);
// Unset fields not related to the comment
foreach (get_all_user_name_fields() as $namefield) {
foreach (\core\user_fields::get_name_fields() as $namefield) {
unset($item->$namefield);
}
unset($item->timecreated);
......
......@@ -1033,7 +1033,8 @@ class core_course_category implements renderable, cacheable_object, IteratorAggr
list($sql2, $params2) = $DB->get_in_or_equal($managerroles, SQL_PARAMS_NAMED, 'rid');
list($sort, $sortparams) = users_order_by_sql('u');
$notdeleted = array('notdeleted' => 0);
$allnames = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT ra.contextid, ra.id AS raid,
r.id AS roleid, r.name AS rolename, r.shortname AS roleshortname,
rn.name AS rolecoursealias, u.id, u.username, $allnames
......
......@@ -77,9 +77,12 @@ class recent_form extends moodleform {
$options[0] = get_string('allparticipants');
$options[$CFG->siteguest] = get_string('guestuser');
$userfieldsapi = \core\user_fields::for_userpic();
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
if (isset($groupoptions[0])) {
// can see all enrolled users
if ($enrolled = get_enrolled_users($context, null, 0, user_picture::fields('u'))) {
if ($enrolled = get_enrolled_users($context, null, 0, $ufields)) {
foreach ($enrolled as $euser) {
$options[$euser->id] = fullname($euser, $viewfullnames);
}
......@@ -87,7 +90,7 @@ class recent_form extends moodleform {
} else {
// can see users from some groups only
foreach ($groupoptions as $groupid=>$unused) {
if ($enrolled = get_enrolled_users($context, null, $groupid, user_picture::fields('u'))) {
if ($enrolled = get_enrolled_users($context, null, $groupid, $ufields)) {
foreach ($enrolled as $euser) {
if (!array_key_exists($euser->id, $options)) {
$options[$euser->id] = fullname($euser, $viewfullnames);
......
......@@ -94,7 +94,8 @@ switch ($action) {
$search = optional_param('search', '', PARAM_RAW);
$page = optional_param('page', 0, PARAM_INT);
$outcome->response = $manager->search_other_users($search, $searchanywhere, $page);
$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();
// User is not enrolled, either link to site profile or do not link at all.
if (has_capability('moodle/user:viewdetails', context_system::instance())) {
......
......@@ -560,7 +560,8 @@ class core_enrol_external extends external_api {
// Add also extra user fields.
$requiredfields = array_merge(
['id', 'fullname', 'profileimageurl', 'profileimageurlsmall'],
get_extra_user_fields($context)
// TODO Does not support custom user profile fields (MDL-70456).
\core\user_fields::get_identity_fields($context, false)
);
foreach ($users['users'] as $id => $user) {
// Note: We pass the course here to validate that the current user can at least view user details in this course.
......@@ -652,7 +653,8 @@ class core_enrol_external extends external_api {
// Add also extra user fields.
$requiredfields = array_merge(
['id', 'fullname', 'profileimageurl', 'profileimageurlsmall'],
get_extra_user_fields($context)
// TODO Does not support custom user profile fields (MDL-70456).
\core\user_fields::get_identity_fields($context, false)
);
foreach ($users['users'] as $user) {
if ($userdetails = user_get_user_details($user, $course, $requiredfields)) {
......
......@@ -238,7 +238,8 @@ class course_enrolment_manager {
list($instancessql, $params, $filter) = $this->get_instance_sql();
list($filtersql, $moreparams) = $this->get_filter_sql();
$params += $moreparams;
$extrafields = get_extra_user_fields($this->get_context());
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->get_context(), false);
$extrafields[] = 'lastaccess';
$ufields = user_picture::fields('u', $extrafields);
$sql = "SELECT DISTINCT $ufields, COALESCE(ul.timeaccess, 0) AS lastcourseaccess
......@@ -268,7 +269,8 @@ class course_enrolment_manager {
global $DB;
// Search condition.
$extrafields = get_extra_user_fields($this->get_context());
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->get_context(), false);
list($sql, $params) = users_search_sql($this->searchfilter, 'u', true, $extrafields);
// Role condition.
......@@ -341,7 +343,8 @@ class course_enrolment_manager {
list($ctxcondition, $params) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'ctx');
$params['courseid'] = $this->course->id;
$params['cid'] = $this->course->id;
$extrafields = get_extra_user_fields($this->get_context());
// TODO Does not support custom user profile fields (MDL-70456).
$extrafields = \core\user_fields::get_identity_fields($this->get_context(), false);
$ufields = user_picture::fields('u', $extrafields);
$sql = "SELECT ra.id as raid, ra.contextid, ra.component, ctx.contextlevel, ra.roleid, $ufields,
coalesce(u.lastaccess,0) AS lastaccess
......@@ -379,8 +382,9 @@ class course_enrolment_manager {
$tests = array("u.id <> :guestid", 'u.deleted = 0', 'u.confirmed = 1');
$params = array('guestid' => $CFG->siteguest);
if (!empty($search)) {
$conditions = get_extra_user_fields($this->get_context());
foreach (get_all_user_name_fields() as $field) {
// TODO Does not support custom user profile fields (MDL-70456).
$conditions = \core\user_fields::get_identity_fields($this->get_context(), false);
foreach (\core\user_fields::get_name_fields() as $field) {
$conditions[] = 'u.'.$field;
}
$conditions[] = $DB->sql_fullname('u.firstname', 'u.lastname');
......@@ -399,7 +403,9 @@ class course_enrolment_manager {
}
$wherecondition = implode(' AND ', $tests);
$extrafields = get_extra_user_fields($this->get_context(), array('username', 'lastaccess'));
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->get_context(), false)->excluding('username', 'lastaccess');
$extrafields = $userfieldsapi->get_required_fields();
$extrafields[] = 'username';
$extrafields[] = 'lastaccess';
$extrafields[] = 'maildisplay';
......@@ -1046,7 +1052,8 @@ class course_enrolment_manager {
$context = $this->get_context();
$now = time();
$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 = array();
foreach ($userroles as $userrole) {
......@@ -1124,7 +1131,8 @@ class course_enrolment_manager {
$canmanagegroups = has_capability('moodle/course:managegroups', $context);
$url = new moodle_url($pageurl, $this->get_url_params());
$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);
$enabledplugins = $this->get_enrolment_plugins(true);
......@@ -1301,7 +1309,8 @@ class course_enrolment_manager {
list($instancesql, $instanceparams) = $DB->get_in_or_equal(array_keys($instances), SQL_PARAMS_NAMED, 'instanceid0000');
}
$userfields = user_picture::fields('u');
$userfieldsapi = \core\user_fields::for_userpic();
$userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
list($idsql, $idparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid0000');
list($sort, $sortparams) = users_order_by_sql('u');
......
......@@ -93,7 +93,8 @@ class enrol_manual_enrol_users_form extends moodleform {
'courseid' => $course->id,
'enrolid' => $instance->id,
'perpage' => $CFG->maxusersperpage,
'userfields' => implode(',', get_extra_user_fields($context))
// TODO Does not support custom user profile fields (MDL-70456).
'userfields' => implode(',', \core\user_fields::get_identity_fields($context, false))
);
$mform->addElement('autocomplete', 'userlist', get_string('selectusers', 'enrol_manual'), array(), $options);
......
......@@ -54,9 +54,10 @@ $userdetails = array (
'firstname' => get_string('firstname'),
'lastname' => get_string('lastname'),
);
$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);
foreach ($extrafields as $field) {
$userdetails[$field] = get_user_field_name($field);
$userdetails[$field] = \core\user_fields::get_display_name($field);
}
$fields = array(
......
......@@ -1021,7 +1021,8 @@ class enrol_self_plugin extends enrol_plugin {
// We only use the first user.
$i = 0;
do {
$allnames = get_all_user_name_fields(true, 'u');
$userfieldsapi = \core\user_fields::for_name();
$allnames = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$rusers = get_role_users($croles[$i], $context, true, 'u.id, u.confirmed, u.username, '. $allnames . ',
u.email, r.sortorder, ra.id', 'r.sortorder, ra.id ASC, ' . $sort, null, '', '', '', '', $sortparams);
$i++;
......
......@@ -82,7 +82,9 @@ class enrol_self_enrol_form extends moodleform {
$mform->addElement('password', 'enrolpassword', get_string('password', 'enrol_self'),
array('id' => 'enrolpassword_'.$instance->id));
$context = context_course::instance($this->instance->courseid);
$keyholders = get_users_by_capability($context, 'enrol/self:holdkey', user_picture::fields('u'));
$userfieldsapi = \core\user_fields::for_userpic();
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$keyholders = get_users_by_capability($context, 'enrol/self:holdkey', $ufields);
$keyholdercount = 0;
foreach ($keyholders as $keyholder) {
$keyholdercount++;
......
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