weekly release 2.2dev
[moodle.git] / lib / modinfolib.php
CommitLineData
0d8b6a69 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.
13//
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/>.
16
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
24 */
25
26
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
31if (!defined('MAX_MODINFO_CACHE_SIZE')) {
32 define('MAX_MODINFO_CACHE_SIZE', 10);
33}
34
35
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.
39 *
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.
42 */
1e4cb189 43class course_modinfo extends stdClass {
0d8b6a69 44 // For convenience we store the course object here as it is needed in other parts of code
45 private $course;
46
47 // Existing data fields
48 ///////////////////////
49
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().
52
53 /**
54 * Course ID
55 * @var int
56 * @deprecated For new code, use get_course_id instead.
57 */
58 public $courseid;
59
60 /**
61 * User ID
62 * @var int
63 * @deprecated For new code, use get_user_id instead.
64 */
65 public $userid;
66
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
72 */
73 public $sections;
74
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.
79 */
80 public $cms;
81
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.
86 */
87 public $instances;
88
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.
95 */
96 public $groups;
97
98 // Get methods for data
99 ///////////////////////
100
101 /**
102 * @return object Moodle course object that was used to construct this data
103 */
104 public function get_course() {
105 return $this->course;
106 }
107
108 /**
109 * @return int Course ID
110 */
111 public function get_course_id() {
112 return $this->courseid;
113 }
114
115 /**
116 * @return int User ID
117 */
118 public function get_user_id() {
119 return $this->userid;
120 }
121
122 /**
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
125 */
126 public function get_sections() {
127 return $this->sections;
128 }
129
130 /**
131 * @return array Array from course-module instance to cm_info object within this course, in
132 * order of appearance
133 */
134 public function get_cms() {
135 return $this->cms;
136 }
137
138 /**
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
143 */
144 public function get_cm($cmid) {
145 if (empty($this->cms[$cmid])) {
146 throw new moodle_exception('invalidcoursemodule', 'error');
147 }
148 return $this->cms[$cmid];
149 }
150
151 /**
152 * Obtains all module instances on this course.
153 * @return array Array from module name => array from instance id => cm_info
154 */
155 public function get_instances() {
156 return $this->instances;
157 }
158
159 /**
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
163 */
164 public function get_instances_of($modname) {
165 if (empty($this->instances[$modname])) {
166 return array();
167 }
168 return $this->instances[$modname];
169 }
170
171 /**
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
176 */
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);
185 }
186 if (!isset($this->groups[$groupingid])) {
187 return array();
188 }
189 return $this->groups[$groupingid];
190 }
191
192 /**
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
198 */
199 public function __construct($course, $userid) {
d6f4508c 200 global $CFG, $DB;
0d8b6a69 201
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;
210
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));
215 }
216
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;
228 }
229 }
230
231 // If we haven't already preloaded contexts for the course, do it now
232 preload_course_contexts($course->id);
233
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;
240 }
241
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;
246 }
247 $modexists[$mod->mod] = true;
248 }
249
250 // Construct info for this module
251 $cm = new cm_info($this, $course, $mod, $info);
252
253 // Store module in instances and cms array
254 if (!isset($this->instances[$cm->modname])) {
255 $this->instances[$cm->modname] = array();
256 }
257 $this->instances[$cm->modname][$cm->instance] = $cm;
258 $this->cms[$cm->id] = $cm;
259
260 // Reconstruct sections. This works because modules are stored in order
261 if (!isset($this->sections[$cm->sectionnum])) {
262 $this->sections[$cm->sectionnum] = array();
263 }
264 $this->sections[$cm->sectionnum][] = $cm->id;
265 }
266
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();
273 }
274 }
275}
276
277
278/**
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.
281 *
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.
285 */
1e4cb189 286class cm_info extends stdClass {
0d8b6a69 287 /**
288 * State: Only basic data from modinfo cache is available.
289 */
290 const STATE_BASIC = 0;
291
292 /**
293 * State: Dynamic data is available too.
294 */
295 const STATE_DYNAMIC = 1;
296
297 /**
298 * State: View data (for course page) is available.
299 */
300 const STATE_VIEW = 2;
301
302 /**
303 * Parent object
304 * @var course_modinfo
305 */
306 private $modinfo;
307
308 /**
309 * Level of information stored inside this object (STATE_xx constant)
310 * @var int
311 */
312 private $state;
313
314 // Existing data fields
315 ///////////////////////
316
317 /**
318 * Course-module ID - from course_modules table
319 * @var int
320 */
321 public $id;
322
323 /**
324 * Module instance (ID within module table) - from course_modules table
325 * @var int
326 */
327 public $instance;
328
329 /**
330 * Course ID - from course_modules table
331 * @var int
332 */
333 public $course;
334
335 /**
336 * 'ID number' from course-modules table (arbitrary text set by user) - from
337 * course_modules table
338 * @var string
339 */
340 public $idnumber;
341
342 /**
adaeccb6 343 * Time that this course-module was added (unix time) - from course_modules table
344 * @var int
345 */
346 public $added;
347
348 /**
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
354 */
355 public $score;
356
357 /**
358 * Visible setting (0 or 1; if this is 0, students cannot see/access the activity) - from
0d8b6a69 359 * course_modules table
360 * @var int
361 */
362 public $visible;
363
adaeccb6 364 /**
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
368 */
369 public $visibleold;
370
0d8b6a69 371 /**
372 * Group mode (one of the constants NONE, SEPARATEGROUPS, or VISIBLEGROUPS) - from
373 * course_modules table
374 * @var int
375 */
376 public $groupmode;
377
378 /**
379 * Grouping ID (0 = all groupings)
380 * @var int
381 */
382 public $groupingid;
383
384 /**
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
389 */
390 public $groupmembersonly;
391
392 /**
393 * Indent level on course page (0 = no indent) - from course_modules table
394 * @var int
395 */
396 public $indent;
397
398 /**
399 * Activity completion setting for this activity, COMPLETION_TRACKING_xx constant - from
400 * course_modules table
401 * @var int
402 */
403 public $completion;
404
adaeccb6 405 /**
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
410 */
411 public $completiongradeitemnumber;
412
413 /**
414 * 1 if 'on view' completion is enabled, 0 otherwise - from course_modules table
415 * @var int
416 */
417 public $completionview;
418
419 /**
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
423 */
424 public $completionexpected;
425
0d8b6a69 426 /**
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
430 */
431 public $availablefrom;
432
433 /**
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
437 */
438 public $availableuntil;
439
440 /**
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
445 */
446 public $showavailability;
447
8c40662e 448 /**
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
453 */
454 public $showdescription;
455
0d8b6a69 456 /**
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
461 */
462 public $extra;
463
464 /**
465 * Name of icon to use - from cached data in modinfo field
466 * @var string
467 */
468 public $icon;
469
470 /**
471 * Component that contains icon - from cached data in modinfo field
472 * @var string
473 */
474 public $iconcomponent;
475
476 /**
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
480 */
481 public $modname;
482
adaeccb6 483 /**
484 * ID of module - from course_modules table
485 * @var int
486 */
487 public $module;
488
0d8b6a69 489 /**
490 * Name of module instance for display on page e.g. 'General discussion forum' - from cached
491 * data in modinfo field
492 * @var string
493 */
494 public $name;
495
496 /**
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
500 */
501 public $sectionnum;
502
adaeccb6 503 /**
504 * Section id - from course_modules table
505 * @var int
506 */
507 public $section;
508
0d8b6a69 509 /**
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
514 */
515 public $conditionscompletion;
516
517 /**
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
521 */
522 public $conditionsgrade;
523
524 /**
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
529 */
530 public $modplural;
531
532 /**
533 * True if this course-module is available to students i.e. if all availability conditions
534 * are met - obtained dynamically
535 * @var bool
536 */
537 public $available;
538
539 /**
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
544 */
545 public $availableinfo;
546
547 /**
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
552 */
553 public $uservisible;
554
4478743c
PS
555 /**
556 * Module context - hacky shortcut
557 * @deprecated
558 * @var stdClass
559 */
560 public $context;
561
562
0d8b6a69 563 // New data available only via functions
564 ////////////////////////////////////////
565
566 /**
567 * @var moodle_url
568 */
569 private $url;
570
571 /**
572 * @var string
573 */
574 private $content;
575
576 /**
577 * @var string
578 */
579 private $extraclasses;
580
581 /**
582 * @var string
583 */
584 private $onclick;
585
586 /**
587 * @var mixed
588 */
589 private $customdata;
590
591 /**
592 * @var string
593 */
594 private $afterlink;
595
596 /**
597 * @var string
598 */
599 private $afterediticons;
600
601 /**
602 * @return bool True if this module has a 'view' page that should be linked to in navigation
603 * etc (note: modules may still have a view.php file, but return false if this is not
604 * intended to be linked to from 'normal' parts of the interface; this is what label does).
605 */
606 public function has_view() {
607 return !is_null($this->url);
608 }
609
610 /**
611 * @return moodle_url URL to link to for this module, or null if it doesn't have a view page
612 */
613 public function get_url() {
614 return $this->url;
615 }
616
617 /**
618 * Obtains content to display on main (view) page.
619 * Note: Will collect view data, if not already obtained.
620 * @return string Content to display on main page below link, or empty string if none
621 */
622 public function get_content() {
623 $this->obtain_view_data();
624 return $this->content;
625 }
626
627 /**
628 * Note: Will collect view data, if not already obtained.
629 * @return string Extra CSS classes to add to html output for this activity on main page
630 */
631 public function get_extra_classes() {
632 $this->obtain_view_data();
633 return $this->extraclasses;
634 }
635
636 /**
637 * @return string Content of HTML on-click attribute. This string will be used literally
638 * as a string so should be pre-escaped.
639 */
640 public function get_on_click() {
641 // Does not need view data; may be used by navigation
642 return $this->onclick;
643 }
644 /**
645 * @return mixed Optional custom data stored in modinfo cache for this activity, or null if none
646 */
647 public function get_custom_data() {
648 return $this->customdata;
649 }
650
651 /**
652 * Note: Will collect view data, if not already obtained.
653 * @return string Extra HTML code to display after link
654 */
655 public function get_after_link() {
656 $this->obtain_view_data();
657 return $this->afterlink;
658 }
659
660 /**
661 * Note: Will collect view data, if not already obtained.
662 * @return string Extra HTML code to display after editing icons (e.g. more icons)
663 */
664 public function get_after_edit_icons() {
665 $this->obtain_view_data();
666 return $this->afterediticons;
667 }
668
669 /**
670 * @param moodle_core_renderer $output Output render to use, or null for default (global)
671 * @return moodle_url Icon URL for a suitable icon to put beside this cm
672 */
673 public function get_icon_url($output = null) {
674 global $OUTPUT;
675 if (!$output) {
676 $output = $OUTPUT;
677 }
678 if (!empty($this->icon)) {
679 if (substr($this->icon, 0, 4) === 'mod/') {
680 list($modname, $iconname) = explode('/', substr($this->icon, 4), 2);
681 $icon = $output->pix_url($iconname, $modname);
682 } else {
683 if (!empty($this->iconcomponent)) {
684 // Icon has specified component
685 $icon = $output->pix_url($this->icon, $this->iconcomponent);
686 } else {
687 // Icon does not have specified component, use default
688 $icon = $output->pix_url($this->icon);
689 }
690 }
691 } else {
692 $icon = $output->pix_url('icon', $this->modname);
693 }
694 return $icon;
695 }
696
697 /**
698 * @return course_modinfo Modinfo object that this came from
699 */
700 public function get_modinfo() {
701 return $this->modinfo;
702 }
703
704 /**
705 * @return object Moodle course object that was used to construct this data
706 */
707 public function get_course() {
708 return $this->modinfo->get_course();
709 }
710
711 // Set functions
712 ////////////////
713
714 /**
715 * Sets content to display on course view page below link (if present).
716 * @param string $content New content as HTML string (empty string if none)
717 * @return void
718 */
719 public function set_content($content) {
720 $this->content = $content;
721 }
722
723 /**
724 * Sets extra classes to include in CSS.
725 * @param string $extraclasses Extra classes (empty string if none)
726 * @return void
727 */
728 public function set_extra_classes($extraclasses) {
729 $this->extraclasses = $extraclasses;
730 }
731
732 /**
733 * Sets value of on-click attribute for JavaScript.
734 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
735 * @param string $onclick New onclick attribute which should be HTML-escaped
736 * (empty string if none)
737 * @return void
738 */
739 public function set_on_click($onclick) {
740 $this->check_not_view_only();
741 $this->onclick = $onclick;
742 }
743
744 /**
745 * Sets HTML that displays after link on course view page.
746 * @param string $afterlink HTML string (empty string if none)
747 * @return void
748 */
749 public function set_after_link($afterlink) {
750 $this->afterlink = $afterlink;
751 }
752
753 /**
754 * Sets HTML that displays after edit icons on course view page.
755 * @param string $afterediticons HTML string (empty string if none)
756 * @return void
757 */
758 public function set_after_edit_icons($afterediticons) {
759 $this->afterediticons = $afterediticons;
760 }
761
762 /**
763 * Changes the name (text of link) for this module instance.
764 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
765 * @param string $name Name of activity / link text
766 * @return void
767 */
768 public function set_name($name) {
769 $this->update_user_visible();
770 $this->name = $name;
771 }
772
773 /**
774 * Turns off the view link for this module instance.
775 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
776 * @return void
777 */
778 public function set_no_view_link() {
779 $this->check_not_view_only();
4478743c 780 $this->url = null;
0d8b6a69 781 }
782
783 /**
784 * Sets the 'uservisible' flag. This can be used (by setting false) to prevent access and
785 * display of this module link for the current user.
786 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
787 * @param bool $uservisible
788 * @return void
789 */
790 public function set_user_visible($uservisible) {
791 $this->check_not_view_only();
792 $this->uservisible = $uservisible;
793 }
794
795 /**
796 * Sets the 'available' flag and related details. This flag is normally used to make
797 * course modules unavailable until a certain date or condition is met. (When a course
798 * module is unavailable, it is still visible to users who have viewhiddenactivities
799 * permission.)
800 *
801 * When this is function is called, user-visible status is recalculated automatically.
802 *
803 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
804 * @param bool $available False if this item is not 'available'
805 * @param int $showavailability 0 = do not show this item at all if it's not available,
806 * 1 = show this item greyed out with the following message
807 * @param string $availableinfo Information about why this is not available which displays
808 * to those who have viewhiddenactivities, and to everyone if showavailability is set;
809 * note that this function replaces the existing data (if any)
810 * @return void
811 */
812 public function set_available($available, $showavailability=0, $availableinfo='') {
813 $this->check_not_view_only();
814 $this->available = $available;
815 $this->showavailability = $showavailability;
816 $this->availableinfo = $availableinfo;
817 $this->update_user_visible();
818 }
819
820 /**
821 * Some set functions can only be called from _cm_info_dynamic and not _cm_info_view.
822 * This is because they may affect parts of this object which are used on pages other
823 * than the view page (e.g. in the navigation block, or when checking access on
824 * module pages).
825 * @return void
826 */
827 private function check_not_view_only() {
828 if ($this->state >= self::STATE_DYNAMIC) {
829 throw new coding_exception('Cannot set this data from _cm_info_view because it may ' .
830 'affect other pages as well as view');
831 }
832 }
833
834 /**
835 * Constructor should not be called directly; use get_fast_modinfo.
836 * @param course_modinfo $modinfo Parent object
837 * @param object $course Course row
838 * @param object $mod Module object from the modinfo field of course table
839 * @param object $info Entire object from modinfo field of course table
840 */
841 public function __construct(course_modinfo $modinfo, $course, $mod, $info) {
842 global $CFG;
843 $this->modinfo = $modinfo;
844
845 $this->id = $mod->cm;
846 $this->instance = $mod->id;
847 $this->course = $course->id;
848 $this->modname = $mod->mod;
849 $this->idnumber = isset($mod->idnumber) ? $mod->idnumber : '';
850 $this->name = $mod->name;
851 $this->visible = $mod->visible;
adaeccb6 852 $this->sectionnum = $mod->section; // Note weirdness with name here
0d8b6a69 853 $this->groupmode = isset($mod->groupmode) ? $mod->groupmode : 0;
854 $this->groupingid = isset($mod->groupingid) ? $mod->groupingid : 0;
855 $this->groupmembersonly = isset($mod->groupmembersonly) ? $mod->groupmembersonly : 0;
856 $this->indent = isset($mod->indent) ? $mod->indent : 0;
0d8b6a69 857 $this->extra = isset($mod->extra) ? $mod->extra : '';
858 $this->extraclasses = isset($mod->extraclasses) ? $mod->extraclasses : '';
859 $this->onclick = isset($mod->onclick) ? $mod->onclick : '';
860 $this->content = isset($mod->content) ? $mod->content : '';
861 $this->icon = isset($mod->icon) ? $mod->icon : '';
862 $this->iconcomponent = isset($mod->iconcomponent) ? $mod->iconcomponent : '';
863 $this->customdata = isset($mod->customdata) ? $mod->customdata : '';
4478743c 864 $this->context = get_context_instance(CONTEXT_MODULE, $mod->cm);
8c40662e 865 $this->showdescription = isset($mod->showdescription) ? $mod->showdescription : 0;
0d8b6a69 866 $this->state = self::STATE_BASIC;
867
868 // This special case handles old label data. Labels used to use the 'name' field for
869 // content
870 if ($this->modname === 'label' && $this->content === '') {
871 $this->content = $this->extra;
872 $this->extra = '';
873 }
874
adaeccb6 875 // Note: These fields from $cm were not present in cm_info in Moodle
876 // 2.0.2 and prior. They may not be available if course cache hasn't
877 // been rebuilt since then.
878 $this->section = isset($mod->sectionid) ? $mod->sectionid : 0;
879 $this->module = isset($mod->module) ? $mod->module : 0;
880 $this->added = isset($mod->added) ? $mod->added : 0;
881 $this->score = isset($mod->score) ? $mod->score : 0;
882 $this->visibleold = isset($mod->visibleold) ? $mod->visibleold : 0;
883
884 // Note: it saves effort and database space to always include the
885 // availability and completion fields, even if availability or completion
886 // are actually disabled
887 $this->completion = isset($mod->completion) ? $mod->completion : 0;
888 $this->completiongradeitemnumber = isset($mod->completiongradeitemnumber)
889 ? $mod->completiongradeitemnumber : null;
890 $this->completionview = isset($mod->completionview)
891 ? $mod->completionview : 0;
892 $this->completionexpected = isset($mod->completionexpected)
893 ? $mod->completionexpected : 0;
894 $this->showavailability = isset($mod->showavailability) ? $mod->showavailability : 0;
895 $this->availablefrom = isset($mod->availablefrom) ? $mod->availablefrom : 0;
896 $this->availableuntil = isset($mod->availableuntil) ? $mod->availableuntil : 0;
897 $this->conditionscompletion = isset($mod->conditionscompletion)
898 ? $mod->conditionscompletion : array();
899 $this->conditionsgrade = isset($mod->conditionsgrade)
900 ? $mod->conditionsgrade : array();
0d8b6a69 901
902 // Get module plural name.
903 // TODO This was a very old performance hack and should now be removed as the information
904 // certainly doesn't belong in modinfo. On a 'normal' page this is only used in the
905 // activity_modules block, so if it needs caching, it should be cached there.
906 static $modplurals;
907 if (!isset($modplurals[$this->modname])) {
908 $modplurals[$this->modname] = get_string('modulenameplural', $this->modname);
909 }
910 $this->modplural = $modplurals[$this->modname];
911
912 static $modviews;
913 if (!isset($modviews[$this->modname])) {
914 $modviews[$this->modname] = !plugin_supports('mod', $this->modname,
915 FEATURE_NO_VIEW_LINK);
916 }
917 $this->url = $modviews[$this->modname]
918 ? new moodle_url('/mod/' . $this->modname . '/view.php', array('id'=>$this->id))
919 : null;
920 }
921
922 /**
923 * If dynamic data for this course-module is not yet available, gets it.
924 *
925 * This function is automatically called when constructing course_modinfo, so users don't
926 * need to call it.
927 *
928 * Dynamic data is data which does not come directly from the cache but is calculated at
929 * runtime based on the current user. Primarily this concerns whether the user can access
930 * the module or not.
931 *
932 * As part of this function, the module's _cm_info_dynamic function from its lib.php will
933 * be called (if it exists).
934 * @return void
935 */
936 public function obtain_dynamic_data() {
937 global $CFG;
938 if ($this->state >= self::STATE_DYNAMIC) {
939 return;
940 }
941 $userid = $this->modinfo->get_user_id();
942
943 if (!empty($CFG->enableavailability)) {
944 // Get availability information
945 $ci = new condition_info($this);
946 // Note that the modinfo currently available only includes minimal details (basic data)
947 // so passing it to this function is a bit dangerous as it would cause infinite
948 // recursion if it tried to get dynamic data, however we know that this function only
949 // uses basic data.
950 $this->available = $ci->is_available($this->availableinfo, true,
951 $userid, $this->modinfo);
952 } else {
953 $this->available = true;
954 }
955
956 // Update visible state for current user
957 $this->update_user_visible();
958
959 // Let module make dynamic changes at this point
960 $this->call_mod_function('cm_info_dynamic');
961 $this->state = self::STATE_DYNAMIC;
962 }
963
964 /**
965 * Works out whether activity is visible *for current user* - if this is false, they
966 * aren't allowed to access it.
967 * @return void
968 */
969 private function update_user_visible() {
970 global $CFG;
971 $modcontext = get_context_instance(CONTEXT_MODULE, $this->id);
972 $userid = $this->modinfo->get_user_id();
973 $this->uservisible = true;
974 if ((!$this->visible or !$this->available) and
975 !has_capability('moodle/course:viewhiddenactivities', $modcontext, $userid)) {
976 // If the activity is hidden or unavailable, and you don't have viewhiddenactivities,
977 // set it so that user can't see or access it
978 $this->uservisible = false;
979 } else if (!empty($CFG->enablegroupmembersonly) and !empty($this->groupmembersonly)
980 and !has_capability('moodle/site:accessallgroups', $modcontext, $userid)) {
981 // If the activity has 'group members only' and you don't have accessallgroups...
12923c92
PS
982 $groups = $this->modinfo->get_groups($this->groupingid);
983 if (empty($groups)) {
0d8b6a69 984 // ...and you don't belong to a group, then set it so you can't see/access it
985 $this->uservisible = false;
986 }
987 }
988 }
989
990 /**
991 * Calls a module function (if exists), passing in one parameter: this object.
992 * @param string $type Name of function e.g. if this is 'grooblezorb' and the modname is
993 * 'forum' then it will try to call 'mod_forum_grooblezorb' or 'forum_grooblezorb'
994 * @return void
995 */
996 private function call_mod_function($type) {
997 global $CFG;
998 $libfile = $CFG->dirroot . '/mod/' . $this->modname . '/lib.php';
999 if (file_exists($libfile)) {
1000 include_once($libfile);
1001 $function = 'mod_' . $this->modname . '_' . $type;
1002 if (function_exists($function)) {
1003 $function($this);
1004 } else {
1005 $function = $this->modname . '_' . $type;
1006 if (function_exists($function)) {
1007 $function($this);
1008 }
1009 }
1010 }
1011 }
1012
1013 /**
1014 * If view data for this course-module is not yet available, obtains it.
1015 *
1016 * This function is automatically called if any of the functions (marked) which require
1017 * view data are called.
1018 *
1019 * View data is data which is needed only for displaying the course main page (& any similar
1020 * functionality on other pages) but is not needed in general. Obtaining view data may have
1021 * a performance cost.
1022 *
1023 * As part of this function, the module's _cm_info_view function from its lib.php will
1024 * be called (if it exists).
1025 * @return void
1026 */
1027 private function obtain_view_data() {
1028 if ($this->state >= self::STATE_VIEW) {
1029 return;
1030 }
1031
1032 // Let module make changes at this point
1033 $this->call_mod_function('cm_info_view');
1034 $this->state = self::STATE_VIEW;
1035 }
1036}
1037
1038
0d8b6a69 1039/**
1040 * Returns reference to full info about modules in course (including visibility).
1041 * Cached and as fast as possible (0 or 1 db query).
1042 *
1043 * @global object
1044 * @global object
1045 * @global moodle_database
1046 * @uses MAX_MODINFO_CACHE_SIZE
1047 * @param mixed $course object or 'reset' string to reset caches, modinfo may be updated in db
1048 * @param int $userid Defaults to current user id
1049 * @return course_modinfo Module information for course, or null if resetting
1050 */
1051function get_fast_modinfo(&$course, $userid=0) {
1052 global $CFG, $USER, $DB;
1053 require_once($CFG->dirroot.'/course/lib.php');
1054
1055 if (!empty($CFG->enableavailability)) {
1056 require_once($CFG->libdir.'/conditionlib.php');
1057 }
1058
1059 static $cache = array();
1060
1061 if ($course === 'reset') {
1062 $cache = array();
1063 return null;
1064 }
1065
1066 if (empty($userid)) {
1067 $userid = $USER->id;
1068 }
1069
1070 if (array_key_exists($course->id, $cache) and $cache[$course->id]->userid == $userid) {
1071 return $cache[$course->id];
1072 }
1073
1074 if (!property_exists($course, 'modinfo')) {
1075 debugging('Coding problem - missing course modinfo property in get_fast_modinfo() call');
1076 }
1077
1078 unset($cache[$course->id]); // prevent potential reference problems when switching users
1079
adaeccb6 1080 $cache[$course->id] = new course_modinfo($course, $userid);
0d8b6a69 1081
1082 // Ensure cache does not use too much RAM
1083 if (count($cache) > MAX_MODINFO_CACHE_SIZE) {
1084 reset($cache);
1085 $key = key($cache);
0420f8dc
AO
1086 unset($cache[$key]->instances);
1087 unset($cache[$key]->cms);
0d8b6a69 1088 unset($cache[$key]);
1089 }
1090
1091 return $cache[$course->id];
1092}
1093
112d3b49
PS
1094/**
1095 * Rebuilds the cached list of course activities stored in the database
1096 * @param int $courseid - id of course to rebuild, empty means all
1097 * @param boolean $clearonly - only clear the modinfo fields, gets rebuild automatically on the fly
1098 */
1099function rebuild_course_cache($courseid=0, $clearonly=false) {
1100 global $COURSE, $DB, $CFG;
1101
1102 // Destroy navigation caches
1103 navigation_cache::destroy_volatile_caches();
1104
1105 if ($clearonly) {
1106 if (empty($courseid)) {
1107 $courseselect = array();
1108 } else {
1109 $courseselect = array('id'=>$courseid);
1110 }
1111 $DB->set_field('course', 'modinfo', null, $courseselect);
1112 // update cached global COURSE too ;-)
1113 if ($courseid == $COURSE->id or empty($courseid)) {
1114 $COURSE->modinfo = null;
1115 }
1116 // reset the fast modinfo cache
1117 $reset = 'reset';
1118 get_fast_modinfo($reset);
1119 return;
1120 }
1121
1122 require_once("$CFG->dirroot/course/lib.php");
1123
1124 if ($courseid) {
1125 $select = array('id'=>$courseid);
1126 } else {
1127 $select = array();
1128 @set_time_limit(0); // this could take a while! MDL-10954
1129 }
1130
1131 $rs = $DB->get_recordset("course", $select,'','id,fullname');
1132 foreach ($rs as $course) {
1133 $modinfo = serialize(get_array_of_activities($course->id));
1134 $DB->set_field("course", "modinfo", $modinfo, array("id"=>$course->id));
1135 // update cached global COURSE too ;-)
1136 if ($course->id == $COURSE->id) {
1137 $COURSE->modinfo = $modinfo;
1138 }
1139 }
1140 $rs->close();
1141 // reset the fast modinfo cache
1142 $reset = 'reset';
1143 get_fast_modinfo($reset);
1144}
1145
0d8b6a69 1146
1147/**
1148 * Class that is the return value for the _get_coursemodule_info module API function.
1149 *
1150 * Note: For backward compatibility, you can also return a stdclass object from that function.
1151 * The difference is that the stdclass object may contain an 'extra' field (deprecated because
1152 * it was crazy, except for label which uses it differently). The stdclass object may not contain
1153 * the new fields defined here (content, extraclasses, customdata).
1154 */
1155class cached_cm_info {
1156 /**
1157 * Name (text of link) for this activity; Leave unset to accept default name
1158 * @var string
1159 */
1160 public $name;
1161
1162 /**
1163 * Name of icon for this activity. Normally, this should be used together with $iconcomponent
1164 * to define the icon, as per pix_url function.
1165 * For backward compatibility, if this value is of the form 'mod/forum/icon' then an icon
1166 * within that module will be used.
1167 * @see cm_info::get_icon_url()
1168 * @see renderer_base::pix_url()
1169 * @var string
1170 */
1171 public $icon;
1172
1173 /**
1174 * Component for icon for this activity, as per pix_url; leave blank to use default 'moodle'
1175 * component
1176 * @see renderer_base::pix_url()
1177 * @var string
1178 */
1179 public $iconcomponent;
1180
1181 /**
1182 * HTML content to be displayed on the main page below the link (if any) for this course-module
1183 * @var string
1184 */
1185 public $content;
1186
1187 /**
1188 * Custom data to be stored in modinfo for this activity; useful if there are cases when
1189 * internal information for this activity type needs to be accessible from elsewhere on the
1190 * course without making database queries. May be of any type but should be short.
1191 * @var mixed
1192 */
1193 public $customdata;
1194
1195 /**
1196 * Extra CSS class or classes to be added when this activity is displayed on the main page;
1197 * space-separated string
1198 * @var string
1199 */
1200 public $extraclasses;
1201
1202 /**
1203 * Content of onclick JavaScript; escaped HTML to be inserted as attribute value
1204 * @var string
1205 */
1206 public $onclick;
1207}