Moodle APIs 4.3
Moodle 4.3.6 (Build: 20240812)
accesslib.php File Reference

Functions

 assign_capability ($capability, $permission, $roleid, $contextid, $overwrite=false, array $performancehints=[])
 Function to write context specific overrides, or default capabilities.
 
 assign_legacy_capabilities ($capability, $legacyperms)
 Assign the defaults found in this capability definition to roles that have the corresponding legacy capabilities assigned to them.
 
 can_access_course (stdClass $course, $user=null, $withcapability='', $onlyactive=false)
 Returns true if the user is able to access the course.
 
 component_level_changed ($cap, $comp, $contextlevel)
 Aids in detecting if a new line is required when reading a new capability.
 
 core_role_set_assign_allowed ($fromroleid, $targetroleid)
 Creates a record in the role_allow_assign table.
 
 core_role_set_override_allowed ($fromroleid, $targetroleid)
 Creates a record in the role_allow_override table.
 
 core_role_set_switch_allowed ($fromroleid, $targetroleid)
 Creates a record in the role_allow_switch table.
 
 core_role_set_view_allowed ($fromroleid, $targetroleid)
 Creates a record in the role_allow_view table.
 
 count_role_users ($roleid, context $context, $parent=false)
 Counts all the users assigned this role in this context or higher.
 
 create_role ($name, $shortname, $description, $archetype='')
 Function that creates a role.
 
 delete_role ($roleid)
 Function that deletes a role and cleanups up after it.
 
 extract_suspended_users ($context, &$users, $ignoreusers=array())
 Given context and array of users, returns array of users whose enrolment status is suspended, or enrolment has expired or has not started.
 
 fix_role_sortorder ($allroles)
 Fix the roles.sortorder field in the database, so it contains sequential integers, and return an array of roleids in order.
 
 get_all_capabilities ()
 Returns all capabilitiy records, preferably from MUC and not database.
 
 get_all_risks ()
 Returns an array of all the known types of risk The array keys can be used, for example as CSS class names, or in calls to print_risk_icon.
 
 get_all_roles (context $context=null)
 Returns all site roles in correct sort order.
 
 get_archetype_roles ($archetype)
 Returns roles of a specified archetype.
 
 get_assignable_roles (context $context, $rolenamedisplay=ROLENAME_ALIAS, $withusercounts=false, $user=null)
 Gets a list of roles that this user can assign in this context.
 
 get_capabilities_from_role_on_context ($role, context $context)
 Get all capabilities for this role on this context (overrides)
 
 get_capability_docs_link ($capability)
 Return a link to moodle docs for a given capability name.
 
 get_capability_info ($capabilityname)
 Returns capability information (cached)
 
 get_capability_string ($capabilityname)
 Returns the human-readable, translated version of the capability.
 
 get_component_string ($component, $contextlevel)
 This gets the mod/block/course/core etc strings.
 
 get_context_info_array ($contextid)
 Returns context instance plus related course and cm instances.
 
 get_default_capabilities ($archetype)
 Returns default capabilities for given role archetype.
 
 get_default_contextlevels ($rolearchetype)
 Returns default context levels where roles can be assigned.
 
 get_default_enrol_roles (context $context, $addroleid=null)
 Create a role menu suitable for default role selection in enrol plugins.
 
 get_default_role_archetype_allows ($type, $archetype)
 Return default roles that can be assigned, overridden or switched by give role archetype.
 
 get_deprecated_capability_info ($capabilityname)
 Returns deprecation info for this particular capabilty (cached)
 
 get_guest_role ()
 Get the default guest role, this is used for guest account, search engine spiders, etc.
 
 get_local_override ($roleid, $contextid, $capability)
 Get the local override (if any) for a given capability in a role in a context.
 
 get_overridable_roles (context $context, $rolenamedisplay=ROLENAME_ALIAS, $withcounts=false)
 Gets a list of roles that this user can override in this context.
 
 get_profile_roles (context $context)
 Gets the list of roles assigned to this context and up (parents) from the aggregation of: a) the list of roles that are visible on user profile page and participants page (profileroles setting) and; b) if applicable, those roles that are assigned in the context.
 
 get_role_archetypes ()
 Returns array of all role archetypes.
 
 get_role_contextlevels ($roleid)
 Return context levels where this role is assignable.
 
 get_role_definitions (array $roleids)
 Fetch raw "site wide" role definitions.
 
 get_role_definitions_uncached (array $roleids)
 Query raw "site wide" role definitions.
 
 get_role_names_with_caps_in_context ($context, $capabilities)
 Returns an array of role names that have ALL of the the supplied capabilities Uses get_roles_with_caps_in_context().
 
 get_role_users ($roleid, context $context, $parent=false, $fields='', $sort=null, $all=true, $group='', $limitfrom='', $limitnum='', $extrawheretest='', $whereorsortparams=array())
 Gets all the users assigned this role in this context or higher.
 
 get_roles_for_contextlevels ($contextlevel)
 Return roles suitable for assignment at the specified context level.
 
 get_roles_used_in_context (context $context, $includeparents=true)
 Gets the list of roles assigned to this context and up (parents)
 
 get_roles_with_cap_in_context ($context, $capability)
 Returns two lists, this can be used to find out if user has capability.
 
 get_roles_with_capability ($capability, $permission=null, $context=null)
 Get the roles that have a given capability assigned to it.
 
 get_roles_with_caps_in_context ($context, $capabilities)
 Returns an array of role IDs that have ALL of the the supplied capabilities Uses get_roles_with_cap_in_context().
 
 get_roles_with_override_on_context (context $context)
 Get any role that has an override on exact context.
 
 get_sorted_contexts ($select, $params=array())
 Runs get_records select on context table and returns the result Does get_records_select on the context table, and returns the results ordered by contextlevel, and then the natural sort order within each level.
 
 get_suspended_userids (context $context, $usecache=false)
 Given context and array of users, returns array of user ids whose enrolment status is suspended, or enrolment has expired or not started.
 
 get_switchable_roles (context $context, $rolenamedisplay=ROLENAME_ALIAS)
 Gets a list of roles that this user can switch to in a context.
 
 get_user_capability_contexts (string $capability, bool $getcategories, $userid=null, $doanything=true, $coursefieldsexceptid='', $categoryfieldsexceptid='', $courseorderby='', $categoryorderby='', $limit=0)
 This function gets the list of course and course category contexts that this user has a particular capability in.
 
 get_user_capability_course ($capability, $userid=null, $doanything=true, $fieldsexceptid='', $orderby='', $limit=0)
 This function gets the list of courses that this user has a particular capability in.
 
 get_user_roles (context $context, $userid=0, $checkparentcontexts=true, $order='c.contextlevel DESC, r.sortorder ASC')
 Gets all the user roles assigned in this context, or higher contexts this is mainly used when checking if a user can assign a role, or overriding a role i.e.
 
 get_user_roles_in_course ($userid, $courseid)
 This function is used to print roles column in user profile page.
 
 get_user_roles_with_special (context $context, $userid=0)
 Like get_user_roles, but adds in the authenticated user role, and the front page roles, if applicable.
 
 get_users_by_capability (context $context, $capability, $fields='', $sort='', $limitfrom='', $limitnum='', $groups='', $exceptions='', $notuseddoanything=null, $notusedview=null, $useviewallgroups=false)
 Who has this capability in this context?
 
 get_users_from_role_on_context ($role, context $context)
 Find all user assignment of users for this role, on this context.
 
 get_users_roles (context $context, $userids=[], $checkparentcontexts=true, $order='c.contextlevel DESC, r.sortorder ASC')
 Gets all the user roles assigned in this context, or higher contexts for a list of users.
 
 get_viewable_roles (context $context, $userid=null, $rolenamedisplay=ROLENAME_ALIAS)
 Gets a list of roles that this user can view in a context.
 
 get_with_capability_join (context $context, $capability, $useridcolumn)
 Gets sql joins for finding users with capability in the given context.
 
 get_with_capability_sql (context $context, $capability)
 Gets sql for finding users with capability in the given context.
 
 guess_if_creator_will_have_course_capability ($capability, context $context, $user=null)
 Is course creator going to have capability in a new course?
 
 has_all_capabilities (array $capabilities, context $context, $user=null, $doanything=true)
 Check if the user has all the capabilities in a list.
 
 has_any_capability (array $capabilities, context $context, $user=null, $doanything=true)
 Check if the user has any one of several capabilities from a list.
 
 has_capability ($capability, context $context, $user=null, $doanything=true)
 Check whether a user has a particular capability in a given context.
 
 has_coursecontact_role ($userid)
 Returns true if user has at least one role assign of 'coursecontact' role (is potentially listed in some course descriptions).
 
 is_guest (context $context, $user=null)
 Does user have a (temporary or real) guest access to course?
 
 is_inside_frontpage (context $context)
 Check if context is the front page context or a context inside it.
 
 is_role_switched ($courseid)
 Checks if the user has switched roles within the given course.
 
 is_safe_capability ($capability)
 Verify capability risks.
 
 is_siteadmin ($user_or_id=null)
 Check if the user is an admin at the site level.
 
 is_viewing (context $context, $user=null, $withcapability='')
 Returns true if the user has moodle/course:view capability in the course, this is intended for admins, managers (aka small admins), inspectors, etc.
 
 isguestuser ($user=null)
 Determines if a user is logged in as real guest user with username 'guest'.
 
 isloggedin ()
 Determines if a user is currently logged in.
 
 load_temp_course_role (context_course $coursecontext, $roleid)
 Adds a temp role to current USER->access array.
 
 mark_user_dirty ($userid)
 Mark a user as dirty (with timestamp) so as to force reloading of the user session.
 
 prohibit_is_removable ($roleid, context $context, $capability)
 This function verifies the prohibit comes from this context and there are no more prohibits in parent contexts.
 
 remove_temp_course_roles (context_course $coursecontext)
 Removes any extra guest roles from current USER->access array.
 
 require_all_capabilities (array $capabilities, context $context, $userid=null, $doanything=true, $errormessage='nopermissions', $stringfile='')
 A convenience function that tests has_capability for a list of capabilities, and displays an error if the user does not have that capability.
 
 require_capability ($capability, context $context, $userid=null, $doanything=true, $errormessage='nopermissions', $stringfile='')
 A convenience function that tests has_capability, and displays an error if the user does not have that capability.
 
 reset_role_capabilities ($roleid)
 Reset role capabilities to default according to selected role archetype.
 
 role_assign ($roleid, $userid, $contextid, $component='', $itemid=0, $timemodified='')
 This function makes a role-assignment (a role for a user in a particular context)
 
 role_cap_duplicate ($sourcerole, $targetrole)
 Duplicates all the base definitions of a role.
 
 role_change_permission ($roleid, $context, $capname, $permission)
 More user friendly role permission changing, it should produce as few overrides as possible.
 
 role_context_capabilities ($roleid, context $context, $cap='')
 This function pulls out all the resolved capabilities (overrides and defaults) of a role used in capability overrides in contexts at a given context.
 
 role_fix_names ($roleoptions, context $context=null, $rolenamedisplay=ROLENAME_ALIAS, $returnmenu=null)
 Prepare list of roles for display, apply aliases and localise default role names.
 
 role_get_description (stdClass $role)
 Returns localised role description if available.
 
 role_get_name (stdClass $role, $context=null, $rolenamedisplay=ROLENAME_ALIAS)
 Get localised role name or alias if exists and format the text.
 
 role_get_names (context $context=null, $rolenamedisplay=ROLENAME_ALIAS, $returnmenu=null)
 Get all the localised role names for a context.
 
 role_switch ($roleid, context $context)
 Switches the current user to another role for the current session and only in the given context.
 
 role_unassign ($roleid, $userid, $contextid, $component='', $itemid=0)
 Removes one role assignment.
 
 role_unassign_all (array $params, $subcontexts=false, $includemanual=false)
 Removes multiple role assignments, parameters may contain: 'roleid', 'userid', 'contextid', 'component', 'enrolid'.
 
 set_role_contextlevels ($roleid, array $contextlevels)
 Set the context levels at which a particular role can be assigned.
 
 sort_by_roleassignment_authority ($users, context $context, $roles=array(), $sortpolicy='locality')
 Re-sort a users array based on a sorting policy.
 
 switch_roles ($first, $second)
 Switch the sort order of two roles (used in admin/roles/manage.php).
 
 unassign_capability ($capability, $roleid, $contextid=null)
 Unassign a capability from a role.
 
 user_can_assign (context $context, $targetroleid)
 Checks if a user can assign users to a particular role in this context.
 
 user_has_role_assignment ($userid, $roleid, $contextid=0)
 Simple function returning a boolean true if user has roles in context or parent contexts, otherwise false.
 

Variables

$ACCESSLIB_PRIVATE accessdatabyuser = array()
 
const ACCESSLIB_HINT_CONTEXT_EXISTS (!defined('CONTEXT_CACHE_MAX_SIZE')) 'contextexists'
 Performance hint for assign_capability: the contextid is known to exist.
 
const ACCESSLIB_HINT_NO_EXISTING 'notexists'
 Performance hint for assign_capability: there is no existing entry in role_capabilities.
 
$ACCESSLIB_PRIVATE cacheroledefs = array()
 
const CAP_ALLOW 1
 Allow permission, overrides CAP_PREVENT defined in parent contexts.
 
const CAP_INHERIT 0
 No capability change.
 
const CAP_PREVENT -1
 Prevent permission, overrides CAP_ALLOW defined in parent contexts.
 
const CAP_PROHIBIT -1000
 Prohibit permission, overrides everything in current and child contexts.
 
const CONTEXT_BLOCK 80
 Block context level - one instance for each block, sticky blocks are tricky because ppl think they should be able to override them at lower contexts.
 
const CONTEXT_COURSE 50
 Course context level - one instances for each course.
 
const CONTEXT_COURSECAT 40
 Course category context level - one instance for each category.
 
const CONTEXT_MODULE 70
 Course module context level - one instance for each course module.
 
const CONTEXT_SYSTEM 10
 System context level - only one instance in every system.
 
const CONTEXT_USER 30
 User context level - one instance for each user describing what others can do to user.
 
$ACCESSLIB_PRIVATE dirtycontexts = null
 
$ACCESSLIB_PRIVATE dirtyusers = null
 
const RISK_CONFIG 0x0002
 Capability allows changes in system configuration - see
 
const RISK_DATALOSS 0x0020
 capability allows mass delete of data belonging to other users - see
 
const RISK_MANAGETRUST 0x0001
 Capability allow management of trusts - NOT IMPLEMENTED YET - see
 
const RISK_PERSONAL 0x0008
 Capability allows access to personal user information - see
 
const RISK_SPAM 0x0010
 Capability allows users to add content others may see - see
 
const RISK_XSS 0x0004
 Capability allows user to add scripted content - see
 
const ROLENAME_ALIAS 1
 rolename displays - the name as defined by a role alias at the course level, falls back to ROLENAME_ORIGINAL if alias not present
 
const ROLENAME_ALIAS_RAW 4
 rolename displays - the name as defined by a role alias, in raw form suitable for editing
 
const ROLENAME_BOTH 2
 rolename displays - Both, like this: Role alias (Original)
 
const ROLENAME_ORIGINAL 0
 rolename displays - the name as defined in the role definition, localised if name empty
 
const ROLENAME_ORIGINALANDSHORT 3
 rolename displays - the name as defined in the role definition and the shortname in brackets
 
const ROLENAME_SHORT 5
 rolename displays - the name is simply short role name