Moodle APIs 4.3
Moodle 4.3.6 (Build: 20240812)
|
Basic moodle context abstraction class. More...
Public Member Functions | |
__get ($name) | |
Magic method getter, redirects to read only values. | |
__isset ($name) | |
Full support for isset on our magic read only properties. | |
__set ($name, $value) | |
Magic setter method, we do not want anybody to modify properties from the outside. | |
__unset ($name) | |
All properties are read only, sorry. | |
delete () | |
Delete the context content and the context record itself. | |
delete_capabilities () | |
Unassign all capabilities from a context. | |
delete_content () | |
Delete all data linked to content, do not delete the context record itself. | |
get_capabilities (string $sort=self::DEFAULT_CAPABILITY_SORT) | |
Returns array of relevant context capability records. | |
get_child_contexts () | |
Recursive function which, given a context, find all its children context ids. | |
get_context_name ($withprefix=true, $short=false, $escape=true) | |
Returns human readable context identifier. | |
get_course_context ($strict=true) | |
Is this context part of any course? If yes return course context. | |
get_parent_context () | |
Returns parent context. | |
get_parent_context_ids ($includeself=false) | |
Returns parent context ids of this context in reversed order, i.e. | |
get_parent_context_paths ($includeself=false) | |
Returns parent context paths of this context. | |
get_parent_contexts ($includeself=false) | |
Returns parent contexts of this context in reversed order, i.e. | |
get_url () | |
Returns the most relevant URL for this context. | |
getIterator () | |
Create an iterator because magic vars can't be seen by 'foreach'. | |
is_child_of (context $possibleparent, bool $includeself) | |
Determine if the current context is a child of the possible parent. | |
is_locked () | |
Whether the current context is locked. | |
is_parent_of (context $possiblechild, bool $includeself) | |
Determine if the current context is a parent of the possible child. | |
mark_dirty () | |
Mark a context as dirty (with timestamp) so as to force reloading of the context. | |
reload_if_dirty () | |
Reset all cached permissions and definitions if the necessary. | |
reset_paths ($rebuild=true) | |
Remove all context path info and optionally rebuild it. | |
set_locked (bool $locked) | |
Set whether this context has been locked or not. | |
update_moved (context $newparent) | |
Update context info after moving context in the tree structure. | |
Static Public Member Functions | |
static | get_possible_parent_levels () |
Returns list of all possible parent context levels, it may include itself if nesting is allowed. | |
static | get_short_name () |
Returns short context name. | |
static | instance_by_id ($id, $strictness=MUST_EXIST) |
Get a context instance as an object, from a given context id. | |
Protected Member Functions | |
__construct (stdClass $record) | |
Constructor is protected so that devs are forced to use context_xxx\instance() or context\instance_by_id(). | |
Static Protected Member Functions | |
static | build_paths ($force) |
Rebuild context paths and depths at context level. | |
static | cache_add (context $context) |
Adds a context to the cache. | |
static | cache_get ($contextlevel, $instance) |
Gets a context from the cache. | |
static | cache_get_by_id ($id) |
Gets a context from the cache based on its id. | |
static | cache_remove (context $context) |
Removes a context from the cache. | |
static | create_instance_from_record (stdClass $record) |
This function is also used to work around 'protected' keyword problems in context_helper. | |
static | create_level_instances () |
Create missing context instances at given level. | |
static | get_behat_reference_columns () |
Returns list of columns that can be used from behat to look up context by reference. | |
static | get_cleanup_sql () |
Returns sql necessary for purging of stale context instances. | |
static | get_compatible_role_archetypes () |
Returns list of all role archetypes that are compatible with role assignments in context level. | |
static | get_instance_table () |
Returns context instance database name. | |
static | insert_context_record ($contextlevel, $instanceid, $parentpath) |
Utility method for context creation. | |
static | 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 | preload_from_record (stdClass $rec) |
Preloads context information from db record and strips the cached info. | |
static | reset_caches () |
Resets the cache to remove all data. | |
Protected Attributes | |
int | $_contextlevel |
The context level Can be accessed publicly through $context->contextlevel One of CONTEXT_* e.g. | |
int | $_depth |
The depth of the context in relation to parent contexts Can be accessed publicly through $context->depth. | |
int | $_id |
The context id Can be accessed publicly through $context->id. | |
int | $_instanceid |
Id of the item this context is related to e.g. | |
int | $_locked |
Whether this context is locked or not. | |
string | $_path |
The path to the context always starting from the system context Can be accessed publicly through $context->path. | |
string const | DEFAULT_CAPABILITY_SORT = 'contextlevel, component, name' |
Default sorting of capabilities in {. | |
Static Protected Attributes | |
static int | $cache_count = 0 |
Context count Why do we do count contexts? Because count($array) is horribly slow for large arrays. | |
static array | $cache_preloaded = array() |
Context caching info. | |
static context system | $systemcontext = null |
The system context once initialised. | |
Basic moodle context abstraction class.
Google confirms that no other important framework is using "context" class, we could use something else like mcontext or moodle_context, but we need to type this very often which would be annoying and it would take too much space...
This class is derived from stdClass for backwards compatibility with odl $context record that was returned from DML $DB->get_record()
|
protected |
Constructor is protected so that devs are forced to use context_xxx\instance() or context\instance_by_id().
stdClass | $record |
Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.
core\context::__get | ( | $name | ) |
Magic method getter, redirects to read only values.
string | $name |
mixed |
core\context::__isset | ( | $name | ) |
Full support for isset on our magic read only properties.
string | $name |
bool |
core\context::__set | ( | $name, | |
$value ) |
Magic setter method, we do not want anybody to modify properties from the outside.
string | $name | |
mixed | $value |
core\context::__unset | ( | $name | ) |
All properties are read only, sorry.
string | $name |
|
staticprotected |
Rebuild context paths and depths at context level.
bool | $force |
void |
Reimplemented in core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.
|
staticprotected |
Adds a context to the cache.
If the cache is full, discards a batch of older entries.
context | $context | New context to add |
void |
|
staticprotected |
Gets a context from the cache.
int | $contextlevel | Context level |
int | $instance | Instance ID |
context|bool | Context or false if not in cache |
|
staticprotected |
Gets a context from the cache based on its id.
int | $id | Context ID |
context|bool | Context or false if not in cache |
|
staticprotected |
Removes a context from the cache.
context | $context | Context object to remove |
void |
|
staticprotected |
This function is also used to work around 'protected' keyword problems in context_helper.
stdClass | $record |
context | instance |
|
staticprotected |
Create missing context instances at given level.
void |
Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.
|
staticprotected |
Returns list of columns that can be used from behat to look up context by reference.
array | list of column names from instance table |
Reimplemented in core\context\course, core\context\coursecat, core\context\module, and core\context\user.
|
abstract |
Returns array of relevant context capability records.
string | $sort | SQL order by snippet for sorting returned capabilities sensibly for display |
array |
Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.
core\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 ;-)
array | Array of child records |
Reimplemented in core\context\block, core\context\coursecat, and core\context\system.
|
staticprotected |
Returns sql necessary for purging of stale context instances.
string | cleanup SQL |
Reimplemented in core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.
|
staticprotected |
Returns list of all role archetypes that are compatible with role assignments in context level.
string[] |
Reimplemented in core\context\course, core\context\coursecat, core\context\module, and core\context\system.
core\context::get_context_name | ( | $withprefix = true, | |
$short = false, | |||
$escape = true ) |
Returns human readable context identifier.
boolean | $withprefix | whether to prefix the name of the context with the type of context, e.g. User, Course, Forum, etc. |
boolean | $short | whether to use the short name of the thing. Only applies to course contexts |
boolean | $escape | Whether the returned name of the thing is to be HTML escaped or not. |
string | the human readable context name. |
Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.
core\context::get_course_context | ( | $strict = true | ) |
Is this context part of any course? If yes return course context.
bool | $strict | true means throw exception if not found, false means return false if not found |
context\course|false | context of the enclosing course, null if not found or exception |
Reimplemented in core\context\block, core\context\course, and core\context\module.
|
staticprotected |
Returns context instance database name.
string|null | table name for all levels except system. |
Reimplemented in core\context\course, core\context\coursecat, core\context\module, and core\context\user.
core\context::get_parent_context | ( | ) |
Returns parent context.
context|false |
core\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.
bool | $includeself | true means include self too |
array | of context ids |
core\context::get_parent_context_paths | ( | $includeself = false | ) |
Returns parent context paths of this context.
bool | $includeself | true means include self too |
array | of context paths |
core\context::get_parent_contexts | ( | $includeself = false | ) |
Returns parent contexts of this context in reversed order, i.e.
parent first, then grand parent, etc.
bool | $includeself | true means include self too |
array | of context instances |
|
static |
Returns list of all possible parent context levels, it may include itself if nesting is allowed.
int[] |
Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.
|
static |
Returns short context name.
string |
Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.
|
abstract |
Returns the most relevant URL for this context.
moodle_url |
Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.
core\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().
|
staticprotected |
Utility method for context creation.
int | $contextlevel | |
int | $instanceid | |
string | $parentpath |
stdClass | context record |
|
static |
Get a context instance as an object, from a given context id.
int | $id | context id |
int | $strictness | IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; MUST_EXIST means throw exception if no record found |
context|bool | the context object or false if not found |
core\context::is_child_of | ( | context | $possibleparent, |
bool | $includeself ) |
Determine if the current context is a child of the possible parent.
context | $possibleparent | |
bool | $includeself | Whether to check the current context |
bool |
core\context::is_locked | ( | ) |
Whether the current context is locked.
bool |
core\context::is_parent_of | ( | context | $possiblechild, |
bool | $includeself ) |
Determine if the current context is a parent of the possible child.
context | $possiblechild | |
bool | $includeself | Whether to check the current context |
bool |
|
staticprotected |
Preloads context information from db record and strips the cached info.
stdClass | $rec |
context|null | (modifies $rec) |
Reimplemented in core\context_helper.
core\context::reload_if_dirty | ( | ) |
Reset all cached permissions and definitions if the necessary.
void |
|
staticprotected |
Resets the cache to remove all data.
Reimplemented in core\context_helper.
core\context::reset_paths | ( | $rebuild = true | ) |
Remove all context path info and optionally rebuild it.
bool | $rebuild |
void |
core\context::set_locked | ( | bool | $locked | ) |
Set whether this context has been locked or not.
bool | $locked |
$this |
Reimplemented in core\context\system.
core\context::update_moved | ( | context | $newparent | ) |
Update context info after moving context in the tree structure.
context | $newparent |
void |
|
protected |
The context level Can be accessed publicly through $context->contextlevel One of CONTEXT_* e.g.
CONTEXT_COURSE, CONTEXT_MODULE
|
protected |
Id of the item this context is related to e.g.
COURSE_CONTEXT => course.id Can be accessed publicly through $context->instanceid
|
protected |
Whether this context is locked or not.
Can be accessed publicly through $context->locked.
|
protected |
Default sorting of capabilities in {.