Moodle APIs
3.8
Moodle 3.8.6 (Build: 20201109)
|
User class to access user details. More...
Classes | |
class | course_form |
Class user_course_form. More... | |
class | participants_table |
Class for the displaying the participants table. More... | |
Static Public Member Functions | |
static | can_edit_preference ($preferencename, $user) |
Can current user edit preference of this/another user. More... | |
static | clean_data ($user) |
Clean the user data. More... | |
static | clean_field ($data, $field) |
Clean a specific user field. More... | |
static | clean_preference ($value, $preferencename) |
Clean value of a user preference. More... | |
static | get_noreply_user () |
Return noreply user record, this is currently used in messaging system only for sending messages from noreply email. More... | |
static | get_property_choices ($property) |
Get the choices of the property. More... | |
static | get_property_default ($property) |
Get the property default. More... | |
static | get_property_definition ($property) |
Get properties of a user field. More... | |
static | get_property_null ($property) |
Discover if the property is NULL_ALLOWED or NULL_NOT_ALLOWED. More... | |
static | get_property_type ($property) |
Get the parameter type of the property. More... | |
static | get_support_user () |
Return support user record, this is currently used in messaging system only for sending messages to support email. More... | |
static | get_user ($userid, $fields=' *', $strictness=IGNORE_MISSING) |
Return user object from db or create noreply or support user, if userid matches corse_user::NOREPLY_USER or corse_user::SUPPORT_USER respectively. More... | |
static | get_user_by_email ($email, $fields=' *', $mnethostid=null, $strictness=IGNORE_MISSING) |
Return user object from db based on their email. More... | |
static | get_user_by_username ($username, $fields=' *', $mnethostid=null, $strictness=IGNORE_MISSING) |
Return user object from db based on their username. More... | |
static | is_real_user ($userid, $checkdb=false) |
Return true if user id is greater than 0 and alternatively check db. More... | |
static | require_active_user ($user, $checksuspended=false, $checknologin=false) |
Check if the given user is an active user in the site. More... | |
static | reset_caches () |
Clean the properties cache. More... | |
static | reset_internal_users () |
Reset self::$noreplyuser and self::$supportuser. More... | |
static | search ($query, context_course $coursecontext=null, $max=30, $querylimit=5) |
Searches for users by name, possibly within a specified context, with current user's access. More... | |
static | update_picture (stdClass $usernew, $filemanageroptions=array()) |
Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms. More... | |
static | validate ($data) |
Validate user data. More... | |
Public Attributes | |
global | $CFG |
const | AUTHSYNCFIELDS |
List of fields that can be synched/locked during authentication. More... | |
if (!defined( 'MOODLE_INTERNAL')) | |
const | MAILDISPLAY_COURSE_MEMBERS_ONLY = 2 |
Display email address to course members only. | |
const | MAILDISPLAY_EVERYONE = 1 |
Display email address to everyone. | |
const | MAILDISPLAY_HIDE = 0 |
Hide email address from everyone. | |
const | NOREPLY_USER = -10 |
No reply user id. | |
const | SUPPORT_USER = -20 |
Support user id. | |
int const | VIEWPROFILE_DO_NOT_PREVENT = 0 |
Indicates that user profile view should not be prevented. | |
int const | VIEWPROFILE_FORCE_ALLOW = 1 |
Indicates that user profile view should be allowed even if Moodle would prevent it. | |
int const | VIEWPROFILE_PREVENT = -1 |
Indicates that user profile view should be prevented. | |
Static Public Attributes | |
static stdClass | $noreplyuser = false |
keep record of noreply user | |
static stdClass | $supportuser = false |
keep record of support user | |
Static Protected Member Functions | |
static | default_preference_permission_check ($user, $preferencename) |
Default callback used for checking if current user is allowed to change permission of user $user. More... | |
static | fill_preferences_cache () |
Definition of updateable user preferences and rules for data and access validation. More... | |
static | fill_properties_cache () |
Definition of user profile fields and the expected parameter type for data validation. More... | |
static | get_dummy_user_record () |
Helper function to return dummy noreply user record. More... | |
static | get_enrolled_sql_on_courses_with_capability ($capability) |
Gets an SQL query that lists all enrolled user ids on any course where the current user has the specified capability. More... | |
static | get_preference_definition ($preferencename) |
Retrieves the preference definition. More... | |
Static Protected Attributes | |
static array | $preferencescache = null |
store user preferences cache. | |
static array | $propertiescache = null |
store user fields properties cache. | |
User class to access user details.
|
static |
Can current user edit preference of this/another user.
string | $preferencename | |
stdClass | $user |
bool |
|
static |
Clean the user data.
stdClass | array | $user | the user data to be validated against properties definition. |
stdClass::$user | the cleaned user data. |
|
static |
Clean a specific user field.
string | $data | the user field data to be cleaned. |
string | $field | the user field name on the property definition cache. |
string | the cleaned user data. |
|
static |
Clean value of a user preference.
string | $value | the user preference value to be cleaned. |
string | $preferencename | the user preference name |
string | the cleaned preference value |
|
staticprotected |
Default callback used for checking if current user is allowed to change permission of user $user.
stdClass | $user | |
string | $preferencename |
bool |
|
staticprotected |
Definition of updateable user preferences and rules for data and access validation.
array( 'preferencename' => array( // Either exact preference name or a regular expression. 'null' => NULL_ALLOWED, // Defaults to NULL_NOT_ALLOWED. Takes NULL_NOT_ALLOWED or NULL_ALLOWED. 'type' => PARAM_TYPE, // Expected parameter type of the user field - mandatory '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 // 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) ) )
void |
|
staticprotected |
Definition of user profile fields and the expected parameter type for data validation.
array( 'property_name' => array( // The user property to be checked. Should match the field on the user table. 'null' => NULL_ALLOWED, // Defaults to NULL_NOT_ALLOWED. Takes NULL_NOT_ALLOWED or NULL_ALLOWED. 'type' => PARAM_TYPE, // Expected parameter type of the user field. 'choices' => array(1, 2..) // An array of accepted values of the user field. 'default' => $CFG->setting // An default value for the field. ) )
The fields choices and default are optional.
void |
|
staticprotected |
Helper function to return dummy noreply user record.
stdClass |
|
staticprotected |
Gets an SQL query that lists all enrolled user ids on any course where the current user has the specified capability.
Helper function used for searching users.
string | $capability | Required capability |
array | Array containing SQL and params, or two nulls if there are no courses |
|
static |
Return noreply user record, this is currently used in messaging system only for sending messages from noreply email.
It will return record of $CFG->noreplyuserid if set else return dummy user object with hard-coded $user->emailstop = 1 so noreply can be sent to user.
stdClass | user record. |
|
staticprotected |
Retrieves the preference definition.
string | $preferencename |
array |
|
static |
Get the choices of the property.
This is a helper method to validate a value against a list of acceptable choices. For instance: country, language, themes and etc.
string | $property | property name to be retrieved. |
coding_exception | if the requested property name is invalid or if it does not has a list of choices. |
array | the property parameter type. |
|
static |
Get the property default.
This method gets the default value of a field (if exists).
string | $property | property name to be retrieved. |
coding_exception | if the requested property name is invalid or if it does not has a default value. |
string | the property default value. |
|
static |
Get properties of a user field.
string | $property | property name to be retrieved. |
coding_exception | if the requested property name is invalid. |
array | the property definition. |
|
static |
Discover if the property is NULL_ALLOWED or NULL_NOT_ALLOWED.
string | $property | property name to be retrieved. |
coding_exception | if the requested property name is invalid. |
bool | true if the property is NULL_ALLOWED, false otherwise. |
|
static |
Get the parameter type of the property.
string | $property | property name to be retrieved. |
coding_exception | if the requested property name is invalid. |
int | the property parameter type. |
|
static |
Return support user record, this is currently used in messaging system only for sending messages to support email.
$CFG->supportuserid is set then returns user record $CFG->supportemail is set then return dummy record with $CFG->supportemail else return admin user record with hard-coded $user->emailstop = 0, so user gets support message.
stdClass | user record. |
|
static |
Return user object from db or create noreply or support user, if userid matches corse_user::NOREPLY_USER or corse_user::SUPPORT_USER respectively.
If userid is not found, then return false.
int | $userid | user id |
string | $fields | A comma separated list of user fields to be returned, support and noreply user will not be filtered by this. |
int | $strictness | IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found; IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended); MUST_EXIST means throw an exception if no user record or multiple records found. |
stdClass|bool | user record if found, else false. |
dml_exception | if user record not found and respective $strictness is set. |
|
static |
Return user object from db based on their email.
string | The email of the user searched. | |
string | $fields | A comma separated list of user fields to be returned, support and noreply user. |
int | $mnethostid | The id of the remote host. |
int | $strictness | IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found; IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended); MUST_EXIST means throw an exception if no user record or multiple records found. |
stdClass|bool | user record if found, else false. |
dml_exception | if user record not found and respective $strictness is set. |
|
static |
Return user object from db based on their username.
string | $username | The username of the user searched. |
string | $fields | A comma separated list of user fields to be returned, support and noreply user. |
int | $mnethostid | The id of the remote host. |
int | $strictness | IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found; IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended); MUST_EXIST means throw an exception if no user record or multiple records found. |
stdClass|bool | user record if found, else false. |
dml_exception | if user record not found and respective $strictness is set. |
|
static |
Return true if user id is greater than 0 and alternatively check db.
int | $userid | user id. |
bool | $checkdb | if true userid will be checked in db. By default it's false, and userid is compared with 0 for performance. |
bool | true is real user else false. |
|
static |
Check if the given user is an active user in the site.
stdClass | $user | user object |
boolean | $checksuspended | whether to check if the user has the account suspended |
boolean | $checknologin | whether to check if the user uses the nologin auth method |
moodle_exception |
|
static |
Clean the properties cache.
During unit tests we need to be able to reset all caches so that each new test starts in a known state. Intended for use only for testing, phpunit calls this before every test.
|
static |
Reset self::$noreplyuser and self::$supportuser.
This is only used by phpunit, and there is no other use case for this function. Please don't use it outside phpunit.
|
static |
Searches for users by name, possibly within a specified context, with current user's access.
Deciding which users to search is complicated because it relies on user permissions; ideally, we shouldn't show names if you aren't allowed to see their profile. The permissions for seeing profile are really complicated.
Even if search is restricted to a course, it's possible that other people might have been able to contribute within the course (e.g. they were enrolled before and not now; or people with system-level roles) so if the user has permission we do want to include everyone. However, if there are multiple results then we prioritise the ones who are enrolled in the course.
If you have moodle/user:viewdetails at system level, you can search everyone. Otherwise we check which courses you do have that permission and search everyone who is enrolled on those courses.
Normally you can only search the user's name. If you have the moodle/site:viewuseridentity capability then we also let you search the fields which are listed as identity fields in the 'showuseridentity' config option. For example, this might include the user's ID number or email.
The $max parameter controls the maximum number of users returned. If users are restricted from view for some reason, multiple runs of the main query might be made; the $querylimit parameter allows this to be restricted. Both parameters can be zero to remove limits.
The returned user objects include id, username, all fields required for user pictures, and user identity fields.
string | $query | Search query text |
context_course | null | $coursecontext | Course context or null if system-wide |
int | $max | Max number of users to return, default 30 (zero = no limit) |
int | $querylimit | Max number of database queries, default 5 (zero = no limit) |
array | Array of user objects with limited fields |
|
static |
Updates the provided users profile picture based upon the expected fields returned from the edit or edit_advanced forms.
stdClass | $usernew | An object that contains some information about the user being updated |
array | $filemanageroptions |
bool | True if the user was updated, false if it stayed the same. |
|
static |
Validate user data.
This method just validates each user field and return an array of errors. It doesn't clean the data, the methods clean() and clean_field() should be used for this purpose.
stdClass | array | $data | user data object or array to be validated. |
array|true::$errors | array of errors found on the user object, true if the validation passed. |
const core_user::AUTHSYNCFIELDS |
List of fields that can be synched/locked during authentication.