Moodle PHP Documentation 4.4
Moodle 4.4.1 (Build: 20240610) (db07c09afc5)
course_modinfo Class Reference

Public Member Functions

 __construct ($course, $userid)
 Constructs based on course.
 
 __empty ($name)
 Magic method for function empty()
 
 __get ($name)
 Magic method getter.
 
 __isset ($name)
 Magic method for function isset()
 
 __set ($name, $value)
 Magic method setter.
 
 get_cm ($cmid)
 Obtains a single course-module object (for a course-module that is on this course).
 
 get_cms ()
 
 get_course ()
 Returns course object that was used in the first get_fast_modinfo() call.
 
 get_course_id ()
 
 get_groups ($groupingid=0)
 Returns groups that the current user belongs to on the course.
 
 get_instances ()
 Obtains all module instances on this course.
 
 get_instances_of ($modname)
 Obtains all instances of a particular module on this course.
 
 get_listed_section_info_all ()
 Gets all sections listed in course page as array from section number => data about section.
 
 get_section_info ($sectionnumber, $strictness=IGNORE_MISSING)
 Gets data about specific numbered section.
 
 get_section_info_all ()
 Gets all sections as array from section number => data about section.
 
 get_section_info_by_component (string $component, int $itemid, int $strictness=IGNORE_MISSING)
 Gets data about specific delegated section.
 
 get_section_info_by_id (int $sectionid, int $strictness=IGNORE_MISSING)
 Gets data about specific section ID.
 
 get_sections ()
 
 get_used_module_names ($plural=false)
 Returns array of localised human-readable module names used in this course.
 
 get_user_id ()
 
 has_delegated_sections ()
 Check if the course has delegated sections.
 

Static Public Member Functions

static build_course_cache (\stdClass $course, bool $partialrebuild=false)
 Builds and stores in MUC object containing information about course modules and sections together with cached fields from table course.
 
static build_section_cache ($courseid)
 This method can not be used anymore.
 
static clear_instance_cache ($courseorid=null, int $newcacherev=0)
 Clears the cache used in course_modinfo\instance()
 
static get_array_of_activities (stdClass $course, bool $usecache=false)
 For a given course, returns an array of course activity objects.
 
static instance ($courseorid, $userid=0)
 Returns the instance of course_modinfo for the specified course and specified user.
 
static purge_course_cache (int $courseid)
 Purge the cache of a given course.
 
static purge_course_caches (array $courseids=[])
 Purges the coursemodinfo caches stored in MUC.
 
static purge_course_module_cache (int $courseid, int $cmid)
 Purge the cache of a course module.
 
static purge_course_modules_cache (int $courseid, array $cmids)
 Purge the cache of multiple course modules.
 
static purge_course_section_cache_by_id (int $courseid, int $sectionid)
 Purge the cache of a course section by its id.
 
static purge_course_section_cache_by_number (int $courseid, int $sectionno)
 Purge the cache of a course section by its number.
 

Public Attributes

int const COURSE_CACHE_LOCK_EXPIRY = 180
 Maximum time the course cache building lock can be held.
 
int const COURSE_CACHE_LOCK_WAIT = 60
 Time to wait for the course cache building lock before throwing an exception.
 

Static Public Attributes

static array $cachedfields
 List of fields from DB table 'course' that are cached in MUC and are always present in course_modinfo\$course.
 

Static Protected Member Functions

static build_course_section_cache (\stdClass $course, bool $usecache=false)
 Builds a list of information about sections on a course to be stored in the course cache.
 
static inner_build_course_cache (\stdClass $course, bool $partialrebuild=false)
 Called to build course cache when there is already a lock obtained.
 

Static Protected Attributes

static float[] $cacheaccessed = array()
 Timestamps (microtime) when the course_modinfo instances were last accessed.
 
static course_modinfo[] $instancecache = array()
 Static cache for generated course_modinfo instances.
 
static int[] $mincacherevs = []
 Store a list of known course cacherev values.
 

Constructor & Destructor Documentation

◆ __construct()

course_modinfo::__construct ( $course,
$userid )

Constructs based on course.

Note: This constructor should not usually be called directly. Use get_fast_modinfo($course) instead as this maintains a cache.

Parameters
stdClass$coursecourse object, only property id is required.
int$useridUser ID
Exceptions
moodle_exceptionif course is not found

Member Function Documentation

◆ __empty()

course_modinfo::__empty ( $name)

Magic method for function empty()

Parameters
string$name
Return values
bool

◆ __get()

course_modinfo::__get ( $name)

Magic method getter.

Parameters
string$name
Return values
mixed

◆ __isset()

course_modinfo::__isset ( $name)

Magic method for function isset()

Parameters
string$name
Return values
bool

◆ __set()

course_modinfo::__set ( $name,
$value )

Magic method setter.

Will display the developer warning when trying to set/overwrite existing property.

Parameters
string$name
mixed$value

◆ build_course_cache()

static course_modinfo::build_course_cache ( \stdClass $course,
bool $partialrebuild = false )
static

Builds and stores in MUC object containing information about course modules and sections together with cached fields from table course.

Parameters
stdClass$courseobject from DB table course. Must have property 'id' but preferably should have all cached fields.
boolean$partialrebuildIndicate if it's partial course cache rebuild or not
Return values
stdClassobject with all cached keys of the course plus fields modinfo and sectioncache. The same object is stored in MUC
Exceptions
moodle_exceptionif course is not found (if $course object misses some of the necessary fields it is re-requested from database)

◆ build_course_section_cache()

static course_modinfo::build_course_section_cache ( \stdClass $course,
bool $usecache = false )
staticprotected

Builds a list of information about sections on a course to be stored in the course cache.

(Does not include information that is already cached in some other way.)

Parameters
stdClass$courseCourse object (must contain fields id and cacherev)
boolean$usecacheuse cached section info if exists, use true for partial course rebuild
Return values
arrayInformation about sections, indexed by section id (not number)

◆ build_section_cache()

static course_modinfo::build_section_cache ( $courseid)
static

This method can not be used anymore.

See also
course_modinfo\build_course_cache()
Deprecated
since 2.6

◆ clear_instance_cache()

static course_modinfo::clear_instance_cache ( $courseorid = null,
int $newcacherev = 0 )
static

Clears the cache used in course_modinfo\instance()

Used in get_fast_modinfo() when called with argument $reset = true and in rebuild_course_cache()

If the cacherev for the course is known to have updated (i.e. when doing rebuild_course_cache), it should be specified here.

Parameters
null | int | stdClass$courseoridif specified removes only cached value for this course
int$newcacherevIf specified, the known cache rev for this course id will be updated

◆ get_array_of_activities()

static course_modinfo::get_array_of_activities ( stdClass $course,
bool $usecache = false )
static

For a given course, returns an array of course activity objects.

Parameters
stdClass$courseCourse object
bool$usecacheget activities from cache if modinfo exists when $usecache is true
Return values
arraylist of activities

◆ get_cm()

course_modinfo::get_cm ( $cmid)

Obtains a single course-module object (for a course-module that is on this course).

Parameters
int$cmidCourse-module ID
Return values
cm_infoInformation about that course-module
Exceptions
moodle_exceptionIf the course-module does not exist

◆ get_cms()

course_modinfo::get_cms ( )
Return values
cm_info[]Array from course-module instance to cm_info object within this course, in order of appearance

◆ get_course()

course_modinfo::get_course ( )

Returns course object that was used in the first get_fast_modinfo() call.

It may not contain all fields from DB table {course} but always has at least the following: id,shortname,fullname,format,enablecompletion,groupmode,groupmodeforce,cacherev

Return values
stdClass

◆ get_course_id()

course_modinfo::get_course_id ( )
Return values
intCourse ID

◆ get_groups()

course_modinfo::get_groups ( $groupingid = 0)

Returns groups that the current user belongs to on the course.

Note: If not already available, this may make a database query.

Parameters
int$groupingidGrouping ID or 0 (default) for all groups
Return values
int[]Array of int (group id) => int (same group id again); empty array if none

◆ get_instances()

course_modinfo::get_instances ( )

Obtains all module instances on this course.

Return values
cm_info[][]Array from module name => array from instance id => cm_info

◆ get_instances_of()

course_modinfo::get_instances_of ( $modname)

Obtains all instances of a particular module on this course.

Parameters
string$modnameName of module (not full frankenstyle) e.g. 'label'
Return values
cm_info[]Array from instance id => cm_info for modules on this course; empty if none

◆ get_listed_section_info_all()

course_modinfo::get_listed_section_info_all ( )

Gets all sections listed in course page as array from section number => data about section.

The method is similar to get_section_info_all but filtering all sections delegated to components.

Return values
section_info[]Array of section_info objects organised by section number

◆ get_section_info()

course_modinfo::get_section_info ( $sectionnumber,
$strictness = IGNORE_MISSING )

Gets data about specific numbered section.

Parameters
int$sectionnumberNumber (not id) of section
int$strictnessUse MUST_EXIST to throw exception if it doesn't
Return values
?section_infoInformation for numbered section or null if not found

◆ get_section_info_all()

course_modinfo::get_section_info_all ( )

Gets all sections as array from section number => data about section.

The method will return all sections of the course, including the ones delegated to a component.

Return values
section_info[]Array of section_info objects organised by section number

◆ get_section_info_by_component()

course_modinfo::get_section_info_by_component ( string $component,
int $itemid,
int $strictness = IGNORE_MISSING )

Gets data about specific delegated section.

Parameters
string$componentComponent name
int$itemidItem id
int$strictnessUse MUST_EXIST to throw exception if it doesn't
Return values
section_info|nullInformation for numbered section or null if not found

◆ get_section_info_by_id()

course_modinfo::get_section_info_by_id ( int $sectionid,
int $strictness = IGNORE_MISSING )

Gets data about specific section ID.

Parameters
int$sectionidID (not number) of section
int$strictnessUse MUST_EXIST to throw exception if it doesn't
Return values
section_info|nullInformation for numbered section or null if not found

◆ get_sections()

course_modinfo::get_sections ( )
Return values
arrayArray from section number (e.g. 0) to array of course-module IDs in that section; this only includes sections that contain at least one course-module

◆ get_used_module_names()

course_modinfo::get_used_module_names ( $plural = false)

Returns array of localised human-readable module names used in this course.

Parameters
bool$pluralif true returns the plural form of modules names
Return values
array

◆ get_user_id()

course_modinfo::get_user_id ( )
Return values
intUser ID

◆ has_delegated_sections()

course_modinfo::has_delegated_sections ( )

Check if the course has delegated sections.

Return values
bool

◆ inner_build_course_cache()

static course_modinfo::inner_build_course_cache ( \stdClass $course,
bool $partialrebuild = false )
staticprotected

Called to build course cache when there is already a lock obtained.

Parameters
stdClass$courseobject from DB table course
bool$partialrebuildIndicate if it's partial course cache rebuild or not
Return values
stdClassCourse object that has been stored in MUC

◆ instance()

static course_modinfo::instance ( $courseorid,
$userid = 0 )
static

Returns the instance of course_modinfo for the specified course and specified user.

This function uses static cache for the retrieved instances. The cache size is limited by MAX_MODINFO_CACHE_SIZE. If instance is not found in the static cache or it was created for another user or the cacherev validation failed - a new instance is constructed and returned.

Used in get_fast_modinfo()

Parameters
int | stdClass$courseoridobject from DB table 'course' (must have field 'id' and recommended to have field 'cacherev') or just a course id
int$useridUser id to populate 'availble' and 'uservisible' attributes of modules and sections. Set to 0 for current user (default). Set to -1 to avoid calculation of dynamic user-depended data.
Return values
course_modinfo

◆ purge_course_cache()

static course_modinfo::purge_course_cache ( int $courseid)
static

Purge the cache of a given course.

Parameters
int$courseidCourse id

◆ purge_course_caches()

static course_modinfo::purge_course_caches ( array $courseids = [])
static

Purges the coursemodinfo caches stored in MUC.

Parameters
int[]$courseidsArray of course ids to purge the course caches for (or all courses if empty array).

◆ purge_course_module_cache()

static course_modinfo::purge_course_module_cache ( int $courseid,
int $cmid )
static

Purge the cache of a course module.

Parameters
int$courseidCourse id
int$cmidCourse module id

◆ purge_course_modules_cache()

static course_modinfo::purge_course_modules_cache ( int $courseid,
array $cmids )
static

Purge the cache of multiple course modules.

Parameters
int$courseidCourse id
int[]$cmidsList of course module ids
Return values
void

◆ purge_course_section_cache_by_id()

static course_modinfo::purge_course_section_cache_by_id ( int $courseid,
int $sectionid )
static

Purge the cache of a course section by its id.

Parameters
int$courseidThe course to purge cache in
int$sectionidThe section id to purge

◆ purge_course_section_cache_by_number()

static course_modinfo::purge_course_section_cache_by_number ( int $courseid,
int $sectionno )
static

Purge the cache of a course section by its number.

Parameters
int$courseidThe course to purge cache in
int$sectionnoThe section number to purge

Member Data Documentation

◆ $cacheaccessed

float [] course_modinfo::$cacheaccessed = array()
staticprotected

Timestamps (microtime) when the course_modinfo instances were last accessed.

It is used to remove the least recent accessed instances when static cache is full

◆ $cachedfields

array course_modinfo::$cachedfields
static
Initial value:
= array('shortname', 'fullname', 'format',
'enablecompletion', 'groupmode', 'groupmodeforce', 'cacherev')

List of fields from DB table 'course' that are cached in MUC and are always present in course_modinfo\$course.

◆ $instancecache

course_modinfo [] course_modinfo::$instancecache = array()
staticprotected

Static cache for generated course_modinfo instances.

See also
course_modinfo\instance()
course_modinfo\clear_instance_cache()

◆ $mincacherevs

int [] course_modinfo::$mincacherevs = []
staticprotected

Store a list of known course cacherev values.

This is in case people reuse a course object (with an old cacherev value) within the same request when calling things like get_fast_modinfo, after rebuild_course_cache.


The documentation for this class was generated from the following file: