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: 1093 lines (37 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: context  - X-Ref

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

get_short_name()   X-Ref
Returns short context name.

return: string

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


cache_add(context $context)   X-Ref
Adds a context to the cache. If the cache is full, discards a batch of
older entries.

param: context $context New context to add
return: void

cache_remove(context $context)   X-Ref
Removes a context from the cache.

param: context $context Context object to remove
return: void

cache_get($contextlevel, $instance)   X-Ref
Gets a context from the cache.

param: int $contextlevel Context level
param: int $instance Instance ID
return: context|bool Context or false if not in cache

cache_get_by_id($id)   X-Ref
Gets a context from the cache based on its id.

param: int $id Context ID
return: context|bool Context or false if not in cache

preload_from_record(stdClass $rec)   X-Ref
Preloads context information from db record and strips the cached info.

param: stdClass $rec
return: context|null (modifies $rec)

__set($name, $value)   X-Ref
Magic setter method, we do not want anybody to modify properties from the outside

param: string $name
param: mixed $value

__get($name)   X-Ref
Magic method getter, redirects to read only values.

param: string $name
return: mixed

__isset($name)   X-Ref
Full support for isset on our magic read only properties.

param: string $name
return: bool

__unset($name)   X-Ref
All properties are read only, sorry.

param: string $name

getIterator()   X-Ref
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().

__construct(stdClass $record)   X-Ref
Constructor is protected so that devs are forced to
use context_xxx::instance() or context::instance_by_id().

param: stdClass $record

create_instance_from_record(stdClass $record)   X-Ref
This function is also used to work around 'protected' keyword problems in context_helper.

param: stdClass $record
return: context instance

merge_context_temp_table()   X-Ref
Copy prepared new contexts from temp table to context table,
we do this in db specific way for perf reasons only.


instance_by_id($id, $strictness = MUST_EXIST)   X-Ref
Get a context instance as an object, from a given context id.

param: int $id context id
param: int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found;
return: context|bool the context object or false if not found

update_moved(context $newparent)   X-Ref
Update context info after moving context in the tree structure.

param: context $newparent
return: void

set_locked(bool $locked)   X-Ref
Set whether this context has been locked or not.

param: bool    $locked
return: $this

reset_paths($rebuild = true)   X-Ref
Remove all context path info and optionally rebuild it.

param: bool $rebuild
return: void

delete_content()   X-Ref
Delete all data linked to content, do not delete the context record itself


delete_capabilities()   X-Ref
Unassign all capabilities from a context.


delete()   X-Ref
Delete the context content and the context record itself


insert_context_record($contextlevel, $instanceid, $parentpath)   X-Ref
Utility method for context creation

param: int $contextlevel
param: int $instanceid
param: string $parentpath
return: stdClass context record

get_context_name($withprefix = true, $short = false, $escape = true)   X-Ref
Returns human readable context identifier.

param: boolean $withprefix whether to prefix the name of the context with the
param: boolean $short whether to use the short name of the thing. Only applies
param: boolean $escape Whether the returned name of the thing is to be
return: string the human readable context name.

is_locked()   X-Ref
Whether the current context is locked.

return: bool

get_instance_table()   X-Ref
Returns context instance database name.

return: string|null table name for all levels except system.

get_behat_reference_columns()   X-Ref
Returns list of columns that can be used from behat
to look up context by reference.

return: array list of column names from instance table

get_compatible_role_archetypes()   X-Ref
Returns list of all role archetypes that are compatible
with role assignments in context level.

return: string[]

get_possible_parent_levels()   X-Ref
Returns list of all possible parent context levels,
it may include itself if nesting is allowed.

return: int[]

get_child_contexts()   X-Ref
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: array Array of child records

is_parent_of(context $possiblechild, bool $includeself)   X-Ref
Determine if the current context is a parent of the possible child.

param: context $possiblechild
param: bool $includeself Whether to check the current context
return: bool

get_parent_contexts($includeself = false)   X-Ref
Returns parent contexts of this context in reversed order, i.e. parent first,
then grand parent, etc.

param: bool $includeself true means include self too
return: array of context instances

is_child_of(context $possibleparent, bool $includeself)   X-Ref
Determine if the current context is a child of the possible parent.

param: context $possibleparent
param: bool $includeself Whether to check the current context
return: bool

get_parent_context_ids($includeself = false)   X-Ref
Returns parent context ids of this context in reversed order, i.e. parent first,
then grand parent, etc.

param: bool $includeself true means include self too
return: array of context ids

get_parent_context_paths($includeself = false)   X-Ref
Returns parent context paths of this context.

param: bool $includeself true means include self too
return: array of context paths

get_parent_context()   X-Ref
Returns parent context

return: context|false

get_course_context($strict = true)   X-Ref
Is this context part of any course? If yes return course context.

param: bool $strict true means throw exception if not found, false means return false if not found
return: context\course|false context of the enclosing course, null if not found or exception

get_cleanup_sql()   X-Ref
Returns sql necessary for purging of stale context instances.

return: string cleanup SQL

build_paths($force)   X-Ref
Rebuild context paths and depths at context level.

param: bool $force
return: void

create_level_instances()   X-Ref
Create missing context instances at given level

return: void

reload_if_dirty()   X-Ref
Reset all cached permissions and definitions if the necessary.

return: void

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