Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/accesslib.php File Reference

Go to the source code of this file.

Data Structures

class  context
class  context_helper
class  context_system
class  context_user
class  context_coursecat
class  context_course
class  context_module
class  context_block

Namespaces

namespace  core

Enumerations

enum  CAP_INHERIT
enum  CAP_ALLOW
enum  CAP_PREVENT
enum  CAP_PROHIBIT
enum  CONTEXT_SYSTEM
enum  CONTEXT_USER
enum  CONTEXT_COURSECAT
enum  CONTEXT_COURSE
enum  CONTEXT_MODULE
enum  CONTEXT_BLOCK
enum  RISK_MANAGETRUST
enum  RISK_CONFIG
enum  RISK_XSS
enum  RISK_PERSONAL
enum  RISK_SPAM
enum  RISK_DATALOSS
enum  ROLENAME_ORIGINAL
enum  ROLENAME_ALIAS
enum  ROLENAME_BOTH
enum  ROLENAME_ORIGINALANDSHORT
enum  ROLENAME_ALIAS_RAW
enum  ROLENAME_SHORT

$ACCESSLIB_PRIVATE

maximum size of context cache - it is possible to tweak this config.php or in any script before inclusion of context.php Although this looks like a global variable, it isn't really.

It is just a private implementation detail to accesslib that MUST NOT be used elsewhere. It is used to cache various bits of data between function calls for performance reasons. Sadly, a PHP global variable is the only way to implement this, without rewriting everything as methods of a class, instead of functions.

stdClass $ACCESSLIB_PRIVATE

global $ACCESSLIB_PRIVATE = new stdClass()
$ACCESSLIB_PRIVATE dirtycontexts = null
$ACCESSLIB_PRIVATE accessdatabyuser = array()
$ACCESSLIB_PRIVATE rolepermissions = array()
$ACCESSLIB_PRIVATE capabilities = null
 accesslib_clear_all_caches_for_unit_testing ()
 get_guest_role ()
 has_capability ($capability, context $context, $user=null, $doanything=true)
 has_any_capability (array $capabilities, context $context, $userid=null, $doanything=true)
 has_all_capabilities (array $capabilities, context $context, $userid=null, $doanything=true)
 is_siteadmin ($user_or_id=null)
 has_coursecontact_role ($userid)
 require_capability ($capability, context $context, $userid=null, $doanything=true, $errormessage= 'nopermissions', $stringfile= '')
 load_temp_course_role (context_course $coursecontext, $roleid)
 remove_temp_course_roles (context_course $coursecontext)
 get_role_archetypes ()
 assign_legacy_capabilities ($capability, $legacyperms)
 is_safe_capability ($capability)
 get_local_override ($roleid, $contextid, $capability)
 get_context_info_array ($contextid)
 create_role ($name, $shortname, $description, $archetype= '')
 delete_role ($roleid)
 assign_capability ($capability, $permission, $roleid, $contextid, $overwrite=false)
 unassign_capability ($capability, $roleid, $contextid=null)
 get_roles_with_capability ($capability, $permission=null, $context=null)
 role_assign ($roleid, $userid, $contextid, $component= '', $itemid=0, $timemodified= '')
 role_unassign ($roleid, $userid, $contextid, $component= '', $itemid=0)
 role_unassign_all (array $params, $subcontexts=false, $includemanual=false)
 isloggedin ()
 isguestuser ($user=null)
 is_guest (context $context, $user=null)
 is_viewing (context $context, $user=null, $withcapability= '')
 is_enrolled (context $context, $user=null, $withcapability= '', $onlyactive=false)
 can_access_course (stdClass $course, $user=null, $withcapability= '', $onlyactive=false)
 get_enrolled_sql (context $context, $withcapability= '', $groupid=0, $onlyactive=false)
 get_enrolled_users (context $context, $withcapability= '', $groupid=0, $userfields= 'u.*', $orderby= '', $limitfrom=0, $limitnum=0)
 count_enrolled_users (context $context, $withcapability= '', $groupid=0)
 load_capability_def ($component)
 get_cached_capabilities ($component= 'moodle')
 get_default_capabilities ($archetype)
 reset_role_capabilities ($roleid)
 update_capabilities ($component= 'moodle')
 capabilities_cleanup ($component, $newcapdef=null)
 get_all_risks ()
 get_capability_docs_link ($capability)
 role_context_capabilities ($roleid, context $context, $cap= '')
 is_inside_frontpage (context $context)
 get_capability_info ($capabilityname)
 get_capability_string ($capabilityname)
 get_component_string ($component, $contextlevel)
 get_profile_roles (context $context)
 get_roles_used_in_context (context $context)
 get_user_roles_in_course ($userid, $courseid)
 user_can_assign (context $context, $targetroleid)
 get_all_roles ()
 get_archetype_roles ($archetype)
 get_user_roles (context $context, $userid=0, $checkparentcontexts=true, $order= 'c.contextlevel DESC, r.sortorder ASC')
 allow_override ($sroleid, $troleid)
 allow_assign ($fromroleid, $targetroleid)
 allow_switch ($fromroleid, $targetroleid)
 get_assignable_roles (context $context, $rolenamedisplay=ROLENAME_ALIAS, $withusercounts=false, $user=null)
 get_switchable_roles (context $context)
 get_overridable_roles (context $context, $rolenamedisplay=ROLENAME_ALIAS, $withcounts=false)
 get_default_enrol_roles (context $context, $addroleid=null)
 get_role_contextlevels ($roleid)
 get_roles_for_contextlevels ($contextlevel)
 get_default_contextlevels ($rolearchetype)
 set_role_contextlevels ($roleid, array $contextlevels)
 get_users_by_capability (context $context, $capability, $fields= '', $sort= '', $limitfrom= '', $limitnum= '', $groups= '', $exceptions= '', $doanything_ignored=null, $view_ignored=null, $useviewallgroups=false)
 sort_by_roleassignment_authority ($users, context $context, $roles=array(), $sortpolicy= 'locality')
 get_role_users ($roleid, context $context, $parent=false, $fields= '', $sort= 'u.lastname, u.firstname', $gethidden_ignored=null, $group= '', $limitfrom= '', $limitnum= '', $extrawheretest= '', $whereparams=array())
 count_role_users ($roleid, context $context, $parent=false)
 get_user_capability_course ($capability, $userid=null, $doanything=true, $fieldsexceptid= '', $orderby= '')
 get_roles_on_exact_context (context $context)
 role_switch ($roleid, context $context)
 is_role_switched ($courseid)
 get_roles_with_override_on_context (context $context)
 get_capabilities_from_role_on_context ($role, context $context)
 get_roles_with_assignment_on_context (context $context)
 get_users_from_role_on_context ($role, context $context)
 user_has_role_assignment ($userid, $roleid, $contextid=0)
 role_get_name ($role, context_course $coursecontext)
 role_fix_names ($roleoptions, context $context, $rolenamedisplay=ROLENAME_ALIAS)
 component_level_changed ($cap, $comp, $contextlevel)
 fix_role_sortorder ($allroles)
 switch_roles ($first, $second)
 role_cap_duplicate ($sourcerole, $targetrole)
 get_roles_with_cap_in_context ($context, $capability)
 get_roles_with_caps_in_context ($context, $capabilities)
 get_role_names_with_caps_in_context ($context, $capabilities)
 prohibit_is_removable ($roleid, context $context, $capability)
 role_change_permission ($roleid, $context, $capname, $permission)
 load_temp_role ($context, $roleid, array $accessdata)
 remove_temp_roles ($context, array $accessdata)
 get_system_context ($cache=true)
 get_context_instance ($contextlevel, $instance=0, $strictness=IGNORE_MISSING)
 get_context_instance_by_id ($id, $strictness=IGNORE_MISSING)
 get_parent_contexts (context $context, $includeself=false)
 get_parent_contextid (context $context)
 get_child_contexts (context $context)
 create_contexts ($contextlevel=null, $buildpaths=true)
 cleanup_contexts ()
 build_context_path ($force=false)
 rebuild_contexts (array $fixcontexts)
 preload_course_contexts ($courseid)
 context_instance_preload_sql ($joinon, $contextlevel, $tablealias)
 context_instance_preload (stdClass $rec)
 mark_context_dirty ($path)
 context_moved (context $context, context $newparent)
 delete_context ($contextlevel, $instanceid, $deleterecord=true)
 get_contextlevel_name ($contextlevel)
 print_context_name (context $context, $withprefix=true, $short=false)
 get_context_url (context $context)
 get_course_context (context $context)
 get_courseid_from_context (context $context)
 get_user_courses_bycap ($userid, $cap, $accessdata_ignored, $doanything_ignored, $sort= 'c.sortorder ASC', $fields=null, $limit_ignored=0)
 fetch_context_capabilities (context $context)
 get_sorted_contexts ($select, $params=array())
 get_role_context_caps ($roleid, context $context)
 get_related_contexts_string (context $context)

Enumeration Type Documentation

enum CAP_ALLOW

Allow permission, overrides CAP_PREVENT defined in parent contexts

Definition at line 131 of file accesslib.php.

No capability change

Definition at line 129 of file accesslib.php.

Prevent permission, overrides CAP_ALLOW defined in parent contexts

Definition at line 133 of file accesslib.php.

Prohibit permission, overrides everything in current and child contexts

Definition at line 135 of file accesslib.php.

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.

Definition at line 152 of file accesslib.php.

Course context level - one instances for each course

Definition at line 144 of file accesslib.php.

Course category context level - one instance for each category

Definition at line 142 of file accesslib.php.

Course module context level - one instance for each course module

Definition at line 146 of file accesslib.php.

System context level - only one instance in every system

Definition at line 138 of file accesslib.php.

User context level - one instance for each user describing what others can do to user

Definition at line 140 of file accesslib.php.

Capability allows changes in system configuration - see http://docs.moodle.org/dev/Hardening_new_Roles_system

Definition at line 157 of file accesslib.php.

capability allows mass delete of data belonging to other users - see http://docs.moodle.org/dev/Hardening_new_Roles_system

Definition at line 165 of file accesslib.php.

Capability allow management of trusts - NOT IMPLEMENTED YET - see http://docs.moodle.org/dev/Hardening_new_Roles_system

Definition at line 155 of file accesslib.php.

Capability allows access to personal user information - see http://docs.moodle.org/dev/Hardening_new_Roles_system

Definition at line 161 of file accesslib.php.

enum RISK_SPAM

Capability allows users to add content otehrs may see - see http://docs.moodle.org/dev/Hardening_new_Roles_system

Definition at line 163 of file accesslib.php.

enum RISK_XSS

Capability allows user to add scritped content - see http://docs.moodle.org/dev/Hardening_new_Roles_system

Definition at line 159 of file accesslib.php.

rolename displays - the name as defined by a role alias

Definition at line 170 of file accesslib.php.

rolename displays - the name as defined by a role alias, in raw form suitable for editing

Definition at line 176 of file accesslib.php.

rolename displays - Both, like this: Role alias (Original)

Definition at line 172 of file accesslib.php.

rolename displays - the name as defined in the role definition

Definition at line 168 of file accesslib.php.

rolename displays - the name as defined in the role definition and the shortname in brackets

Definition at line 174 of file accesslib.php.

rolename displays - the name is simply short role name

Definition at line 178 of file accesslib.php.


Function Documentation

Clears accesslib's private caches. ONLY BE USED BY UNIT TESTS

This method should ONLY BE USED BY UNIT TESTS. It clears all of accesslib's private caches. You need to do this before setting up test data, and also at the end of the tests.

Returns:
void

Definition at line 213 of file accesslib.php.

Here is the caller graph for this function:

allow_assign ( fromroleid,
targetroleid 
)

Creates a record in the role_allow_assign table

Parameters:
int$fromroleidsource roleid
int$targetroleidtarget roleid
Returns:
void

Definition at line 3003 of file accesslib.php.

Here is the caller graph for this function:

allow_override ( sroleid,
troleid 
)

Creates a record in the role_allow_override table

Parameters:
int$sroleidsource roleid
int$troleidtarget roleid
Returns:
void

Definition at line 2987 of file accesslib.php.

Here is the caller graph for this function:

allow_switch ( fromroleid,
targetroleid 
)

Creates a record in the role_allow_switch table

Parameters:
int$fromroleidsource roleid
int$targetroleidtarget roleid
Returns:
void

Definition at line 3019 of file accesslib.php.

Here is the caller graph for this function:

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

Function to write context specific overrides, or default capabilities.

NOTE: use $context->mark_dirty() after this

Parameters:
string$capabilitystring name
int$permissionCAP_ constants
int$roleidrole id
int | context$contextidcontext id
bool$overwrite
Returns:
bool always true or exception

Definition at line 1444 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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
Returns:
boolean success or failure.

Definition at line 1275 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

build_context_path ( force = false)

Populate context.path and context.depth where missing.

Deprecated:
since 2.2, use context_helper::build_all_paths() instead
Parameters:
bool$forceforce a complete rebuild of the path and depth fields, defaults to false
Returns:
void

Definition at line 6903 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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
Returns:
boolean Returns true if the user is able to access the course

Definition at line 2004 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

capabilities_cleanup ( component,
newcapdef = null 
)

Deletes cached capabilities that are no longer needed by the component. Also unassigns these capabilities from any roles that have them.

Parameters:
string$componentexamples: 'moodle', 'mod_forum', 'block_quiz_results'
array$newcapdefarray of the new capability definitions that will be compared with the cached capabilities
Returns:
int number of deprecated capabilities that have been removed

Definition at line 2535 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

Remove stale context records

Deprecated:
since 2.2, use context_helper::cleanup_instances() instead
Returns:
bool

Definition at line 6891 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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
Returns:
bool whether 2 component are in different "sections"

Definition at line 4163 of file accesslib.php.

Here is the caller graph for this function:

context_instance_preload ( stdClass $  rec)

Preloads context information from db record and strips the cached info. The db request has to contain both the $join and $select from context_instance_preload_sql()

Deprecated:
since 2.2
Parameters:
stdClass$rec
Returns:
void (modifies $rec)

Definition at line 6958 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

context_instance_preload_sql ( joinon,
contextlevel,
tablealias 
)

Preloads context information together with instances. Use context_instance_preload() to strip the context info from the record and cache the context instance.

Deprecated:
Parameters:
string$joinonfor example 'u.id'
string$contextlevelcontext level of instance in $joinon
string$tablealiascontext table alias
Returns:
array with two values - select and join part

Definition at line 6944 of file accesslib.php.

Here is the caller graph for this function:

context_moved ( context context,
context newparent 
)

Update the path field of the context and all dep. subcontexts that follow

Update the path field of the context and all the dependent subcontexts that follow the move.

The most important thing here is to be as DB efficient as possible. This op can have a massive impact in the DB.

Deprecated:
since 2.2
Parameters:
context$contextcontext obj
context$newparentnew parent obj
Returns:
void

Definition at line 7011 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

count_enrolled_users ( context context,
withcapability = '',
groupid = 0 
)

Counts list of users enrolled into course (as per above function)

Parameters:
context$context
string$withcapability
int$groupid0 means ignore groups, any other value limits the result by group id
Returns:
array of user records

Definition at line 2303 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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
Returns:
int Returns the result count

Definition at line 3790 of file accesslib.php.

Here is the call graph for this function:

create_contexts ( contextlevel = null,
buildpaths = true 
)

Precreates all contexts including all parents

Deprecated:
since 2.2
Parameters:
int$contextlevelempty means all
bool$buildpathsupdate paths and depths
Returns:
void

Definition at line 6881 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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

Function that creates a role

Parameters:
string$namerole name
string$shortnamerole short name
string$descriptionrole description
string$archetype
Returns:
int id or dml_exception

Definition at line 1370 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

delete_context ( contextlevel,
instanceid,
deleterecord = true 
)

Remove a context record and any dependent entries, removes context from static context cache too

Deprecated:
since 2.2, use $context->delete_content() instead
Parameters:
int$contextlevel
int$instanceid
bool$deleterecordfalse means keep record for now
Returns:
bool returns true or throws an exception

Definition at line 7025 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

delete_role ( roleid)

Function that deletes a role and cleanups up after it

Parameters:
int$roleidid of role to delete
Returns:
bool always true

Definition at line 1406 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

Extracts the relevant capabilities given a contextid. All case based, example an instance of forum context. Will fetch all forum related capabilities, while course contexts Will fetch all capabilities

capabilities `name` varchar(150) NOT NULL, `captype` varchar(50) NOT NULL, `contextlevel` int(10) NOT NULL, `component` varchar(100) NOT NULL,

Deprecated:
since 2.2
Parameters:
context$context
Returns:
array

Definition at line 7146 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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();
Returns:
array $role->sortorder =-> $role->id with the keys in ascending order.

Definition at line 4195 of file accesslib.php.

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.

Returns:
array all the known types of risk.

Definition at line 2569 of file accesslib.php.

Here is the caller graph for this function:

Returns all site roles in correct sort order.

Returns:
array

Definition at line 2920 of file accesslib.php.

Here is the caller graph for this function:

get_archetype_roles ( archetype)

Returns roles of a specified archetype

Parameters:
string$archetype
Returns:
array of full role records

Definition at line 2931 of file accesslib.php.

Here is the caller graph for this function:

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.
Returns:
array if $withusercounts is false, then an array $roleid => $rolename. if $withusercounts is true, returns a list of three arrays, $rolenames, $rolecounts, and $nameswithcounts.

Definition at line 3040 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_cached_capabilities ( component = 'moodle')

Gets the capabilities that have been cached in the database for this component.

Parameters:
string$component- examples: 'moodle', 'mod_forum'
Returns:
array array of capabilities

Definition at line 2347 of file accesslib.php.

Here is the caller graph for this function:

get_capabilities_from_role_on_context ( role,
context context 
)

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

Parameters:
stdClass$role
context$context
Returns:
array

Definition at line 4001 of file accesslib.php.

get_capability_docs_link ( capability)

Return a link to moodle docs for a given capability name

Parameters:
object$capabilitya capability - a row from the mdl_capabilities table.
Returns:
string the human-readable capability name as a link to Moodle Docs.

Definition at line 2586 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_capability_info ( capabilityname)

Returns capability information (cached)

Parameters:
string$capabilityname
Returns:
object or null if capability not found

Definition at line 2676 of file accesslib.php.

Here is the caller graph for this function:

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
Returns:
string

Definition at line 2703 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_child_contexts ( context context)

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

For course category contexts it will return immediate children only categories and courses. It will NOT recurse into courses or child 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.

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

Deprecated:
since 2.2, use $context->get_child_contexts() instead
Parameters:
context$context,.
Returns:
array Array of child records

Definition at line 6869 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_component_string ( component,
contextlevel 
)

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

Parameters:
string$component
int$contextlevel
Returns:
string|bool String is success, false if failed

Definition at line 2740 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_context_info_array ( contextid)

Returns context instance plus related course and cm instances

Parameters:
int$contextid
Returns:
array of ($context, $course, $cm)

Definition at line 1333 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_context_instance ( contextlevel,
instance = 0,
strictness = IGNORE_MISSING 
)

Get the context instance as an object. This function will create the context instance if it does not exist yet.

Deprecated:
since 2.2, use context_course::instance() or other relevant class instead
Parameters:
integer$contextlevelThe context level, for example CONTEXT_COURSE, or CONTEXT_MODULE.
integer$instanceThe instance id. For $level = CONTEXT_COURSE, this would be $course->id, for $level = CONTEXT_MODULE, this would be $cm->id. And so on. Defaults to 0
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 or multiple records found
Returns:
context The context object.

Definition at line 6791 of file accesslib.php.

Here is the call graph for this function:

get_context_instance_by_id ( id,
strictness = IGNORE_MISSING 
)

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

Deprecated:
since 2.2, use context::instance_by_id($id) instead
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 or multiple records found
Returns:
context|bool the context object or false if not found.

Definition at line 6818 of file accesslib.php.

Here is the call graph for this function:

get_context_url ( context context)

Get a URL for a context, if there is a natural one. For example, for CONTEXT_COURSE, this is the course page. For CONTEXT_USER it is the user profile page.

Deprecated:
since 2.2
Parameters:
context$contextthe context.
Returns:
moodle_url

Definition at line 7072 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_contextlevel_name ( contextlevel)

Returns context level name

Deprecated:
since 2.2
Parameters:
integer$contextlevel$context->context level. One of the CONTEXT_... constants.
Returns:
string the name for this type of context.

Definition at line 7044 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_course_context ( context context)

Is this context part of any course? if yes return course context, if not return null or throw exception.

Deprecated:
since 2.2, use $context->get_course_context() instead
Parameters:
context$context
Returns:
course_context context of the enclosing course, null if not found or exception

Definition at line 7084 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

Returns current course id or null if outside of course based on context parameter.

Deprecated:
since 2.2, use $context->get_course_context instead
Parameters:
context$context
Returns:
int|bool related course id or false

Definition at line 7095 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_default_capabilities ( archetype)

Returns default capabilities for given role archetype.

Parameters:
string$archetyperole archetype
Returns:
array

Definition at line 2358 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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();
Returns:
array list of the context levels at which this type of role may be assigned by default.

Definition at line 3307 of file accesslib.php.

Here is the caller graph for this function:

get_default_enrol_roles ( context context,
addroleid = null 
)

Create a role menu suitable for default role selection in enrol plugins.

Parameters:
context$context
int$addroleidcurrent or default role - always added to list
Returns:
array roleid=>localised role name

Definition at line 3253 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_enrolled_sql ( context context,
withcapability = '',
groupid = 0,
onlyactive = false 
)

Returns array with sql code and parameters returning all ids of users enrolled into course.

This function is using 'eu[0-9]+_' prefix for table names and parameters.

Parameters:
context$context
string$withcapability
int$groupid0 means ignore groups, any other value limits the result by group id
bool$onlyactiveconsider only active enrolments in enabled plugins and time restrictions
Returns:
array list($sql, $params)

Definition at line 2111 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_enrolled_users ( context context,
withcapability = '',
groupid = 0,
userfields = 'u.*',
orderby = '',
limitfrom = 0,
limitnum = 0 
)

Returns list of users enrolled into course.

Parameters:
context$context
string$withcapability
int$groupid0 means ignore groups, any other value limits the result by group id
string$userfieldsrequested user record fields
string$orderby
int$limitfromreturn a subset of records, starting at this point (optional, required if $limitnum is set).
int$limitnumreturn a subset comprising this many records (optional, required if $limitfrom is set).
Returns:
array of user records

Definition at line 2277 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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

Returns:
stdClass role record

Definition at line 306 of file accesslib.php.

Here is the call graph for this function:

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
Returns:
stdClass local capability override

Definition at line 1322 of file accesslib.php.

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.
Returns:
array if $withcounts is false, then an array $roleid => $rolename. if $withusercounts is true, returns a list of three arrays, $rolenames, $rolecounts, and $nameswithcounts.

Definition at line 3176 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_parent_contextid ( context context)

Return the id of the parent of this context, or false if there is no parent (only happens if this is the site context.)

Deprecated:
since 2.2, use $context->get_parent_context() instead
Parameters:
context$context
Returns:
integer the id of the parent context.

Definition at line 6844 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_parent_contexts ( context context,
includeself = false 
)

Recursive function which, given a context, find all parent context ids, and return the array in reverse order, i.e. parent first, then grand parent, etc.

Deprecated:
since 2.2, use $context->get_parent_context_ids() instead
Parameters:
context$context
bool$includeselfoptional, defaults to false
Returns:
array

Definition at line 6832 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_profile_roles ( context context)

Gets the list of roles assigned to this context and up (parents) from the list of roles that are visible on user profile page and participants page.

Parameters:
context$context
Returns:
array

Definition at line 2789 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

Gets a string for sql calls, searching for stuff in this context or above

NOTE: use $DB->get_in_or_equal($context->get_parent_context_ids()...

Deprecated:
since 2.2, $context->use get_parent_context_ids() instead
Parameters:
context$context
Returns:
string

Definition at line 7236 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

Returns array of all role archetypes.

Returns:
array

Definition at line 1248 of file accesslib.php.

Here is the caller graph for this function:

get_role_context_caps ( roleid,
context context 
)

This is really slow!!! do not use above course context level

Deprecated:
since 2.2
Parameters:
int$roleid
context$context
Returns:
array

Definition at line 7191 of file accesslib.php.

Here is the call graph for this function:

get_role_contextlevels ( roleid)

Return context levels where this role is assignable.

Parameters:
integer$roleidthe id of a role.
Returns:
array list of the context levels at which this role may be assigned.

Definition at line 3280 of file accesslib.php.

Here is the caller graph for this function:

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:
object$context
array$capabilitiesAn array of capabilities
Returns:
array of roles with all of the required capabilities

Definition at line 4356 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_role_users ( roleid,
context context,
parent = false,
fields = '',
sort = 'u.lastname,
u.firstname ,
gethidden_ignored = null,
group = '',
limitfrom = '',
limitnum = '',
extrawheretest = '',
whereparams = array() 
)

Gets all the users assigned this role in this context or higher

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.)
bool$gethidden_ignoreduse enrolments instead
string$groupdefaults to ''
mixed$limitfromdefaults to ''
mixed$limitnumdefaults to ''
string$extrawheretestdefaults to ''
string | array$whereparamsdefaults to ''
Returns:
array

Definition at line 3723 of file accesslib.php.

Here is the caller graph for this function:

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.
Returns:
array list of role ids that are assignable at this context level.

Definition at line 3294 of file accesslib.php.

Here is the caller graph for this function:

This function finds the roles assigned directly to this context only i.e. no roles in parent contexts

Parameters:
context$context
Returns:
array

Definition at line 3891 of file accesslib.php.

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

Parameters:
context$context
Returns:
array

Definition at line 2816 of file accesslib.php.

Here is the call graph for this function:

Find out which roles has assignment on this context

Parameters:
context$context
Returns:
array

Definition at line 4017 of file accesslib.php.

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:
object$context
string$capability
Returns:
array($neededroles, $forbiddenroles)

Definition at line 4262 of file accesslib.php.

Here is the caller graph for this function:

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$capability- capability name (string)
string$permission- optional, the permission defined for this capability either CAP_ALLOW, CAP_PREVENT or CAP_PROHIBIT. Defaults to null which means any.
stdClass$context,nullmeans any
Returns:
array of role records

Definition at line 1523 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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:
object$context
array$capabilitiesAn array of capabilities
Returns:
array of roles with all of the required capabilities

Definition at line 4321 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

Get any role that has an override on exact context

Parameters:
context$contextThe context to check
Returns:
array An array of roles

Definition at line 3985 of file accesslib.php.

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. for the purpose of $select, you need to know that the context table has been aliased to ctx, so for example, you can call get_sorted_contexts('ctx.depth = 3');

Deprecated:
since 2.2
Parameters:
string$selectthe contents of the WHERE clause. Remember to do ctx.fieldname.
array$paramsany parameters required by $select.
Returns:
array the requested context records.

Definition at line 7162 of file accesslib.php.

get_switchable_roles ( context context)

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.
Returns:
array an array $roleid => $rolename.

Definition at line 3133 of file accesslib.php.

Here is the call graph for this function:

get_system_context ( cache = true)

Returns system context or null if can not be created yet.

Deprecated:
since 2.2, use context_system::instance()
Parameters:
bool$cacheuse caching
Returns:
context system context (null if context table not created yet)

Definition at line 6775 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

get_user_capability_course ( capability,
userid = null,
doanything = true,
fieldsexceptid = '',
orderby = '' 
)

This function gets the list of courses that this user has a particular capability in. It is still not very efficient.

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
string$orderbyIf set, use a comma-separated list of fields from course table with sql modifiers (DESC) if needed
Returns:
array Array of courses, may have zero entries. Or false if query failed.

Definition at line 3836 of file accesslib.php.

Here is the call graph for this function:

get_user_courses_bycap ( userid,
cap,
accessdata_ignored,
doanything_ignored,
sort = 'c.sortorder ASC',
fields = null,
limit_ignored = 0 
)

Get an array of courses where cap requested is available and user is enrolled, this can be relatively slow.

Deprecated:
since 2.2, use enrol_get_users_courses() instead
Parameters:
int$useridA user id. By default (null) checks the permissions of the current user.
string$cap- name of the capability
array$accessdata_ignored
bool$doanything_ignored
string$sort- sorting fields - prefix each fieldname with "c."
array$fields- additional fields you are interested in...
int$limit_ignored
Returns:
array $courses - ordered array of course objects - see notes above

Definition at line 7117 of file accesslib.php.

Here is the call graph for this function:

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'
Returns:
array

Definition at line 2948 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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
Returns:
string

Definition at line 2839 of file accesslib.php.

Here is the call graph for this function:

get_users_by_capability ( context context,
capability,
fields = '',
sort = '',
limitfrom = '',
limitnum = '',
groups = '',
exceptions = '',
doanything_ignored = null,
view_ignored = 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$doanything_ignorednot used any more, admin accounts are never returned
bool$view_ignored- 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.
Returns:
mixed

***** Set up default fields ******

Set up default sort

We never return deleted users or guest account.

Groups

User exceptions

Ok, let's get the users!

Definition at line 3373 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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
Returns:
array

Definition at line 4033 of file accesslib.php.

has_all_capabilities ( array capabilities,
context context,
userid = null,
doanything = true 
)

Check if the user has all the capabilities in a list.

This is just a utility method that calls has_capability in a loop. Try to put the capabilities that fewest users are likely to have first in the list for best performance.

See also:
has_capability()
Parameters:
array$capabilitiesan array of capability names.
context$contextthe context to check the capability in. You normally get this with get_context_instance.
integer$useridA user id. By default (null) checks the permissions of the current user.
boolean$doanythingIf false, ignore effect of admin role assignment
Returns:
boolean true if the user has all of these capabilities. Otherwise false.

Definition at line 515 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

has_any_capability ( array capabilities,
context context,
userid = null,
doanything = true 
)

Check if the user has any one of several capabilities from a list.

This is just a utility method that calls has_capability in a loop. Try to put the capabilities that most users are likely to have first in the list for best performance.

See also:
has_capability()
Parameters:
array$capabilitiesan array of capability names.
context$contextthe context to check the capability in. You normally get this with get_context_instance.
integer$useridA user id. By default (null) checks the permissions of the current user.
boolean$doanythingIf false, ignore effect of admin role assignment
Returns:
boolean true if the user has any of these capabilities. Otherwise false.

Definition at line 492 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

has_capability ( capability,
context context,
user = null,
doanything = true 
)

Check whether a user has a particular capability in a given context.

For example: $context = get_context_instance(CONTEXT_MODULE, $cm->id); has_capability('mod/forum:replypost',$context)

By default checks the capabilities of the current user, but you can pass a different userid. By default will return true for admin users, but you can override that with the fourth argument.

Guest and not-logged-in users can never get any dangerous capability - that is any write capability or capabilities with XSS, config or data loss risks.

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 get_context_instance.
integer | object$userA user id or object. By default (null) checks the permissions of the current user.
boolean$doanythingIf false, ignores effect of admin role assignment
Returns:
boolean true if the user has this capability. Otherwise false.

Definition at line 348 of file accesslib.php.

Here is the call graph for this function:

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
Returns:
bool

Definition at line 560 of file accesslib.php.

Here is the caller graph for this function:

is_enrolled ( context context,
user = null,
withcapability = '',
onlyactive = false 
)

Returns true if user is enrolled (is participating) in course this is intended for students and teachers.

Since 2.2 the result for active enrolments and current user are cached.

Parameters:
context$context
int | stdClass$user,ifnull $USER is used, otherwise user object or id expected
string$withcapabilityextra capability name
bool$onlyactiveconsider only active enrolments in enabled plugins and time restrictions
Returns:
bool

Definition at line 1909 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

is_guest ( context context,
user = null 
)

Does user have a (temporary or real) guest access to course?

Parameters:
context$context
stdClass | int$user
Returns:
bool

Definition at line 1835 of file accesslib.php.

Here is the call graph for this function:

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.
Returns:
bool

Definition at line 2665 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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 courseid 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
Returns:
bool True if the user has switched roles within the course.

Definition at line 3973 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

is_safe_capability ( capability)

Verify capability risks.

Parameters:
object$capabilitya capability - a row from the capabilities table.
Returns:
boolean whether this capability is safe - that is, whether people with the safeoverrides capability should be allowed to change it.

Definition at line 1310 of file accesslib.php.

Here is the caller graph for this function:

is_siteadmin ( user_or_id = null)

Check if the user is an admin at the site level.

Please note that use of proper capabilities is always encouraged, this function is supposed to be used from core or for temporary hacks.

Parameters:
int | stdClass$user_or_iduser id or user object
Returns:
bool true if user is one of the administrators, false otherwise

Definition at line 533 of file accesslib.php.

Here is the caller graph for this function:

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.

Parameters:
context$context
int | stdClass$user,ifnull $USER is used
string$withcapabilityextra capability name
Returns:
bool

Definition at line 1875 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

isguestuser ( user = null)

Determines if a user is logged in as real guest user with username 'guest'.

Parameters:
int | object$usermixed user object or id, $USER if not specified
Returns:
bool true if user is the real guest user, false if not logged in or other user

Definition at line 1794 of file accesslib.php.

Here is the call graph for this function:

Determines if a user is currently logged in

Returns:
bool

Definition at line 1782 of file accesslib.php.

load_capability_def ( component)

Loads the capability definitions for the component (from file).

Loads the capability definitions for the component (from file). If no capabilities are defined for the component, we simply return an empty array.

Parameters:
string$componentfull plugin name, examples: 'moodle', 'mod_forum'
Returns:
array array of capabilities

Definition at line 2324 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Since:
2.2
Parameters:
context_course$coursecontext
int$roleid
Returns:
void

Definition at line 1181 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

load_temp_role ( context,
roleid,
array accessdata 
)

Not available any more, use load_temp_course_role() instead.

Deprecated:
since 2.2
Parameters:
stdClass$context
int$roleid
array$accessdata
Returns:
array

Definition at line 6750 of file accesslib.php.

Here is the call graph for this function:

mark_context_dirty ( path)

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

Deprecated:
since 2.2, use $context->mark_dirty() instead
Parameters:
string$pathcontext path

Definition at line 6968 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

preload_course_contexts ( courseid)

Preloads all contexts relating to a course: course, modules. Block contexts are no longer loaded here. The contexts for all the blocks on the current page are now efficiently loaded by block_manager::load_blocks().

Deprecated:
since 2.2
Parameters:
int$courseidCourse ID
Returns:
void

Definition at line 6930 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

print_context_name ( context context,
withprefix = true,
short = false 
)

Prints human readable context identifier.

Deprecated:
since 2.2
Parameters:
context$contextthe context.
boolean$withprefixwhether to prefix the name of the context with the type of context, e.g. User, Course, Forum, etc.
boolean$shortwhether to user the short name of the thing. Only applies to course contexts
Returns:
string the human readable context name.

Definition at line 7059 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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
Returns:
bool

Definition at line 4384 of file accesslib.php.

Here is the caller graph for this function:

rebuild_contexts ( array fixcontexts)

Rebuild all related context depth and path caches

Deprecated:
Parameters:
array$fixcontextsarray of contexts, strongtyped
Returns:
void

Definition at line 6914 of file accesslib.php.

Here is the call graph for this function:

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

Since:
2.2
Parameters:
context_course$coursecontext
Returns:
void

Definition at line 1219 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

remove_temp_roles ( context,
array accessdata 
)

Not available any more, use remove_temp_course_roles() instead.

Deprecated:
since 2.2
Parameters:
object$context
array$accessdata
Returns:
array access data

Definition at line 6763 of file accesslib.php.

Here is the call graph for this function:

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 get_context_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'.
Returns:
void terminates with an error if the user does not have the given capability.

Definition at line 688 of file accesslib.php.

Here is the call graph for this function:

reset_role_capabilities ( roleid)

Reset role capabilities to default according to selected role archetype. If no archetype selected, removes all capabilities.

Parameters:
int$roleid
Returns:
void

Definition at line 2400 of file accesslib.php.

Here is the call graph for this function:

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
Returns:
int new/existing id of the assignment

Definition at line 1566 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

role_cap_duplicate ( sourcerole,
targetrole 
)

Duplicates all the base definitions of a role

Parameters:
object$sourcerolerole to copy from
int$targetroleid of role to copy to

Definition at line 4236 of file accesslib.php.

Here is the call graph for this function:

role_change_permission ( roleid,
context,
capname,
permission 
)

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

Parameters:
int$roleid
object$context
string$capnamecapability name
int$permission
Returns:
void

Definition at line 4420 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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:
context$context
int$roleid
string$capcapability, optional, defaults to ''
Returns:
array of capabilities

Definition at line 2601 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

role_fix_names ( roleoptions,
context context,
rolenamedisplay = ROLENAME_ALIAS 
)

Prepare list of roles for display, apply aliases and format text

Parameters:
array$roleoptionsarray roleid => rolename or roleid => roleobject
context$contexta context
int$rolenamedisplay
Returns:
array Array of context-specific role names, or role objexts with a ->localname field added.

Definition at line 4100 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

role_get_name ( role,
context_course coursecontext 
)

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

Parameters:
stdClass$rolerole object
context_course$coursecontext
Returns:
string name of role in course context

Definition at line 4082 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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.
Returns:
bool success or failure.

Definition at line 3917 of file accesslib.php.

Here is the caller graph for this function:

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

Removes one role assignment

Parameters:
int$roleid
int$userid
int | context$contextid
string$component
int$itemid
Returns:
void

Definition at line 1664 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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
Returns:
void

If the user is the current user, then do full reload of capabilities too.

If the user is the current user, then do full reload of capabilities too.

Definition at line 1693 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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.
Returns:
void

Definition at line 3334 of file accesslib.php.

Here is the caller graph for this function:

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
Returns:
array sorted copy of the array

Definition at line 3657 of file accesslib.php.

Here is the caller graph for this function:

switch_roles ( first,
second 
)

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

Parameters:
object$firstThe first role. Actually, only ->sortorder is used.
object$secondThe second role. Actually, only ->sortorder is used.
Returns:
boolean success or failure

Definition at line 4221 of file accesslib.php.

unassign_capability ( capability,
roleid,
contextid = null 
)

Unassign a capability from a role.

NOTE: use $context->mark_dirty() after this

Parameters:
string$capabilitythe name of the capability
int$roleidthe role id
int | context$contextidnull means all contexts
Returns:
boolean true or exception

Definition at line 1493 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

update_capabilities ( component = 'moodle')

Updates the capabilities table with the component capability definitions. If no parameters are given, the function updates the core moodle capabilities.

Note that the absence of the db/access.php capabilities definition file will cause any stored capabilities for the component to be removed from the database.

Parameters:
string$componentexamples: 'moodle', 'mod/forum', 'block/quiz_results'
Returns:
boolean true if success, exception in case of any problems

Definition at line 2427 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

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
Returns:
boolean

Definition at line 2894 of file accesslib.php.

Here is the call graph for this function:

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
Returns:
bool

Definition at line 4051 of file accesslib.php.

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

$ACCESSLIB_PRIVATE = new stdClass()

Definition at line 197 of file accesslib.php.

$ACCESSLIB_PRIVATE accessdatabyuser = array()

Definition at line 200 of file accesslib.php.

$ACCESSLIB_PRIVATE capabilities = null

Definition at line 202 of file accesslib.php.

$ACCESSLIB_PRIVATE dirtycontexts = null

Definition at line 199 of file accesslib.php.

$ACCESSLIB_PRIVATE rolepermissions = array()

Definition at line 201 of file accesslib.php.

 All Data Structures Namespaces Files Functions Variables Enumerations