context_support
class context_support
| internal |
Utility to handle Moodle context classes across versions.
Since Moodle 4.2 there are namespaced classes core\context\*.
In 4.1 these classes do not exist and only legacy contexts context_*
are available. Also, cross-version aliases are managed by Moodle core —
we must not create them here.
This helper:
- Prioritises namespaced classes when present (4.2+);
- Safely falls back to legacy classes on 4.1;
- Exposes utility methods to obtain context instances;
- Avoids direct type hints for classes that may not exist in 4.1.
Methods
Retrieves the system context.
Retrieves the course context for a given course ID.
Retrieves the course category context for a given category ID.
Retrieves the course module context for a given module ID.
Retrieves the user context for a given user ID.
Retrieves the block context for a given block instance ID.
Retrieves a context instance by its ID.
Details
at line 53
static context_system
system(int $strictness = MUST_EXIST)
Retrieves the system context.
at line 69
static context_course
course(int $courseid, int $strictness = MUST_EXIST)
Retrieves the course context for a given course ID.
at line 85
static context_coursecat
coursecat(int $categoryid, int $strictness = MUST_EXIST)
Retrieves the course category context for a given category ID.
at line 101
static context_module
module(int $cmid, int $strictness = MUST_EXIST)
Retrieves the course module context for a given module ID.
at line 117
static context_user
user(int $userid, int $strictness = MUST_EXIST)
Retrieves the user context for a given user ID.
at line 133
static context_block
block(int $blockid, int $strictness = MUST_EXIST)
Retrieves the block context for a given block instance ID.
at line 155
static object
instance_by_id(int $id, int $strictness = MUST_EXIST)
Retrieves a context instance by its ID.
In Moodle 4.2+ the canonical entry point is \core\context\base::instance_by_id(). In 4.1 the legacy global \context::instance_by_id() must be used instead.
This helper hides that difference so callers do not need to depend
directly on the global context class.