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