Moodle PHP Documentation 4.1
Moodle 4.1dev (Build: 20220512) (5e5e12e0)
Namespaces | Classes | Functions | Variables
core_access

Namespaces

namespace  core\event
  
 

Classes

class  context
 
class  context_course
 
class  context_coursecat
 
class  context_helper
 
class  context_module
 
class  context_system
 
class  context_user
 
class  core\event\context_locked
 Event triggered after a context has been frozen. More...
 
class  core\event\context_unlocked
 Event triggered after a context has been unfrozen. More...
 
class  require_login_exception
 Course/activity access exception. More...
 
class  require_login_session_timeout_exception
 Session timeout exception. More...
 
class  required_capability_exception
 Exceptions indicating user does not have permissions to do something and the execution can not continue. More...
 

Functions

 context_helper::__construct ()
 Instance does not make sense here, only static use.
 
 context::__construct (stdClass $record)
 Constructor is protected so that devs are forced to use context_xxx::instance() or context::instance_by_id(). More...
 
 context_system::__construct (stdClass $record)
 Please use context_system::instance() if you need the instance of context. More...
 
 context_user::__construct (stdClass $record)
 Please use context_user::instance($userid) if you need the instance of context. More...
 
 context_coursecat::__construct (stdClass $record)
 Please use context_coursecat::instance($coursecatid) if you need the instance of context. More...
 
 context_course::__construct (stdClass $record)
 Please use context_course::instance($courseid) if you need the instance of context. More...
 
 context_module::__construct (stdClass $record)
 Please use context_module::instance($cmid) if you need the instance of context. More...
 
 context::__get ($name)
 Magic method getter, redirects to read only values. More...
 
 context::__isset ($name)
 Full support for isset on our magic read only properties. More...
 
 context::__set ($name, $value)
 Magic setter method, we do not want anybody to modify properties from the outside. More...
 
 context::__unset ($name)
 All properties are read only, sorry. More...
 
 assign_capability ($capability, $permission, $roleid, $contextid, $overwrite=false)
 Function to write context specific overrides, or default capabilities. More...
 
 assign_legacy_capabilities ($capability, $legacyperms)
 Assign the defaults found in this capability definition to roles that have the corresponding legacy capabilities assigned to them. More...
 
static context_helper::build_all_paths ($force=false)
 Rebuild paths and depths in all context levels. More...
 
static context::build_paths ($force)
 Rebuild context paths and depths at context level. More...
 
static context_system::build_paths ($force)
 Rebuild context paths and depths at system context level. More...
 
static context_user::build_paths ($force)
 Rebuild context paths and depths at user context level. More...
 
static context_coursecat::build_paths ($force)
 Rebuild context paths and depths at course category context level. More...
 
static context_course::build_paths ($force)
 Rebuild context paths and depths at course context level. More...
 
static context_module::build_paths ($force)
 Rebuild context paths and depths at module context level. More...
 
static context::cache_add (context $context)
 Adds a context to the cache. More...
 
static context::cache_get ($contextlevel, $instance)
 Gets a context from the cache. More...
 
static context::cache_get_by_id ($id)
 Gets a context from the cache based on its id. More...
 
static context::cache_remove (context $context)
 Removes a context from the cache. More...
 
 can_access_course (stdClass $course, $user=null, $withcapability='', $onlyactive=false)
 Returns true if the user is able to access the course. More...
 
static context_helper::cleanup_instances ()
 Remove stale contexts that belonged to deleted instances. More...
 
 component_level_changed ($cap, $comp, $contextlevel)
 Aids in detecting if a new line is required when reading a new capability. More...
 
 core_role_set_assign_allowed ($fromroleid, $targetroleid)
 Creates a record in the role_allow_assign table. More...
 
 core_role_set_override_allowed ($fromroleid, $targetroleid)
 Creates a record in the role_allow_override table. More...
 
 core_role_set_switch_allowed ($fromroleid, $targetroleid)
 Creates a record in the role_allow_switch table. More...
 
 core_role_set_view_allowed ($fromroleid, $targetroleid)
 Creates a record in the role_allow_view table. More...
 
 count_role_users ($roleid, context $context, $parent=false)
 Counts all the users assigned this role in this context or higher. More...
 
static context::create_instance_from_record (stdClass $record)
 This function is also used to work around 'protected' keyword problems in context_helper. More...
 
static context_helper::create_instances ($contextlevel=null, $buildpaths=true)
 Create all context instances at the given level and above. More...
 
static context::create_level_instances ()
 Create missing context instances at given level. More...
 
static context_system::create_level_instances ()
 Create missing context instances at system context. More...
 
static context_user::create_level_instances ()
 Create missing context instances at user context level. More...
 
static context_coursecat::create_level_instances ()
 Create missing context instances at course category context level. More...
 
static context_course::create_level_instances ()
 Create missing context instances at course context level. More...
 
static context_module::create_level_instances ()
 Create missing context instances at module context level. More...
 
 create_role ($name, $shortname, $description, $archetype='')
 Function that creates a role. More...
 
 context::delete ()
 Delete the context content and the context record itself.
 
 context::delete_capabilities ()
 Unassign all capabilities from a context.
 
 context::delete_content ()
 Delete all data linked to content, do not delete the context record itself.
 
static context_helper::delete_instance ($contextlevel, $instanceid)
 Delete context instance. More...
 
 delete_role ($roleid)
 Function that deletes a role and cleanups up after it. More...
 
 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. More...
 
 get_all_capabilities ()
 Returns all capabilitiy records, preferably from MUC and not database. More...
 
static context_helper::get_all_levels ()
 Returns a list of all context levels. More...
 
 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. More...
 
 get_all_roles (context $context=null)
 Returns all site roles in correct sort order. More...
 
 get_archetype_roles ($archetype)
 Returns roles of a specified archetype. More...
 
 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. More...
 
 context::get_capabilities (string $sort=self::DEFAULT_CAPABILITY_SORT)
 Returns array of relevant context capability records. More...
 
 context_helper::get_capabilities (string $sort=self::DEFAULT_CAPABILITY_SORT)
 not used More...
 
 context_system::get_capabilities (string $sort=self::DEFAULT_CAPABILITY_SORT)
 Returns array of relevant context capability records. More...
 
 context_user::get_capabilities (string $sort=self::DEFAULT_CAPABILITY_SORT)
 Returns array of relevant context capability records. More...
 
 context_coursecat::get_capabilities (string $sort=self::DEFAULT_CAPABILITY_SORT)
 Returns array of relevant context capability records. More...
 
 context_course::get_capabilities (string $sort=self::DEFAULT_CAPABILITY_SORT)
 Returns array of relevant context capability records. More...
 
 context_module::get_capabilities (string $sort=self::DEFAULT_CAPABILITY_SORT)
 Returns array of relevant context capability records. More...
 
 get_capabilities_from_role_on_context ($role, context $context)
 Get all capabilities for this role on this context (overrides) More...
 
 get_capability_docs_link ($capability)
 Return a link to moodle docs for a given capability name. More...
 
 get_capability_info ($capabilityname)
 Returns capability information (cached) More...
 
 get_capability_string ($capabilityname)
 Returns the human-readable, translated version of the capability. More...
 
 context::get_child_contexts ()
 Recursive function which, given a context, find all its children context ids. More...
 
 context_system::get_child_contexts ()
 Returns all site contexts except the system context, DO NOT call on production servers!! More...
 
 context_coursecat::get_child_contexts ()
 Returns immediate child contexts of category and all subcategories, children of subcategories and courses are not returned. More...
 
static context_helper::get_class_for_level ($contextlevel)
 Returns a class name of the context level class. More...
 
static context::get_cleanup_sql ()
 Returns sql necessary for purging of stale context instances. More...
 
static context_system::get_cleanup_sql ()
 Returns sql necessary for purging of stale context instances. More...
 
static context_user::get_cleanup_sql ()
 Returns sql necessary for purging of stale context instances. More...
 
static context_coursecat::get_cleanup_sql ()
 Returns sql necessary for purging of stale context instances. More...
 
static context_course::get_cleanup_sql ()
 Returns sql necessary for purging of stale context instances. More...
 
static context_module::get_cleanup_sql ()
 Returns sql necessary for purging of stale context instances. More...
 
 get_component_string ($component, $contextlevel)
 This gets the mod/block/course/core etc strings. More...
 
 get_context_info_array ($contextid)
 Returns context instance plus related course and cm instances. More...
 
 context::get_context_name ($withprefix=true, $short=false, $escape=true)
 Returns human readable context identifier. More...
 
 context_system::get_context_name ($withprefix=true, $short=false, $escape=true)
 Returns human readable context identifier. More...
 
 context_user::get_context_name ($withprefix=true, $short=false, $escape=true)
 Returns human readable context identifier. More...
 
 context_coursecat::get_context_name ($withprefix=true, $short=false, $escape=true)
 Returns human readable context identifier. More...
 
 context_course::get_context_name ($withprefix=true, $short=false, $escape=true)
 Returns human readable context identifier. More...
 
 context_module::get_context_name ($withprefix=true, $short=false, $escape=true)
 Returns human readable context identifier. More...
 
 context::get_course_context ($strict=true)
 Is this context part of any course? If yes return course context. More...
 
 context_course::get_course_context ($strict=true)
 Is this context part of any course? If yes return course context. More...
 
 context_module::get_course_context ($strict=true)
 Is this context part of any course? If yes return course context. More...
 
 get_default_capabilities ($archetype)
 Returns default capabilities for given role archetype. More...
 
 get_default_contextlevels ($rolearchetype)
 Returns default context levels where roles can be assigned. More...
 
 get_default_role_archetype_allows ($type, $archetype)
 Return default roles that can be assigned, overridden or switched by give role archetype. More...
 
 get_guest_role ()
 Get the default guest role, this is used for guest account, search engine spiders, etc. More...
 
static context_helper::get_level_name ($contextlevel)
 Returns the name of specified context level. More...
 
static context_system::get_level_name ()
 Returns human readable context level name. More...
 
static context_user::get_level_name ()
 Returns human readable context level name. More...
 
static context_coursecat::get_level_name ()
 Returns human readable context level name. More...
 
static context_course::get_level_name ()
 Returns human readable context level name. More...
 
static context_module::get_level_name ()
 Returns human readable context level name. More...
 
 get_local_override ($roleid, $contextid, $capability)
 Get the local override (if any) for a given capability in a role in a context. More...
 
static context_helper::get_navigation_filter_context (?context $context)
 Gets the current context to be used for navigation tree filtering. More...
 
 get_overridable_roles (context $context, $rolenamedisplay=ROLENAME_ALIAS, $withcounts=false)
 Gets a list of roles that this user can override in this context. More...
 
 context::get_parent_context ()
 Returns parent context. More...
 
 context::get_parent_context_ids ($includeself=false)
 Returns parent context ids of this context in reversed order, i.e. More...
 
 context::get_parent_context_paths ($includeself=false)
 Returns parent context paths of this context. More...
 
 context::get_parent_contexts ($includeself=false)
 Returns parent contexts of this context in reversed order, i.e. More...
 
static context_helper::get_preload_record_columns ($tablealias)
 Returns all fields necessary for context preloading from user $rec. More...
 
static context_helper::get_preload_record_columns_sql ($tablealias)
 Returns all fields necessary for context preloading from user $rec. More...
 
 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. More...
 
 get_role_archetypes ()
 Returns array of all role archetypes. More...
 
 get_role_contextlevels ($roleid)
 Return context levels where this role is assignable. More...
 
 get_role_definitions (array $roleids)
 Fetch raw "site wide" role definitions. More...
 
 get_role_definitions_uncached (array $roleids)
 Query raw "site wide" role definitions. More...
 
 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(). More...
 
 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. More...
 
 get_roles_for_contextlevels ($contextlevel)
 Return roles suitable for assignment at the specified context level. More...
 
 get_roles_used_in_context (context $context, $includeparents=true)
 Gets the list of roles assigned to this context and up (parents) More...
 
 get_roles_with_cap_in_context ($context, $capability)
 Returns two lists, this can be used to find out if user has capability. More...
 
 get_roles_with_capability ($capability, $permission=null, $context=null)
 Get the roles that have a given capability assigned to it. More...
 
 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(). More...
 
 get_roles_with_override_on_context (context $context)
 Get any role that has an override on exact context. More...
 
 get_switchable_roles (context $context, $rolenamedisplay=ROLENAME_ALIAS)
 Gets a list of roles that this user can switch to in a context. More...
 
 context::get_url ()
 Returns the most relevant URL for this context. More...
 
 context_helper::get_url ()
 not used More...
 
 context_system::get_url ()
 Returns the most relevant URL for this context. More...
 
 context_user::get_url ()
 Returns the most relevant URL for this context. More...
 
 context_coursecat::get_url ()
 Returns the most relevant URL for this context. More...
 
 context_course::get_url ()
 Returns the most relevant URL for this context. More...
 
 context_module::get_url ()
 Returns the most relevant URL for this context. More...
 
 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. More...
 
 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. More...
 
 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. More...
 
 get_user_roles_in_course ($userid, $courseid)
 This function is used to print roles column in user profile page. More...
 
 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. More...
 
 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? More...
 
 get_users_from_role_on_context ($role, context $context)
 Find all user assignment of users for this role, on this context. More...
 
 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. More...
 
 get_viewable_roles (context $context, $userid=null, $rolenamedisplay=ROLENAME_ALIAS)
 Gets a list of roles that this user can view in a context. More...
 
 get_with_capability_join (context $context, $capability, $useridcolumn)
 Gets sql joins for finding users with capability in the given context. More...
 
 context::getIterator ()
 Create an iterator because magic vars can't be seen by 'foreach'. More...
 
 guess_if_creator_will_have_course_capability ($capability, context $context, $user=null)
 
 has_all_capabilities (array $capabilities, context $context, $user=null, $doanything=true)
 
 has_any_capability (array $capabilities, context $context, $user=null, $doanything=true)
 
 has_capability ($capability, context $context, $user=null, $doanything=true)
 
 has_coursecontact_role ($userid)
 Returns true if user has at least one role assign of 'coursecontact' role (is potentially listed in some course descriptions). More...
 
static context::insert_context_record ($contextlevel, $instanceid, $parentpath)
 Utility method for context creation. More...
 
static context_coursecat::instance ($categoryid, $strictness=MUST_EXIST)
 Returns course category context instance. More...
 
static context_module::instance ($cmid, $strictness=MUST_EXIST)
 Returns module context instance. More...
 
static context_course::instance ($courseid, $strictness=MUST_EXIST)
 Returns course context instance. More...
 
static context_system::instance ($instanceid=0, $strictness=MUST_EXIST, $cache=true)
 Returns system context instance. More...
 
static context_user::instance ($userid, $strictness=MUST_EXIST)
 Returns user context instance. More...
 
static context::instance_by_id ($id, $strictness=MUST_EXIST)
 Get a context instance as an object, from a given context id. More...
 
 context::is_child_of (context $possibleparent, bool $includeself)
 Determine if the current context is a child of the possible parent. More...
 
 is_guest (context $context, $user=null)
 
 is_inside_frontpage (context $context)
 Check if context is the front page context or a context inside it. More...
 
 context::is_locked ()
 Whether the current context is locked. More...
 
 context::is_parent_of (context $possiblechild, bool $includeself)
 Determine if the current context is a parent of the possible child. More...
 
 is_role_switched ($courseid)
 Checks if the user has switched roles within the given course. More...
 
 is_safe_capability ($capability)
 Verify capability risks. More...
 
 is_siteadmin ($user_or_id=null)
 
 is_viewing (context $context, $user=null, $withcapability='')
 
 isguestuser ($user=null)
 
 isloggedin ()
 
 load_temp_course_role (context_course $coursecontext, $roleid)
 Adds a temp role to current USER->access array. More...
 
 context::mark_dirty ()
 Mark a context as dirty (with timestamp) so as to force reloading of the context.
 
 mark_user_dirty ($userid)
 Mark a user as dirty (with timestamp) so as to force reloading of the user session. More...
 
static context::merge_context_temp_table ()
 Copy prepared new contexts from temp table to context table, we do this in db specific way for perf reasons only.
 
static context_helper::preload_contexts_by_id (array $contextids)
 Preload a set of contexts using their contextid. More...
 
static context_helper::preload_course ($courseid)
 Preload all contexts instances from course. More...
 
static context::preload_from_record (stdClass $rec)
 Preloads context information from db record and strips the cached info. More...
 
static context_helper::preload_from_record (stdClass $rec)
 Preloads context information from db record and strips the cached info. More...
 
 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. More...
 
 context::reload_if_dirty ()
 Reset all cached permissions and definitions if the necessary. More...
 
 remove_temp_course_roles (context_course $coursecontext)
 Removes any extra guest roles from current USER->access array. More...
 
 require_all_capabilities (array $capabilities, context $context, $userid=null, $doanything=true, $errormessage='nopermissions', $stringfile='')
 
 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. More...
 
static context::reset_caches ()
 Resets the cache to remove all data. More...
 
static context_helper::reset_caches ()
 Resets the cache to remove all data. More...
 
static context_helper::reset_levels ()
 Reset internal context levels array.
 
 context::reset_paths ($rebuild=true)
 Remove all context path info and optionally rebuild it. More...
 
 reset_role_capabilities ($roleid)
 Reset role capabilities to default according to selected role archetype. More...
 
 role_assign ($roleid, $userid, $contextid, $component='', $itemid=0, $timemodified='')
 This function makes a role-assignment (a role for a user in a particular context) More...
 
 role_cap_duplicate ($sourcerole, $targetrole)
 Duplicates all the base definitions of a role. More...
 
 role_change_permission ($roleid, $context, $capname, $permission)
 More user friendly role permission changing, it should produce as few overrides as possible. More...
 
 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. More...
 
 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. More...
 
 role_get_description (stdClass $role)
 Returns localised role description if available. More...
 
 role_get_name (stdClass $role, $context=null, $rolenamedisplay=ROLENAME_ALIAS)
 Get localised role name or alias if exists and format the text. More...
 
 role_get_names (context $context=null, $rolenamedisplay=ROLENAME_ALIAS, $returnmenu=null)
 Get all the localised role names for a context. More...
 
 role_switch ($roleid, context $context)
 Switches the current user to another role for the current session and only in the given context. More...
 
 role_unassign ($roleid, $userid, $contextid, $component='', $itemid=0)
 Removes one role assignment. More...
 
 role_unassign_all (array $params, $subcontexts=false, $includemanual=false)
 Removes multiple role assignments, parameters may contain: 'roleid', 'userid', 'contextid', 'component', 'enrolid'. More...
 
 context::set_locked (bool $locked)
 Set whether this context has been locked or not. More...
 
 context_system::set_locked (bool $locked)
 Set whether this context has been locked or not. More...
 
 set_role_contextlevels ($roleid, array $contextlevels)
 Set the context levels at which a particular role can be assigned. More...
 
 sort_by_roleassignment_authority ($users, context $context, $roles=array(), $sortpolicy='locality')
 Re-sort a users array based on a sorting policy. More...
 
 switch_roles ($first, $second)
 Switch the sort order of two roles (used in admin/roles/manage.php). More...
 
 unassign_capability ($capability, $roleid, $contextid=null)
 Unassign a capability from a role. More...
 
 context::update_moved (context $newparent)
 Update context info after moving context in the tree structure. More...
 
 user_can_assign (context $context, $targetroleid)
 Checks if a user can assign users to a particular role in this context. More...
 
 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. More...
 

Variables

int context::$_contextlevel
 The context level Can be accessed publicly through $context->contextlevel One of CONTEXT_* e.g. More...
 
int context::$_depth
 The depth of the context in relation to parent contexts Can be accessed publicly through $context->depth.
 
int context::$_id
 The context id Can be accessed publicly through $context->id.
 
int context::$_instanceid
 Id of the item this context is related to e.g. More...
 
int context::$_locked
 Whether this context is locked or not. More...
 
string context::$_path
 The path to the context always starting from the system context Can be accessed publicly through $context->path.
 
static int context::$cache_count = 0
 Context count Why do we do count contexts? Because count($array) is horribly slow for large arrays.
 
static array context::$cache_preloaded = array()
 Context caching info.
 
 $capabilities
 
static context_system context::$systemcontext = null
 The system context once initialised.
 
$ACCESSLIB_PRIVATE accessdatabyuser = array()
 
$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. More...
 
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.
 
string const context::DEFAULT_CAPABILITY_SORT = 'contextlevel, component, name'
 Default sorting of capabilities in {. More...
 
$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
 

Detailed Description

Function Documentation

◆ __construct() [1/6]

context::__construct ( stdClass  $record)
protected

Constructor is protected so that devs are forced to use context_xxx::instance() or context::instance_by_id().

Parameters
stdClass$record

Reimplemented in context_system, context_user, context_coursecat, context_course, and context_module.

◆ __construct() [2/6]

context_system::__construct ( stdClass  $record)
protected

Please use context_system::instance() if you need the instance of context.

Parameters
stdClass$record

Reimplemented from context.

◆ __construct() [3/6]

context_user::__construct ( stdClass  $record)
protected

Please use context_user::instance($userid) if you need the instance of context.

Alternatively if you know only the context id use context::instance_by_id($contextid)

Parameters
stdClass$record

Reimplemented from context.

◆ __construct() [4/6]

context_coursecat::__construct ( stdClass  $record)
protected

Please use context_coursecat::instance($coursecatid) if you need the instance of context.

Alternatively if you know only the context id use context::instance_by_id($contextid)

Parameters
stdClass$record

Reimplemented from context.

◆ __construct() [5/6]

context_course::__construct ( stdClass  $record)
protected

Please use context_course::instance($courseid) if you need the instance of context.

Alternatively if you know only the context id use context::instance_by_id($contextid)

Parameters
stdClass$record

Reimplemented from context.

◆ __construct() [6/6]

context_module::__construct ( stdClass  $record)
protected

Please use context_module::instance($cmid) if you need the instance of context.

Alternatively if you know only the context id use context::instance_by_id($contextid)

Parameters
stdClass$record

Reimplemented from context.

◆ __get()

context::__get (   $name)

Magic method getter, redirects to read only values.

Parameters
string$name
Return values
mixed

◆ __isset()

context::__isset (   $name)

Full support for isset on our magic read only properties.

Parameters
string$name
Return values
bool

◆ __set()

context::__set (   $name,
  $value 
)

Magic setter method, we do not want anybody to modify properties from the outside.

Parameters
string$name
mixed$value

◆ __unset()

context::__unset (   $name)

All properties are read only, sorry.

Parameters
string$name

◆ assign_capability()

assign_capability (   $capability,
  $permission,
  $roleid,
  $contextid,
  $overwrite = false 
)

Function to write context specific overrides, or default capabilities.

Parameters
string$capabilitystring name
int$permissionCAP_ constants
int$roleidrole id
int | context$contextidcontext id
bool$overwrite
Return values
boolalways true or exception

◆ assign_legacy_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.

Parameters
string$capability
array$legacypermsan array in the format (example): 'guest' => CAP_PREVENT, 'student' => CAP_ALLOW, 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, 'coursecreator' => CAP_ALLOW, 'manager' => CAP_ALLOW
Return values
booleansuccess or failure.

◆ build_all_paths()

static context_helper::build_all_paths (   $force = false)
static

Rebuild paths and depths in all context levels.

Parameters
bool$forcefalse means add missing only
Return values
void

◆ build_paths() [1/6]

static context::build_paths (   $force)
staticprotected

Rebuild context paths and depths at context level.

Parameters
bool$force
Return values
void

Reimplemented in context_system, context_user, context_coursecat, context_course, and context_module.

◆ build_paths() [2/6]

static context_system::build_paths (   $force)
staticprotected

Rebuild context paths and depths at system context level.

Parameters
bool$force

Reimplemented from context.

◆ build_paths() [3/6]

static context_user::build_paths (   $force)
staticprotected

Rebuild context paths and depths at user context level.

Parameters
bool$force

Reimplemented from context.

◆ build_paths() [4/6]

static context_coursecat::build_paths (   $force)
staticprotected

Rebuild context paths and depths at course category context level.

Parameters
bool$force

Reimplemented from context.

◆ build_paths() [5/6]

static context_course::build_paths (   $force)
staticprotected

Rebuild context paths and depths at course context level.

Parameters
bool$force

Reimplemented from context.

◆ build_paths() [6/6]

static context_module::build_paths (   $force)
staticprotected

Rebuild context paths and depths at module context level.

Parameters
bool$force

Reimplemented from context.

◆ cache_add()

static context::cache_add ( context  $context)
staticprotected

Adds a context to the cache.

If the cache is full, discards a batch of older entries.

Parameters
context$contextNew context to add
Return values
void

◆ cache_get()

static context::cache_get (   $contextlevel,
  $instance 
)
staticprotected

Gets a context from the cache.

Parameters
int$contextlevelContext level
int$instanceInstance ID
Return values
context|boolContext or false if not in cache

◆ cache_get_by_id()

static context::cache_get_by_id (   $id)
staticprotected

Gets a context from the cache based on its id.

Parameters
int$idContext ID
Return values
context|boolContext or false if not in cache

◆ cache_remove()

static context::cache_remove ( context  $context)
staticprotected

Removes a context from the cache.

Parameters
context$contextContext object to remove
Return values
void

◆ can_access_course()

can_access_course ( stdClass  $course,
  $user = null,
  $withcapability = '',
  $onlyactive = false 
)

Returns true if the user is able to access the course.

This function is in no way, shape, or form a substitute for require_login. It should only be used in circumstances where it is not possible to call require_login such as the navigation.

This function checks many of the methods of access to a course such as the view capability, enrollments, and guest access. It also makes use of the cache generated by require_login for guest access.

The flags within the $USER object that are used here should NEVER be used outside of this function can_access_course and require_login. Doing so WILL break future versions.

Parameters
stdClass$courserecord
stdClass | int | null$useruser record or id, current user if null
string$withcapabilityCheck for this capability as well.
bool$onlyactiveconsider only active enrolments in enabled plugins and time restrictions
Return values
booleanReturns true if the user is able to access the course

◆ cleanup_instances()

static context_helper::cleanup_instances ( )
static

Remove stale contexts that belonged to deleted instances.

Ideally all code should cleanup contexts properly, unfortunately accidents happen...

Return values
void

◆ component_level_changed()

component_level_changed (   $cap,
  $comp,
  $contextlevel 
)

Aids in detecting if a new line is required when reading a new capability.

This function helps admin/roles/manage.php etc to detect if a new line should be printed when we read in a new capability. Most of the time, if the 2 components are different we should print a new line, (e.g. course system->rss client) but when we are in grade, all reports/import/export capabilities should be together

Parameters
string$capcomponent string a
string$compcomponent string b
int$contextlevel
Return values
boolwhether 2 component are in different "sections"

◆ core_role_set_assign_allowed()

core_role_set_assign_allowed (   $fromroleid,
  $targetroleid 
)

Creates a record in the role_allow_assign table.

Parameters
int$fromroleidsource roleid
int$targetroleidtarget roleid
Return values
void

◆ core_role_set_override_allowed()

core_role_set_override_allowed (   $fromroleid,
  $targetroleid 
)

Creates a record in the role_allow_override table.

Parameters
int$fromroleidsource roleid
int$targetroleidtarget roleid
Return values
void

◆ core_role_set_switch_allowed()

core_role_set_switch_allowed (   $fromroleid,
  $targetroleid 
)

Creates a record in the role_allow_switch table.

Parameters
int$fromroleidsource roleid
int$targetroleidtarget roleid
Return values
void

◆ core_role_set_view_allowed()

core_role_set_view_allowed (   $fromroleid,
  $targetroleid 
)

Creates a record in the role_allow_view table.

Parameters
int$fromroleidsource roleid
int$targetroleidtarget roleid
Return values
void

◆ count_role_users()

count_role_users (   $roleid,
context  $context,
  $parent = false 
)

Counts all the users assigned this role in this context or higher.

Parameters
int | array$roleideither int or an array of ints
context$context
bool$parentif true, get list of users assigned in higher context too
Return values
intReturns the result count

◆ create_instance_from_record()

static context::create_instance_from_record ( stdClass  $record)
staticprotected

This function is also used to work around 'protected' keyword problems in context_helper.

Parameters
stdClass$record
Return values
contextinstance

◆ create_instances()

static context_helper::create_instances (   $contextlevel = null,
  $buildpaths = true 
)
static

Create all context instances at the given level and above.

Parameters
int$contextlevelnull means all levels
bool$buildpaths
Return values
void

◆ create_level_instances() [1/6]

static context::create_level_instances ( )
staticprotected

Create missing context instances at given level.

Return values
void

Reimplemented in context_system, context_user, context_coursecat, context_course, and context_module.

◆ create_level_instances() [2/6]

static context_system::create_level_instances ( )
staticprotected

Create missing context instances at system context.

Reimplemented from context.

◆ create_level_instances() [3/6]

static context_user::create_level_instances ( )
staticprotected

Create missing context instances at user context level.

Reimplemented from context.

◆ create_level_instances() [4/6]

static context_coursecat::create_level_instances ( )
staticprotected

Create missing context instances at course category context level.

Reimplemented from context.

◆ create_level_instances() [5/6]

static context_course::create_level_instances ( )
staticprotected

Create missing context instances at course context level.

Reimplemented from context.

◆ create_level_instances() [6/6]

static context_module::create_level_instances ( )
staticprotected

Create missing context instances at module context level.

Reimplemented from context.

◆ create_role()

create_role (   $name,
  $shortname,
  $description,
  $archetype = '' 
)

Function that creates a role.

Parameters
string$namerole name
string$shortnamerole short name
string$descriptionrole description
string$archetype
Return values
intid or dml_exception

◆ delete_instance()

static context_helper::delete_instance (   $contextlevel,
  $instanceid 
)
static

Delete context instance.

Parameters
int$contextlevel
int$instanceid
Return values
void

◆ delete_role()

delete_role (   $roleid)

Function that deletes a role and cleanups up after it.

Parameters
int$roleidid of role to delete
Return values
boolalways true

◆ fix_role_sortorder()

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.

Parameters
array$allrolesarray of roles, as returned by get_all_roles();
Return values
array::$role-,>sortorder=-> $role->id with the keys in ascending order.

◆ get_all_capabilities()

get_all_capabilities ( )

Returns all capabilitiy records, preferably from MUC and not database.

Return values
arrayAll capability records indexed by capability name

◆ get_all_levels()

static context_helper::get_all_levels ( )
static

Returns a list of all context levels.

Return values
arrayint=>string (level=>level class name)

◆ get_all_risks()

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.

The values are the corresponding RISK_ constants.

Return values
arrayall the known types of risk.

◆ get_all_roles()

get_all_roles ( context  $context = null)

Returns all site roles in correct sort order.

Note: this method does not localise role names or descriptions, use role_get_names() if you need role names.

Parameters
context$contextoptional context for course role name aliases
Return values
arrayof role records with optional coursealias property

◆ get_archetype_roles()

get_archetype_roles (   $archetype)

Returns roles of a specified archetype.

Parameters
string$archetype
Return values
arrayof full role records

◆ get_assignable_roles()

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.

Parameters
context$contextthe context.
int$rolenamedisplaythe type of role name to display. One of the ROLENAME_X constants. Default ROLENAME_ALIAS.
bool$withusercountsif true, count the number of users with each role.
integer | object$userA user id or object. By default (null) checks the permissions of the current user.
Return values
arrayif $withusercounts is false, then an array $roleid => $rolename. if $withusercounts is true, returns a list of three arrays, $rolenames, $rolecounts, and $nameswithcounts.

◆ get_capabilities() [1/7]

context::get_capabilities ( string  $sort = self::DEFAULT_CAPABILITY_SORT)
abstract

Returns array of relevant context capability records.

Parameters
string$sortSQL order by snippet for sorting returned capabilities sensibly for display
Return values
array

Reimplemented in context_helper, context_system, context_user, context_coursecat, context_course, and context_module.

◆ get_capabilities() [2/7]

context_helper::get_capabilities ( string  $sort = self::DEFAULT_CAPABILITY_SORT)

not used

Parameters
string$sort

Reimplemented from context.

◆ get_capabilities() [3/7]

context_system::get_capabilities ( string  $sort = self::DEFAULT_CAPABILITY_SORT)

Returns array of relevant context capability records.

Parameters
string$sort
Return values
array

Reimplemented from context.

◆ get_capabilities() [4/7]

context_user::get_capabilities ( string  $sort = self::DEFAULT_CAPABILITY_SORT)

Returns array of relevant context capability records.

Parameters
string$sort
Return values
array

Reimplemented from context.

◆ get_capabilities() [5/7]

context_coursecat::get_capabilities ( string  $sort = self::DEFAULT_CAPABILITY_SORT)

Returns array of relevant context capability records.

Parameters
string$sort
Return values
array

Reimplemented from context.

◆ get_capabilities() [6/7]

context_course::get_capabilities ( string  $sort = self::DEFAULT_CAPABILITY_SORT)

Returns array of relevant context capability records.

Parameters
string$sort
Return values
array

Reimplemented from context.

◆ get_capabilities() [7/7]

context_module::get_capabilities ( string  $sort = self::DEFAULT_CAPABILITY_SORT)

Returns array of relevant context capability records.

Parameters
string$sort
Return values
array

Reimplemented from context.

◆ get_capabilities_from_role_on_context()

get_capabilities_from_role_on_context (   $role,
context  $context 
)

Get all capabilities for this role on this context (overrides)

Parameters
stdClass$role
context$context
Return values
array

◆ get_capability_docs_link()

get_capability_docs_link (   $capability)

Return a link to moodle docs for a given capability name.

Parameters
stdClass$capabilitya capability - a row from the mdl_capabilities table.
Return values
stringthe human-readable capability name as a link to Moodle Docs.

◆ get_capability_info()

get_capability_info (   $capabilityname)

Returns capability information (cached)

Parameters
string$capabilityname
Return values
stdClassor null if capability not found

◆ get_capability_string()

get_capability_string (   $capabilityname)

Returns the human-readable, translated version of the capability.

Basically a big switch statement.

Parameters
string$capabilitynamee.g. mod/choice:readresponses
Return values
string

◆ get_child_contexts() [1/3]

context::get_child_contexts ( )

Recursive function which, given a context, find all its children context ids.

For course category contexts it will return immediate children and all subcategory contexts. It will NOT recurse into courses or subcategories categories. If you want to do that, call it on the returned courses/categories.

When called for a course context, it will return the modules and blocks displayed in the course page and blocks displayed on the module pages.

If called on a user/course/module context it will populate the cache with the appropriate contexts ;-)

Return values
arrayArray of child records

Reimplemented in context_system, and context_coursecat.

◆ get_child_contexts() [2/3]

context_system::get_child_contexts ( )

Returns all site contexts except the system context, DO NOT call on production servers!!

Contexts are not cached.

Return values
array

Reimplemented from context.

◆ get_child_contexts() [3/3]

context_coursecat::get_child_contexts ( )

Returns immediate child contexts of category and all subcategories, children of subcategories and courses are not returned.

Return values
array

Reimplemented from context.

◆ get_class_for_level()

static context_helper::get_class_for_level (   $contextlevel)
static

Returns a class name of the context level class.

Parameters
int$contextlevel(CONTEXT_SYSTEM, etc.)
Return values
stringclass name of the context class

◆ get_cleanup_sql() [1/6]

static context::get_cleanup_sql ( )
staticprotected

Returns sql necessary for purging of stale context instances.

Return values
stringcleanup SQL

Reimplemented in context_system, context_user, context_coursecat, context_course, and context_module.

◆ get_cleanup_sql() [2/6]

static context_system::get_cleanup_sql ( )
staticprotected

Returns sql necessary for purging of stale context instances.

Return values
stringcleanup SQL

Reimplemented from context.

◆ get_cleanup_sql() [3/6]

static context_user::get_cleanup_sql ( )
staticprotected

Returns sql necessary for purging of stale context instances.

Return values
stringcleanup SQL

Reimplemented from context.

◆ get_cleanup_sql() [4/6]

static context_coursecat::get_cleanup_sql ( )
staticprotected

Returns sql necessary for purging of stale context instances.

Return values
stringcleanup SQL

Reimplemented from context.

◆ get_cleanup_sql() [5/6]

static context_course::get_cleanup_sql ( )
staticprotected

Returns sql necessary for purging of stale context instances.

Return values
stringcleanup SQL

Reimplemented from context.

◆ get_cleanup_sql() [6/6]

static context_module::get_cleanup_sql ( )
staticprotected

Returns sql necessary for purging of stale context instances.

Return values
stringcleanup SQL

Reimplemented from context.

◆ get_component_string()

get_component_string (   $component,
  $contextlevel 
)

This gets the mod/block/course/core etc strings.

Parameters
string$component
int$contextlevel
Return values
string|boolString is success, false if failed

◆ get_context_info_array()

get_context_info_array (   $contextid)

Returns context instance plus related course and cm instances.

Parameters
int$contextid
Return values
arrayof ($context, $course, $cm)

◆ get_context_name() [1/6]

context::get_context_name (   $withprefix = true,
  $short = false,
  $escape = true 
)

Returns human readable context identifier.

Parameters
boolean$withprefixwhether to prefix the name of the context with the type of context, e.g. User, Course, Forum, etc.
boolean$shortwhether to use the short name of the thing. Only applies to course contexts
boolean$escapeWhether the returned name of the thing is to be HTML escaped or not.
Return values
stringthe human readable context name.

Reimplemented in context_system, context_user, context_coursecat, context_course, and context_module.

◆ get_context_name() [2/6]

context_system::get_context_name (   $withprefix = true,
  $short = false,
  $escape = true 
)

Returns human readable context identifier.

Parameters
boolean$withprefixdoes not apply to system context
boolean$shortdoes not apply to system context
boolean$escapedoes not apply to system context
Return values
stringthe human readable context name.

Reimplemented from context.

◆ get_context_name() [3/6]

context_user::get_context_name (   $withprefix = true,
  $short = false,
  $escape = true 
)

Returns human readable context identifier.

Parameters
boolean$withprefixwhether to prefix the name of the context with User
boolean$shortdoes not apply to user context
boolean$escapedoes not apply to user context
Return values
stringthe human readable context name.

Reimplemented from context.

◆ get_context_name() [4/6]

context_coursecat::get_context_name (   $withprefix = true,
  $short = false,
  $escape = true 
)

Returns human readable context identifier.

Parameters
boolean$withprefixwhether to prefix the name of the context with Category
boolean$shortdoes not apply to course categories
boolean$escapeWhether the returned name of the context is to be HTML escaped or not.
Return values
stringthe human readable context name.

Reimplemented from context.

◆ get_context_name() [5/6]

context_course::get_context_name (   $withprefix = true,
  $short = false,
  $escape = true 
)

Returns human readable context identifier.

Parameters
boolean$withprefixwhether to prefix the name of the context with Course
boolean$shortwhether to use the short name of the thing.
bool$escapeWhether the returned category name is to be HTML escaped or not.
Return values
stringthe human readable context name.

Reimplemented from context.

◆ get_context_name() [6/6]

context_module::get_context_name (   $withprefix = true,
  $short = false,
  $escape = true 
)

Returns human readable context identifier.

Parameters
boolean$withprefixwhether to prefix the name of the context with the module name, e.g. Forum, Glossary, etc.
boolean$shortdoes not apply to module context
boolean$escapeWhether the returned name of the context is to be HTML escaped or not.
Return values
stringthe human readable context name.

Reimplemented from context.

◆ get_course_context() [1/3]

context::get_course_context (   $strict = true)

Is this context part of any course? If yes return course context.

Parameters
bool$stricttrue means throw exception if not found, false means return false if not found
Return values
context_coursecontext of the enclosing course, null if not found or exception

Reimplemented in context_course, and context_module.

◆ get_course_context() [2/3]

context_course::get_course_context (   $strict = true)

Is this context part of any course? If yes return course context.

Parameters
bool$stricttrue means throw exception if not found, false means return false if not found
Return values
context_coursecontext of the enclosing course, null if not found or exception

Reimplemented from context.

◆ get_course_context() [3/3]

context_module::get_course_context (   $strict = true)

Is this context part of any course? If yes return course context.

Parameters
bool$stricttrue means throw exception if not found, false means return false if not found
Return values
context_coursecontext of the enclosing course, null if not found or exception

Reimplemented from context.

◆ get_default_capabilities()

get_default_capabilities (   $archetype)

Returns default capabilities for given role archetype.

Parameters
string$archetyperole archetype
Return values
array

◆ get_default_contextlevels()

get_default_contextlevels (   $rolearchetype)

Returns default context levels where roles can be assigned.

Parameters
string$rolearchetypeone of the role archetypes - that is, one of the keys from the array returned by get_role_archetypes();
Return values
arraylist of the context levels at which this type of role may be assigned by default.

◆ get_default_role_archetype_allows()

get_default_role_archetype_allows (   $type,
  $archetype 
)

Return default roles that can be assigned, overridden or switched by give role archetype.

Parameters
string$typeassign|override|switch|view
string$archetype
Return values
arrayof role ids

◆ get_guest_role()

get_guest_role ( )

Get the default guest role, this is used for guest account, search engine spiders, etc.

Return values
stdClassrole record

◆ get_level_name() [1/6]

static context_helper::get_level_name (   $contextlevel)
static

Returns the name of specified context level.

Parameters
int$contextlevel
Return values
stringname of the context level

◆ get_level_name() [2/6]

static context_system::get_level_name ( )
static

Returns human readable context level name.

Return values
stringthe human readable context level name.

◆ get_level_name() [3/6]

static context_user::get_level_name ( )
static

Returns human readable context level name.

Return values
stringthe human readable context level name.

◆ get_level_name() [4/6]

static context_coursecat::get_level_name ( )
static

Returns human readable context level name.

Return values
stringthe human readable context level name.

◆ get_level_name() [5/6]

static context_course::get_level_name ( )
static

Returns human readable context level name.

Return values
stringthe human readable context level name.

◆ get_level_name() [6/6]

static context_module::get_level_name ( )
static

Returns human readable context level name.

Return values
stringthe human readable context level name.

◆ get_local_override()

get_local_override (   $roleid,
  $contextid,
  $capability 
)

Get the local override (if any) for a given capability in a role in a context.

Parameters
int$roleid
int$contextid
string$capability
Return values
stdClasslocal capability override

◆ get_navigation_filter_context()

static context_helper::get_navigation_filter_context ( ?context  $context)
static

Gets the current context to be used for navigation tree filtering.

Parameters
context | null$contextThe current context to be checked against.
Return values
context|nullthe context that navigation tree filtering should use.

◆ get_overridable_roles()

get_overridable_roles ( context  $context,
  $rolenamedisplay = ROLENAME_ALIAS,
  $withcounts = false 
)

Gets a list of roles that this user can override in this context.

Parameters
context$contextthe context.
int$rolenamedisplaythe type of role name to display. One of the ROLENAME_X constants. Default ROLENAME_ALIAS.
bool$withcountsif true, count the number of overrides that are set for each role.
Return values
arrayif $withcounts is false, then an array $roleid => $rolename. if $withusercounts is true, returns a list of three arrays, $rolenames, $rolecounts, and $nameswithcounts.

◆ get_parent_context()

context::get_parent_context ( )

Returns parent context.

Return values
context

◆ get_parent_context_ids()

context::get_parent_context_ids (   $includeself = false)

Returns parent context ids of this context in reversed order, i.e.

parent first, then grand parent, etc.

Parameters
bool$includeselftrue means include self too
Return values
arrayof context ids

◆ get_parent_context_paths()

context::get_parent_context_paths (   $includeself = false)

Returns parent context paths of this context.

Parameters
bool$includeselftrue means include self too
Return values
arrayof context paths

◆ get_parent_contexts()

context::get_parent_contexts (   $includeself = false)

Returns parent contexts of this context in reversed order, i.e.

parent first, then grand parent, etc.

Parameters
bool$includeselftrue means include self too
Return values
arrayof context instances

◆ get_preload_record_columns()

static context_helper::get_preload_record_columns (   $tablealias)
static

Returns all fields necessary for context preloading from user $rec.

This helps with performance when dealing with hundreds of contexts.

Parameters
string$tablealiascontext table alias in the query
Return values
array(table.column=>alias, ...)

◆ get_preload_record_columns_sql()

static context_helper::get_preload_record_columns_sql (   $tablealias)
static

Returns all fields necessary for context preloading from user $rec.

This helps with performance when dealing with hundreds of contexts.

Parameters
string$tablealiascontext table alias in the query
Return values
string

◆ get_profile_roles()

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.

Parameters
context$context
Return values
array

◆ get_role_archetypes()

get_role_archetypes ( )

Returns array of all role archetypes.

Return values
array

◆ get_role_contextlevels()

get_role_contextlevels (   $roleid)

Return context levels where this role is assignable.

Parameters
integer$roleidthe id of a role.
Return values
arraylist of the context levels at which this role may be assigned.

◆ get_role_definitions()

get_role_definitions ( array  $roleids)

Fetch raw "site wide" role definitions.

Even MUC static acceleration cache appears a bit slow for this. Important as can be hit hundreds of times per page.

Parameters
array$roleidsList of role ids to fetch definitions for.
Return values
arrayComplete definition for each requested role.

◆ get_role_definitions_uncached()

get_role_definitions_uncached ( array  $roleids)

Query raw "site wide" role definitions.

Parameters
array$roleidsList of role ids to fetch definitions for.
Return values
arrayComplete definition for each requested role.

◆ get_role_names_with_caps_in_context()

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().

Returns $allowed minus $forbidden

Parameters
stdClass$context
array$capabilitiesAn array of capabilities
Return values
arrayof roles with all of the required capabilities

◆ get_role_users()

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.

Note that moodle is based on capabilities and it is usually better to check permissions than to check role ids as the capabilities system is more flexible. If you really need, you can to use this function but consider has_capability() as a possible substitute.

All $sort fields are added into $fields if not present there yet.

If $roleid is an array or is empty (all roles) you need to set $fields (and $sort by extension) params according to it, as the first field returned by the database should be unique (ra.id is the best candidate).

Parameters
int$roleid(can also be an array of ints!)
context$context
bool$parentif true, get list of users assigned in higher context too
string$fieldsfields from user (u.) , role assignment (ra) or role (r.)
string$sortsort from user (u.) , role assignment (ra.) or role (r.). null => use default sort from users_order_by_sql.
bool$alltrue means all, false means limit to enrolled users
string$groupdefaults to ''
mixed$limitfromdefaults to ''
mixed$limitnumdefaults to ''
string$extrawheretestdefaults to ''
array$whereorsortparamsany paramter values used by $sort or $extrawheretest.
Return values
array

◆ get_roles_for_contextlevels()

get_roles_for_contextlevels (   $contextlevel)

Return roles suitable for assignment at the specified context level.

NOTE: this function name looks like a typo, should be probably get_roles_for_contextlevel()

Parameters
integer$contextlevela contextlevel.
Return values
arraylist of role ids that are assignable at this context level.

◆ get_roles_used_in_context()

get_roles_used_in_context ( context  $context,
  $includeparents = true 
)

Gets the list of roles assigned to this context and up (parents)

Parameters
context$context
boolean$includeparents,falsemeans without parents.
Return values
array

◆ get_roles_with_cap_in_context()

get_roles_with_cap_in_context (   $context,
  $capability 
)

Returns two lists, this can be used to find out if user has capability.

Having any needed role and no forbidden role in this context means user has this capability in this context. Use get_role_names_with_cap_in_context() if you need role names to display in the UI

Parameters
stdClass$context
string$capability
Return values
array($neededroles,$forbiddenroles)

◆ get_roles_with_capability()

get_roles_with_capability (   $capability,
  $permission = null,
  $context = null 
)

Get the roles that have a given capability assigned to it.

This function does not resolve the actual permission of the capability. It just checks for permissions and overrides. Use get_roles_with_cap_in_context() if resolution is required.

Parameters
string$capabilitycapability name (string)
string$permissionoptional, the permission defined for this capability either CAP_ALLOW, CAP_PREVENT or CAP_PROHIBIT. Defaults to null which means any.
stdClass$contextnull means any
Return values
arrayof role records

◆ get_roles_with_caps_in_context()

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().

Returns $allowed minus $forbidden

Parameters
stdClass$context
array$capabilitiesAn array of capabilities
Return values
arrayof roles with all of the required capabilities

◆ get_roles_with_override_on_context()

get_roles_with_override_on_context ( context  $context)

Get any role that has an override on exact context.

Parameters
context$contextThe context to check
Return values
arrayAn array of roles

◆ get_switchable_roles()

get_switchable_roles ( context  $context,
  $rolenamedisplay = ROLENAME_ALIAS 
)

Gets a list of roles that this user can switch to in a context.

Gets a list of roles that this user can switch to in a context, for the switchrole menu. This function just process the contents of the role_allow_switch table. You also need to test the moodle/role:switchroles to see if the user is allowed to switch in the first place.

Parameters
context$contexta context.
int$rolenamedisplaythe type of role name to display. One of the ROLENAME_X constants. Default ROLENAME_ALIAS.
Return values
arrayan array $roleid => $rolename.

◆ get_url() [1/7]

context::get_url ( )
abstract

Returns the most relevant URL for this context.

Return values
moodle_url

Reimplemented in context_helper, context_system, context_user, context_coursecat, context_course, and context_module.

◆ get_url() [2/7]

context_helper::get_url ( )

not used

Reimplemented from context.

◆ get_url() [3/7]

context_system::get_url ( )

Returns the most relevant URL for this context.

Return values
moodle_url

Reimplemented from context.

◆ get_url() [4/7]

context_user::get_url ( )

Returns the most relevant URL for this context.

Return values
moodle_url

Reimplemented from context.

◆ get_url() [5/7]

context_coursecat::get_url ( )

Returns the most relevant URL for this context.

Return values
moodle_url

Reimplemented from context.

◆ get_url() [6/7]

context_course::get_url ( )

Returns the most relevant URL for this context.

Return values
moodle_url

Reimplemented from context.

◆ get_url() [7/7]

context_module::get_url ( )

Returns the most relevant URL for this context.

Return values
moodle_url

Reimplemented from context.

◆ get_user_capability_contexts()

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.

It is now reasonably efficient, but bear in mind that if there are users who have the capability everywhere, it may return an array of all contexts.

Parameters
string$capabilityCapability in question
int$useridUser ID or null for current user
bool$getcategoriesWether to return also course_categories
bool$doanythingTrue if 'doanything' is permitted (default)
string$coursefieldsexceptidLeave blank if you only need 'id' in the course records; otherwise use a comma-separated list of the fields you require, not including id. Add ctxid, ctxpath, ctxdepth etc to return course context information for preloading.
string$categoryfieldsexceptidLeave blank if you only need 'id' in the course records; otherwise use a comma-separated list of the fields you require, not including id. Add ctxid, ctxpath, ctxdepth etc to return course context information for preloading.
string$courseorderbyIf set, use a comma-separated list of fields from course table with sql modifiers (DESC) if needed
string$categoryorderbyIf set, use a comma-separated list of fields from course_category table with sql modifiers (DESC) if needed
int$limitLimit the number of courses to return on success. Zero equals all entries.
Return values
arrayArray of categories and courses.

◆ get_user_capability_course()

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.

It is now reasonably efficient, but bear in mind that if there are users who have the capability everywhere, it may return an array of all courses.

Parameters
string$capabilityCapability in question
int$useridUser ID or null for current user
bool$doanythingTrue if 'doanything' is permitted (default)
string$fieldsexceptidLeave blank if you only need 'id' in the course records; otherwise use a comma-separated list of the fields you require, not including id. Add ctxid, ctxpath, ctxdepth etc to return course context information for preloading.
string$orderbyIf set, use a comma-separated list of fields from course table with sql modifiers (DESC) if needed
int$limitLimit the number of courses to return on success. Zero equals all entries.
Return values
array|boolArray of courses, if none found false is returned.

◆ get_user_roles()

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.

we need to know what this user holds, in order to verify against allow_assign and allow_override tables

Parameters
context$context
int$userid
bool$checkparentcontextsdefaults to true
string$orderdefaults to 'c.contextlevel DESC, r.sortorder ASC'
Return values
array

◆ get_user_roles_in_course()

get_user_roles_in_course (   $userid,
  $courseid 
)

This function is used to print roles column in user profile page.

It is using the CFG->profileroles to limit the list to only interesting roles. (The permission tab has full details of user role assignments.)

Parameters
int$userid
int$courseid
Return values
string

◆ get_user_roles_with_special()

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.

Parameters
context$contextthe context.
int$useridoptional. Defaults to $USER->id
Return values
arrayof objects with fields ->userid, ->contextid and ->roleid.

◆ get_users_by_capability()

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?

This can be a very expensive call - use sparingly and keep the results if you are going to need them again soon.

Note if $fields is empty this function attempts to get u.* which can get rather large - and has a serious perf impact on some DBs.

Parameters
context$context
string | array$capability- capability name(s)
string$fields- fields to be pulled. The user table is aliased to 'u'. u.id MUST be included.
string$sort- the sort order. Default is lastaccess time.
mixed$limitfrom- number of records to skip (offset)
mixed$limitnum- number of records to fetch
string | array$groups- single group or array of groups - only return users who are in one of these group(s).
string | array$exceptions- list of users to exclude, comma separated or array
bool$notuseddoanythingnot used any more, admin accounts are never returned
bool$notusedview- use get_enrolled_sql() instead
bool$useviewallgroupsif $groups is set the return users who have capability both $capability and moodle/site:accessallgroups in this context, as well as users who have $capability and who are in $groups.
Return values
arrayof user records

◆ get_users_from_role_on_context()

get_users_from_role_on_context (   $role,
context  $context 
)

Find all user assignment of users for this role, on this context.

Parameters
stdClass$role
context$context
Return values
array

◆ get_users_roles()

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.

If you try using the combination $userids = [], $checkparentcontexts = true then this is likely to cause an out-of-memory error on large Moodle sites, so this combination is deprecated and outputs a warning, even though it is the default.

Parameters
context$context
array$userids,.An empty list means fetch all role assignments for the context.
bool$checkparentcontextsdefaults to true
string$orderdefaults to 'c.contextlevel DESC, r.sortorder ASC'
Return values
array

◆ get_viewable_roles()

get_viewable_roles ( context  $context,
  $userid = null,
  $rolenamedisplay = ROLENAME_ALIAS 
)

Gets a list of roles that this user can view in a context.

Parameters
context$contexta context.
int$useridid of user.
int$rolenamedisplaythe type of role name to display. One of the ROLENAME_X constants. Default ROLENAME_ALIAS.
Return values
arrayan array $roleid => $rolename.

◆ get_with_capability_join()

get_with_capability_join ( context  $context,
  $capability,
  $useridcolumn 
)

Gets sql joins for finding users with capability in the given context.

Parameters
context$contextContext for the join.
string | array$capabilityCapability name or array of names. If an array is provided then this is the equivalent of a logical 'OR', i.e. the user needs to have one of these capabilities.
string$useridcolumne.g. 'u.id'.
Return values
core::dml::sql_joinContains joins, wheres, params. This function will set ->cannotmatchanyrows if applicable. This may let you skip doing a DB query.

◆ getIterator()

context::getIterator ( )

Create an iterator because magic vars can't be seen by 'foreach'.

Now we can convert context object to array using convert_to_array(), and feed it properly to json_encode().

◆ has_coursecontact_role()

has_coursecontact_role (   $userid)

Returns true if user has at least one role assign of 'coursecontact' role (is potentially listed in some course descriptions).

Parameters
int$userid
Return values
bool

◆ insert_context_record()

static context::insert_context_record (   $contextlevel,
  $instanceid,
  $parentpath 
)
staticprotected

Utility method for context creation.

Parameters
int$contextlevel
int$instanceid
string$parentpath
Return values
stdClasscontext record

◆ instance() [1/5]

static context_coursecat::instance (   $categoryid,
  $strictness = MUST_EXIST 
)
static

Returns course category context instance.

Parameters
int$categoryidid from {course_categories} table
int$strictness
Return values
context_coursecatcontext instance

◆ instance() [2/5]

static context_module::instance (   $cmid,
  $strictness = MUST_EXIST 
)
static

Returns module context instance.

Parameters
int$cmidid of the record from {course_modules} table; pass cmid there, NOT id in the instance column
int$strictness
Return values
context_modulecontext instance

◆ instance() [3/5]

static context_course::instance (   $courseid,
  $strictness = MUST_EXIST 
)
static

Returns course context instance.

Parameters
int$courseidid from {course} table
int$strictness
Return values
context_coursecontext instance

◆ instance() [4/5]

static context_system::instance (   $instanceid = 0,
  $strictness = MUST_EXIST,
  $cache = true 
)
static

Returns system context instance.

Parameters
int$instanceidshould be 0
int$strictness
bool$cache
Return values
context_systemcontext instance

◆ instance() [5/5]

static context_user::instance (   $userid,
  $strictness = MUST_EXIST 
)
static

Returns user context instance.

Parameters
int$useridid from {user} table
int$strictness
Return values
context_usercontext instance

◆ instance_by_id()

static context::instance_by_id (   $id,
  $strictness = MUST_EXIST 
)
static

Get a context instance as an object, from a given context id.

Parameters
int$idcontext id
int$strictnessIGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; MUST_EXIST means throw exception if no record found
Return values
context|boolthe context object or false if not found

◆ is_child_of()

context::is_child_of ( context  $possibleparent,
bool  $includeself 
)

Determine if the current context is a child of the possible parent.

Parameters
context$possibleparent
bool$includeselfWhether to check the current context
Return values
bool

◆ is_inside_frontpage()

is_inside_frontpage ( context  $context)

Check if context is the front page context or a context inside it.

Returns true if this context is the front page context, or a context inside it, otherwise false.

Parameters
context$contexta context object.
Return values
bool

◆ is_locked()

context::is_locked ( )

Whether the current context is locked.

Return values
bool

◆ is_parent_of()

context::is_parent_of ( context  $possiblechild,
bool  $includeself 
)

Determine if the current context is a parent of the possible child.

Parameters
context$possiblechild
bool$includeselfWhether to check the current context
Return values
bool

◆ is_role_switched()

is_role_switched (   $courseid)

Checks if the user has switched roles within the given course.

Note: You can only switch roles within the course, hence it takes a course id rather than a context. On that note Petr volunteered to implement this across all other contexts, all requests for this should be forwarded to him ;)

Parameters
int$courseidThe id of the course to check
Return values
boolTrue if the user has switched roles within the course.

◆ is_safe_capability()

is_safe_capability (   $capability)

Verify capability risks.

Parameters
stdClass$capabilitya capability - a row from the capabilities table.
Return values
booleanwhether this capability is safe - that is, whether people with the safeoverrides capability should be allowed to change it.

◆ load_temp_course_role()

load_temp_course_role ( context_course  $coursecontext,
  $roleid 
)

Adds a temp role to current USER->access array.

Useful for the "temporary guest" access we grant to logged-in users. This is useful for enrol plugins only.

Since
Moodle 2.2
Parameters
context_course$coursecontext
int$roleid
Return values
void

◆ mark_user_dirty()

mark_user_dirty (   $userid)

Mark a user as dirty (with timestamp) so as to force reloading of the user session.

Parameters
int$userid
Return values
void

◆ preload_contexts_by_id()

static context_helper::preload_contexts_by_id ( array  $contextids)
static

Preload a set of contexts using their contextid.

Parameters
array$contextids

◆ preload_course()

static context_helper::preload_course (   $courseid)
static

Preload all contexts instances from course.

To be used if you expect multiple queries for course activities...

Parameters
int$courseid

◆ preload_from_record() [1/2]

static context::preload_from_record ( stdClass  $rec)
staticprotected

Preloads context information from db record and strips the cached info.

Parameters
stdClass$rec
Return values
void(modifies $rec)

Reimplemented in context_helper.

◆ preload_from_record() [2/2]

static context_helper::preload_from_record ( stdClass  $rec)
static

Preloads context information from db record and strips the cached info.

The db request has to contain all columns from context_helper::get_preload_record_columns().

Parameters
stdClass$rec
Return values
void(modifies $rec)

Reimplemented from context.

◆ prohibit_is_removable()

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.

Parameters
int$roleid
context$context
string$capabilityname
Return values
bool

◆ reload_if_dirty()

context::reload_if_dirty ( )

Reset all cached permissions and definitions if the necessary.

Return values
void

◆ remove_temp_course_roles()

remove_temp_course_roles ( context_course  $coursecontext)

Removes any extra guest roles from current USER->access array.

This is useful for enrol plugins only.

Since
Moodle 2.2
Parameters
context_course$coursecontext
Return values
void

◆ require_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.

NOTE before Moodle 2.0, this function attempted to make an appropriate require_login call before checking the capability. This is no longer the case. You must call require_login (or one of its variants) if you want to check the user is logged in, before you call this function.

See also
has_capability()
Parameters
string$capabilitythe name of the capability to check. For example mod/forum:view
context$contextthe context to check the capability in. You normally get this with context_xxxx::instance().
int$useridA user id. By default (null) checks the permissions of the current user.
bool$doanythingIf false, ignore effect of admin role assignment
string$errormessageThe error string to to user. Defaults to 'nopermissions'.
string$stringfileThe language file to load the error string from. Defaults to 'error'.
Return values
voidterminates with an error if the user does not have the given capability.

◆ reset_caches() [1/2]

static context::reset_caches ( )
staticprotected

Resets the cache to remove all data.

Reimplemented in context_helper.

◆ reset_caches() [2/2]

static context_helper::reset_caches ( )
static

Resets the cache to remove all data.

Reimplemented from context.

◆ reset_paths()

context::reset_paths (   $rebuild = true)

Remove all context path info and optionally rebuild it.

Parameters
bool$rebuild
Return values
void

◆ reset_role_capabilities()

reset_role_capabilities (   $roleid)

Reset role capabilities to default according to selected role archetype.

If no archetype selected, removes all capabilities.

This applies to capabilities that are assigned to the role (that you could edit in the 'define roles' interface), and not to any capability overrides in different locations.

Parameters
int$roleidID of role to reset capabilities for

◆ role_assign()

role_assign (   $roleid,
  $userid,
  $contextid,
  $component = '',
  $itemid = 0,
  $timemodified = '' 
)

This function makes a role-assignment (a role for a user in a particular context)

Parameters
int$roleidthe role of the id
int$useriduserid
int | context$contextidid of the context
string$componentexample 'enrol_ldap', defaults to '' which means manual assignment,
int$itemidid of enrolment/auth plugin
string$timemodifieddefaults to current time
Return values
intnew/existing id of the assignment

◆ role_cap_duplicate()

role_cap_duplicate (   $sourcerole,
  $targetrole 
)

Duplicates all the base definitions of a role.

Parameters
stdClass$sourcerolerole to copy from
int$targetroleid of role to copy to

◆ role_change_permission()

role_change_permission (   $roleid,
  $context,
  $capname,
  $permission 
)

More user friendly role permission changing, it should produce as few overrides as possible.

Parameters
int$roleid
stdClass$context
string$capnamecapability name
int$permission
Return values
void

◆ role_context_capabilities()

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.

Parameters
int$roleid
context$context
string$capcapability, optional, defaults to ''
Return values
arrayArray of capabilities

◆ role_fix_names()

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.

Parameters
array$roleoptionsarray roleid => roleobject (with optional coursealias), strings are accepted for backwards compatibility only
context$contextthe context, null means system context
int$rolenamedisplay
bool$returnmenunull means keep the same format as $roleoptions, true means id=>localname, false means id=>rolerecord
Return values
arrayArray of context-specific role names, or role objects with a ->localname field added.

◆ role_get_description()

role_get_description ( stdClass  $role)

Returns localised role description if available.

If the name is empty it tries to find the default role name using hardcoded list of default role names or other methods in the future.

Parameters
stdClass$role
Return values
stringlocalised role name

◆ role_get_name()

role_get_name ( stdClass  $role,
  $context = null,
  $rolenamedisplay = ROLENAME_ALIAS 
)

Get localised role name or alias if exists and format the text.

Parameters
stdClass$rolerole object
  • optional 'coursealias' property should be included for performance reasons if course context used
  • description property is not required here
context | bool$contextempty means system context
int$rolenamedisplaytype of role name
Return values
stringlocalised role name or course role name alias

◆ role_get_names()

role_get_names ( context  $context = null,
  $rolenamedisplay = ROLENAME_ALIAS,
  $returnmenu = null 
)

Get all the localised role names for a context.

In new installs default roles have empty names, this function add localised role names using current language pack.

Parameters
context$contextthe context, null means system context
arrayof role objects with a ->localname field containing the context-specific role name.
int$rolenamedisplay
bool$returnmenutrue means id=>localname, false means id=>rolerecord
Return values
arrayArray of context-specific role names, or role objects with a ->localname field added.

◆ role_switch()

role_switch (   $roleid,
context  $context 
)

Switches the current user to another role for the current session and only in the given context.

The caller must check

  • that this op is allowed
  • that the requested role can be switched to in this context (use get_switchable_roles)
  • that the requested role is NOT $CFG->defaultuserroleid

To "unswitch" pass 0 as the roleid.

This function will modify $USER->access - beware

Parameters
integer$roleidthe role to switch to.
context$contextthe context in which to perform the switch.
Return values
boolsuccess or failure.

◆ role_unassign()

role_unassign (   $roleid,
  $userid,
  $contextid,
  $component = '',
  $itemid = 0 
)

Removes one role assignment.

Parameters
int$roleid
int$userid
int$contextid
string$component
int$itemid
Return values
void

◆ role_unassign_all()

role_unassign_all ( array  $params,
  $subcontexts = false,
  $includemanual = false 
)

Removes multiple role assignments, parameters may contain: 'roleid', 'userid', 'contextid', 'component', 'enrolid'.

Parameters
array$paramsrole assignment parameters
bool$subcontextsunassign in subcontexts too
bool$includemanualinclude manual role assignments too
Return values
void

◆ set_locked() [1/2]

context::set_locked ( bool  $locked)

Set whether this context has been locked or not.

Parameters
bool$locked
Return values
$this

Reimplemented in context_system.

◆ set_locked() [2/2]

context_system::set_locked ( bool  $locked)

Set whether this context has been locked or not.

Parameters
bool$locked
Return values
$this

Reimplemented from context.

◆ set_role_contextlevels()

set_role_contextlevels (   $roleid,
array  $contextlevels 
)

Set the context levels at which a particular role can be assigned.

Throws exceptions in case of error.

Parameters
integer$roleidthe id of a role.
array$contextlevelsthe context levels at which this role should be assignable, duplicate levels are removed.
Return values
void

◆ sort_by_roleassignment_authority()

sort_by_roleassignment_authority (   $users,
context  $context,
  $roles = array(),
  $sortpolicy = 'locality' 
)

Re-sort a users array based on a sorting policy.

Will re-sort a $users results array (from get_users_by_capability(), usually) based on a sorting policy. This is to support the odd practice of sorting teachers by 'authority', where authority was "lowest id of the role assignment".

Will execute 1 database query. Only suitable for small numbers of users, as it uses an u.id IN() clause.

Notes about the sorting criteria.

As a default, we cannot rely on role.sortorder because then admins/coursecreators will always win. That is why the sane rule "is locality matters most", with sortorder as 2nd consideration.

If you want role.sortorder, use the 'sortorder' policy, and name explicitly what roles you want to cover. It's probably a good idea to see what roles have the capabilities you want (array_diff() them against roiles that have 'can-do-anything' to weed out admin-ish roles. Or fetch a list of roles from variables like $CFG->coursecontact .

Parameters
array$usersUsers array, keyed on userid
context$context
array$rolesids of the roles to include, optional
string$sortpolicydefaults to locality, more about
Return values
arraysorted copy of the array

◆ switch_roles()

switch_roles (   $first,
  $second 
)

Switch the sort order of two roles (used in admin/roles/manage.php).

Parameters
stdClass$firstThe first role. Actually, only ->sortorder is used.
stdClass$secondThe second role. Actually, only ->sortorder is used.
Return values
booleansuccess or failure

◆ unassign_capability()

unassign_capability (   $capability,
  $roleid,
  $contextid = null 
)

Unassign a capability from a role.

Parameters
string$capabilitythe name of the capability
int$roleidthe role id
int | context$contextidnull means all contexts
Return values
booleantrue or exception

◆ update_moved()

context::update_moved ( context  $newparent)

Update context info after moving context in the tree structure.

Parameters
context$newparent
Return values
void

◆ user_can_assign()

user_can_assign ( context  $context,
  $targetroleid 
)

Checks if a user can assign users to a particular role in this context.

Parameters
context$context
int$targetroleid- the id of the role you want to assign users to
Return values
boolean

◆ user_has_role_assignment()

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.

Parameters
int$userid
int$roleid
int$contextidempty means any context
Return values
bool

Variable Documentation

◆ $_contextlevel

int context::$_contextlevel
protected

The context level Can be accessed publicly through $context->contextlevel One of CONTEXT_* e.g.

CONTEXT_COURSE, CONTEXT_MODULE

◆ $_instanceid

int context::$_instanceid
protected

Id of the item this context is related to e.g.

COURSE_CONTEXT => course.id Can be accessed publicly through $context->instanceid

◆ $_locked

int context::$_locked
protected

Whether this context is locked or not.

Can be accessed publicly through $context->locked.

◆ CONTEXT_BLOCK

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.

Any other context level instance can be parent of block context.

◆ DEFAULT_CAPABILITY_SORT

string const context::DEFAULT_CAPABILITY_SORT = 'contextlevel, component, name'
protected

Default sorting of capabilities in {.

See also
get_capabilities}