Moodle APIs  3.8
Moodle 3.8.6 (Build: 20201109)
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Static Protected Member Functions | Static Protected Attributes | List of all members
course_modinfo Class Reference

Public Member Functions

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

Static Public Member Functions

static build_course_cache ($course)
 Builds and stores in MUC object containing information about course modules and sections together with cached fields from table course. More...
 
static build_section_cache ($courseid)
 This method can not be used anymore. More...
 
static clear_instance_cache ($courseorid=null)
 Clears the cache used in course_modinfo::instance() More...
 
static instance ($courseorid, $userid=0)
 Returns the instance of course_modinfo for the specified course and specified user. More...
 

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. More...
 

Static Protected Member Functions

static build_course_section_cache ($course)
 Builds a list of information about sections on a course to be stored in the course cache. More...
 
static get_course_cache_lock ($courseid)
 Gets a lock for rebuilding the cache of a single course. More...
 
static inner_build_course_cache ($course, core\lock\lock $lock)
 Called to build course cache when there is already a lock obtained. More...
 

Static Protected Attributes

static float[] $cacheaccessed = array()
 Timestamps (microtime) when the course_modinfo instances were last accessed. More...
 
static course_modinfo[] $instancecache = array()
 Static cache for generated course_modinfo instances. More...
 

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 (   $course)
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.
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 (   $course)
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
Return values
arrayInformation about sections, indexed by section number (not id)

◆ 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)
static

Clears the cache used in course_modinfo::instance()

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

Parameters
null | int | stdClass$courseoridif specified removes only cached value for this course

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

static course_modinfo::get_course_cache_lock (   $courseid)
staticprotected

Gets a lock for rebuilding the cache of a single course.

Caller must release the returned lock.

This is used to ensure that the cache rebuild doesn't happen multiple times in parallel. This function will wait up to 1 minute for the lock to be obtained. If the lock cannot be obtained, it throws an exception.

Parameters
int$courseidCourse id
Return values
core

◆ 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
$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_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.

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

◆ 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

◆ inner_build_course_cache()

static course_modinfo::inner_build_course_cache (   $course,
core\lock\lock  $lock 
)
staticprotected

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

Parameters
stdClass$courseobject from DB table course
core\lock\lock$lockLock object - not actually used, just there to indicate you have a lock
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

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

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