MDL-81031 core: Coding style fixes for \core\user

This commit is contained in:
Andrew Nicols 2024-07-19 13:31:24 +08:00 committed by Jun Pataleta
parent f943311753
commit d3d5d8bc73
No known key found for this signature in database
GPG key ID: F83510526D99E2C7

View file

@ -32,7 +32,7 @@ use stdClass;
/** /**
* User class to access user details. * User class to access user details.
* *
* @todo move api's from user/lib.php and deprecate old ones. * @todo MDL-82650 Move api's from user/lib.php and deprecate old ones.
* @package core * @package core
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com> * @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@ -83,7 +83,7 @@ class user {
'firstnamephonetic', 'firstnamephonetic',
'lastnamephonetic', 'lastnamephonetic',
'middlename', 'middlename',
'alternatename' 'alternatename',
]; ];
/** @var int Indicates that user profile view should be prevented */ /** @var int Indicates that user profile view should be prevented */
@ -131,7 +131,7 @@ class user {
return self::get_support_user(); return self::get_support_user();
break; break;
default: default:
return $DB->get_record('user', array('id' => $userid), $fields, $strictness); return $DB->get_record('user', ['id' => $userid], $fields, $strictness);
} }
} }
@ -156,7 +156,7 @@ class user {
$mnethostid = $CFG->mnet_localhost_id; $mnethostid = $CFG->mnet_localhost_id;
} }
return $DB->get_record('user', array('email' => $email, 'mnethostid' => $mnethostid), $fields, $strictness); return $DB->get_record('user', ['email' => $email, 'mnethostid' => $mnethostid], $fields, $strictness);
} }
/** /**
@ -180,7 +180,7 @@ class user {
$mnethostid = $CFG->mnet_localhost_id; $mnethostid = $CFG->mnet_localhost_id;
} }
return $DB->get_record('user', array('username' => $username, 'mnethostid' => $mnethostid), $fields, $strictness); return $DB->get_record('user', ['username' => $username, 'mnethostid' => $mnethostid], $fields, $strictness);
} }
/** /**
@ -218,8 +218,12 @@ class user {
* @param int $querylimit Max number of database queries, default 5 (zero = no limit) * @param int $querylimit Max number of database queries, default 5 (zero = no limit)
* @return array Array of user objects with limited fields * @return array Array of user objects with limited fields
*/ */
public static function search($query, context_course $coursecontext = null, public static function search(
$max = 30, $querylimit = 5) { $query,
?context_course $coursecontext = null,
$max = 30,
$querylimit = 5
) {
global $CFG, $DB; global $CFG, $DB;
require_once($CFG->dirroot . '/user/lib.php'); require_once($CFG->dirroot . '/user/lib.php');
@ -239,8 +243,9 @@ class user {
$userparams = []; $userparams = [];
} }
if (!$viewsystem) { if (!$viewsystem) {
list($userquery, $userparams) = self::get_enrolled_sql_on_courses_with_capability( [$userquery, $userparams] = self::get_enrolled_sql_on_courses_with_capability(
'moodle/user:viewdetails'); 'moodle/user:viewdetails'
);
if (!$userquery) { if (!$userquery) {
// No permissions anywhere, return nothing. // No permissions anywhere, return nothing.
return []; return [];
@ -248,7 +253,7 @@ class user {
} }
// Start building the WHERE clause based on name. // Start building the WHERE clause based on name.
list ($where, $whereparams) = users_search_sql($query, 'u'); [$where, $whereparams] = users_search_sql($query, 'u');
// We allow users to search with extra identity fields (as well as name) but only if they // We allow users to search with extra identity fields (as well as name) but only if they
// have the permission to display those identity fields. // have the permission to display those identity fields.
@ -281,8 +286,9 @@ class user {
$whereparams = array_merge($whereparams, $extraparams); $whereparams = array_merge($whereparams, $extraparams);
} else { } else {
// Get all courses where user can view full user identity. // Get all courses where user can view full user identity.
list($sql, $params) = self::get_enrolled_sql_on_courses_with_capability( [$sql, $params] = self::get_enrolled_sql_on_courses_with_capability(
'moodle/site:viewuseridentity'); 'moodle/site:viewuseridentity'
);
if ($sql) { if ($sql) {
// Join that with the user query to get an extra field indicating if we can. // Join that with the user query to get an extra field indicating if we can.
$userquery = " $userquery = "
@ -302,9 +308,9 @@ class user {
// Default order is just name order. But if searching within a course then we show users // Default order is just name order. But if searching within a course then we show users
// within the course first. // within the course first.
list ($order, $orderparams) = users_order_by_sql('u', $query, $systemcontext); [$order, $orderparams] = users_order_by_sql('u', $query, $systemcontext);
if ($coursecontext) { if ($coursecontext) {
list ($sql, $params) = get_enrolled_sql($coursecontext); [$sql, $params] = get_enrolled_sql($coursecontext);
$mainfield = 'innerusers2.id'; $mainfield = 'innerusers2.id';
if ($usingshowidentity) { if ($usingshowidentity) {
$mainfield .= ', innerusers2.showidentity'; $mainfield .= ', innerusers2.showidentity';
@ -326,13 +332,17 @@ class user {
$pos = 0; $pos = 0;
$readcount = $max + 2; $readcount = $max + 2;
for ($i = 0; $i < $querylimit; $i++) { for ($i = 0; $i < $querylimit; $i++) {
$rawresult = $DB->get_records_sql(" $rawresult = $DB->get_records_sql(
"
SELECT $selectfields SELECT $selectfields
FROM ($userquery) users FROM ($userquery) users
JOIN {user} u ON u.id = users.id JOIN {user} u ON u.id = users.id
WHERE $where WHERE $where
ORDER BY $order", array_merge($userparams, $whereparams, $orderparams), ORDER BY $order",
$pos, $readcount); array_merge($userparams, $whereparams, $orderparams),
$pos,
$readcount
);
foreach ($rawresult as $user) { foreach ($rawresult as $user) {
// Skip guest. // Skip guest.
if ($user->username === 'guest') { if ($user->username === 'guest') {
@ -374,8 +384,12 @@ class user {
*/ */
protected static function get_enrolled_sql_on_courses_with_capability($capability) { protected static function get_enrolled_sql_on_courses_with_capability($capability) {
// Get all courses where user have the capability. // Get all courses where user have the capability.
$courses = get_user_capability_course($capability, null, true, $courses = get_user_capability_course(
implode(',', array_values(context_helper::get_preload_record_columns('ctx')))); $capability,
null,
true,
implode(',', array_values(context_helper::get_preload_record_columns('ctx')))
);
if (!$courses) { if (!$courses) {
return [null, null]; return [null, null];
} }
@ -388,7 +402,7 @@ class user {
foreach ($courses as $course) { foreach ($courses as $course) {
// Get SQL to list user ids enrolled in this course. // Get SQL to list user ids enrolled in this course.
context_helper::preload_from_record($course); context_helper::preload_from_record($course);
list ($sql, $params) = get_enrolled_sql(context_course::instance($course->id)); [$sql, $params] = get_enrolled_sql(context_course::instance($course->id));
// Combine to a big union query. // Combine to a big union query.
if ($unionsql) { if ($unionsql) {
@ -531,7 +545,7 @@ class user {
return false; return false;
} }
if ($checkdb) { if ($checkdb) {
return $DB->record_exists('user', array('id' => $userid)); return $DB->record_exists('user', ['id' => $userid]);
} else { } else {
return true; return true;
} }
@ -576,11 +590,11 @@ class user {
throw new moodle_exception('guestsarenotallowed', 'error'); throw new moodle_exception('guestsarenotallowed', 'error');
} }
if ($checksuspended and $user->suspended) { if ($checksuspended && $user->suspended) {
throw new moodle_exception('suspended', 'auth'); throw new moodle_exception('suspended', 'auth');
} }
if ($checknologin and $user->auth == 'nologin') { if ($checknologin && $user->auth == 'nologin') {
throw new moodle_exception('suspended', 'auth'); throw new moodle_exception('suspended', 'auth');
} }
} }
@ -592,7 +606,7 @@ class user {
* @param array $filemanageroptions * @param array $filemanageroptions
* @return bool True if the user was updated, false if it stayed the same. * @return bool True if the user was updated, false if it stayed the same.
*/ */
public static function update_picture(stdClass $usernew, $filemanageroptions = array()) { public static function update_picture(stdClass $usernew, $filemanageroptions = []) {
global $CFG, $DB; global $CFG, $DB;
require_once("$CFG->libdir/gdlib.php"); require_once("$CFG->libdir/gdlib.php");
@ -639,7 +653,7 @@ class user {
} }
if ($newpicture != $user->picture) { if ($newpicture != $user->picture) {
$DB->set_field('user', 'picture', $newpicture, array('id' => $user->id)); $DB->set_field('user', 'picture', $newpicture, ['id' => $user->id]);
return true; return true;
} else { } else {
return false; return false;
@ -672,66 +686,112 @@ class user {
// Array of user fields properties and expected parameters. // Array of user fields properties and expected parameters.
// Every new field on the user table should be added here otherwise it won't be validated. // Every new field on the user table should be added here otherwise it won't be validated.
$fields = array(); $fields = [];
$fields['id'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); $fields['id'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['auth'] = array('type' => PARAM_AUTH, 'null' => NULL_NOT_ALLOWED); $fields['auth'] = ['type' => PARAM_AUTH, 'null' => NULL_NOT_ALLOWED];
$fields['confirmed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED); $fields['confirmed'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
$fields['policyagreed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED); $fields['policyagreed'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
$fields['deleted'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED); $fields['deleted'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
$fields['suspended'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED); $fields['suspended'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
$fields['mnethostid'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); $fields['mnethostid'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['username'] = array('type' => PARAM_USERNAME, 'null' => NULL_NOT_ALLOWED); $fields['username'] = ['type' => PARAM_USERNAME, 'null' => NULL_NOT_ALLOWED];
$fields['password'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED); $fields['password'] = ['type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED];
$fields['idnumber'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED); $fields['idnumber'] = ['type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED];
$fields['firstname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED); $fields['firstname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
$fields['lastname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED); $fields['lastname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
$fields['surname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED); $fields['surname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
$fields['email'] = array('type' => PARAM_RAW_TRIMMED, 'null' => NULL_NOT_ALLOWED); $fields['email'] = ['type' => PARAM_RAW_TRIMMED, 'null' => NULL_NOT_ALLOWED];
$fields['emailstop'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0); $fields['emailstop'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0];
$fields['phone1'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED); $fields['phone1'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
$fields['phone2'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED); $fields['phone2'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
$fields['institution'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED); $fields['institution'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
$fields['department'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED); $fields['department'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
$fields['address'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED); $fields['address'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
$fields['city'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->defaultcity); $fields['city'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->defaultcity];
$fields['country'] = array('type' => PARAM_ALPHA, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->country, $fields['country'] = [
'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_countries(true, true))); 'type' => PARAM_ALPHA,
$fields['lang'] = array('type' => PARAM_LANG, 'null' => NULL_NOT_ALLOWED, 'null' => NULL_NOT_ALLOWED,
'default' => $CFG->country,
'choices' => array_merge(
['' => ''],
get_string_manager()->get_list_of_countries(true, true)
),
];
$fields['lang'] = [
'type' => PARAM_LANG,
'null' => NULL_NOT_ALLOWED,
'default' => (!empty($CFG->autolangusercreation) && !empty($SESSION->lang)) ? $SESSION->lang : $CFG->lang, 'default' => (!empty($CFG->autolangusercreation) && !empty($SESSION->lang)) ? $SESSION->lang : $CFG->lang,
'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_translations(false))); 'choices' => array_merge(
$fields['calendartype'] = array('type' => PARAM_PLUGIN, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->calendartype, ['' => ''],
'choices' => array_merge(array('' => ''), \core_calendar\type_factory::get_list_of_calendar_types())); get_string_manager()->get_list_of_translations(false)
$fields['theme'] = array('type' => PARAM_THEME, 'null' => NULL_NOT_ALLOWED, ),
'default' => theme_config::DEFAULT_THEME, 'choices' => array_merge(array('' => ''), get_list_of_themes())); ];
$fields['timezone'] = array('type' => PARAM_TIMEZONE, 'null' => NULL_NOT_ALLOWED, $fields['calendartype'] = [
'default' => core_date::get_server_timezone()); // Must not use choices here: timezones can come and go. 'type' => PARAM_PLUGIN,
$fields['firstaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); 'null' => NULL_NOT_ALLOWED,
$fields['lastaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); 'default' => $CFG->calendartype,
$fields['lastlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); 'choices' => array_merge(
$fields['currentlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); ['' => ''],
$fields['lastip'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED); \core_calendar\type_factory::get_list_of_calendar_types()
$fields['secret'] = array('type' => PARAM_ALPHANUM, 'null' => NULL_NOT_ALLOWED); ),
$fields['picture'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); ];
$fields['description'] = array('type' => PARAM_RAW, 'null' => NULL_ALLOWED); $fields['theme'] = [
$fields['descriptionformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); 'type' => PARAM_THEME,
$fields['mailformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_mailformat); 'default' => theme_config::DEFAULT_THEME,
$fields['maildigest'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'choices' => array_merge(
'default' => $CFG->defaultpreference_maildigest); ['' => ''],
$fields['maildisplay'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, get_list_of_themes()
'default' => $CFG->defaultpreference_maildisplay); ),
$fields['autosubscribe'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, ];
'default' => $CFG->defaultpreference_autosubscribe); $fields['timezone'] = [
$fields['trackforums'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, // Must not use choices here: timezones can come and go.
'default' => $CFG->defaultpreference_trackforums); 'type' => PARAM_TIMEZONE,
$fields['timecreated'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); 'null' => NULL_NOT_ALLOWED,
$fields['timemodified'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); 'default' => core_date::get_server_timezone(),
$fields['trustbitmask'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED); ];
$fields['imagealt'] = array('type' => PARAM_TEXT, 'null' => NULL_ALLOWED); $fields['firstaccess'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['lastnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED); $fields['lastaccess'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['firstnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED); $fields['lastlogin'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['middlename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED); $fields['currentlogin'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['alternatename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED); $fields['lastip'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
$fields['secret'] = ['type' => PARAM_ALPHANUM, 'null' => NULL_NOT_ALLOWED];
$fields['picture'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['description'] = ['type' => PARAM_RAW, 'null' => NULL_ALLOWED];
$fields['descriptionformat'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['mailformat'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_mailformat,
];
$fields['maildigest'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_maildigest,
];
$fields['maildisplay'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_maildisplay,
];
$fields['autosubscribe'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_autosubscribe,
];
$fields['trackforums'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'default' => $CFG->defaultpreference_trackforums,
];
$fields['timecreated'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['timemodified'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['trustbitmask'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
$fields['imagealt'] = ['type' => PARAM_TEXT, 'null' => NULL_ALLOWED];
$fields['lastnamephonetic'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
$fields['firstnamephonetic'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
$fields['middlename'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
$fields['alternatename'] = ['type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED];
self::$propertiescache = $fields; self::$propertiescache = $fields;
} }
@ -773,8 +833,10 @@ class user {
validate_param($value, self::$propertiescache[$property]['type'], self::$propertiescache[$property]['null']); validate_param($value, self::$propertiescache[$property]['type'], self::$propertiescache[$property]['null']);
} }
// Check that the value is part of a list of allowed values. // Check that the value is part of a list of allowed values.
if (!empty(self::$propertiescache[$property]['choices']) && if (
!isset(self::$propertiescache[$property]['choices'][$value])) { !empty(self::$propertiescache[$property]['choices']) &&
!isset(self::$propertiescache[$property]['choices'][$value])
) {
throw new invalid_parameter_exception($value); throw new invalid_parameter_exception($value);
} }
} catch (invalid_parameter_exception $e) { } catch (invalid_parameter_exception $e) {
@ -903,9 +965,10 @@ class user {
self::fill_properties_cache(); self::fill_properties_cache();
if (!array_key_exists($property, self::$propertiescache) && !array_key_exists('choices', if (
self::$propertiescache[$property])) { !array_key_exists($property, self::$propertiescache) &&
!array_key_exists('choices', self::$propertiescache[$property])
) {
throw new coding_exception('Invalid property requested, or the property does not has a list of choices.'); throw new coding_exception('Invalid property requested, or the property does not has a list of choices.');
} }
@ -942,11 +1005,12 @@ class user {
* 'choices' => array(1, 2..) // An array of accepted values of the user field - optional * 'choices' => array(1, 2..) // An array of accepted values of the user field - optional
* 'default' => $CFG->setting // An default value for the field - optional * 'default' => $CFG->setting // An default value for the field - optional
* 'isregex' => false/true // Whether the name of the preference is a regular expression (default false). * 'isregex' => false/true // Whether the name of the preference is a regular expression (default false).
* 'permissioncallback' => callable // Function accepting arguments ($user, $preferencename) that checks if current user * 'permissioncallback' => callable // Function accepting arguments ($user, $preferencename) that checks if current
* // user
* // is allowed to modify this preference for given user. * // is allowed to modify this preference for given user.
* // If not specified \core\user::default_preference_permission_check() will be assumed. * // If not specified \core\user::default_preference_permission_check() will be assumed.
* 'cleancallback' => callable // Custom callback for cleaning value if something more difficult than just type/choices is needed * 'cleancallback' => callable // Custom callback for cleaning value if something more difficult than just type/choices
* // accepts arguments ($value, $preferencename) * // is needed accepts arguments ($value, $preferencename)
* ) * )
* ) * )
* *
@ -961,32 +1025,44 @@ class user {
// Array of user preferences and expected types/values. // Array of user preferences and expected types/values.
// Every preference that can be updated directly by user should be added here. // Every preference that can be updated directly by user should be added here.
$preferences = array(); $preferences = [];
$preferences['auth_forcepasswordchange'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'choices' => array(0, 1), $preferences['auth_forcepasswordchange'] = [
'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED,
'choices' => [0, 1],
'permissioncallback' => function ($user, $preferencename) { 'permissioncallback' => function ($user, $preferencename) {
global $USER; global $USER;
$systemcontext = context_system::instance(); $systemcontext = context_system::instance();
return ($USER->id != $user->id && (has_capability('moodle/user:update', $systemcontext) || return ($USER->id != $user->id && (has_capability('moodle/user:update', $systemcontext) ||
($user->timecreated > time() - 10 && has_capability('moodle/user:create', $systemcontext)))); ($user->timecreated > time() - 10 && has_capability('moodle/user:create', $systemcontext))));
}); },
$preferences['forum_markasreadonnotification'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1, ];
'choices' => array(0, 1)); $preferences['forum_markasreadonnotification'] = [
$preferences['htmleditor'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED, 'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1,
'choices' => [0, 1],
];
$preferences['htmleditor'] = [
'type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED,
'cleancallback' => function ($value, $preferencename) { 'cleancallback' => function ($value, $preferencename) {
if (empty($value) || !array_key_exists($value, component::get_plugin_list('editor'))) { if (empty($value) || !array_key_exists($value, component::get_plugin_list('editor'))) {
return null; return null;
} }
return $value; return $value;
}); },
$preferences['badgeprivacysetting'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1, ];
'choices' => array(0, 1), 'permissioncallback' => function($user, $preferencename) { $preferences['badgeprivacysetting'] = [
'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1,
'choices' => [0, 1], 'permissioncallback' => function ($user, $preferencename) {
global $CFG; global $CFG;
return !empty($CFG->enablebadges) && self::is_current_user($user); return !empty($CFG->enablebadges) && self::is_current_user($user);
}); },
$preferences['blogpagesize'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 10, ];
$preferences['blogpagesize'] = [
'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 10,
'permissioncallback' => function ($user, $preferencename) { 'permissioncallback' => function ($user, $preferencename) {
return self::is_current_user($user) && has_capability('moodle/blog:view', context_system::instance()); return self::is_current_user($user) && has_capability('moodle/blog:view', context_system::instance());
}); },
];
$preferences['filemanager_recentviewmode'] = [ $preferences['filemanager_recentviewmode'] = [
'type' => PARAM_INT, 'type' => PARAM_INT,
'null' => NULL_NOT_ALLOWED, 'null' => NULL_NOT_ALLOWED,
@ -1061,7 +1137,7 @@ class user {
global $CFG; global $CFG;
return self::is_current_user($user) && return self::is_current_user($user) &&
(!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER)); (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER));
} },
]; ];
// Core components that may want to define their preferences. // Core components that may want to define their preferences.
@ -1136,7 +1212,7 @@ class user {
if (!has_capability('moodle/user:editprofile', $personalcontext)) { if (!has_capability('moodle/user:editprofile', $personalcontext)) {
return false; return false;
} }
if (is_siteadmin($user->id) and !is_siteadmin($USER)) { if (is_siteadmin($user->id) && !is_siteadmin($USER)) {
// Only admins may edit other admins. // Only admins may edit other admins.
return false; return false;
} }
@ -1364,7 +1440,7 @@ class user {
} }
} }
$requirednames = array(); $requirednames = [];
// With each name, see if it is in the display name template, and add it to the required names array if it is. // With each name, see if it is in the display name template, and add it to the required names array if it is.
foreach ($allnames as $allname) { foreach ($allnames as $allname) {
if (strpos($template, $allname) !== false) { if (strpos($template, $allname) !== false) {
@ -1389,7 +1465,7 @@ class user {
// Tidy up any misc. characters (Not perfect, but gets most characters). // Tidy up any misc. characters (Not perfect, but gets most characters).
// Don't remove the "u" at the end of the first expression unless you want garbled characters when combining hiragana or // Don't remove the "u" at the end of the first expression unless you want garbled characters when combining hiragana or
// katakana and parenthesis. // katakana and parenthesis.
$patterns = array(); $patterns = [];
// This regular expression replacement is to fix problems such as 'James () Kirk' Where 'Tiberius' (middlename) has not been // This regular expression replacement is to fix problems such as 'James () Kirk' Where 'Tiberius' (middlename) has not been
// filled in by a user. // filled in by a user.
// The special characters are Japanese brackets that are common enough to make allowances for them (not covered by :punct:). // The special characters are Japanese brackets that are common enough to make allowances for them (not covered by :punct:).
@ -1417,7 +1493,7 @@ class user {
* @param context|null $context The context will be used to determine the visibility of the user's profile url. * @param context|null $context The context will be used to determine the visibility of the user's profile url.
* @return url Profile url of the user * @return url Profile url of the user
*/ */
public static function get_profile_url(stdClass $user, context $context = null): url { public static function get_profile_url(stdClass $user, ?context $context = null): url {
if (empty($user->id)) { if (empty($user->id)) {
throw new coding_exception('User id is required when displaying profile url.'); throw new coding_exception('User id is required when displaying profile url.');
} }