Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Course module

From MoodleDocs
Revision as of 20:42, 24 March 2021 by Richard Jones (talk | contribs) (Added a note about getting module id from instance.)

Summary

A course module (often abbreviated 'cm') represents each of the activities and resources found in a course. It contains information about which course and section the activity / resource is displayed on, as well as details about the visibility, group and completion status of the activity.

Database tables

The data for the course module is stored in the database table 'mdl_course_modules' (the 'mdl_' part will be different if you have chosen a non-default prefix for your database tables). The fields in this table link it to a number of other tables in the database.

  • The 'course' field links to the 'mdl_course' table, which contains everything you need to know about a course.
  • The 'module' table links to the 'mdl_modules' table, which gives information about the type of the module (e.g. 'quiz', 'resource') as well as its current version number.
  • After looking up the type of the module, the rest of the details about this module can be found by looking up the 'instance' value (from the 'mdl_course_modules' table) in the 'mdl_{type}' table. Here you can find the name and introductory paragraph for the activity / resource, as well as information that is specific to the type of activity being looked at.

You can quickly gather all of this information, by using the 'get_fast_modinfo($course)' function. The easiest way to understand the data returned is to use the following code: global $DB; $course = $DB->get_record('course', array('id' => $courseid)); $info = get_fast_modinfo($course); print_object($info);

Usage

The cmid (course module id) is used widely throughout Moodle to identify a specific activity / resource. Some of the most important uses are:

  • When linking to a modules 'view.php' script, it is passed as the 'id' parameter
  • To get the 'context' for the module (used when checking user capabilities or linking files to an activity), via the function call 'context_module::instance($cmid)' (before Moodle 2.2 this was 'get_context_instance(CONTEXT_MODULE, $cmid)')
  • Every log entry in 'mdl_log' that relates to a specific activity has the 'cmid' field set

Note

You can also get the course module id from a module instance, for example see: https://github.com/moodle/moodle/blob/master/mod/page/view.php#L36.

More documentation