MDL-32843 import YUI 3.5.1
[moodle.git] / lib / modinfolib.php
blob20ac75cc34654720f6953118ea0a7fb92b61624f
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * modinfolib.php - Functions/classes relating to cached information about module instances on
19 * a course.
20 * @package core
21 * @subpackage lib
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @author sam marshall
27 // Maximum number of modinfo items to keep in memory cache. Do not increase this to a large
28 // number because:
29 // a) modinfo can be big (megabyte range) for some courses
30 // b) performance of cache will deteriorate if there are very many items in it
31 if (!defined('MAX_MODINFO_CACHE_SIZE')) {
32 define('MAX_MODINFO_CACHE_SIZE', 10);
36 /**
37 * Information about a course that is cached in the course table 'modinfo' field (and then in
38 * memory) in order to reduce the need for other database queries.
40 * This includes information about the course-modules and the sections on the course. It can also
41 * include dynamic data that has been updated for the current user.
43 class course_modinfo extends stdClass {
44 // For convenience we store the course object here as it is needed in other parts of code
45 private $course;
47 // Existing data fields
48 ///////////////////////
50 // These are public for backward compatibility. Note: it is not possible to retain BC
51 // using PHP magic get methods because behaviour is different with regard to empty().
53 /**
54 * Course ID
55 * @var int
56 * @deprecated For new code, use get_course_id instead.
58 public $courseid;
60 /**
61 * User ID
62 * @var int
63 * @deprecated For new code, use get_user_id instead.
65 public $userid;
67 /**
68 * Array from int (section num, e.g. 0) => array of int (course-module id); this list only
69 * includes sections that actually contain at least one course-module
70 * @var array
71 * @deprecated For new code, use get_sections instead
73 public $sections;
75 /**
76 * Array from int (cm id) => cm_info object
77 * @var array
78 * @deprecated For new code, use get_cms or get_cm instead.
80 public $cms;
82 /**
83 * Array from string (modname) => int (instance id) => cm_info object
84 * @var array
85 * @deprecated For new code, use get_instances or get_instances_of instead.
87 public $instances;
89 /**
90 * Groups that the current user belongs to. This value is usually not available (set to null)
91 * unless the course has activities set to groupmembersonly. When set, it is an array of
92 * grouping id => array of group id => group id. Includes grouping id 0 for 'all groups'.
93 * @var array
94 * @deprecated Don't use this! For new code, use get_groups.
96 public $groups;
98 // Get methods for data
99 ///////////////////////
102 * @return object Moodle course object that was used to construct this data
104 public function get_course() {
105 return $this->course;
109 * @return int Course ID
111 public function get_course_id() {
112 return $this->courseid;
116 * @return int User ID
118 public function get_user_id() {
119 return $this->userid;
123 * @return array Array from section number (e.g. 0) to array of course-module IDs in that
124 * section; this only includes sections that contain at least one course-module
126 public function get_sections() {
127 return $this->sections;
131 * @return array Array from course-module instance to cm_info object within this course, in
132 * order of appearance
134 public function get_cms() {
135 return $this->cms;
139 * Obtains a single course-module object (for a course-module that is on this course).
140 * @param int $cmid Course-module ID
141 * @return cm_info Information about that course-module
142 * @throws moodle_exception If the course-module does not exist
144 public function get_cm($cmid) {
145 if (empty($this->cms[$cmid])) {
146 throw new moodle_exception('invalidcoursemodule', 'error');
148 return $this->cms[$cmid];
152 * Obtains all module instances on this course.
153 * @return array Array from module name => array from instance id => cm_info
155 public function get_instances() {
156 return $this->instances;
160 * Obtains all instances of a particular module on this course.
161 * @param $modname Name of module (not full frankenstyle) e.g. 'label'
162 * @return array Array from instance id => cm_info for modules on this course; empty if none
164 public function get_instances_of($modname) {
165 if (empty($this->instances[$modname])) {
166 return array();
168 return $this->instances[$modname];
172 * Returns groups that the current user belongs to on the course. Note: If not already
173 * available, this may make a database query.
174 * @param int $groupingid Grouping ID or 0 (default) for all groups
175 * @return array Array of int (group id) => int (same group id again); empty array if none
177 public function get_groups($groupingid=0) {
178 if (is_null($this->groups)) {
179 // NOTE: Performance could be improved here. The system caches user groups
180 // in $USER->groupmember[$courseid] => array of groupid=>groupid. Unfortunately this
181 // structure does not include grouping information. It probably could be changed to
182 // do so, without a significant performance hit on login, thus saving this one query
183 // each request.
184 $this->groups = groups_get_user_groups($this->courseid, $this->userid);
186 if (!isset($this->groups[$groupingid])) {
187 return array();
189 return $this->groups[$groupingid];
193 * Constructs based on course.
194 * Note: This constructor should not usually be called directly.
195 * Use get_fast_modinfo($course) instead as this maintains a cache.
196 * @param object $course Moodle course object, which may include modinfo
197 * @param int $userid User ID
199 public function __construct($course, $userid) {
200 global $CFG, $DB;
202 // Set initial values
203 $this->courseid = $course->id;
204 $this->userid = $userid;
205 $this->sections = array();
206 $this->cms = array();
207 $this->instances = array();
208 $this->groups = null;
209 $this->course = $course;
211 // Check modinfo field is set. If not, build and load it.
212 if (empty($course->modinfo)) {
213 rebuild_course_cache($course->id);
214 $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
217 // Load modinfo field into memory as PHP object and check it's valid
218 $info = unserialize($course->modinfo);
219 if (!is_array($info)) {
220 // hmm, something is wrong - lets try to fix it
221 rebuild_course_cache($course->id);
222 $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id));
223 $info = unserialize($course->modinfo);
224 if (!is_array($info)) {
225 // If it still fails, abort
226 debugging('Problem with "modinfo" data for this course');
227 return;
231 // If we haven't already preloaded contexts for the course, do it now
232 preload_course_contexts($course->id);
234 // Loop through each piece of module data, constructing it
235 $modexists = array();
236 foreach ($info as $mod) {
237 if (empty($mod->name)) {
238 // something is wrong here
239 continue;
242 // Skip modules which don't exist
243 if (empty($modexists[$mod->mod])) {
244 if (!file_exists("$CFG->dirroot/mod/$mod->mod/lib.php")) {
245 continue;
247 $modexists[$mod->mod] = true;
250 // Construct info for this module
251 $cm = new cm_info($this, $course, $mod, $info);
253 // Store module in instances and cms array
254 if (!isset($this->instances[$cm->modname])) {
255 $this->instances[$cm->modname] = array();
257 $this->instances[$cm->modname][$cm->instance] = $cm;
258 $this->cms[$cm->id] = $cm;
260 // Reconstruct sections. This works because modules are stored in order
261 if (!isset($this->sections[$cm->sectionnum])) {
262 $this->sections[$cm->sectionnum] = array();
264 $this->sections[$cm->sectionnum][] = $cm->id;
267 // We need at least 'dynamic' data from each course-module (this is basically the remaining
268 // data which was always present in previous version of get_fast_modinfo, so it's required
269 // for BC). Creating it in a second pass is necessary because obtain_dynamic_data sometimes
270 // needs to be able to refer to a 'complete' (with basic data) modinfo.
271 foreach ($this->cms as $cm) {
272 $cm->obtain_dynamic_data();
279 * Data about a single module on a course. This contains most of the fields in the course_modules
280 * table, plus additional data when required.
282 * This object has many public fields; code should treat all these fields as read-only and set
283 * data only using the supplied set functions. Setting the fields directly is not supported
284 * and may cause problems later.
286 class cm_info extends stdClass {
288 * State: Only basic data from modinfo cache is available.
290 const STATE_BASIC = 0;
293 * State: Dynamic data is available too.
295 const STATE_DYNAMIC = 1;
298 * State: View data (for course page) is available.
300 const STATE_VIEW = 2;
303 * Parent object
304 * @var course_modinfo
306 private $modinfo;
309 * Level of information stored inside this object (STATE_xx constant)
310 * @var int
312 private $state;
314 // Existing data fields
315 ///////////////////////
318 * Course-module ID - from course_modules table
319 * @var int
321 public $id;
324 * Module instance (ID within module table) - from course_modules table
325 * @var int
327 public $instance;
330 * Course ID - from course_modules table
331 * @var int
333 public $course;
336 * 'ID number' from course-modules table (arbitrary text set by user) - from
337 * course_modules table
338 * @var string
340 public $idnumber;
343 * Time that this course-module was added (unix time) - from course_modules table
344 * @var int
346 public $added;
349 * This variable is not used and is included here only so it can be documented.
350 * Once the database entry is removed from course_modules, it should be deleted
351 * here too.
352 * @var int
353 * @deprecated Do not use this variable
355 public $score;
358 * Visible setting (0 or 1; if this is 0, students cannot see/access the activity) - from
359 * course_modules table
360 * @var int
362 public $visible;
365 * Old visible setting (if the entire section is hidden, the previous value for
366 * visible is stored in this field) - from course_modules table
367 * @var int
369 public $visibleold;
372 * Group mode (one of the constants NONE, SEPARATEGROUPS, or VISIBLEGROUPS) - from
373 * course_modules table
374 * @var int
376 public $groupmode;
379 * Grouping ID (0 = all groupings)
380 * @var int
382 public $groupingid;
385 * Group members only (if set to 1, only members of a suitable group see this link on the
386 * course page; 0 = everyone sees it even if they don't belong to a suitable group) - from
387 * course_modules table
388 * @var int
390 public $groupmembersonly;
393 * Indent level on course page (0 = no indent) - from course_modules table
394 * @var int
396 public $indent;
399 * Activity completion setting for this activity, COMPLETION_TRACKING_xx constant - from
400 * course_modules table
401 * @var int
403 public $completion;
406 * Set to the item number (usually 0) if completion depends on a particular
407 * grade of this activity, or null if completion does not depend on a grade - from
408 * course_modules table
409 * @var mixed
411 public $completiongradeitemnumber;
414 * 1 if 'on view' completion is enabled, 0 otherwise - from course_modules table
415 * @var int
417 public $completionview;
420 * Set to a unix time if completion of this activity is expected at a
421 * particular time, 0 if no time set - from course_modules table
422 * @var int
424 public $completionexpected;
427 * Available date for this activity (0 if not set, or set to seconds since epoch; before this
428 * date, activity does not display to students) - from course_modules table
429 * @var int
431 public $availablefrom;
434 * Available until date for this activity (0 if not set, or set to seconds since epoch; from
435 * this date, activity does not display to students) - from course_modules table
436 * @var int
438 public $availableuntil;
441 * When activity is unavailable, this field controls whether it is shown to students (0 =
442 * hide completely, 1 = show greyed out with information about when it will be available) -
443 * from course_modules table
444 * @var int
446 public $showavailability;
449 * Controls whether the description of the activity displays on the course main page (in
450 * addition to anywhere it might display within the activity itself). 0 = do not show
451 * on main page, 1 = show on main page.
452 * @var int
454 public $showdescription;
457 * Extra HTML that is put in an unhelpful part of the HTML when displaying this module in
458 * course page - from cached data in modinfo field
459 * @deprecated This is crazy, don't use it. Replaced by ->extraclasses and ->onclick
460 * @var string
462 public $extra;
465 * Name of icon to use - from cached data in modinfo field
466 * @var string
468 public $icon;
471 * Component that contains icon - from cached data in modinfo field
472 * @var string
474 public $iconcomponent;
477 * Name of module e.g. 'forum' (this is the same name as the module's main database
478 * table) - from cached data in modinfo field
479 * @var string
481 public $modname;
484 * ID of module - from course_modules table
485 * @var int
487 public $module;
490 * Name of module instance for display on page e.g. 'General discussion forum' - from cached
491 * data in modinfo field
492 * @var string
494 public $name;
497 * Section number that this course-module is in (section 0 = above the calendar, section 1
498 * = week/topic 1, etc) - from cached data in modinfo field
499 * @var string
501 public $sectionnum;
504 * Section id - from course_modules table
505 * @var int
507 public $section;
510 * Availability conditions for this course-module based on the completion of other
511 * course-modules (array from other course-module id to required completion state for that
512 * module) - from cached data in modinfo field
513 * @var array
515 public $conditionscompletion;
518 * Availability conditions for this course-module based on course grades (array from
519 * grade item id to object with ->min, ->max fields) - from cached data in modinfo field
520 * @var array
522 public $conditionsgrade;
525 * Plural name of module type, e.g. 'Forums' - from lang file
526 * @deprecated Do not use this value (you can obtain it by calling get_string instead); it
527 * will be removed in a future version (see later TODO in this file)
528 * @var string
530 public $modplural;
533 * True if this course-module is available to students i.e. if all availability conditions
534 * are met - obtained dynamically
535 * @var bool
537 public $available;
540 * If course-module is not available to students, this string gives information about
541 * availability which can be displayed to students and/or staff (e.g. 'Available from 3
542 * January 2010') for display on main page - obtained dynamically
543 * @var string
545 public $availableinfo;
548 * True if this course-module is available to the CURRENT user (for example, if current user
549 * has viewhiddenactivities capability, they can access the course-module even if it is not
550 * visible or not available, so this would be true in that case)
551 * @var bool
553 public $uservisible;
556 * Module context - hacky shortcut
557 * @deprecated
558 * @var stdClass
560 public $context;
563 // New data available only via functions
564 ////////////////////////////////////////
567 * @var moodle_url
569 private $url;
572 * @var string
574 private $content;
577 * @var string
579 private $extraclasses;
582 * @var moodle_url full external url pointing to icon image for activity
584 private $iconurl;
587 * @var string
589 private $onclick;
592 * @var mixed
594 private $customdata;
597 * @var string
599 private $afterlink;
602 * @var string
604 private $afterediticons;
607 * @return bool True if this module has a 'view' page that should be linked to in navigation
608 * etc (note: modules may still have a view.php file, but return false if this is not
609 * intended to be linked to from 'normal' parts of the interface; this is what label does).
611 public function has_view() {
612 return !is_null($this->url);
616 * @return moodle_url URL to link to for this module, or null if it doesn't have a view page
618 public function get_url() {
619 return $this->url;
623 * Obtains content to display on main (view) page.
624 * Note: Will collect view data, if not already obtained.
625 * @return string Content to display on main page below link, or empty string if none
627 public function get_content() {
628 $this->obtain_view_data();
629 return $this->content;
633 * Note: Will collect view data, if not already obtained.
634 * @return string Extra CSS classes to add to html output for this activity on main page
636 public function get_extra_classes() {
637 $this->obtain_view_data();
638 return $this->extraclasses;
642 * @return string Content of HTML on-click attribute. This string will be used literally
643 * as a string so should be pre-escaped.
645 public function get_on_click() {
646 // Does not need view data; may be used by navigation
647 return $this->onclick;
650 * @return mixed Optional custom data stored in modinfo cache for this activity, or null if none
652 public function get_custom_data() {
653 return $this->customdata;
657 * Note: Will collect view data, if not already obtained.
658 * @return string Extra HTML code to display after link
660 public function get_after_link() {
661 $this->obtain_view_data();
662 return $this->afterlink;
666 * Note: Will collect view data, if not already obtained.
667 * @return string Extra HTML code to display after editing icons (e.g. more icons)
669 public function get_after_edit_icons() {
670 $this->obtain_view_data();
671 return $this->afterediticons;
675 * @param moodle_core_renderer $output Output render to use, or null for default (global)
676 * @return moodle_url Icon URL for a suitable icon to put beside this cm
678 public function get_icon_url($output = null) {
679 global $OUTPUT;
680 if (!$output) {
681 $output = $OUTPUT;
683 // Support modules setting their own, external, icon image
684 if (!empty($this->iconurl)) {
685 $icon = $this->iconurl;
687 // Fallback to normal local icon + component procesing
688 } else if (!empty($this->icon)) {
689 if (substr($this->icon, 0, 4) === 'mod/') {
690 list($modname, $iconname) = explode('/', substr($this->icon, 4), 2);
691 $icon = $output->pix_url($iconname, $modname);
692 } else {
693 if (!empty($this->iconcomponent)) {
694 // Icon has specified component
695 $icon = $output->pix_url($this->icon, $this->iconcomponent);
696 } else {
697 // Icon does not have specified component, use default
698 $icon = $output->pix_url($this->icon);
701 } else {
702 $icon = $output->pix_url('icon', $this->modname);
704 return $icon;
708 * @return course_modinfo Modinfo object that this came from
710 public function get_modinfo() {
711 return $this->modinfo;
715 * @return object Moodle course object that was used to construct this data
717 public function get_course() {
718 return $this->modinfo->get_course();
721 // Set functions
722 ////////////////
725 * Sets content to display on course view page below link (if present).
726 * @param string $content New content as HTML string (empty string if none)
727 * @return void
729 public function set_content($content) {
730 $this->content = $content;
734 * Sets extra classes to include in CSS.
735 * @param string $extraclasses Extra classes (empty string if none)
736 * @return void
738 public function set_extra_classes($extraclasses) {
739 $this->extraclasses = $extraclasses;
743 * Sets the external full url that points to the icon being used
744 * by the activity. Useful for external-tool modules (lti...)
745 * If set, takes precedence over $icon and $iconcomponent
747 * @param moodle_url $iconurl full external url pointing to icon image for activity
748 * @return void
750 public function set_icon_url(moodle_url $iconurl) {
751 $this->iconurl = $iconurl;
755 * Sets value of on-click attribute for JavaScript.
756 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
757 * @param string $onclick New onclick attribute which should be HTML-escaped
758 * (empty string if none)
759 * @return void
761 public function set_on_click($onclick) {
762 $this->check_not_view_only();
763 $this->onclick = $onclick;
767 * Sets HTML that displays after link on course view page.
768 * @param string $afterlink HTML string (empty string if none)
769 * @return void
771 public function set_after_link($afterlink) {
772 $this->afterlink = $afterlink;
776 * Sets HTML that displays after edit icons on course view page.
777 * @param string $afterediticons HTML string (empty string if none)
778 * @return void
780 public function set_after_edit_icons($afterediticons) {
781 $this->afterediticons = $afterediticons;
785 * Changes the name (text of link) for this module instance.
786 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
787 * @param string $name Name of activity / link text
788 * @return void
790 public function set_name($name) {
791 $this->update_user_visible();
792 $this->name = $name;
796 * Turns off the view link for this module instance.
797 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
798 * @return void
800 public function set_no_view_link() {
801 $this->check_not_view_only();
802 $this->url = null;
806 * Sets the 'uservisible' flag. This can be used (by setting false) to prevent access and
807 * display of this module link for the current user.
808 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
809 * @param bool $uservisible
810 * @return void
812 public function set_user_visible($uservisible) {
813 $this->check_not_view_only();
814 $this->uservisible = $uservisible;
818 * Sets the 'available' flag and related details. This flag is normally used to make
819 * course modules unavailable until a certain date or condition is met. (When a course
820 * module is unavailable, it is still visible to users who have viewhiddenactivities
821 * permission.)
823 * When this is function is called, user-visible status is recalculated automatically.
825 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
826 * @param bool $available False if this item is not 'available'
827 * @param int $showavailability 0 = do not show this item at all if it's not available,
828 * 1 = show this item greyed out with the following message
829 * @param string $availableinfo Information about why this is not available which displays
830 * to those who have viewhiddenactivities, and to everyone if showavailability is set;
831 * note that this function replaces the existing data (if any)
832 * @return void
834 public function set_available($available, $showavailability=0, $availableinfo='') {
835 $this->check_not_view_only();
836 $this->available = $available;
837 $this->showavailability = $showavailability;
838 $this->availableinfo = $availableinfo;
839 $this->update_user_visible();
843 * Some set functions can only be called from _cm_info_dynamic and not _cm_info_view.
844 * This is because they may affect parts of this object which are used on pages other
845 * than the view page (e.g. in the navigation block, or when checking access on
846 * module pages).
847 * @return void
849 private function check_not_view_only() {
850 if ($this->state >= self::STATE_DYNAMIC) {
851 throw new coding_exception('Cannot set this data from _cm_info_view because it may ' .
852 'affect other pages as well as view');
857 * Constructor should not be called directly; use get_fast_modinfo.
858 * @param course_modinfo $modinfo Parent object
859 * @param object $course Course row
860 * @param object $mod Module object from the modinfo field of course table
861 * @param object $info Entire object from modinfo field of course table
863 public function __construct(course_modinfo $modinfo, $course, $mod, $info) {
864 global $CFG;
865 $this->modinfo = $modinfo;
867 $this->id = $mod->cm;
868 $this->instance = $mod->id;
869 $this->course = $course->id;
870 $this->modname = $mod->mod;
871 $this->idnumber = isset($mod->idnumber) ? $mod->idnumber : '';
872 $this->name = $mod->name;
873 $this->visible = $mod->visible;
874 $this->sectionnum = $mod->section; // Note weirdness with name here
875 $this->groupmode = isset($mod->groupmode) ? $mod->groupmode : 0;
876 $this->groupingid = isset($mod->groupingid) ? $mod->groupingid : 0;
877 $this->groupmembersonly = isset($mod->groupmembersonly) ? $mod->groupmembersonly : 0;
878 $this->indent = isset($mod->indent) ? $mod->indent : 0;
879 $this->extra = isset($mod->extra) ? $mod->extra : '';
880 $this->extraclasses = isset($mod->extraclasses) ? $mod->extraclasses : '';
881 $this->iconurl = isset($mod->iconurl) ? $mod->iconurl : '';
882 $this->onclick = isset($mod->onclick) ? $mod->onclick : '';
883 $this->content = isset($mod->content) ? $mod->content : '';
884 $this->icon = isset($mod->icon) ? $mod->icon : '';
885 $this->iconcomponent = isset($mod->iconcomponent) ? $mod->iconcomponent : '';
886 $this->customdata = isset($mod->customdata) ? $mod->customdata : '';
887 $this->context = get_context_instance(CONTEXT_MODULE, $mod->cm);
888 $this->showdescription = isset($mod->showdescription) ? $mod->showdescription : 0;
889 $this->state = self::STATE_BASIC;
891 // This special case handles old label data. Labels used to use the 'name' field for
892 // content
893 if ($this->modname === 'label' && $this->content === '') {
894 $this->content = $this->extra;
895 $this->extra = '';
898 // Note: These fields from $cm were not present in cm_info in Moodle
899 // 2.0.2 and prior. They may not be available if course cache hasn't
900 // been rebuilt since then.
901 $this->section = isset($mod->sectionid) ? $mod->sectionid : 0;
902 $this->module = isset($mod->module) ? $mod->module : 0;
903 $this->added = isset($mod->added) ? $mod->added : 0;
904 $this->score = isset($mod->score) ? $mod->score : 0;
905 $this->visibleold = isset($mod->visibleold) ? $mod->visibleold : 0;
907 // Note: it saves effort and database space to always include the
908 // availability and completion fields, even if availability or completion
909 // are actually disabled
910 $this->completion = isset($mod->completion) ? $mod->completion : 0;
911 $this->completiongradeitemnumber = isset($mod->completiongradeitemnumber)
912 ? $mod->completiongradeitemnumber : null;
913 $this->completionview = isset($mod->completionview)
914 ? $mod->completionview : 0;
915 $this->completionexpected = isset($mod->completionexpected)
916 ? $mod->completionexpected : 0;
917 $this->showavailability = isset($mod->showavailability) ? $mod->showavailability : 0;
918 $this->availablefrom = isset($mod->availablefrom) ? $mod->availablefrom : 0;
919 $this->availableuntil = isset($mod->availableuntil) ? $mod->availableuntil : 0;
920 $this->conditionscompletion = isset($mod->conditionscompletion)
921 ? $mod->conditionscompletion : array();
922 $this->conditionsgrade = isset($mod->conditionsgrade)
923 ? $mod->conditionsgrade : array();
925 // Get module plural name.
926 // TODO This was a very old performance hack and should now be removed as the information
927 // certainly doesn't belong in modinfo. On a 'normal' page this is only used in the
928 // activity_modules block, so if it needs caching, it should be cached there.
929 static $modplurals;
930 if (!isset($modplurals[$this->modname])) {
931 $modplurals[$this->modname] = get_string('modulenameplural', $this->modname);
933 $this->modplural = $modplurals[$this->modname];
935 static $modviews;
936 if (!isset($modviews[$this->modname])) {
937 $modviews[$this->modname] = !plugin_supports('mod', $this->modname,
938 FEATURE_NO_VIEW_LINK);
940 $this->url = $modviews[$this->modname]
941 ? new moodle_url('/mod/' . $this->modname . '/view.php', array('id'=>$this->id))
942 : null;
946 * If dynamic data for this course-module is not yet available, gets it.
948 * This function is automatically called when constructing course_modinfo, so users don't
949 * need to call it.
951 * Dynamic data is data which does not come directly from the cache but is calculated at
952 * runtime based on the current user. Primarily this concerns whether the user can access
953 * the module or not.
955 * As part of this function, the module's _cm_info_dynamic function from its lib.php will
956 * be called (if it exists).
957 * @return void
959 public function obtain_dynamic_data() {
960 global $CFG;
961 if ($this->state >= self::STATE_DYNAMIC) {
962 return;
964 $userid = $this->modinfo->get_user_id();
966 if (!empty($CFG->enableavailability)) {
967 // Get availability information
968 $ci = new condition_info($this);
969 // Note that the modinfo currently available only includes minimal details (basic data)
970 // so passing it to this function is a bit dangerous as it would cause infinite
971 // recursion if it tried to get dynamic data, however we know that this function only
972 // uses basic data.
973 $this->available = $ci->is_available($this->availableinfo, true,
974 $userid, $this->modinfo);
975 } else {
976 $this->available = true;
979 // Update visible state for current user
980 $this->update_user_visible();
982 // Let module make dynamic changes at this point
983 $this->call_mod_function('cm_info_dynamic');
984 $this->state = self::STATE_DYNAMIC;
988 * Works out whether activity is visible *for current user* - if this is false, they
989 * aren't allowed to access it.
990 * @return void
992 private function update_user_visible() {
993 global $CFG;
994 $modcontext = get_context_instance(CONTEXT_MODULE, $this->id);
995 $userid = $this->modinfo->get_user_id();
996 $this->uservisible = true;
997 if ((!$this->visible or !$this->available) and
998 !has_capability('moodle/course:viewhiddenactivities', $modcontext, $userid)) {
999 // If the activity is hidden or unavailable, and you don't have viewhiddenactivities,
1000 // set it so that user can't see or access it
1001 $this->uservisible = false;
1002 } else if (!empty($CFG->enablegroupmembersonly) and !empty($this->groupmembersonly)
1003 and !has_capability('moodle/site:accessallgroups', $modcontext, $userid)) {
1004 // If the activity has 'group members only' and you don't have accessallgroups...
1005 $groups = $this->modinfo->get_groups($this->groupingid);
1006 if (empty($groups)) {
1007 // ...and you don't belong to a group, then set it so you can't see/access it
1008 $this->uservisible = false;
1014 * Calls a module function (if exists), passing in one parameter: this object.
1015 * @param string $type Name of function e.g. if this is 'grooblezorb' and the modname is
1016 * 'forum' then it will try to call 'mod_forum_grooblezorb' or 'forum_grooblezorb'
1017 * @return void
1019 private function call_mod_function($type) {
1020 global $CFG;
1021 $libfile = $CFG->dirroot . '/mod/' . $this->modname . '/lib.php';
1022 if (file_exists($libfile)) {
1023 include_once($libfile);
1024 $function = 'mod_' . $this->modname . '_' . $type;
1025 if (function_exists($function)) {
1026 $function($this);
1027 } else {
1028 $function = $this->modname . '_' . $type;
1029 if (function_exists($function)) {
1030 $function($this);
1037 * If view data for this course-module is not yet available, obtains it.
1039 * This function is automatically called if any of the functions (marked) which require
1040 * view data are called.
1042 * View data is data which is needed only for displaying the course main page (& any similar
1043 * functionality on other pages) but is not needed in general. Obtaining view data may have
1044 * a performance cost.
1046 * As part of this function, the module's _cm_info_view function from its lib.php will
1047 * be called (if it exists).
1048 * @return void
1050 private function obtain_view_data() {
1051 if ($this->state >= self::STATE_VIEW) {
1052 return;
1055 // Let module make changes at this point
1056 $this->call_mod_function('cm_info_view');
1057 $this->state = self::STATE_VIEW;
1063 * Returns reference to full info about modules in course (including visibility).
1064 * Cached and as fast as possible (0 or 1 db query).
1066 * @global object
1067 * @global object
1068 * @global moodle_database
1069 * @uses MAX_MODINFO_CACHE_SIZE
1070 * @param mixed $course object or 'reset' string to reset caches, modinfo may be updated in db
1071 * @param int $userid Defaults to current user id
1072 * @return course_modinfo Module information for course, or null if resetting
1074 function get_fast_modinfo(&$course, $userid=0) {
1075 global $CFG, $USER, $DB;
1076 require_once($CFG->dirroot.'/course/lib.php');
1078 if (!empty($CFG->enableavailability)) {
1079 require_once($CFG->libdir.'/conditionlib.php');
1082 static $cache = array();
1084 if ($course === 'reset') {
1085 $cache = array();
1086 return null;
1089 if (empty($userid)) {
1090 $userid = $USER->id;
1093 if (array_key_exists($course->id, $cache) and $cache[$course->id]->userid == $userid) {
1094 return $cache[$course->id];
1097 if (!property_exists($course, 'modinfo')) {
1098 debugging('Coding problem - missing course modinfo property in get_fast_modinfo() call');
1101 unset($cache[$course->id]); // prevent potential reference problems when switching users
1103 $cache[$course->id] = new course_modinfo($course, $userid);
1105 // Ensure cache does not use too much RAM
1106 if (count($cache) > MAX_MODINFO_CACHE_SIZE) {
1107 reset($cache);
1108 $key = key($cache);
1109 unset($cache[$key]->instances);
1110 unset($cache[$key]->cms);
1111 unset($cache[$key]);
1114 return $cache[$course->id];
1118 * Rebuilds the cached list of course activities stored in the database
1119 * @param int $courseid - id of course to rebuild, empty means all
1120 * @param boolean $clearonly - only clear the modinfo fields, gets rebuild automatically on the fly
1122 function rebuild_course_cache($courseid=0, $clearonly=false) {
1123 global $COURSE, $DB, $CFG;
1125 // Destroy navigation caches
1126 navigation_cache::destroy_volatile_caches();
1128 if ($clearonly) {
1129 if (empty($courseid)) {
1130 $courseselect = array();
1131 } else {
1132 $courseselect = array('id'=>$courseid);
1134 $DB->set_field('course', 'modinfo', null, $courseselect);
1135 // update cached global COURSE too ;-)
1136 if ($courseid == $COURSE->id or empty($courseid)) {
1137 $COURSE->modinfo = null;
1139 // reset the fast modinfo cache
1140 $reset = 'reset';
1141 get_fast_modinfo($reset);
1142 return;
1145 require_once("$CFG->dirroot/course/lib.php");
1147 if ($courseid) {
1148 $select = array('id'=>$courseid);
1149 } else {
1150 $select = array();
1151 @set_time_limit(0); // this could take a while! MDL-10954
1154 $rs = $DB->get_recordset("course", $select,'','id,fullname');
1155 foreach ($rs as $course) {
1156 $modinfo = serialize(get_array_of_activities($course->id));
1157 $DB->set_field("course", "modinfo", $modinfo, array("id"=>$course->id));
1158 // update cached global COURSE too ;-)
1159 if ($course->id == $COURSE->id) {
1160 $COURSE->modinfo = $modinfo;
1163 $rs->close();
1164 // reset the fast modinfo cache
1165 $reset = 'reset';
1166 get_fast_modinfo($reset);
1171 * Class that is the return value for the _get_coursemodule_info module API function.
1173 * Note: For backward compatibility, you can also return a stdclass object from that function.
1174 * The difference is that the stdclass object may contain an 'extra' field (deprecated because
1175 * it was crazy, except for label which uses it differently). The stdclass object may not contain
1176 * the new fields defined here (content, extraclasses, customdata).
1178 class cached_cm_info {
1180 * Name (text of link) for this activity; Leave unset to accept default name
1181 * @var string
1183 public $name;
1186 * Name of icon for this activity. Normally, this should be used together with $iconcomponent
1187 * to define the icon, as per pix_url function.
1188 * For backward compatibility, if this value is of the form 'mod/forum/icon' then an icon
1189 * within that module will be used.
1190 * @see cm_info::get_icon_url()
1191 * @see renderer_base::pix_url()
1192 * @var string
1194 public $icon;
1197 * Component for icon for this activity, as per pix_url; leave blank to use default 'moodle'
1198 * component
1199 * @see renderer_base::pix_url()
1200 * @var string
1202 public $iconcomponent;
1205 * HTML content to be displayed on the main page below the link (if any) for this course-module
1206 * @var string
1208 public $content;
1211 * Custom data to be stored in modinfo for this activity; useful if there are cases when
1212 * internal information for this activity type needs to be accessible from elsewhere on the
1213 * course without making database queries. May be of any type but should be short.
1214 * @var mixed
1216 public $customdata;
1219 * Extra CSS class or classes to be added when this activity is displayed on the main page;
1220 * space-separated string
1221 * @var string
1223 public $extraclasses;
1226 * External URL image to be used by activity as icon, useful for some external-tool modules
1227 * like lti. If set, takes precedence over $icon and $iconcomponent
1228 * @var $moodle_url
1230 public $iconurl;
1233 * Content of onclick JavaScript; escaped HTML to be inserted as attribute value
1234 * @var string
1236 public $onclick;