Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/user/ -> lib.php (source)

Functions that are not part of a class:

user_create_user($user, $updatepassword = true, $triggerevent = true)   X-Ref
Creates a user

param: stdClass $user user to create
param: bool $updatepassword if true, authentication plugin will update password.
param: bool $triggerevent set false if user_created event should not be triggred.
return: int id of the newly created user

user_update_user($user, $updatepassword = true, $triggerevent = true)   X-Ref
Update a user with a user object (will compare against the ID)

param: stdClass $user the user to update
param: bool $updatepassword if true, authentication plugin will update password.
param: bool $triggerevent set false if user_updated event should not be triggred.

user_delete_user($user)   X-Ref
Marks user deleted in internal user database and notifies the auth plugin.
Also unenrols user from all roles and does other cleanup.

param: object $user Userobject before delete    (without system magic quotes)
return: boolean success

user_get_users_by_id($userids)   X-Ref
Get users by id

param: array $userids id of users to retrieve
return: array

user_get_default_fields()   X-Ref
Returns the list of default 'displayable' fields

Contains database field names but also names used to generate information, such as enrolledcourses

return: array of user fields

user_get_user_details($user, $course = null, array $userfields = array()   X-Ref
Give user record from mdl_user, build an array contains all user details.

Warning: description file urls are 'webservice/pluginfile.php' is use.
it can be changed with $CFG->moodlewstextformatlinkstoimagesfile

param: stdClass $user user record from mdl_user
param: stdClass $course moodle course
param: array $userfields required fields
return: array|null

user_get_user_details_courses($user, array $userfields = [])   X-Ref
Tries to obtain user details, either recurring directly to the user's system profile
or through one of the user's course enrollments (course profile).

You can use the $userfields parameter to reduce the amount of a user record that is required by the method.
The minimum user fields are:
* id
* deleted
* all potential fullname fields

param: stdClass $user The user.
param: array $userfields An array of userfields to be returned, the values must be a
return: array if unsuccessful or the allowed user details.

can_view_user_details_cap($user, $course = null)   X-Ref
Check if $USER have the necessary capabilities to obtain user details.

param: stdClass $user
param: stdClass $course if null then only consider system profile otherwise also consider the course's profile.
return: bool true if $USER can view user details.

user_page_type_list($pagetype, $parentcontext, $currentcontext)   X-Ref
Return a list of page types

param: string $pagetype current page type
param: stdClass $parentcontext Block's parent context
param: stdClass $currentcontext Current context of block
return: array

user_count_login_failures($user, $reset = true)   X-Ref
Count the number of failed login attempts for the given user, since last successful login.

param: int|stdclass $user user id or object.
param: bool $reset Resets failed login count, if set to true.
return: int number of failed login attempts since the last successful login.

user_convert_text_to_menu_items($text, $page)   X-Ref
Converts a string into a flat array of menu items, where each menu items is a
stdClass with fields type, url, title.

param: string $text the menu items definition
param: moodle_page $page the current page
return: array

user_get_user_navigation_info($user, $page, $options = array()   X-Ref
Get a list of essential user navigation items.

param: stdclass $user user object.
param: moodle_page $page page object.
param: array $options associative array.
return: stdClass $returnobj navigation information object, where:

user_add_password_history($userid, $password)   X-Ref
Add password to the list of used hashes for this user.

This is supposed to be used from:
1/ change own password form
2/ password reset process
3/ user signup in auth plugins if password changing supported

param: int $userid user id
param: string $password plaintext password
return: void

user_is_previously_used_password($userid, $password)   X-Ref
Was this password used before on change or reset password page?

The $CFG->passwordreuselimit setting determines
how many times different password needs to be used
before allowing previously used password again.

param: int $userid user id
param: string $password plaintext password
return: bool true if password reused

user_remove_user_device($uuid, $appid = "")   X-Ref
Remove a user device from the Moodle database (for PUSH notifications usually).

param: string $uuid The device UUID.
param: string $appid The app id. If empty all the devices matching the UUID for the user will be removed.
return: bool true if removed, false if the device didn't exists in the database

user_list_view($course, $context)   X-Ref
Trigger user_list_viewed event.

param: stdClass  $course course  object
param: stdClass  $context course context object

user_mygrades_url($userid = null, $courseid = SITEID)   X-Ref
Returns the url to use for the "Grades" link in the user navigation.

param: int $userid The user's ID.
param: int $courseid The course ID if available.
return: mixed A URL to be directed to for "Grades".

user_can_view_profile($user, $course = null, $usercontext = null)   X-Ref
Check if the current user has permission to view details of the supplied user.

This function supports two modes:
If the optional $course param is omitted, then this function finds all shared courses and checks whether the current user has
permission in any of them, returning true if so.
If the $course param is provided, then this function checks permissions in ONLY that course.

param: object $user The other user's details.
param: object $course if provided, only check permissions in this course.
param: context $usercontext The user context if available.
return: bool true for ability to view this user, else false.

user_get_tagged_users($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = 1, $page = 0)   X-Ref
Returns users tagged with a specified tag.

param: core_tag_tag $tag
param: bool $exclusivemode if set to true it means that no other entities tagged with this tag
param: int $fromctx context id where the link was displayed, may be used by callbacks
param: int $ctx context id where to search for records
param: bool $rec search in subcontexts as well
param: int $page 0-based number of page being displayed
return: \core_tag\output\tagindex

user_get_course_lastaccess_sql($accesssince = null, $tableprefix = 'ul', $haveaccessed = false)   X-Ref
Returns SQL that can be used to limit a query to a period where the user last accessed / did not access a course.

param: int $accesssince The unix timestamp to compare to users' last access
param: string $tableprefix
param: bool $haveaccessed Whether to match against users who HAVE accessed since $accesssince (optional)
return: string

user_get_user_lastaccess_sql($accesssince = null, $tableprefix = 'u', $haveaccessed = false)   X-Ref
Returns SQL that can be used to limit a query to a period where the user last accessed / did not access the system.

param: int $accesssince The unix timestamp to compare to users' last access
param: string $tableprefix
param: bool $haveaccessed Whether to match against users who HAVE accessed since $accesssince (optional)
return: string

user_get_lastaccess_sql($columnname, $accesssince, $tableprefix, $haveaccessed = false)   X-Ref
Returns SQL that can be used to limit a query to a period where the user last accessed or
did not access something recorded by a given table.

param: string $columnname The name of the access column to check against
param: int $accesssince The unix timestamp to compare to users' last access
param: string $tableprefix The query prefix of the table to check
param: bool $haveaccessed Whether to match against users who HAVE accessed since $accesssince (optional)
return: string

core_user_inplace_editable($itemtype, $itemid, $newvalue)   X-Ref
Callback for inplace editable API.

param: string $itemtype - Only user_roles is supported.
param: string $itemid - Courseid and userid separated by a :
param: string $newvalue - json encoded list of roleids.
return: \core\output\inplace_editable

user_edit_map_field_purpose($userid, $fieldname)   X-Ref
Map an internal field name to a valid purpose from: "https://www.w3.org/TR/WCAG21/#input-purposes"

param: integer $userid
param: string $fieldname
return: string $purpose (empty string if there is no mapping).

user_update_device_public_key(string $uuid, string $appid, string $publickey)   X-Ref
Update the users public key for the specified device and app.

param: string $uuid The device UUID.
param: string $appid The app id, usually something like com.moodle.moodlemobile.
param: string $publickey The app generated public key.
return: bool