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

static context_system
system(int $strictness = MUST_EXIST)

Retrieves the system context.

static context_course
course(int $courseid, int $strictness = MUST_EXIST)

Retrieves the course context for a given course ID.

static context_coursecat
coursecat(int $categoryid, int $strictness = MUST_EXIST)

Retrieves the course category context for a given category ID.

static context_module
module(int $cmid, int $strictness = MUST_EXIST)

Retrieves the course module context for a given module ID.

static context_user
user(int $userid, int $strictness = MUST_EXIST)

Retrieves the user context for a given user ID.

static context_block
block(int $blockid, int $strictness = MUST_EXIST)

Retrieves the block context for a given block instance ID.

static object
instance_by_id(int $id, int $strictness = MUST_EXIST)

Retrieves a context instance by its ID.

Details

at line 53
static context_system system(int $strictness = MUST_EXIST)

Retrieves the system context.

Parameters

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

context_system

System context instance

at line 69
static context_course course(int $courseid, int $strictness = MUST_EXIST)

Retrieves the course context for a given course ID.

Parameters

int $courseid

Course ID

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

context_course

Course context instance

at line 85
static context_coursecat coursecat(int $categoryid, int $strictness = MUST_EXIST)

Retrieves the course category context for a given category ID.

Parameters

int $categoryid

Category ID

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

context_coursecat

Category context instance

at line 101
static context_module module(int $cmid, int $strictness = MUST_EXIST)

Retrieves the course module context for a given module ID.

Parameters

int $cmid

Course Module ID (cmid)

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

context_module

Module context instance

at line 117
static context_user user(int $userid, int $strictness = MUST_EXIST)

Retrieves the user context for a given user ID.

Parameters

int $userid

User ID

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

context_user

User context instance

at line 133
static context_block block(int $blockid, int $strictness = MUST_EXIST)

Retrieves the block context for a given block instance ID.

Parameters

int $blockid

Block instance ID

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

context_block

Block context instance

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.

Parameters

int $id

Context ID

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

object

The resolved context instance