class course_support

internal  
 

Utility functions for Moodle courses.

Methods

static course|null
get_course(int|null $courseid, int $strictness = IGNORE_MISSING)

Retrieves a course entity by its ID.

static array
get_courses_options()

Retrieves visible course options as an options list [courseid => label].

static array
get_course_with_contextid_options()

Retrieves course options indexed by context ID.

static array
get_cms_by_section(int $courseid, int $sectionnumber)

Retrieves visible course modules within a specific section.

static array
get_courses_from_categoryid(int $categoryid, bool $includesub = true)

Retrieves courses belonging to a category.

static course|null
get_course_by_contextid(int $contextid, int $strictness = IGNORE_MISSING)

Retrieves a course entity by its context ID.

static context_course|null
get_course_context(int $courseid)

Retrieves the course context for a given course ID.

static moodle_url
get_course_url(int $courseid, int|null $sectionid = null, string|null $anchor = null)

Retrieves the URL to view a course.

static bool
is_course_visible(int $courseid)

Checks if a course is currently visible.

static array
get_course_modules(int $courseid, string|null $modname = null)

Retrieves all course modules for a specific course.

static completion_info|null
get_course_completion_info(int $courseid)

Retrieves completion information for a specific course.

static int
get_enrolled_users_count(int $courseid, bool|null $activeonly = true, int|null $groupid = null)

Retrieves the count of enrolled users in a course.

static category|null
get_course_category(int $courseid)

Retrieves the category entity for a specific course.

static bool
is_user_enrolled(int $courseid, int $userid)

Checks if a specific user is enrolled in a course.

static string|null
get_course_format(int $courseid)

Retrieves the course format identifier (e.g., 'topics', 'weeks', 'social').

static moodle_url
course_group_viewurl()

Retrieves the URL for the course group view tool.

Details

at line 48
static course|null get_course(int|null $courseid, int $strictness = IGNORE_MISSING)

Retrieves a course entity by its ID.

Parameters

int|null $courseid

Course ID

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

course|null

Course entity or null if not found

at line 75
static array get_courses_options()

Retrieves visible course options as an options list [courseid => label].

Uses a single query with JOIN on categories to reduce overhead and avoid multiple calls to core_course_category::get().

Return Value

array

Map of course ID to formatted label

at line 105
static array get_course_with_contextid_options()

Retrieves course options indexed by context ID.

Return Value

array

Map of context ID to course label

at line 127
static array get_cms_by_section(int $courseid, int $sectionnumber)

Retrieves visible course modules within a specific section.

Parameters

int $courseid

Course ID

int $sectionnumber

Section number

Return Value

array

List of visible course modules in the section

at line 180
static array get_courses_from_categoryid(int $categoryid, bool $includesub = true)

Retrieves courses belonging to a category.

Parameters

int $categoryid

Category ID

bool $includesub

whether to include courses from subcategories

Return Value

array

List of course records

at line 218
static course|null get_course_by_contextid(int $contextid, int $strictness = IGNORE_MISSING)

Retrieves a course entity by its context ID.

This is useful when you have a context ID and need to find the associated course.

Parameters

int $contextid

Context ID

int $strictness

Moodle strictness constant (IGNORE_MISSING, IGNORE_MULTIPLE, MUST_EXIST)

Return Value

course|null

Course entity or null on failure

at line 257
static context_course|null get_course_context(int $courseid)

Retrieves the course context for a given course ID.

Parameters

int $courseid

Course ID

Return Value

context_course|null

Course context or null on failure

at line 281
static moodle_url get_course_url(int $courseid, int|null $sectionid = null, string|null $anchor = null)

Retrieves the URL to view a course.

Parameters

int $courseid

Course ID

int|null $sectionid

Optional section ID to jump to

string|null $anchor

Optional URL anchor (e.g., 'module-123')

Return Value

moodle_url

Course view URL

at line 305
static bool is_course_visible(int $courseid)

Checks if a course is currently visible.

Parameters

int $courseid

Course ID

Return Value

bool

True if visible, false otherwise

at line 324
static array get_course_modules(int $courseid, string|null $modname = null)

Retrieves all course modules for a specific course.

Parameters

int $courseid

Course ID

string|null $modname

Optional module name filter by module name (e.g., 'forum', 'quiz')

Return Value

array

List of course module records

at line 357
static completion_info|null get_course_completion_info(int $courseid)

Retrieves completion information for a specific course.

Parameters

int $courseid

Course ID

Return Value

completion_info|null

Completion info object or null on failure

at line 383
static int get_enrolled_users_count(int $courseid, bool|null $activeonly = true, int|null $groupid = null)

Retrieves the count of enrolled users in a course.

Parameters

int $courseid

Course ID

bool|null $activeonly

Whether to count only active enrolments

int|null $groupid

Optional group ID filter

Return Value

int

Number of enrolled users

at line 433
static category|null get_course_category(int $courseid)

Retrieves the category entity for a specific course.

Parameters

int $courseid

Course ID

Return Value

category|null

Category entity or null on failure

at line 462
static bool is_user_enrolled(int $courseid, int $userid)

Checks if a specific user is enrolled in a course.

Parameters

int $courseid

Course ID

int $userid

User ID

Return Value

bool

True if enrolled, false otherwise

at line 486
static string|null get_course_format(int $courseid)

Retrieves the course format identifier (e.g., 'topics', 'weeks', 'social').

Parameters

int $courseid

Course ID

Return Value

string|null

Course format or null on failure

at line 502
static moodle_url course_group_viewurl()

Retrieves the URL for the course group view tool.

Return Value

moodle_url

the group view tool URL