Moodle APIs 4.3
Moodle 4.3.6 (Build: 20240812)
cm_info Class Reference

Data about a single module on a course. More...

Inheritance diagram for cm_info:

Public Member Functions

 __construct (course_modinfo $modinfo, $notused1, $mod, $notused2)
 Constructor should not be called directly; use get_fast_modinfo().
 
 __call ($name, $arguments)
 Magic method to call functions that are now declared as private but were public in Moodle before 2.6.
 
 __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_activitybadge (?renderer_base $output=null)
 Get the activity badge data associated to this course module (if the module supports it).
 
 get_course ()
 Returns course object that was used in the first get_fast_modinfo() call.
 
 get_course_module_record ($additionalfields=false)
 Returns itself in the form of stdClass.
 
 get_custom_data ()
 Getter method for property $customdata, ensures that dynamic data is retrieved.
 
 get_formatted_content ($options=array())
 Returns the content to display on course/overview page, formatted and passed through filters.
 
 get_formatted_name ($options=array())
 Returns the name to display on course/overview page, formatted and passed through filters.
 
 get_grouping_label ($textclasses='')
 
 get_icon_url ($output=null)
 Fetch the module's icon URL.
 
 get_modinfo ()
 
 get_module_type_name ($plural=false)
 Returns a localised human-readable name of the module type.
 
 get_name ()
 Getter method for property $name, ensures that dynamic data is obtained.
 
 get_section_info ()
 Returns the section this module belongs to.
 
 get_url ()
 Gets the URL to link to for this module.
 
 get_user_visible ()
 Getter method for property $uservisible, ensures that dynamic data is retrieved.
 
 getIterator ()
 Implementation of IteratorAggregate\getIterator(), allows to cycle through properties and use convert_to_array().
 
 has_custom_cmlist_item ()
 Return the module custom cmlist item flag.
 
 has_view ()
 
 is_stealth ()
 Whether this module is available but hidden from course page.
 
 is_user_access_restricted_by_capability ()
 Checks whether mod/...:view capability restricts the current user's access.
 
 is_user_access_restricted_by_conditional_access ()
 Checks whether the module's conditional access settings mean that the user cannot see the activity at all.
 
 is_user_access_restricted_by_group ()
 This method has been deprecated and should not be used.
 
 is_visible_on_course_page ()
 Returns whether this module is visible to the current user on course page.
 
 override_customdata ($name, $value)
 Overrides the value of an element in the customdata array.
 
 set_after_edit_icons ($afterediticons)
 Sets HTML that displays after edit icons on course view page.
 
 set_after_link ($afterlink)
 Sets HTML that displays after link on course view page.
 
 set_available ($available, $showavailability=0, $availableinfo='')
 Sets the 'available' flag and related details.
 
 set_content ($content, $isformatted=false)
 Sets content to display on course view page below link (if present).
 
 set_custom_cmlist_item (bool $customcmlistitem)
 Sets the 'customcmlistitem' flag.
 
 set_extra_classes ($extraclasses)
 Sets extra classes to include in CSS.
 
 set_icon_url (moodle_url $iconurl)
 Sets the external full url that points to the icon being used by the activity.
 
 set_name ($name)
 Changes the name (text of link) for this module instance.
 
 set_no_view_link ()
 Turns off the view link for this module instance.
 
 set_on_click ($onclick)
 Sets value of on-click attribute for JavaScript.
 
 set_user_visible ($uservisible)
 Sets the 'uservisible' flag.
 

Static Public Member Functions

static create ($cm, $userid=0)
 Creates a cm_info object from a database record (also accepts cm_info in which case it is just returned unchanged).
 

Public Attributes

const STATE_BASIC = 0
 State: Only basic data from modinfo cache is available.
 
const STATE_BUILDING_DYNAMIC = 1
 State: In the process of building dynamic data (to avoid recursive calls to obtain_dynamic_data())
 
const STATE_BUILDING_VIEW = 3
 State: In the process of building view data (to avoid recursive calls to obtain_view_data())
 
const STATE_DYNAMIC = 2
 State: Dynamic data is available too.
 
const STATE_VIEW = 4
 State: View data (for course page) is available.
 

Detailed Description

Data about a single module on a course.

This contains most of the fields in the course_modules table, plus additional data when required.

The object can be accessed by core or any plugin (i.e. course format, block, filter, etc.) as get_fast_modinfo($courseorid)->cms[$coursemoduleid] or get_fast_modinfo($courseorid)->instances[$moduletype][$instanceid]

There are three stages when activity module can add/modify data in this object:

Stage 1 - during building the cache. Allows to add to the course cache static user-independent information about the module. Modules should try to include only absolutely necessary information that may be required when displaying course view page. The information is stored in application-level cache and reset when rebuild_course_cache() is called or cache is purged by admin.

Modules can implement callback XXX_get_coursemodule_info() returning instance of object cached_cm_info

Stage 2 - dynamic data. Dynamic data is user-dependent, it is stored in request-level cache. To reset this cache get_fast_modinfo() with $reset argument may be called.

Dynamic data is obtained when any of the following properties/methods is requested:

Modules can implement callback XXX_cm_info_dynamic() and inside this callback they are allowed to use any of the following set methods:

Stage 3 (view data). Also user-dependend data stored in request-level cache. Second stage is created because populating the view data can be expensive as it may access much more Moodle APIs such as filters, user information, output renderers and we don't want to request it until necessary. View data is obtained when any of the following properties/methods is requested:

Modules can implement callback XXX_cm_info_view() and inside this callback they are allowed to use any of the following set methods:

  • cm_info::set_after_edit_icons()
  • cm_info::set_after_link()
  • cm_info::set_content()
  • cm_info::set_extra_classes()

    course_modules table

    course_modules table

    whether course format allows this module to have the "stealth" mode

    visible is stored in this field) - from course_modules table

    course_modules table. Use cm_info::$effectivegroupmode to find the actual group mode that may be forced by course.

    This means that cm_info\$groupmode should be ignored and cm_info\$coursegroupmode be used instead

    course table - as specified for the course containing the module Effective only if cm_info::$coursegroupmodeforce is set

    or VISIBLEGROUPS). This can be different from groupmode set for the module if the groupmode is forced for the course. This value will always be NOGROUPS if module type does not support group mode.

    course_modules table

    grade of this activity, or null if completion does not depend on a grade - from course_modules table

    particular time, 0 if no time set - from course_modules table

    from course_modules table

    addition to anywhere it might display within the activity itself). 0 = do not show on main page, 1 = show on main page.

    course page - from cached data in modinfo field. Deprecated, replaced by ->extraclasses and ->onclick

    table) - from cached data in modinfo field

    data in modinfo field

    = week/topic 1, etc) - from cached data in modinfo field

    course-modules (array from other course-module id to required completion state for that module) - from cached data in modinfo field

    grade item id to object with ->min, ->max fields) - from cached data in modinfo field

    are met - obtained dynamically

    availability which can be displayed to students and/or staff (e.g. 'Available from 3 January 2010') for display on main page - obtained dynamically

    has viewhiddenactivities capability, they can access the course-module even if it is not visible or not available, so this would be true in that case)

Constructor & Destructor Documentation

◆ __construct()

cm_info::__construct ( course_modinfo $modinfo,
$notused1,
$mod,
$notused2 )

Constructor should not be called directly; use get_fast_modinfo().

Parameters
course_modinfo$modinfoParent object
stdClass$notused1Argument not used
stdClass$modModule object from the modinfo field of course table
stdClass$notused2Argument not used

Member Function Documentation

◆ __call()

cm_info::__call ( $name,
$arguments )

Magic method to call functions that are now declared as private but were public in Moodle before 2.6.

These private methods can not be used anymore.

Parameters
string$name
array$arguments
Return values
mixed
Exceptions
coding_exception

◆ __empty()

cm_info::__empty ( $name)

Magic method for function empty()

Parameters
string$name
Return values
bool

◆ __get()

cm_info::__get ( $name)

Magic method getter.

Parameters
string$name
Return values
mixed

◆ __isset()

cm_info::__isset ( $name)

Magic method for function isset()

Parameters
string$name
Return values
bool

◆ __set()

cm_info::__set ( $name,
$value )

Magic method setter.

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

Parameters
string$name
mixed$value

◆ create()

static cm_info::create ( $cm,
$userid = 0 )
static

Creates a cm_info object from a database record (also accepts cm_info in which case it is just returned unchanged).

Parameters
stdClass | cm_info | null | bool$cmStdclass or cm_info (or null or false)
int$useridOptional userid (default to current)
Return values
cm_info|nullObject as cm_info, or null if input was null/false

◆ get_activitybadge()

cm_info::get_activitybadge ( ?renderer_base $output = null)

Get the activity badge data associated to this course module (if the module supports it).

Modules can use this method to provide additional data to be displayed in the activity badge.

Parameters
renderer_base$outputOutput render to use, or null for default (global)
Return values
stdClass|nullThe activitybadge data (badgecontent, badgestyle...) or null if the module doesn't implement it.

◆ get_course()

cm_info::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

If the course object lacks the field you need you can use the global function get_course() that will save extra query if you access current course or frontpage course.

Return values
stdClass

◆ get_course_module_record()

cm_info::get_course_module_record ( $additionalfields = false)

Returns itself in the form of stdClass.

The object includes all fields that table course_modules has and additionally fields 'name', 'modname', 'sectionnum' (if requested).

This can be used as a faster alternative to get_coursemodule_from_id()

Parameters
bool$additionalfieldsinclude additional fields 'name', 'modname', 'sectionnum'
Return values
stdClass

◆ get_custom_data()

cm_info::get_custom_data ( )

Getter method for property $customdata, ensures that dynamic data is retrieved.

This method is normally called by the property ->customdata, but can be called directly if there is a case when it might be called recursively (you can't call property values recursively).

Return values
mixedOptional custom data stored in modinfo cache for this activity, or null if none

◆ get_formatted_content()

cm_info::get_formatted_content ( $options = array())

Returns the content to display on course/overview page, formatted and passed through filters.

if $options['context'] is not specified, the module context is used

Parameters
array | stdClass$optionsformatting options, see format_text()
Return values
string

◆ get_formatted_name()

cm_info::get_formatted_name ( $options = array())

Returns the name to display on course/overview page, formatted and passed through filters.

if $options['context'] is not specified, the module context is used

Parameters
array | stdClass$optionsformatting options, see format_string()
Return values
string

◆ get_grouping_label()

cm_info::get_grouping_label ( $textclasses = '')
Parameters
string$textclassesadditionnal classes for grouping label
Return values
stringAn empty string or HTML grouping label span tag

◆ get_icon_url()

cm_info::get_icon_url ( $output = null)

Fetch the module's icon URL.

This function fetches the course module instance's icon URL. This method adds a filtericon parameter in the URL when rendering the monologo version of the course module icon or when the plugin declares, via its filtericon custom data, that the icon needs to be filtered. This additional information can be used by plugins when rendering the module icon to determine whether to apply CSS filtering to the icon.

Parameters
core_renderer$outputOutput render to use, or null for default (global)
Return values
moodle_urlIcon URL for a suitable icon to put beside this cm

◆ get_modinfo()

cm_info::get_modinfo ( )
Return values
course_modinfoModinfo object that this came from

◆ get_module_type_name()

cm_info::get_module_type_name ( $plural = false)

Returns a localised human-readable name of the module type.

Parameters
bool$pluralIf true, the function returns the plural form of the name.
Return values
lang_string

◆ get_name()

cm_info::get_name ( )

Getter method for property $name, ensures that dynamic data is obtained.

This method is normally called by the property ->name, but can be called directly if there is a case when it might be called recursively (you can't call property values recursively).

Return values
string

◆ get_section_info()

cm_info::get_section_info ( )

Returns the section this module belongs to.

Return values
section_info

◆ get_url()

cm_info::get_url ( )

Gets the URL to link to for this module.

This method is normally called by the property ->url, but can be called directly if there is a case when it might be called recursively (you can't call property values recursively).

Return values
moodle_urlURL to link to for this module, or null if it doesn't have a view page

◆ get_user_visible()

cm_info::get_user_visible ( )

Getter method for property $uservisible, ensures that dynamic data is retrieved.

This method is normally called by the property ->uservisible, but can be called directly if there is a case when it might be called recursively (you can't call property values recursively).

Return values
bool

◆ getIterator()

cm_info::getIterator ( )

Implementation of IteratorAggregate\getIterator(), allows to cycle through properties and use convert_to_array().

Return values
ArrayIterator

◆ has_custom_cmlist_item()

cm_info::has_custom_cmlist_item ( )

Return the module custom cmlist item flag.

Activities like label uses this flag to indicate that it should be displayed as a custom course item instead of a tipical activity card.

Return values
bool

◆ has_view()

cm_info::has_view ( )
Return values
boolTrue if this module has a 'view' page that should be linked to in navigation etc (note: modules may still have a view.php file, but return false if this is not intended to be linked to from 'normal' parts of the interface; this is what label does).

◆ is_stealth()

cm_info::is_stealth ( )

Whether this module is available but hidden from course page.

"Stealth" modules are the ones that are not shown on course page but available by following url. They are normally also displayed in grade reports and other reports. Module will be stealth either if visibleoncoursepage=0 or it is a visible module inside the hidden section.

Return values
bool

◆ is_user_access_restricted_by_capability()

cm_info::is_user_access_restricted_by_capability ( )

Checks whether mod/...:view capability restricts the current user's access.

Return values
boolTrue if the user access is restricted.

◆ is_user_access_restricted_by_conditional_access()

cm_info::is_user_access_restricted_by_conditional_access ( )

Checks whether the module's conditional access settings mean that the user cannot see the activity at all.

Deprecated
since 2.7 MDL-44070

◆ is_user_access_restricted_by_group()

cm_info::is_user_access_restricted_by_group ( )

This method has been deprecated and should not be used.

See also
$uservisible
Deprecated
Since Moodle 2.8

◆ is_visible_on_course_page()

cm_info::is_visible_on_course_page ( )

Returns whether this module is visible to the current user on course page.

Activity may be visible on the course page but not available, for example when it is hidden conditionally but the condition information is displayed.

Return values
bool

◆ override_customdata()

cm_info::override_customdata ( $name,
$value )

Overrides the value of an element in the customdata array.

Parameters
string$nameThe key in the customdata array
mixed$valueThe value

◆ set_after_edit_icons()

cm_info::set_after_edit_icons ( $afterediticons)

Sets HTML that displays after edit icons on course view page.

Parameters
string$afterediticonsHTML string (empty string if none)
Return values
void

◆ set_after_link()

cm_info::set_after_link ( $afterlink)

Sets HTML that displays after link on course view page.

Parameters
string$afterlinkHTML string (empty string if none)
Return values
void

◆ set_available()

cm_info::set_available ( $available,
$showavailability = 0,
$availableinfo = '' )

Sets the 'available' flag and related details.

This flag is normally used to make course modules unavailable until a certain date or condition is met. (When a course module is unavailable, it is still visible to users who have viewhiddenactivities permission.)

When this is function is called, user-visible status is recalculated automatically.

The $showavailability flag does not really do anything any more, but is retained for backward compatibility. Setting this to false will cause $availableinfo to be ignored.

Note: May not be called from _cm_info_view (only _cm_info_dynamic).

Parameters
bool$availableFalse if this item is not 'available'
int$showavailability0 = do not show this item at all if it's not available, 1 = show this item greyed out with the following message
string$availableinfoInformation about why this is not available, or empty string if not displaying
Return values
void

◆ set_content()

cm_info::set_content ( $content,
$isformatted = false )

Sets content to display on course view page below link (if present).

Parameters
string$contentNew content as HTML string (empty string if none)
bool$isformattedWhether user content is already passed through format_text/format_string and should not be formatted again. This can be useful when module adds interactive elements on top of formatted user text.
Return values
void

◆ set_custom_cmlist_item()

cm_info::set_custom_cmlist_item ( bool $customcmlistitem)

Sets the 'customcmlistitem' flag.

This can be used (by setting true) to prevent the course from rendering the activity item as a regular activity card. This is applied to activities like labels.

Parameters
bool$customcmlistitemif the cmlist item of that activity has a special dysplay other than a card.

◆ set_extra_classes()

cm_info::set_extra_classes ( $extraclasses)

Sets extra classes to include in CSS.

Parameters
string$extraclassesExtra classes (empty string if none)
Return values
void

◆ set_icon_url()

cm_info::set_icon_url ( moodle_url $iconurl)

Sets the external full url that points to the icon being used by the activity.

Useful for external-tool modules (lti...) If set, takes precedence over $icon and $iconcomponent

Parameters
moodle_url$iconurlfull external url pointing to icon image for activity
Return values
void

◆ set_name()

cm_info::set_name ( $name)

Changes the name (text of link) for this module instance.

Note: May not be called from _cm_info_view (only _cm_info_dynamic).

Parameters
string$nameName of activity / link text
Return values
void

◆ set_no_view_link()

cm_info::set_no_view_link ( )

Turns off the view link for this module instance.

Note: May not be called from _cm_info_view (only _cm_info_dynamic).

Return values
void

◆ set_on_click()

cm_info::set_on_click ( $onclick)

Sets value of on-click attribute for JavaScript.

Note: May not be called from _cm_info_view (only _cm_info_dynamic).

Parameters
string$onclickNew onclick attribute which should be HTML-escaped (empty string if none)
Return values
void

◆ set_user_visible()

cm_info::set_user_visible ( $uservisible)

Sets the 'uservisible' flag.

This can be used (by setting false) to prevent access and display of this module link for the current user. Note: May not be called from _cm_info_view (only _cm_info_dynamic).

Parameters
bool$uservisible
Return values
void

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