Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
core\context_helper Class Reference
Inheritance diagram for core\context_helper:
core\context

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 build_all_paths ($force=false)
 Rebuild paths and depths in all context levels.
 
static cleanup_instances ()
 Remove stale contexts that belonged to deleted instances.
 
static create_instances ($contextlevel=null, $buildpaths=true)
 Create all context instances at the given level and above.
 
static delete_instance ($contextlevel, $instanceid)
 Delete context instance.
 
static get_all_levels ()
 Returns a list of all context levels.
 
static get_child_levels (int $parentlevel)
 Get list of possible child levels for given level.
 
static get_class_for_level (int $contextlevel)
 Returns a class name of the context level class.
 
static get_compatible_levels (string $archetype)
 Returns context levels that compatible with role archetype assignments.
 
static get_level_name ($contextlevel)
 Returns the name of specified context level.
 
static get_navigation_filter_context (?context $context)
 Gets the current context to be used for navigation tree filtering.
 
static get_possible_parent_levels ()
 Returns list of all possible parent context levels, it may include itself if nesting is allowed.
 
static get_preload_record_columns ($tablealias)
 Returns all fields necessary for context preloading from user $rec.
 
static get_preload_record_columns_sql ($tablealias)
 Returns all fields necessary for context preloading from user $rec.
 
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.
 
static parse_external_level ($extlevel)
 Converts legacy context_* class name to new class name.
 
static preload_contexts_by_id (array $contextids)
 Preload a set of contexts using their contextid.
 
static preload_course ($courseid)
 Preload all contexts instances from course.
 
static preload_from_record (stdClass $rec)
 Preloads context cache with information from db record and strips the cached info.
 
static reset_caches ()
 Resets the cache to remove all data.
 
static reset_levels ()
 Reset internal context levels array.
 
static resolve_behat_reference (string $level, string $reference)
 Resolve reference to context used in behat feature files.
 

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.
 

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.
 

Detailed Description

License
https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Since
Moodle 4.2

Member Function Documentation

◆ __get()

core\context::__get ( $name)
inherited

Magic method getter, redirects to read only values.

Parameters
string$name
Return values
mixed

◆ __isset()

core\context::__isset ( $name)
inherited

Full support for isset on our magic read only properties.

Parameters
string$name
Return values
bool

◆ __set()

core\context::__set ( $name,
$value )
inherited

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

Parameters
string$name
mixed$value

◆ __unset()

core\context::__unset ( $name)
inherited

All properties are read only, sorry.

Parameters
string$name

◆ build_all_paths()

static core\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()

static core\context::build_paths ( $force)
staticprotectedinherited

Rebuild context paths and depths at context level.

Parameters
bool$force
Return values
void

Reimplemented in core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.

◆ cache_add()

static core\context::cache_add ( context $context)
staticprotectedinherited

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 core\context::cache_get ( $contextlevel,
$instance )
staticprotectedinherited

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 core\context::cache_get_by_id ( $id)
staticprotectedinherited

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 core\context::cache_remove ( context $context)
staticprotectedinherited

Removes a context from the cache.

Parameters
context$contextContext object to remove
Return values
void

◆ cleanup_instances()

static core\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

◆ create_instance_from_record()

static core\context::create_instance_from_record ( stdClass $record)
staticprotectedinherited

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

Parameters
stdClass$record
Return values
contextinstance

◆ create_instances()

static core\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()

static core\context::create_level_instances ( )
staticprotectedinherited

Create missing context instances at given level.

Return values
void

Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.

◆ delete_instance()

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

Delete context instance.

Parameters
int$contextlevel
int$instanceid
Return values
void

◆ get_all_levels()

static core\context_helper::get_all_levels ( )
static

Returns a list of all context levels.

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

◆ get_behat_reference_columns()

static core\context::get_behat_reference_columns ( )
staticprotectedinherited

Returns list of columns that can be used from behat to look up context by reference.

Return values
arraylist of column names from instance table

Reimplemented in core\context\course, core\context\coursecat, core\context\module, and core\context\user.

◆ get_capabilities()

core\context::get_capabilities ( string $sort = self::DEFAULT_CAPABILITY_SORT)
abstractinherited

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 core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.

◆ get_child_contexts()

core\context::get_child_contexts ( )
inherited

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 core\context\block, core\context\coursecat, and core\context\system.

◆ get_child_levels()

static core\context_helper::get_child_levels ( int $parentlevel)
static

Get list of possible child levels for given level.

Since
Moodle 4.2
Parameters
int$parentlevel
Return values
int[]list of context levels that my be children of given context level.

◆ get_class_for_level()

static core\context_helper::get_class_for_level ( int $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
Exceptions
coding_exceptionif level does not exist

◆ get_cleanup_sql()

static core\context::get_cleanup_sql ( )
staticprotectedinherited

Returns sql necessary for purging of stale context instances.

Return values
stringcleanup SQL

Reimplemented in core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.

◆ get_compatible_levels()

static core\context_helper::get_compatible_levels ( string $archetype)
static

Returns context levels that compatible with role archetype assignments.

Since
Moodle 4.2
Parameters
string$archetype
Return values
array

◆ get_compatible_role_archetypes()

static core\context::get_compatible_role_archetypes ( )
staticprotectedinherited

Returns list of all role archetypes that are compatible with role assignments in context level.

Since
Moodle 4.2
Return values
string[]

Reimplemented in core\context\course, core\context\coursecat, core\context\module, and core\context\system.

◆ get_context_name()

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

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 core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.

◆ get_course_context()

core\context::get_course_context ( $strict = true)
inherited

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\course|falsecontext of the enclosing course, null if not found or exception

Reimplemented in core\context\block, core\context\course, and core\context\module.

◆ get_instance_table()

static core\context::get_instance_table ( )
staticprotectedinherited

Returns context instance database name.

Return values
string|nulltable name for all levels except system.

Reimplemented in core\context\course, core\context\coursecat, core\context\module, and core\context\user.

◆ get_level_name()

static core\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_navigation_filter_context()

static core\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_parent_context()

core\context::get_parent_context ( )
inherited

Returns parent context.

Return values
context|false

◆ get_parent_context_ids()

core\context::get_parent_context_ids ( $includeself = false)
inherited

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

core\context::get_parent_context_paths ( $includeself = false)
inherited

Returns parent context paths of this context.

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

◆ get_parent_contexts()

core\context::get_parent_contexts ( $includeself = false)
inherited

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

static core\context::get_possible_parent_levels ( )
staticinherited

Returns list of all possible parent context levels, it may include itself if nesting is allowed.

Since
Moodle 4.2
Return values
int[]

Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.

◆ get_preload_record_columns()

static core\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 core\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_short_name()

static core\context::get_short_name ( )
staticinherited

Returns short context name.

Since
Moodle 4.2
Return values
string

Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.

◆ get_url()

core\context::get_url ( )
abstractinherited

Returns the most relevant URL for this context.

Return values
moodle_url

Reimplemented in core\context\block, core\context\course, core\context\coursecat, core\context\module, core\context\system, and core\context\user.

◆ getIterator()

core\context::getIterator ( )
inherited

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

◆ insert_context_record()

static core\context::insert_context_record ( $contextlevel,
$instanceid,
$parentpath )
staticprotectedinherited

Utility method for context creation.

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

◆ instance_by_id()

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

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

core\context::is_child_of ( context $possibleparent,
bool $includeself )
inherited

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

core\context::is_locked ( )
inherited

Whether the current context is locked.

Return values
bool

◆ is_parent_of()

core\context::is_parent_of ( context $possiblechild,
bool $includeself )
inherited

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

◆ parse_external_level()

static core\context_helper::parse_external_level ( $extlevel)
static

Converts legacy context_* class name to new class name.

NOTE: this is needed for external API which uses short context names.

Since
Moodle 4.2
Parameters
int | string$extlevel
Return values
string|nullcontext class name or null if not found

◆ preload_contexts_by_id()

static core\context_helper::preload_contexts_by_id ( array $contextids)
static

Preload a set of contexts using their contextid.

Parameters
array$contextids

◆ preload_course()

static core\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()

static core\context_helper::preload_from_record ( stdClass $rec)
static

Preloads context cache with 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
voidThis is intentional. See MDL-37115. You will need to get the context in the normal way, but it is now cached, so that will be fast.

Reimplemented from core\context.

◆ reload_if_dirty()

core\context::reload_if_dirty ( )
inherited

Reset all cached permissions and definitions if the necessary.

Return values
void

◆ reset_caches()

static core\context_helper::reset_caches ( )
static

Resets the cache to remove all data.

Reimplemented from core\context.

◆ reset_paths()

core\context::reset_paths ( $rebuild = true)
inherited

Remove all context path info and optionally rebuild it.

Parameters
bool$rebuild
Return values
void

◆ resolve_behat_reference()

static core\context_helper::resolve_behat_reference ( string $level,
string $reference )
static

Resolve reference to context used in behat feature files.

Parameters
string$level
string$reference
Return values
context|null

◆ set_locked()

core\context::set_locked ( bool $locked)
inherited

Set whether this context has been locked or not.

Parameters
bool$locked
Return values
$this

Reimplemented in core\context\system.

◆ update_moved()

core\context::update_moved ( context $newparent)
inherited

Update context info after moving context in the tree structure.

Parameters
context$newparent
Return values
void

Member Data Documentation

◆ $_contextlevel

int core\context::$_contextlevel
protectedinherited

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

CONTEXT_COURSE, CONTEXT_MODULE

◆ $_instanceid

int core\context::$_instanceid
protectedinherited

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

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

◆ $_locked

int core\context::$_locked
protectedinherited

Whether this context is locked or not.

Can be accessed publicly through $context->locked.

◆ DEFAULT_CAPABILITY_SORT

string const core\context::DEFAULT_CAPABILITY_SORT = 'contextlevel, component, name'
protectedinherited

Default sorting of capabilities in {.

See also
get_capabilities}

The documentation for this class was generated from the following file: