Commit 5e72715e authored by sam marshall's avatar sam marshall
Browse files

MDL-71099 Lib: Move new user_fields class from core to core_user

This class would belong more appropriately within the 'user' API
(core_user) instead of within the 'core' API, since it is
directly related to user data.

Since the class has only just been added to Moodle, now is a good
time to move it.
parent d65ed58e
......@@ -3085,8 +3085,8 @@ function user_get_participants_sql($courseid, $groupid = 0, $accesssince = 0, $r
$wheres = array();
// 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);
$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) {
......@@ -3175,7 +3175,7 @@ function user_get_participants_sql($courseid, $groupid = 0, $accesssince = 0, $r
$conditions[] = $idnumber;
// TODO Does not support custom user profile fields (MDL-70456).
$extrasearchfields = \core\user_fields::get_identity_fields($context, false);
$extrasearchfields = \core_user\fields::get_identity_fields($context, false);
if (!empty($extrasearchfields)) {
// Search all user identify fields.
foreach ($extrasearchfields as $extrasearchfield) {
......@@ -3326,12 +3326,12 @@ function make_categories_options() {
* @return array Array of field names from user table, not including anything
* listed in $already
* @deprecated since Moodle 3.11 MDL-45242
* @see \core\user_fields
* @see \core_user\fields
*/
function get_extra_user_fields($context, $already = array()) {
debugging('get_extra_user_fields() is deprecated. Please use the \core\user_fields API instead.', DEBUG_DEVELOPER);
debugging('get_extra_user_fields() is deprecated. Please use the \core_user\fields API instead.', DEBUG_DEVELOPER);
$fields = \core\user_fields::for_identity($context, false)->excluding(...$already);
$fields = \core_user\fields::for_identity($context, false)->excluding(...$already);
return $fields->get_required_fields();
}
......@@ -3348,12 +3348,12 @@ function get_extra_user_fields($context, $already = array()) {
* @param array $already Array of fields that we're going to include anyway so don't list them (default none)
* @return string Partial SQL select clause, beginning with comma, for example ',u.idnumber,u.department' unless it is blank
* @deprecated since Moodle 3.11 MDL-45242
* @see \core\user_fields
* @see \core_user\fields
*/
function get_extra_user_fields_sql($context, $alias='', $prefix='', $already = array()) {
debugging('get_extra_user_fields_sql() is deprecated. Please use the \core\user_fields API instead.', DEBUG_DEVELOPER);
debugging('get_extra_user_fields_sql() is deprecated. Please use the \core_user\fields API instead.', DEBUG_DEVELOPER);
$fields = \core\user_fields::for_identity($context, false)->excluding(...$already);
$fields = \core_user\fields::for_identity($context, false)->excluding(...$already);
// Note: There will never be any joins or join params because we turned off profile fields.
$selects = $fields->get_sql($alias, false, $prefix)->selects;
......@@ -3368,12 +3368,12 @@ function get_extra_user_fields_sql($context, $alias='', $prefix='', $already = a
* @param string $field Field name, e.g. 'phone1'
* @return string Text description taken from language file, e.g. 'Phone number'
* @deprecated since Moodle 3.11 MDL-45242
* @see \core\user_fields
* @see \core_user\fields
*/
function get_user_field_name($field) {
debugging('get_user_field_name() is deprecated. Please use \core\user_fields::get_display_name() instead', DEBUG_DEVELOPER);
debugging('get_user_field_name() is deprecated. Please use \core_user\fields::get_display_name() instead', DEBUG_DEVELOPER);
return \core\user_fields::get_display_name($field);
return \core_user\fields::get_display_name($field);
}
/**
......@@ -3386,15 +3386,15 @@ function get_user_field_name($field) {
* @param bool $order moves firstname and lastname to the top of the array / start of the string.
* @return array|string All name fields.
* @deprecated since Moodle 3.11 MDL-45242
* @see \core\user_fields
* @see \core_user\fields
*/
function get_all_user_name_fields($returnsql = false, $tableprefix = null, $prefix = null, $fieldprefix = null, $order = false) {
debugging('get_all_user_name_fields() is deprecated. Please use the \core\user_fields API instead', DEBUG_DEVELOPER);
debugging('get_all_user_name_fields() is deprecated. Please use the \core_user\fields API instead', DEBUG_DEVELOPER);
// This array is provided in this order because when called by fullname() (above) if firstname is before
// firstnamephonetic str_replace() will change the wrong placeholder.
$alternatenames = [];
foreach (\core\user_fields::get_name_fields() as $field) {
foreach (\core_user\fields::get_name_fields() as $field) {
$alternatenames[$field] = $field;
}
......
......@@ -1321,7 +1321,7 @@ function groups_user_groups_visible($course, $userid, $cm = null) {
function groups_get_groups_members($groupsids, $extrafields=null, $sort='lastname ASC') {
global $DB;
$userfieldsapi = \core\user_fields::for_userpic()->including(...($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);
......
......@@ -3540,7 +3540,7 @@ function fullname($user, $override=false) {
}
// Get all of the name fields.
$allnames = \core\user_fields::get_name_fields();
$allnames = \core_user\fields::get_name_fields();
if ($CFG->debugdeveloper) {
foreach ($allnames as $allname) {
if (!property_exists($user, $allname)) {
......@@ -3646,7 +3646,7 @@ function fullname($user, $override=false) {
*/
function username_load_fields_from_object($addtoobject, $secondobject, $prefix = null, $additionalfields = null) {
$fields = [];
foreach (\core\user_fields::get_name_fields() as $field) {
foreach (\core_user\fields::get_name_fields() as $field) {
$fields[$field] = $prefix . $field;
}
if ($additionalfields) {
......
......@@ -126,7 +126,7 @@ function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user,
$hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
}
// TODO Does not support custom user profile fields (MDL-70456).
$identityfields = array_flip(\core\user_fields::get_identity_fields($courseorusercontext, false));
$identityfields = array_flip(\core_user\fields::get_identity_fields($courseorusercontext, false));
if (is_mnet_remote_user($user)) {
$sql = "SELECT h.id, h.name, h.wwwroot,
......
......@@ -220,18 +220,18 @@ class user_picture implements renderable {
// only touch the DB if we are missing data and complain loudly...
$needrec = false;
foreach (\core\user_fields::get_picture_fields() as $field) {
foreach (\core_user\fields::get_picture_fields() as $field) {
if (!property_exists($user, $field)) {
$needrec = true;
debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. '
.'Please use the \core\user_fields API to get the full list of required fields.', DEBUG_DEVELOPER);
.'Please use the \core_user\fields API to get the full list of required fields.', DEBUG_DEVELOPER);
break;
}
}
if ($needrec) {
$this->user = $DB->get_record('user', array('id' => $user->id),
implode(',', \core\user_fields::get_picture_fields()), MUST_EXIST);
implode(',', \core_user\fields::get_picture_fields()), MUST_EXIST);
} else {
$this->user = clone($user);
}
......@@ -250,11 +250,11 @@ class user_picture implements renderable {
* @param string $fieldprefix prefix to add to all columns in their aliases, does not apply to 'id'
* @return string
* @deprecated since Moodle 3.11 MDL-45242
* @see \core\user_fields
* @see \core_user\fields
*/
public static function fields($tableprefix = '', array $extrafields = NULL, $idalias = 'id', $fieldprefix = '') {
debugging('user_picture::fields() is deprecated. Please use the \core\user_fields API instead.', DEBUG_DEVELOPER);
$userfields = \core\user_fields::for_userpic();
debugging('user_picture::fields() is deprecated. Please use the \core_user\fields API instead.', DEBUG_DEVELOPER);
$userfields = \core_user\fields::for_userpic();
if ($extrafields) {
$userfields->including(...$extrafields);
}
......@@ -288,7 +288,7 @@ class user_picture implements renderable {
$return = new stdClass();
foreach (\core\user_fields::get_picture_fields() as $field) {
foreach (\core_user\fields::get_picture_fields() as $field) {
if ($field === 'id') {
if (property_exists($record, $idalias)) {
$return->id = $record->{$idalias};
......
......@@ -608,7 +608,7 @@ class flexible_table {
if (isset($this->columns[$column])) {
continue; // This column is OK.
}
if (in_array($column, \core\user_fields::get_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(\core\user_fields::get_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 = \core\user_fields::get_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]);
......
......@@ -3827,7 +3827,7 @@ EOF;
// User information for showing a picture.
$user = new stdClass();
$additionalfields = explode(',', implode(',', \core\user_fields::get_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();
......@@ -3856,7 +3856,7 @@ EOF;
// Return an object with user picture information.
$user = new stdClass();
$additionalfields = explode(',', implode(',', \core\user_fields::get_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();
......
......@@ -74,7 +74,7 @@ class core_outputcomponents_testcase extends advanced_testcase {
* Tests user_picture::unalias.
*/
public function test_fields_unaliasing() {
$fields = implode(',', \core\user_fields::get_picture_fields());
$fields = implode(',', \core_user\fields::get_picture_fields());
$fields = array_map('trim', explode(',', $fields));
$fakerecord = new stdClass();
......@@ -101,7 +101,7 @@ class core_outputcomponents_testcase extends advanced_testcase {
* Tests user_picture::unalias with null values.
*/
public function test_fields_unaliasing_null() {
$fields = implode(',', \core\user_fields::get_picture_fields());
$fields = implode(',', \core_user\fields::get_picture_fields());
$fields = array_map('trim', explode(',', $fields));
$fakerecord = new stdClass();
......
......@@ -111,7 +111,7 @@ information provided here is intended especially for developers.
e.g. ['completionsubmit' => 'Must submit']
* Admin setting admin_setting_configmulticheckbox now supports lazy-loading the options list by
supplying a callback function instead of an array of options.
* A new core API class \core\user_fields provides ways to get lists of user fields, and SQL related to
* A new core API class \core_user\fields provides ways to get lists of user fields, and SQL related to
those fields. This replaces existing functions get_extra_user_fields(), get_extra_user_fields_sql(),
get_user_field_name(), get_all_user_name_fields(), and user_picture::fields(), which have all been
deprecated.
......
......@@ -111,7 +111,7 @@ class api {
global $DB;
// Get the user fields we want.
$userfieldsapi = \core\user_fields::for_userpic()->including('lastaccess');
$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.
......@@ -1023,7 +1023,7 @@ class api {
debugging('\core_message\api::get_contacts_with_unread_message_count is deprecated and no longer used',
DEBUG_DEVELOPER);
$userfieldsapi = \core\user_fields::for_userpic()->including('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
......@@ -1065,7 +1065,7 @@ class api {
debugging('\core_message\api::get_non_contacts_with_unread_message_count is deprecated and no longer used',
DEBUG_DEVELOPER);
$userfieldsapi = \core\user_fields::for_userpic()->including('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
......@@ -1888,7 +1888,7 @@ class api {
public static function get_blocked_users($userid) {
global $DB;
$userfieldsapi = \core\user_fields::for_userpic()->including('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
......
......@@ -429,7 +429,7 @@ class helper {
}
list($useridsql, $usersparams) = $DB->get_in_or_equal($userids);
$userfieldsapi = \core\user_fields::for_userpic()->including('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
......
......@@ -183,7 +183,7 @@ function message_search_users($courseids, $searchtext, $sort='', $exceptions='')
}
$fullname = $DB->sql_fullname();
$userfieldsapi = \core\user_fields::for_userpic();
$userfieldsapi = \core_user\fields::for_userpic();
$ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
if (!empty($sort)) {
......@@ -550,7 +550,7 @@ 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();
$userfieldsapi = \core_user\fields::for_name();
if (empty($useridto)) {
$userfields = $userfieldsapi->get_sql('u', false, 'userto', '', false)->selects;
$messageuseridtosql = 'u.id as useridto';
......
......@@ -161,7 +161,7 @@ class send_email_task extends scheduled_task {
protected function get_users_messages_for_conversation(int $conversationid, int $userid) : moodle_recordset {
global $DB;
$userfieldsapi = \core\user_fields::for_userpic();
$userfieldsapi = \core_user\fields::for_userpic();
$usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT $usernamefields, m.*
FROM {messages} m
......
......@@ -169,7 +169,7 @@ class grading_app implements templatable, renderable {
$export->larrow = $output->larrow();
// List of identity fields to display (the user info will not contain any fields the user cannot view anyway).
// TODO Does not support custom user profile fields (MDL-70456).
$export->showuseridentity = implode(',', \core\user_fields::get_identity_fields(null, false));
$export->showuseridentity = implode(',', \core_user\fields::get_identity_fields(null, false));
$export->currentuserid = $USER->id;
$helpicon = new \help_icon('sendstudentnotifications', 'assign');
$export->helpicon = $helpicon->export_for_template($output);
......
......@@ -59,7 +59,7 @@ class mod_assign_extension_form extends moodleform {
$usershtml = '';
// TODO Does not support custom user profile fields (MDL-70456).
$extrauserfields = \core\user_fields::get_identity_fields($assign->get_context(), false);
$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);
......
......@@ -475,7 +475,7 @@ class assign_feedback_file extends assign_feedback_plugin {
$this->assignment->is_blind_marking(),
$this->assignment->get_uniqueid_for_user($user->id),
// TODO Does not support custom user profile fields (MDL-70456).
\core\user_fields::get_identity_fields($this->assignment->get_context(), false));
\core_user\fields::get_identity_fields($this->assignment->get_context(), false));
$usershtml .= $this->assignment->get_renderer()->render($usersummary);
$usercount += 1;
}
......
......@@ -135,9 +135,9 @@ class assign_grading_table extends table_sql implements renderable {
$params['newstatus'] = ASSIGN_SUBMISSION_STATUS_NEW;
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->assignment->get_context(), false)->with_userpic();
$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]);
$extrauserfields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
$fields = $userfields . ', ';
$fields .= 'u.id as userid, ';
$fields .= 's.status as status, ';
......@@ -408,7 +408,7 @@ class assign_grading_table extends table_sql implements renderable {
foreach ($extrauserfields as $extrafield) {
$columns[] = $extrafield;
$headers[] = \core\user_fields::get_display_name($extrafield);
$headers[] = \core_user\fields::get_display_name($extrafield);
}
} else {
// Record ID.
......
......@@ -587,7 +587,7 @@ 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);
$userfieldsapi = \core\user_fields::for_userpic();
$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
......@@ -745,7 +745,7 @@ function assign_get_recent_mod_activity(&$activities,
$params['timestart'] = $timestart;
$params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
$userfieldsapi = \core\user_fields::for_userpic();
$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, ' .
......@@ -833,7 +833,7 @@ function assign_get_recent_mod_activity(&$activities,
$activity->grade = $grades->items[0]->grades[$submission->userid]->str_long_grade;
}
$userfields = explode(',', implode(',', \core\user_fields::get_picture_fields()));
$userfields = explode(',', implode(',', \core_user\fields::get_picture_fields()));
foreach ($userfields as $userfield) {
if ($userfield == 'id') {
// Aliased in SQL above.
......
......@@ -2066,7 +2066,7 @@ class assign {
private function get_grading_sort_sql() {
$usersort = flexible_table::get_sort_for_table('mod_assign_grading');
// TODO Does not support custom user profile fields (MDL-70456).
$userfieldsapi = \core\user_fields::for_identity($this->context, false)->with_userpic();
$userfieldsapi = \core_user\fields::for_identity($this->context, false)->with_userpic();
$userfields = $userfieldsapi->get_required_fields();
$orderfields = explode(',', $usersort);
$validlist = [];
......@@ -4149,7 +4149,7 @@ class assign {
$this->is_blind_marking(),
$this->get_uniqueid_for_user($user->id),
// TODO Does not support custom user profile fields (MDL-70456).
\core\user_fields::get_identity_fields($this->get_context(), false),
\core_user\fields::get_identity_fields($this->get_context(), false),
!$this->is_active_user($userid));
$o .= $this->get_renderer()->render($usersummary);
}
......@@ -4999,7 +4999,7 @@ class assign {
$usercount = 0;
// TODO Does not support custom user profile fields (MDL-70456).
$extrauserfields = \core\user_fields::get_identity_fields($this->get_context(), false);
$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) {
......@@ -5064,7 +5064,7 @@ class assign {
$usercount = 0;
// TODO Does not support custom user profile fields (MDL-70456).
$extrauserfields = \core\user_fields::get_identity_fields($this->get_context(), false);
$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,7 +156,7 @@ 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();
$userfieldsapi = \core_user\fields::for_name();
if ($accessallgroups) {
$users = get_enrolled_users($this->context, '', 0,
'u.id, u.email, ' . $userfieldsapi->get_sql('u', false, '', '', false)->selects, $sort);
......@@ -187,7 +187,7 @@ class assign_override_form extends moodleform {
$userchoices = array();
// TODO Does not support custom user profile fields (MDL-70456).
$canviewemail = in_array('email', \core\user_fields::get_identity_fields($this->context, false));
$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)) {
......
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