Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

(no description)

File Size: 484 lines (16 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: context_helper  - X-Ref

Context maintenance and helper methods.

This is "extends context" is a bloody hack that tires to work around the deficiencies
in the "protected" keyword in PHP, this helps us to hide all the internals of context
level implementation from the rest of code, the code completion returns what developers need.

Thank you Tim Hunt for helping me with this nasty trick.

reset_levels()   X-Ref
Reset internal context levels array.


init_levels()   X-Ref
Initialise context levels, call before using self::$alllevels.


parse_external_level($extlevel)   X-Ref
Converts legacy context_* class name to new class name.

NOTE: this is needed for external API which uses short context names.
param: int|string $extlevel
return: string|null context class name or null if not found

resolve_behat_reference(string $level, string $reference)   X-Ref
Resolve reference to context used in behat feature files.

param: string $level
param: string $reference
return: context|null

get_class_for_level(int $contextlevel)   X-Ref
Returns a class name of the context level class

param: int $contextlevel (CONTEXT_SYSTEM, etc.)
return: string class name of the context class

get_all_levels()   X-Ref
Returns a list of all context levels

return: array int=>string (level=>level class name)

get_child_levels(int $parentlevel)   X-Ref
Get list of possible child levels for given level.

param: int $parentlevel
return: int[] list of context levels that my be children of given context level.

get_compatible_levels(string $archetype)   X-Ref
Returns context levels that compatible with role archetype assignments.

param: string $archetype
return: array

cleanup_instances()   X-Ref
Remove stale contexts that belonged to deleted instances.
Ideally all code should cleanup contexts properly, unfortunately accidents happen...

return: void

create_instances($contextlevel = null, $buildpaths = true)   X-Ref
Create all context instances at the given level and above.

param: int $contextlevel null means all levels
param: bool $buildpaths
return: void

build_all_paths($force = false)   X-Ref
Rebuild paths and depths in all context levels.

param: bool $force false means add missing only
return: void

reset_caches()   X-Ref
Resets the cache to remove all data.


get_preload_record_columns($tablealias)   X-Ref
Returns all fields necessary for context preloading from user $rec.

This helps with performance when dealing with hundreds of contexts.

param: string $tablealias context table alias in the query
return: array (table.column=>alias, ...)

get_preload_record_columns_sql($tablealias)   X-Ref
Returns all fields necessary for context preloading from user $rec.

This helps with performance when dealing with hundreds of contexts.

param: string $tablealias context table alias in the query
return: string

preload_from_record(stdClass $rec)   X-Ref
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().

param: stdClass $rec
return: void This is intentional. See MDL-37115. You will need to get the context

preload_contexts_by_id(array $contextids)   X-Ref
Preload a set of contexts using their contextid.

param: array $contextids

preload_course($courseid)   X-Ref
Preload all contexts instances from course.

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

param: int $courseid

delete_instance($contextlevel, $instanceid)   X-Ref
Delete context instance

param: int $contextlevel
param: int $instanceid
return: void

get_level_name($contextlevel)   X-Ref
Returns the name of specified context level

param: int $contextlevel
return: string name of the context level

get_navigation_filter_context(?context $context)   X-Ref
Gets the current context to be used for navigation tree filtering.

param: context|null $context The current context to be checked against.
return: context|null the context that navigation tree filtering should use.