mirror of
https://github.com/moodle/moodle.git
synced 2025-08-09 10:56:56 +02:00
MDL-81031 core: Coding style fixes for \core\user
This commit is contained in:
parent
f943311753
commit
d3d5d8bc73
1 changed files with 190 additions and 114 deletions
|
@ -32,7 +32,7 @@ use stdClass;
|
|||
/**
|
||||
* 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
|
||||
* @copyright 2013 Rajesh Taneja <rajesh@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
|
@ -83,7 +83,7 @@ class user {
|
|||
'firstnamephonetic',
|
||||
'lastnamephonetic',
|
||||
'middlename',
|
||||
'alternatename'
|
||||
'alternatename',
|
||||
];
|
||||
|
||||
/** @var int Indicates that user profile view should be prevented */
|
||||
|
@ -131,7 +131,7 @@ class user {
|
|||
return self::get_support_user();
|
||||
break;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
* @return array Array of user objects with limited fields
|
||||
*/
|
||||
public static function search($query, context_course $coursecontext = null,
|
||||
$max = 30, $querylimit = 5) {
|
||||
public static function search(
|
||||
$query,
|
||||
?context_course $coursecontext = null,
|
||||
$max = 30,
|
||||
$querylimit = 5
|
||||
) {
|
||||
global $CFG, $DB;
|
||||
require_once($CFG->dirroot . '/user/lib.php');
|
||||
|
||||
|
@ -239,8 +243,9 @@ class user {
|
|||
$userparams = [];
|
||||
}
|
||||
if (!$viewsystem) {
|
||||
list($userquery, $userparams) = self::get_enrolled_sql_on_courses_with_capability(
|
||||
'moodle/user:viewdetails');
|
||||
[$userquery, $userparams] = self::get_enrolled_sql_on_courses_with_capability(
|
||||
'moodle/user:viewdetails'
|
||||
);
|
||||
if (!$userquery) {
|
||||
// No permissions anywhere, return nothing.
|
||||
return [];
|
||||
|
@ -248,7 +253,7 @@ class user {
|
|||
}
|
||||
|
||||
// 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
|
||||
// have the permission to display those identity fields.
|
||||
|
@ -281,8 +286,9 @@ class user {
|
|||
$whereparams = array_merge($whereparams, $extraparams);
|
||||
} else {
|
||||
// Get all courses where user can view full user identity.
|
||||
list($sql, $params) = self::get_enrolled_sql_on_courses_with_capability(
|
||||
'moodle/site:viewuseridentity');
|
||||
[$sql, $params] = self::get_enrolled_sql_on_courses_with_capability(
|
||||
'moodle/site:viewuseridentity'
|
||||
);
|
||||
if ($sql) {
|
||||
// Join that with the user query to get an extra field indicating if we can.
|
||||
$userquery = "
|
||||
|
@ -302,9 +308,9 @@ class user {
|
|||
|
||||
// Default order is just name order. But if searching within a course then we show users
|
||||
// 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) {
|
||||
list ($sql, $params) = get_enrolled_sql($coursecontext);
|
||||
[$sql, $params] = get_enrolled_sql($coursecontext);
|
||||
$mainfield = 'innerusers2.id';
|
||||
if ($usingshowidentity) {
|
||||
$mainfield .= ', innerusers2.showidentity';
|
||||
|
@ -326,13 +332,17 @@ class user {
|
|||
$pos = 0;
|
||||
$readcount = $max + 2;
|
||||
for ($i = 0; $i < $querylimit; $i++) {
|
||||
$rawresult = $DB->get_records_sql("
|
||||
$rawresult = $DB->get_records_sql(
|
||||
"
|
||||
SELECT $selectfields
|
||||
FROM ($userquery) users
|
||||
JOIN {user} u ON u.id = users.id
|
||||
WHERE $where
|
||||
ORDER BY $order", array_merge($userparams, $whereparams, $orderparams),
|
||||
$pos, $readcount);
|
||||
ORDER BY $order",
|
||||
array_merge($userparams, $whereparams, $orderparams),
|
||||
$pos,
|
||||
$readcount
|
||||
);
|
||||
foreach ($rawresult as $user) {
|
||||
// Skip guest.
|
||||
if ($user->username === 'guest') {
|
||||
|
@ -374,8 +384,12 @@ class user {
|
|||
*/
|
||||
protected static function get_enrolled_sql_on_courses_with_capability($capability) {
|
||||
// Get all courses where user have the capability.
|
||||
$courses = get_user_capability_course($capability, null, true,
|
||||
implode(',', array_values(context_helper::get_preload_record_columns('ctx'))));
|
||||
$courses = get_user_capability_course(
|
||||
$capability,
|
||||
null,
|
||||
true,
|
||||
implode(',', array_values(context_helper::get_preload_record_columns('ctx')))
|
||||
);
|
||||
if (!$courses) {
|
||||
return [null, null];
|
||||
}
|
||||
|
@ -388,7 +402,7 @@ class user {
|
|||
foreach ($courses as $course) {
|
||||
// Get SQL to list user ids enrolled in this 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.
|
||||
if ($unionsql) {
|
||||
|
@ -531,7 +545,7 @@ class user {
|
|||
return false;
|
||||
}
|
||||
if ($checkdb) {
|
||||
return $DB->record_exists('user', array('id' => $userid));
|
||||
return $DB->record_exists('user', ['id' => $userid]);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
@ -576,11 +590,11 @@ class user {
|
|||
throw new moodle_exception('guestsarenotallowed', 'error');
|
||||
}
|
||||
|
||||
if ($checksuspended and $user->suspended) {
|
||||
if ($checksuspended && $user->suspended) {
|
||||
throw new moodle_exception('suspended', 'auth');
|
||||
}
|
||||
|
||||
if ($checknologin and $user->auth == 'nologin') {
|
||||
if ($checknologin && $user->auth == 'nologin') {
|
||||
throw new moodle_exception('suspended', 'auth');
|
||||
}
|
||||
}
|
||||
|
@ -592,7 +606,7 @@ class user {
|
|||
* @param array $filemanageroptions
|
||||
* @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;
|
||||
require_once("$CFG->libdir/gdlib.php");
|
||||
|
||||
|
@ -639,7 +653,7 @@ class user {
|
|||
}
|
||||
|
||||
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;
|
||||
} else {
|
||||
return false;
|
||||
|
@ -672,66 +686,112 @@ class user {
|
|||
|
||||
// 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.
|
||||
$fields = array();
|
||||
$fields['id'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['auth'] = array('type' => PARAM_AUTH, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['confirmed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['policyagreed'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['deleted'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['suspended'] = array('type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['mnethostid'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['username'] = array('type' => PARAM_USERNAME, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['password'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['idnumber'] = array('type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['firstname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['lastname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['surname'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['email'] = array('type' => PARAM_RAW_TRIMMED, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['emailstop'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0);
|
||||
$fields['phone1'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['phone2'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['institution'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['department'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['address'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['city'] = array('type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->defaultcity);
|
||||
$fields['country'] = array('type' => PARAM_ALPHA, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->country,
|
||||
'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_countries(true, true)));
|
||||
$fields['lang'] = array('type' => PARAM_LANG, 'null' => NULL_NOT_ALLOWED,
|
||||
'default' => (!empty($CFG->autolangusercreation) && !empty($SESSION->lang)) ? $SESSION->lang : $CFG->lang,
|
||||
'choices' => array_merge(array('' => ''), get_string_manager()->get_list_of_translations(false)));
|
||||
$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()));
|
||||
$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,
|
||||
'default' => core_date::get_server_timezone()); // Must not use choices here: timezones can come and go.
|
||||
$fields['firstaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['lastaccess'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['lastlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['currentlogin'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['lastip'] = array('type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED);
|
||||
$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['descriptionformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['mailformat'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
|
||||
'default' => $CFG->defaultpreference_mailformat);
|
||||
$fields['maildigest'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
|
||||
'default' => $CFG->defaultpreference_maildigest);
|
||||
$fields['maildisplay'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
|
||||
'default' => $CFG->defaultpreference_maildisplay);
|
||||
$fields['autosubscribe'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
|
||||
'default' => $CFG->defaultpreference_autosubscribe);
|
||||
$fields['trackforums'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED,
|
||||
'default' => $CFG->defaultpreference_trackforums);
|
||||
$fields['timecreated'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['timemodified'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['trustbitmask'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED);
|
||||
$fields['imagealt'] = array('type' => PARAM_TEXT, 'null' => NULL_ALLOWED);
|
||||
$fields['lastnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
|
||||
$fields['firstnamephonetic'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
|
||||
$fields['middlename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
|
||||
$fields['alternatename'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED);
|
||||
$fields = [];
|
||||
$fields['id'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['auth'] = ['type' => PARAM_AUTH, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['confirmed'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['policyagreed'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['deleted'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['suspended'] = ['type' => PARAM_BOOL, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['mnethostid'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['username'] = ['type' => PARAM_USERNAME, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['password'] = ['type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['idnumber'] = ['type' => PARAM_RAW, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['firstname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['lastname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['surname'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['email'] = ['type' => PARAM_RAW_TRIMMED, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['emailstop'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 0];
|
||||
$fields['phone1'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['phone2'] = ['type' => PARAM_NOTAGS, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['institution'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['department'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['address'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['city'] = ['type' => PARAM_TEXT, 'null' => NULL_NOT_ALLOWED, 'default' => $CFG->defaultcity];
|
||||
$fields['country'] = [
|
||||
'type' => PARAM_ALPHA,
|
||||
'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,
|
||||
'choices' => array_merge(
|
||||
['' => ''],
|
||||
get_string_manager()->get_list_of_translations(false)
|
||||
),
|
||||
];
|
||||
$fields['calendartype'] = [
|
||||
'type' => PARAM_PLUGIN,
|
||||
'null' => NULL_NOT_ALLOWED,
|
||||
'default' => $CFG->calendartype,
|
||||
'choices' => array_merge(
|
||||
['' => ''],
|
||||
\core_calendar\type_factory::get_list_of_calendar_types()
|
||||
),
|
||||
];
|
||||
$fields['theme'] = [
|
||||
'type' => PARAM_THEME,
|
||||
'null' => NULL_NOT_ALLOWED,
|
||||
'default' => theme_config::DEFAULT_THEME,
|
||||
'choices' => array_merge(
|
||||
['' => ''],
|
||||
get_list_of_themes()
|
||||
),
|
||||
];
|
||||
$fields['timezone'] = [
|
||||
// Must not use choices here: timezones can come and go.
|
||||
'type' => PARAM_TIMEZONE,
|
||||
'null' => NULL_NOT_ALLOWED,
|
||||
'default' => core_date::get_server_timezone(),
|
||||
];
|
||||
$fields['firstaccess'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['lastaccess'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['lastlogin'] = ['type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED];
|
||||
$fields['currentlogin'] = ['type' => PARAM_INT, 'null' => NULL_NOT_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;
|
||||
}
|
||||
|
@ -773,8 +833,10 @@ class user {
|
|||
validate_param($value, self::$propertiescache[$property]['type'], self::$propertiescache[$property]['null']);
|
||||
}
|
||||
// Check that the value is part of a list of allowed values.
|
||||
if (!empty(self::$propertiescache[$property]['choices']) &&
|
||||
!isset(self::$propertiescache[$property]['choices'][$value])) {
|
||||
if (
|
||||
!empty(self::$propertiescache[$property]['choices']) &&
|
||||
!isset(self::$propertiescache[$property]['choices'][$value])
|
||||
) {
|
||||
throw new invalid_parameter_exception($value);
|
||||
}
|
||||
} catch (invalid_parameter_exception $e) {
|
||||
|
@ -903,9 +965,10 @@ class user {
|
|||
|
||||
self::fill_properties_cache();
|
||||
|
||||
if (!array_key_exists($property, self::$propertiescache) && !array_key_exists('choices',
|
||||
self::$propertiescache[$property])) {
|
||||
|
||||
if (
|
||||
!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.');
|
||||
}
|
||||
|
||||
|
@ -942,11 +1005,12 @@ class user {
|
|||
* 'choices' => array(1, 2..) // An array of accepted values of the user 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).
|
||||
* '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.
|
||||
* // 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
|
||||
* // accepts arguments ($value, $preferencename)
|
||||
* 'cleancallback' => callable // Custom callback for cleaning value if something more difficult than just type/choices
|
||||
* // is needed accepts arguments ($value, $preferencename)
|
||||
* )
|
||||
* )
|
||||
*
|
||||
|
@ -961,32 +1025,44 @@ class user {
|
|||
|
||||
// Array of user preferences and expected types/values.
|
||||
// Every preference that can be updated directly by user should be added here.
|
||||
$preferences = array();
|
||||
$preferences['auth_forcepasswordchange'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'choices' => array(0, 1),
|
||||
'permissioncallback' => function($user, $preferencename) {
|
||||
$preferences = [];
|
||||
$preferences['auth_forcepasswordchange'] = [
|
||||
'type' => PARAM_INT,
|
||||
'null' => NULL_NOT_ALLOWED,
|
||||
'choices' => [0, 1],
|
||||
'permissioncallback' => function ($user, $preferencename) {
|
||||
global $USER;
|
||||
$systemcontext = context_system::instance();
|
||||
return ($USER->id != $user->id && (has_capability('moodle/user:update', $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['htmleditor'] = array('type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED,
|
||||
'cleancallback' => function($value, $preferencename) {
|
||||
},
|
||||
];
|
||||
$preferences['forum_markasreadonnotification'] = [
|
||||
'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 1,
|
||||
'choices' => [0, 1],
|
||||
];
|
||||
$preferences['htmleditor'] = [
|
||||
'type' => PARAM_NOTAGS, 'null' => NULL_ALLOWED,
|
||||
'cleancallback' => function ($value, $preferencename) {
|
||||
if (empty($value) || !array_key_exists($value, component::get_plugin_list('editor'))) {
|
||||
return null;
|
||||
}
|
||||
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;
|
||||
return !empty($CFG->enablebadges) && self::is_current_user($user);
|
||||
});
|
||||
$preferences['blogpagesize'] = array('type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 10,
|
||||
'permissioncallback' => function($user, $preferencename) {
|
||||
},
|
||||
];
|
||||
$preferences['blogpagesize'] = [
|
||||
'type' => PARAM_INT, 'null' => NULL_NOT_ALLOWED, 'default' => 10,
|
||||
'permissioncallback' => function ($user, $preferencename) {
|
||||
return self::is_current_user($user) && has_capability('moodle/blog:view', context_system::instance());
|
||||
});
|
||||
},
|
||||
];
|
||||
$preferences['filemanager_recentviewmode'] = [
|
||||
'type' => PARAM_INT,
|
||||
'null' => NULL_NOT_ALLOWED,
|
||||
|
@ -1061,7 +1137,7 @@ class user {
|
|||
global $CFG;
|
||||
return self::is_current_user($user) &&
|
||||
(!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER));
|
||||
}
|
||||
},
|
||||
];
|
||||
|
||||
// Core components that may want to define their preferences.
|
||||
|
@ -1130,13 +1206,13 @@ class user {
|
|||
// Editing own profile.
|
||||
$systemcontext = context_system::instance();
|
||||
return has_capability('moodle/user:editownprofile', $systemcontext);
|
||||
} else {
|
||||
} else {
|
||||
// Teachers, parents, etc.
|
||||
$personalcontext = context_user::instance($user->id);
|
||||
if (!has_capability('moodle/user:editprofile', $personalcontext)) {
|
||||
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.
|
||||
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.
|
||||
foreach ($allnames as $allname) {
|
||||
if (strpos($template, $allname) !== false) {
|
||||
|
@ -1389,7 +1465,7 @@ class user {
|
|||
// 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
|
||||
// katakana and parenthesis.
|
||||
$patterns = array();
|
||||
$patterns = [];
|
||||
// This regular expression replacement is to fix problems such as 'James () Kirk' Where 'Tiberius' (middlename) has not been
|
||||
// filled in by a user.
|
||||
// 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.
|
||||
* @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)) {
|
||||
throw new coding_exception('User id is required when displaying profile url.');
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue