Merge branch 'MDL-26440_uid' of git://github.com/andyjdavis/moodle
[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 /**
343 * Visible setting (0 or 1; if this is 0, students cannot see/access the activity) - from
344 * course_modules table
345 * @var int
346 */
347 public $visible;
348
349 /**
350 * Group mode (one of the constants NONE, SEPARATEGROUPS, or VISIBLEGROUPS) - from
351 * course_modules table
352 * @var int
353 */
354 public $groupmode;
355
356 /**
357 * Grouping ID (0 = all groupings)
358 * @var int
359 */
360 public $groupingid;
361
362 /**
363 * Group members only (if set to 1, only members of a suitable group see this link on the
364 * course page; 0 = everyone sees it even if they don't belong to a suitable group) - from
365 * course_modules table
366 * @var int
367 */
368 public $groupmembersonly;
369
370 /**
371 * Indent level on course page (0 = no indent) - from course_modules table
372 * @var int
373 */
374 public $indent;
375
376 /**
377 * Activity completion setting for this activity, COMPLETION_TRACKING_xx constant - from
378 * course_modules table
379 * @var int
380 */
381 public $completion;
382
383 /**
384 * Available date for this activity (0 if not set, or set to seconds since epoch; before this
385 * date, activity does not display to students) - from course_modules table
386 * @var int
387 */
388 public $availablefrom;
389
390 /**
391 * Available until date for this activity (0 if not set, or set to seconds since epoch; from
392 * this date, activity does not display to students) - from course_modules table
393 * @var int
394 */
395 public $availableuntil;
396
397 /**
398 * When activity is unavailable, this field controls whether it is shown to students (0 =
399 * hide completely, 1 = show greyed out with information about when it will be available) -
400 * from course_modules table
401 * @var int
402 */
403 public $showavailability;
404
405 /**
406 * Extra HTML that is put in an unhelpful part of the HTML when displaying this module in
407 * course page - from cached data in modinfo field
408 * @deprecated This is crazy, don't use it. Replaced by ->extraclasses and ->onclick
409 * @var string
410 */
411 public $extra;
412
413 /**
414 * Name of icon to use - from cached data in modinfo field
415 * @var string
416 */
417 public $icon;
418
419 /**
420 * Component that contains icon - from cached data in modinfo field
421 * @var string
422 */
423 public $iconcomponent;
424
425 /**
426 * Name of module e.g. 'forum' (this is the same name as the module's main database
427 * table) - from cached data in modinfo field
428 * @var string
429 */
430 public $modname;
431
432 /**
433 * Name of module instance for display on page e.g. 'General discussion forum' - from cached
434 * data in modinfo field
435 * @var string
436 */
437 public $name;
438
439 /**
440 * Section number that this course-module is in (section 0 = above the calendar, section 1
441 * = week/topic 1, etc) - from cached data in modinfo field
442 * @var string
443 */
444 public $sectionnum;
445
446 /**
447 * Availability conditions for this course-module based on the completion of other
448 * course-modules (array from other course-module id to required completion state for that
449 * module) - from cached data in modinfo field
450 * @var array
451 */
452 public $conditionscompletion;
453
454 /**
455 * Availability conditions for this course-module based on course grades (array from
456 * grade item id to object with ->min, ->max fields) - from cached data in modinfo field
457 * @var array
458 */
459 public $conditionsgrade;
460
461 /**
462 * Plural name of module type, e.g. 'Forums' - from lang file
463 * @deprecated Do not use this value (you can obtain it by calling get_string instead); it
464 * will be removed in a future version (see later TODO in this file)
465 * @var string
466 */
467 public $modplural;
468
469 /**
470 * True if this course-module is available to students i.e. if all availability conditions
471 * are met - obtained dynamically
472 * @var bool
473 */
474 public $available;
475
476 /**
477 * If course-module is not available to students, this string gives information about
478 * availability which can be displayed to students and/or staff (e.g. 'Available from 3
479 * January 2010') for display on main page - obtained dynamically
480 * @var string
481 */
482 public $availableinfo;
483
484 /**
485 * True if this course-module is available to the CURRENT user (for example, if current user
486 * has viewhiddenactivities capability, they can access the course-module even if it is not
487 * visible or not available, so this would be true in that case)
488 * @var bool
489 */
490 public $uservisible;
491
4478743c
PS
492 /**
493 * Module context - hacky shortcut
494 * @deprecated
495 * @var stdClass
496 */
497 public $context;
498
499
0d8b6a69 500 // New data available only via functions
501 ////////////////////////////////////////
502
503 /**
504 * @var moodle_url
505 */
506 private $url;
507
508 /**
509 * @var string
510 */
511 private $content;
512
513 /**
514 * @var string
515 */
516 private $extraclasses;
517
518 /**
519 * @var string
520 */
521 private $onclick;
522
523 /**
524 * @var mixed
525 */
526 private $customdata;
527
528 /**
529 * @var string
530 */
531 private $afterlink;
532
533 /**
534 * @var string
535 */
536 private $afterediticons;
537
538 /**
539 * @return bool True if this module has a 'view' page that should be linked to in navigation
540 * etc (note: modules may still have a view.php file, but return false if this is not
541 * intended to be linked to from 'normal' parts of the interface; this is what label does).
542 */
543 public function has_view() {
544 return !is_null($this->url);
545 }
546
547 /**
548 * @return moodle_url URL to link to for this module, or null if it doesn't have a view page
549 */
550 public function get_url() {
551 return $this->url;
552 }
553
554 /**
555 * Obtains content to display on main (view) page.
556 * Note: Will collect view data, if not already obtained.
557 * @return string Content to display on main page below link, or empty string if none
558 */
559 public function get_content() {
560 $this->obtain_view_data();
561 return $this->content;
562 }
563
564 /**
565 * Note: Will collect view data, if not already obtained.
566 * @return string Extra CSS classes to add to html output for this activity on main page
567 */
568 public function get_extra_classes() {
569 $this->obtain_view_data();
570 return $this->extraclasses;
571 }
572
573 /**
574 * @return string Content of HTML on-click attribute. This string will be used literally
575 * as a string so should be pre-escaped.
576 */
577 public function get_on_click() {
578 // Does not need view data; may be used by navigation
579 return $this->onclick;
580 }
581 /**
582 * @return mixed Optional custom data stored in modinfo cache for this activity, or null if none
583 */
584 public function get_custom_data() {
585 return $this->customdata;
586 }
587
588 /**
589 * Note: Will collect view data, if not already obtained.
590 * @return string Extra HTML code to display after link
591 */
592 public function get_after_link() {
593 $this->obtain_view_data();
594 return $this->afterlink;
595 }
596
597 /**
598 * Note: Will collect view data, if not already obtained.
599 * @return string Extra HTML code to display after editing icons (e.g. more icons)
600 */
601 public function get_after_edit_icons() {
602 $this->obtain_view_data();
603 return $this->afterediticons;
604 }
605
606 /**
607 * @param moodle_core_renderer $output Output render to use, or null for default (global)
608 * @return moodle_url Icon URL for a suitable icon to put beside this cm
609 */
610 public function get_icon_url($output = null) {
611 global $OUTPUT;
612 if (!$output) {
613 $output = $OUTPUT;
614 }
615 if (!empty($this->icon)) {
616 if (substr($this->icon, 0, 4) === 'mod/') {
617 list($modname, $iconname) = explode('/', substr($this->icon, 4), 2);
618 $icon = $output->pix_url($iconname, $modname);
619 } else {
620 if (!empty($this->iconcomponent)) {
621 // Icon has specified component
622 $icon = $output->pix_url($this->icon, $this->iconcomponent);
623 } else {
624 // Icon does not have specified component, use default
625 $icon = $output->pix_url($this->icon);
626 }
627 }
628 } else {
629 $icon = $output->pix_url('icon', $this->modname);
630 }
631 return $icon;
632 }
633
634 /**
635 * @return course_modinfo Modinfo object that this came from
636 */
637 public function get_modinfo() {
638 return $this->modinfo;
639 }
640
641 /**
642 * @return object Moodle course object that was used to construct this data
643 */
644 public function get_course() {
645 return $this->modinfo->get_course();
646 }
647
648 // Set functions
649 ////////////////
650
651 /**
652 * Sets content to display on course view page below link (if present).
653 * @param string $content New content as HTML string (empty string if none)
654 * @return void
655 */
656 public function set_content($content) {
657 $this->content = $content;
658 }
659
660 /**
661 * Sets extra classes to include in CSS.
662 * @param string $extraclasses Extra classes (empty string if none)
663 * @return void
664 */
665 public function set_extra_classes($extraclasses) {
666 $this->extraclasses = $extraclasses;
667 }
668
669 /**
670 * Sets value of on-click attribute for JavaScript.
671 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
672 * @param string $onclick New onclick attribute which should be HTML-escaped
673 * (empty string if none)
674 * @return void
675 */
676 public function set_on_click($onclick) {
677 $this->check_not_view_only();
678 $this->onclick = $onclick;
679 }
680
681 /**
682 * Sets HTML that displays after link on course view page.
683 * @param string $afterlink HTML string (empty string if none)
684 * @return void
685 */
686 public function set_after_link($afterlink) {
687 $this->afterlink = $afterlink;
688 }
689
690 /**
691 * Sets HTML that displays after edit icons on course view page.
692 * @param string $afterediticons HTML string (empty string if none)
693 * @return void
694 */
695 public function set_after_edit_icons($afterediticons) {
696 $this->afterediticons = $afterediticons;
697 }
698
699 /**
700 * Changes the name (text of link) for this module instance.
701 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
702 * @param string $name Name of activity / link text
703 * @return void
704 */
705 public function set_name($name) {
706 $this->update_user_visible();
707 $this->name = $name;
708 }
709
710 /**
711 * Turns off the view link for this module instance.
712 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
713 * @return void
714 */
715 public function set_no_view_link() {
716 $this->check_not_view_only();
4478743c 717 $this->url = null;
0d8b6a69 718 }
719
720 /**
721 * Sets the 'uservisible' flag. This can be used (by setting false) to prevent access and
722 * display of this module link for the current user.
723 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
724 * @param bool $uservisible
725 * @return void
726 */
727 public function set_user_visible($uservisible) {
728 $this->check_not_view_only();
729 $this->uservisible = $uservisible;
730 }
731
732 /**
733 * Sets the 'available' flag and related details. This flag is normally used to make
734 * course modules unavailable until a certain date or condition is met. (When a course
735 * module is unavailable, it is still visible to users who have viewhiddenactivities
736 * permission.)
737 *
738 * When this is function is called, user-visible status is recalculated automatically.
739 *
740 * Note: May not be called from _cm_info_view (only _cm_info_dynamic).
741 * @param bool $available False if this item is not 'available'
742 * @param int $showavailability 0 = do not show this item at all if it's not available,
743 * 1 = show this item greyed out with the following message
744 * @param string $availableinfo Information about why this is not available which displays
745 * to those who have viewhiddenactivities, and to everyone if showavailability is set;
746 * note that this function replaces the existing data (if any)
747 * @return void
748 */
749 public function set_available($available, $showavailability=0, $availableinfo='') {
750 $this->check_not_view_only();
751 $this->available = $available;
752 $this->showavailability = $showavailability;
753 $this->availableinfo = $availableinfo;
754 $this->update_user_visible();
755 }
756
757 /**
758 * Some set functions can only be called from _cm_info_dynamic and not _cm_info_view.
759 * This is because they may affect parts of this object which are used on pages other
760 * than the view page (e.g. in the navigation block, or when checking access on
761 * module pages).
762 * @return void
763 */
764 private function check_not_view_only() {
765 if ($this->state >= self::STATE_DYNAMIC) {
766 throw new coding_exception('Cannot set this data from _cm_info_view because it may ' .
767 'affect other pages as well as view');
768 }
769 }
770
771 /**
772 * Constructor should not be called directly; use get_fast_modinfo.
773 * @param course_modinfo $modinfo Parent object
774 * @param object $course Course row
775 * @param object $mod Module object from the modinfo field of course table
776 * @param object $info Entire object from modinfo field of course table
777 */
778 public function __construct(course_modinfo $modinfo, $course, $mod, $info) {
779 global $CFG;
780 $this->modinfo = $modinfo;
781
782 $this->id = $mod->cm;
783 $this->instance = $mod->id;
784 $this->course = $course->id;
785 $this->modname = $mod->mod;
786 $this->idnumber = isset($mod->idnumber) ? $mod->idnumber : '';
787 $this->name = $mod->name;
788 $this->visible = $mod->visible;
789 $this->sectionnum = $mod->section;
790 $this->groupmode = isset($mod->groupmode) ? $mod->groupmode : 0;
791 $this->groupingid = isset($mod->groupingid) ? $mod->groupingid : 0;
792 $this->groupmembersonly = isset($mod->groupmembersonly) ? $mod->groupmembersonly : 0;
793 $this->indent = isset($mod->indent) ? $mod->indent : 0;
794 $this->completion = isset($mod->completion) ? $mod->completion : 0;
795 $this->extra = isset($mod->extra) ? $mod->extra : '';
796 $this->extraclasses = isset($mod->extraclasses) ? $mod->extraclasses : '';
797 $this->onclick = isset($mod->onclick) ? $mod->onclick : '';
798 $this->content = isset($mod->content) ? $mod->content : '';
799 $this->icon = isset($mod->icon) ? $mod->icon : '';
800 $this->iconcomponent = isset($mod->iconcomponent) ? $mod->iconcomponent : '';
801 $this->customdata = isset($mod->customdata) ? $mod->customdata : '';
4478743c 802 $this->context = get_context_instance(CONTEXT_MODULE, $mod->cm);
0d8b6a69 803 $this->state = self::STATE_BASIC;
804
805 // This special case handles old label data. Labels used to use the 'name' field for
806 // content
807 if ($this->modname === 'label' && $this->content === '') {
808 $this->content = $this->extra;
809 $this->extra = '';
810 }
811
812 if (!empty($CFG->enableavailability)) {
813 // We must have completion information from modinfo. If it's not
814 // there, cache needs rebuilding
815 if (!isset($mod->showavailability)) {
816 throw new modinfo_rebuild_cache_exception(
817 'enableavailability option was changed; rebuilding '.
818 'cache for course ' . $course->id);
819 }
820 $this->showavailability = $mod->showavailability;
821 $this->availablefrom = isset($mod->availablefrom) ? $mod->availablefrom : 0;
822 $this->availableuntil = isset($mod->availableuntil) ? $mod->availableuntil : 0;
823 $this->conditionscompletion = isset($mod->conditionscompletion)
824 ? $mod->conditionscompletion : array();
825 $this->conditionsgrade = isset($mod->conditionsgrade)
826 ? $mod->conditionsgrade : array();
827 }
828
829 // Get module plural name.
830 // TODO This was a very old performance hack and should now be removed as the information
831 // certainly doesn't belong in modinfo. On a 'normal' page this is only used in the
832 // activity_modules block, so if it needs caching, it should be cached there.
833 static $modplurals;
834 if (!isset($modplurals[$this->modname])) {
835 $modplurals[$this->modname] = get_string('modulenameplural', $this->modname);
836 }
837 $this->modplural = $modplurals[$this->modname];
838
839 static $modviews;
840 if (!isset($modviews[$this->modname])) {
841 $modviews[$this->modname] = !plugin_supports('mod', $this->modname,
842 FEATURE_NO_VIEW_LINK);
843 }
844 $this->url = $modviews[$this->modname]
845 ? new moodle_url('/mod/' . $this->modname . '/view.php', array('id'=>$this->id))
846 : null;
847 }
848
849 /**
850 * If dynamic data for this course-module is not yet available, gets it.
851 *
852 * This function is automatically called when constructing course_modinfo, so users don't
853 * need to call it.
854 *
855 * Dynamic data is data which does not come directly from the cache but is calculated at
856 * runtime based on the current user. Primarily this concerns whether the user can access
857 * the module or not.
858 *
859 * As part of this function, the module's _cm_info_dynamic function from its lib.php will
860 * be called (if it exists).
861 * @return void
862 */
863 public function obtain_dynamic_data() {
864 global $CFG;
865 if ($this->state >= self::STATE_DYNAMIC) {
866 return;
867 }
868 $userid = $this->modinfo->get_user_id();
869
870 if (!empty($CFG->enableavailability)) {
871 // Get availability information
872 $ci = new condition_info($this);
873 // Note that the modinfo currently available only includes minimal details (basic data)
874 // so passing it to this function is a bit dangerous as it would cause infinite
875 // recursion if it tried to get dynamic data, however we know that this function only
876 // uses basic data.
877 $this->available = $ci->is_available($this->availableinfo, true,
878 $userid, $this->modinfo);
879 } else {
880 $this->available = true;
881 }
882
883 // Update visible state for current user
884 $this->update_user_visible();
885
886 // Let module make dynamic changes at this point
887 $this->call_mod_function('cm_info_dynamic');
888 $this->state = self::STATE_DYNAMIC;
889 }
890
891 /**
892 * Works out whether activity is visible *for current user* - if this is false, they
893 * aren't allowed to access it.
894 * @return void
895 */
896 private function update_user_visible() {
897 global $CFG;
898 $modcontext = get_context_instance(CONTEXT_MODULE, $this->id);
899 $userid = $this->modinfo->get_user_id();
900 $this->uservisible = true;
901 if ((!$this->visible or !$this->available) and
902 !has_capability('moodle/course:viewhiddenactivities', $modcontext, $userid)) {
903 // If the activity is hidden or unavailable, and you don't have viewhiddenactivities,
904 // set it so that user can't see or access it
905 $this->uservisible = false;
906 } else if (!empty($CFG->enablegroupmembersonly) and !empty($this->groupmembersonly)
907 and !has_capability('moodle/site:accessallgroups', $modcontext, $userid)) {
908 // If the activity has 'group members only' and you don't have accessallgroups...
12923c92
PS
909 $groups = $this->modinfo->get_groups($this->groupingid);
910 if (empty($groups)) {
0d8b6a69 911 // ...and you don't belong to a group, then set it so you can't see/access it
912 $this->uservisible = false;
913 }
914 }
915 }
916
917 /**
918 * Calls a module function (if exists), passing in one parameter: this object.
919 * @param string $type Name of function e.g. if this is 'grooblezorb' and the modname is
920 * 'forum' then it will try to call 'mod_forum_grooblezorb' or 'forum_grooblezorb'
921 * @return void
922 */
923 private function call_mod_function($type) {
924 global $CFG;
925 $libfile = $CFG->dirroot . '/mod/' . $this->modname . '/lib.php';
926 if (file_exists($libfile)) {
927 include_once($libfile);
928 $function = 'mod_' . $this->modname . '_' . $type;
929 if (function_exists($function)) {
930 $function($this);
931 } else {
932 $function = $this->modname . '_' . $type;
933 if (function_exists($function)) {
934 $function($this);
935 }
936 }
937 }
938 }
939
940 /**
941 * If view data for this course-module is not yet available, obtains it.
942 *
943 * This function is automatically called if any of the functions (marked) which require
944 * view data are called.
945 *
946 * View data is data which is needed only for displaying the course main page (& any similar
947 * functionality on other pages) but is not needed in general. Obtaining view data may have
948 * a performance cost.
949 *
950 * As part of this function, the module's _cm_info_view function from its lib.php will
951 * be called (if it exists).
952 * @return void
953 */
954 private function obtain_view_data() {
955 if ($this->state >= self::STATE_VIEW) {
956 return;
957 }
958
959 // Let module make changes at this point
960 $this->call_mod_function('cm_info_view');
961 $this->state = self::STATE_VIEW;
962 }
963}
964
965
966/**
967 * Special exception that may only be thrown within the constructor for course_modinfo to
968 * indicate that the cache needs to be rebuilt. Not for use anywhere else.
969 */
970class modinfo_rebuild_cache_exception extends coding_exception {
971 function __construct($why) {
972 // If it ever escapes, that's a code bug
973 parent::__construct('This exception should be caught by code', $why);
974 }
975}
976
977
978/**
979 * Returns reference to full info about modules in course (including visibility).
980 * Cached and as fast as possible (0 or 1 db query).
981 *
982 * @global object
983 * @global object
984 * @global moodle_database
985 * @uses MAX_MODINFO_CACHE_SIZE
986 * @param mixed $course object or 'reset' string to reset caches, modinfo may be updated in db
987 * @param int $userid Defaults to current user id
988 * @return course_modinfo Module information for course, or null if resetting
989 */
990function get_fast_modinfo(&$course, $userid=0) {
991 global $CFG, $USER, $DB;
992 require_once($CFG->dirroot.'/course/lib.php');
993
994 if (!empty($CFG->enableavailability)) {
995 require_once($CFG->libdir.'/conditionlib.php');
996 }
997
998 static $cache = array();
999
1000 if ($course === 'reset') {
1001 $cache = array();
1002 return null;
1003 }
1004
1005 if (empty($userid)) {
1006 $userid = $USER->id;
1007 }
1008
1009 if (array_key_exists($course->id, $cache) and $cache[$course->id]->userid == $userid) {
1010 return $cache[$course->id];
1011 }
1012
1013 if (!property_exists($course, 'modinfo')) {
1014 debugging('Coding problem - missing course modinfo property in get_fast_modinfo() call');
1015 }
1016
1017 unset($cache[$course->id]); // prevent potential reference problems when switching users
1018
1019 try {
1020 $cache[$course->id] = new course_modinfo($course, $userid);
1021 } catch (modinfo_rebuild_cache_exception $e) {
1022 debugging($e->debuginfo);
1023 rebuild_course_cache($course->id, true);
1024 $course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
1025 // This second time we don't catch the exception - if you request cache rebuild twice
1026 // in a row, that's a bug => coding_exception
1027 $cache[$course->id] = new course_modinfo($course, $userid);
1028 }
1029
1030 // Ensure cache does not use too much RAM
1031 if (count($cache) > MAX_MODINFO_CACHE_SIZE) {
1032 reset($cache);
1033 $key = key($cache);
1034 unset($cache[$key]);
1035 }
1036
1037 return $cache[$course->id];
1038}
1039
1040
1041/**
1042 * Class that is the return value for the _get_coursemodule_info module API function.
1043 *
1044 * Note: For backward compatibility, you can also return a stdclass object from that function.
1045 * The difference is that the stdclass object may contain an 'extra' field (deprecated because
1046 * it was crazy, except for label which uses it differently). The stdclass object may not contain
1047 * the new fields defined here (content, extraclasses, customdata).
1048 */
1049class cached_cm_info {
1050 /**
1051 * Name (text of link) for this activity; Leave unset to accept default name
1052 * @var string
1053 */
1054 public $name;
1055
1056 /**
1057 * Name of icon for this activity. Normally, this should be used together with $iconcomponent
1058 * to define the icon, as per pix_url function.
1059 * For backward compatibility, if this value is of the form 'mod/forum/icon' then an icon
1060 * within that module will be used.
1061 * @see cm_info::get_icon_url()
1062 * @see renderer_base::pix_url()
1063 * @var string
1064 */
1065 public $icon;
1066
1067 /**
1068 * Component for icon for this activity, as per pix_url; leave blank to use default 'moodle'
1069 * component
1070 * @see renderer_base::pix_url()
1071 * @var string
1072 */
1073 public $iconcomponent;
1074
1075 /**
1076 * HTML content to be displayed on the main page below the link (if any) for this course-module
1077 * @var string
1078 */
1079 public $content;
1080
1081 /**
1082 * Custom data to be stored in modinfo for this activity; useful if there are cases when
1083 * internal information for this activity type needs to be accessible from elsewhere on the
1084 * course without making database queries. May be of any type but should be short.
1085 * @var mixed
1086 */
1087 public $customdata;
1088
1089 /**
1090 * Extra CSS class or classes to be added when this activity is displayed on the main page;
1091 * space-separated string
1092 * @var string
1093 */
1094 public $extraclasses;
1095
1096 /**
1097 * Content of onclick JavaScript; escaped HTML to be inserted as attribute value
1098 * @var string
1099 */
1100 public $onclick;
1101}