Commit | Line | Data |
---|---|---|
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 | |
31 | if (!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 | 43 | class 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; | |
ce4dfd27 | 46 | // Array of section data from cache |
47 | private $sectioninfo; | |
0d8b6a69 | 48 | |
49 | // Existing data fields | |
50 | /////////////////////// | |
51 | ||
52 | // These are public for backward compatibility. Note: it is not possible to retain BC | |
53 | // using PHP magic get methods because behaviour is different with regard to empty(). | |
54 | ||
55 | /** | |
56 | * Course ID | |
57 | * @var int | |
58 | * @deprecated For new code, use get_course_id instead. | |
59 | */ | |
60 | public $courseid; | |
61 | ||
62 | /** | |
63 | * User ID | |
64 | * @var int | |
65 | * @deprecated For new code, use get_user_id instead. | |
66 | */ | |
67 | public $userid; | |
68 | ||
69 | /** | |
70 | * Array from int (section num, e.g. 0) => array of int (course-module id); this list only | |
71 | * includes sections that actually contain at least one course-module | |
72 | * @var array | |
73 | * @deprecated For new code, use get_sections instead | |
74 | */ | |
75 | public $sections; | |
76 | ||
77 | /** | |
78 | * Array from int (cm id) => cm_info object | |
79 | * @var array | |
80 | * @deprecated For new code, use get_cms or get_cm instead. | |
81 | */ | |
82 | public $cms; | |
83 | ||
84 | /** | |
85 | * Array from string (modname) => int (instance id) => cm_info object | |
86 | * @var array | |
87 | * @deprecated For new code, use get_instances or get_instances_of instead. | |
88 | */ | |
89 | public $instances; | |
90 | ||
91 | /** | |
92 | * Groups that the current user belongs to. This value is usually not available (set to null) | |
93 | * unless the course has activities set to groupmembersonly. When set, it is an array of | |
94 | * grouping id => array of group id => group id. Includes grouping id 0 for 'all groups'. | |
95 | * @var array | |
96 | * @deprecated Don't use this! For new code, use get_groups. | |
97 | */ | |
98 | public $groups; | |
99 | ||
100 | // Get methods for data | |
101 | /////////////////////// | |
102 | ||
103 | /** | |
104 | * @return object Moodle course object that was used to construct this data | |
105 | */ | |
106 | public function get_course() { | |
107 | return $this->course; | |
108 | } | |
109 | ||
110 | /** | |
111 | * @return int Course ID | |
112 | */ | |
113 | public function get_course_id() { | |
114 | return $this->courseid; | |
115 | } | |
116 | ||
117 | /** | |
118 | * @return int User ID | |
119 | */ | |
120 | public function get_user_id() { | |
121 | return $this->userid; | |
122 | } | |
123 | ||
124 | /** | |
125 | * @return array Array from section number (e.g. 0) to array of course-module IDs in that | |
126 | * section; this only includes sections that contain at least one course-module | |
127 | */ | |
128 | public function get_sections() { | |
129 | return $this->sections; | |
130 | } | |
131 | ||
132 | /** | |
133 | * @return array Array from course-module instance to cm_info object within this course, in | |
134 | * order of appearance | |
135 | */ | |
136 | public function get_cms() { | |
137 | return $this->cms; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Obtains a single course-module object (for a course-module that is on this course). | |
142 | * @param int $cmid Course-module ID | |
143 | * @return cm_info Information about that course-module | |
144 | * @throws moodle_exception If the course-module does not exist | |
145 | */ | |
146 | public function get_cm($cmid) { | |
147 | if (empty($this->cms[$cmid])) { | |
148 | throw new moodle_exception('invalidcoursemodule', 'error'); | |
149 | } | |
150 | return $this->cms[$cmid]; | |
151 | } | |
152 | ||
153 | /** | |
154 | * Obtains all module instances on this course. | |
155 | * @return array Array from module name => array from instance id => cm_info | |
156 | */ | |
157 | public function get_instances() { | |
158 | return $this->instances; | |
159 | } | |
160 | ||
161 | /** | |
162 | * Obtains all instances of a particular module on this course. | |
163 | * @param $modname Name of module (not full frankenstyle) e.g. 'label' | |
164 | * @return array Array from instance id => cm_info for modules on this course; empty if none | |
165 | */ | |
166 | public function get_instances_of($modname) { | |
167 | if (empty($this->instances[$modname])) { | |
168 | return array(); | |
169 | } | |
170 | return $this->instances[$modname]; | |
171 | } | |
172 | ||
173 | /** | |
174 | * Returns groups that the current user belongs to on the course. Note: If not already | |
175 | * available, this may make a database query. | |
176 | * @param int $groupingid Grouping ID or 0 (default) for all groups | |
177 | * @return array Array of int (group id) => int (same group id again); empty array if none | |
178 | */ | |
179 | public function get_groups($groupingid=0) { | |
180 | if (is_null($this->groups)) { | |
181 | // NOTE: Performance could be improved here. The system caches user groups | |
182 | // in $USER->groupmember[$courseid] => array of groupid=>groupid. Unfortunately this | |
183 | // structure does not include grouping information. It probably could be changed to | |
184 | // do so, without a significant performance hit on login, thus saving this one query | |
185 | // each request. | |
186 | $this->groups = groups_get_user_groups($this->courseid, $this->userid); | |
187 | } | |
188 | if (!isset($this->groups[$groupingid])) { | |
189 | return array(); | |
190 | } | |
191 | return $this->groups[$groupingid]; | |
192 | } | |
193 | ||
ce4dfd27 | 194 | /** |
195 | * Gets all sections as array from section number => data about section. | |
196 | * @return array Array of section_info objects organised by section number | |
197 | */ | |
198 | public function get_section_info_all() { | |
199 | return $this->sectioninfo; | |
200 | } | |
201 | ||
202 | /** | |
94dc3c7d EL |
203 | * Gets data about specific numbered section. |
204 | * @param int $sectionnumber Number (not id) of section | |
60a72a03 | 205 | * @param int $strictness Use MUST_EXIST to throw exception if it doesn't |
206 | * @return section_info Information for numbered section or null if not found | |
207 | */ | |
208 | public function get_section_info($sectionnumber, $strictness = IGNORE_MISSING) { | |
209 | if (!array_key_exists($sectionnumber, $this->sectioninfo)) { | |
210 | if ($strictness === MUST_EXIST) { | |
211 | throw new moodle_exception('sectionnotexist'); | |
212 | } else { | |
213 | return null; | |
214 | } | |
215 | } | |
ce4dfd27 | 216 | return $this->sectioninfo[$sectionnumber]; |
217 | } | |
218 | ||
0d8b6a69 | 219 | /** |
220 | * Constructs based on course. | |
221 | * Note: This constructor should not usually be called directly. | |
222 | * Use get_fast_modinfo($course) instead as this maintains a cache. | |
223 | * @param object $course Moodle course object, which may include modinfo | |
224 | * @param int $userid User ID | |
225 | */ | |
226 | public function __construct($course, $userid) { | |
d6f4508c | 227 | global $CFG, $DB; |
0d8b6a69 | 228 | |
ce4dfd27 | 229 | // Check modinfo field is set. If not, build and load it. |
230 | if (empty($course->modinfo) || empty($course->sectioncache)) { | |
231 | rebuild_course_cache($course->id); | |
232 | $course = $DB->get_record('course', array('id'=>$course->id), '*', MUST_EXIST); | |
233 | } | |
234 | ||
0d8b6a69 | 235 | // Set initial values |
236 | $this->courseid = $course->id; | |
237 | $this->userid = $userid; | |
238 | $this->sections = array(); | |
239 | $this->cms = array(); | |
240 | $this->instances = array(); | |
241 | $this->groups = null; | |
242 | $this->course = $course; | |
243 | ||
0d8b6a69 | 244 | // Load modinfo field into memory as PHP object and check it's valid |
245 | $info = unserialize($course->modinfo); | |
246 | if (!is_array($info)) { | |
247 | // hmm, something is wrong - lets try to fix it | |
248 | rebuild_course_cache($course->id); | |
249 | $course->modinfo = $DB->get_field('course', 'modinfo', array('id'=>$course->id)); | |
250 | $info = unserialize($course->modinfo); | |
251 | if (!is_array($info)) { | |
252 | // If it still fails, abort | |
253 | debugging('Problem with "modinfo" data for this course'); | |
254 | return; | |
255 | } | |
256 | } | |
257 | ||
ce4dfd27 | 258 | // Load sectioncache field into memory as PHP object and check it's valid |
259 | $sectioncache = unserialize($course->sectioncache); | |
94e36bc0 | 260 | if (!is_array($sectioncache)) { |
ce4dfd27 | 261 | // hmm, something is wrong - let's fix it |
262 | rebuild_course_cache($course->id); | |
263 | $course->sectioncache = $DB->get_field('course', 'sectioncache', array('id'=>$course->id)); | |
264 | $sectioncache = unserialize($course->sectioncache); | |
265 | if (!is_array($sectioncache)) { | |
266 | // If it still fails, abort | |
267 | debugging('Problem with "sectioncache" data for this course'); | |
268 | return; | |
269 | } | |
270 | } | |
271 | ||
0d8b6a69 | 272 | // If we haven't already preloaded contexts for the course, do it now |
273 | preload_course_contexts($course->id); | |
274 | ||
275 | // Loop through each piece of module data, constructing it | |
276 | $modexists = array(); | |
277 | foreach ($info as $mod) { | |
278 | if (empty($mod->name)) { | |
279 | // something is wrong here | |
280 | continue; | |
281 | } | |
282 | ||
283 | // Skip modules which don't exist | |
284 | if (empty($modexists[$mod->mod])) { | |
285 | if (!file_exists("$CFG->dirroot/mod/$mod->mod/lib.php")) { | |
286 | continue; | |
287 | } | |
288 | $modexists[$mod->mod] = true; | |
289 | } | |
290 | ||
291 | // Construct info for this module | |
292 | $cm = new cm_info($this, $course, $mod, $info); | |
293 | ||
294 | // Store module in instances and cms array | |
295 | if (!isset($this->instances[$cm->modname])) { | |
296 | $this->instances[$cm->modname] = array(); | |
297 | } | |
298 | $this->instances[$cm->modname][$cm->instance] = $cm; | |
299 | $this->cms[$cm->id] = $cm; | |
300 | ||
301 | // Reconstruct sections. This works because modules are stored in order | |
302 | if (!isset($this->sections[$cm->sectionnum])) { | |
303 | $this->sections[$cm->sectionnum] = array(); | |
304 | } | |
305 | $this->sections[$cm->sectionnum][] = $cm->id; | |
306 | } | |
307 | ||
ce4dfd27 | 308 | // Expand section objects |
309 | $this->sectioninfo = array(); | |
310 | foreach ($sectioncache as $number => $data) { | |
311 | // Calculate sequence | |
312 | if (isset($this->sections[$number])) { | |
313 | $sequence = implode(',', $this->sections[$number]); | |
314 | } else { | |
315 | $sequence = ''; | |
316 | } | |
317 | // Expand | |
318 | $this->sectioninfo[$number] = new section_info($data, $number, $course->id, $sequence, | |
319 | $this, $userid); | |
320 | } | |
321 | ||
0d8b6a69 | 322 | // We need at least 'dynamic' data from each course-module (this is basically the remaining |
323 | // data which was always present in previous version of get_fast_modinfo, so it's required | |
324 | // for BC). Creating it in a second pass is necessary because obtain_dynamic_data sometimes | |
325 | // needs to be able to refer to a 'complete' (with basic data) modinfo. | |
326 | foreach ($this->cms as $cm) { | |
327 | $cm->obtain_dynamic_data(); | |
328 | } | |
329 | } | |
ce4dfd27 | 330 | |
331 | /** | |
332 | * Builds a list of information about sections on a course to be stored in | |
333 | * the course cache. (Does not include information that is already cached | |
334 | * in some other way.) | |
335 | * | |
336 | * Used internally by rebuild_course_cache function; do not use otherwise. | |
337 | * @param int $courseid Course ID | |
338 | * @return array Information about sections, indexed by section number (not id) | |
339 | */ | |
340 | public static function build_section_cache($courseid) { | |
341 | global $DB; | |
342 | ||
343 | // Get section data | |
344 | $sections = $DB->get_records('course_sections', array('course' => $courseid), 'section', | |
345 | 'section, id, course, name, summary, summaryformat, sequence, visible, ' . | |
346 | 'availablefrom, availableuntil, showavailability, groupingid'); | |
347 | $compressedsections = array(); | |
348 | ||
349 | // Remove unnecessary data and add availability | |
350 | foreach ($sections as $number => $section) { | |
351 | // Clone just in case it is reused elsewhere (get_all_sections cache) | |
352 | $compressedsections[$number] = clone($section); | |
353 | section_info::convert_for_section_cache($compressedsections[$number]); | |
354 | } | |
355 | ||
356 | return $compressedsections; | |
357 | } | |
0d8b6a69 | 358 | } |
359 | ||
360 | ||
361 | /** | |
362 | * Data about a single module on a course. This contains most of the fields in the course_modules | |
363 | * table, plus additional data when required. | |
364 | * | |
365 | * This object has many public fields; code should treat all these fields as read-only and set | |
366 | * data only using the supplied set functions. Setting the fields directly is not supported | |
367 | * and may cause problems later. | |
368 | */ | |
ce4dfd27 | 369 | class cm_info extends stdClass { |
0d8b6a69 | 370 | /** |
371 | * State: Only basic data from modinfo cache is available. | |
372 | */ | |
373 | const STATE_BASIC = 0; | |
374 | ||
375 | /** | |
376 | * State: Dynamic data is available too. | |
377 | */ | |
378 | const STATE_DYNAMIC = 1; | |
379 | ||
380 | /** | |
381 | * State: View data (for course page) is available. | |
382 | */ | |
383 | const STATE_VIEW = 2; | |
384 | ||
385 | /** | |
386 | * Parent object | |
387 | * @var course_modinfo | |
388 | */ | |
389 | private $modinfo; | |
390 | ||
391 | /** | |
392 | * Level of information stored inside this object (STATE_xx constant) | |
393 | * @var int | |
394 | */ | |
395 | private $state; | |
396 | ||
397 | // Existing data fields | |
398 | /////////////////////// | |
399 | ||
400 | /** | |
401 | * Course-module ID - from course_modules table | |
402 | * @var int | |
403 | */ | |
404 | public $id; | |
405 | ||
406 | /** | |
407 | * Module instance (ID within module table) - from course_modules table | |
408 | * @var int | |
409 | */ | |
410 | public $instance; | |
411 | ||
412 | /** | |
413 | * Course ID - from course_modules table | |
414 | * @var int | |
415 | */ | |
416 | public $course; | |
417 | ||
418 | /** | |
419 | * 'ID number' from course-modules table (arbitrary text set by user) - from | |
420 | * course_modules table | |
421 | * @var string | |
422 | */ | |
423 | public $idnumber; | |
424 | ||
425 | /** | |
adaeccb6 | 426 | * Time that this course-module was added (unix time) - from course_modules table |
427 | * @var int | |
428 | */ | |
429 | public $added; | |
430 | ||
431 | /** | |
432 | * This variable is not used and is included here only so it can be documented. | |
433 | * Once the database entry is removed from course_modules, it should be deleted | |
434 | * here too. | |
435 | * @var int | |
436 | * @deprecated Do not use this variable | |
437 | */ | |
438 | public $score; | |
439 | ||
440 | /** | |
441 | * Visible setting (0 or 1; if this is 0, students cannot see/access the activity) - from | |
0d8b6a69 | 442 | * course_modules table |
443 | * @var int | |
444 | */ | |
445 | public $visible; | |
446 | ||
adaeccb6 | 447 | /** |
448 | * Old visible setting (if the entire section is hidden, the previous value for | |
449 | * visible is stored in this field) - from course_modules table | |
450 | * @var int | |
451 | */ | |
452 | public $visibleold; | |
453 | ||
0d8b6a69 | 454 | /** |
455 | * Group mode (one of the constants NONE, SEPARATEGROUPS, or VISIBLEGROUPS) - from | |
456 | * course_modules table | |
457 | * @var int | |
458 | */ | |
459 | public $groupmode; | |
460 | ||
461 | /** | |
462 | * Grouping ID (0 = all groupings) | |
463 | * @var int | |
464 | */ | |
465 | public $groupingid; | |
466 | ||
467 | /** | |
468 | * Group members only (if set to 1, only members of a suitable group see this link on the | |
469 | * course page; 0 = everyone sees it even if they don't belong to a suitable group) - from | |
470 | * course_modules table | |
471 | * @var int | |
472 | */ | |
473 | public $groupmembersonly; | |
474 | ||
475 | /** | |
476 | * Indent level on course page (0 = no indent) - from course_modules table | |
477 | * @var int | |
478 | */ | |
479 | public $indent; | |
480 | ||
481 | /** | |
482 | * Activity completion setting for this activity, COMPLETION_TRACKING_xx constant - from | |
483 | * course_modules table | |
484 | * @var int | |
485 | */ | |
486 | public $completion; | |
487 | ||
adaeccb6 | 488 | /** |
489 | * Set to the item number (usually 0) if completion depends on a particular | |
490 | * grade of this activity, or null if completion does not depend on a grade - from | |
491 | * course_modules table | |
492 | * @var mixed | |
493 | */ | |
494 | public $completiongradeitemnumber; | |
495 | ||
496 | /** | |
497 | * 1 if 'on view' completion is enabled, 0 otherwise - from course_modules table | |
498 | * @var int | |
499 | */ | |
500 | public $completionview; | |
501 | ||
502 | /** | |
503 | * Set to a unix time if completion of this activity is expected at a | |
504 | * particular time, 0 if no time set - from course_modules table | |
505 | * @var int | |
506 | */ | |
507 | public $completionexpected; | |
508 | ||
0d8b6a69 | 509 | /** |
510 | * Available date for this activity (0 if not set, or set to seconds since epoch; before this | |
511 | * date, activity does not display to students) - from course_modules table | |
512 | * @var int | |
513 | */ | |
514 | public $availablefrom; | |
515 | ||
516 | /** | |
517 | * Available until date for this activity (0 if not set, or set to seconds since epoch; from | |
518 | * this date, activity does not display to students) - from course_modules table | |
519 | * @var int | |
520 | */ | |
521 | public $availableuntil; | |
522 | ||
523 | /** | |
524 | * When activity is unavailable, this field controls whether it is shown to students (0 = | |
525 | * hide completely, 1 = show greyed out with information about when it will be available) - | |
526 | * from course_modules table | |
527 | * @var int | |
528 | */ | |
529 | public $showavailability; | |
530 | ||
8c40662e | 531 | /** |
532 | * Controls whether the description of the activity displays on the course main page (in | |
533 | * addition to anywhere it might display within the activity itself). 0 = do not show | |
534 | * on main page, 1 = show on main page. | |
535 | * @var int | |
536 | */ | |
537 | public $showdescription; | |
538 | ||
0d8b6a69 | 539 | /** |
540 | * Extra HTML that is put in an unhelpful part of the HTML when displaying this module in | |
541 | * course page - from cached data in modinfo field | |
542 | * @deprecated This is crazy, don't use it. Replaced by ->extraclasses and ->onclick | |
543 | * @var string | |
544 | */ | |
545 | public $extra; | |
546 | ||
547 | /** | |
548 | * Name of icon to use - from cached data in modinfo field | |
549 | * @var string | |
550 | */ | |
551 | public $icon; | |
552 | ||
553 | /** | |
554 | * Component that contains icon - from cached data in modinfo field | |
555 | * @var string | |
556 | */ | |
557 | public $iconcomponent; | |
558 | ||
559 | /** | |
560 | * Name of module e.g. 'forum' (this is the same name as the module's main database | |
561 | * table) - from cached data in modinfo field | |
562 | * @var string | |
563 | */ | |
564 | public $modname; | |
565 | ||
adaeccb6 | 566 | /** |
567 | * ID of module - from course_modules table | |
568 | * @var int | |
569 | */ | |
570 | public $module; | |
571 | ||
0d8b6a69 | 572 | /** |
573 | * Name of module instance for display on page e.g. 'General discussion forum' - from cached | |
574 | * data in modinfo field | |
575 | * @var string | |
576 | */ | |
577 | public $name; | |
578 | ||
579 | /** | |
580 | * Section number that this course-module is in (section 0 = above the calendar, section 1 | |
581 | * = week/topic 1, etc) - from cached data in modinfo field | |
582 | * @var string | |
583 | */ | |
584 | public $sectionnum; | |
585 | ||
adaeccb6 | 586 | /** |
587 | * Section id - from course_modules table | |
588 | * @var int | |
589 | */ | |
590 | public $section; | |
591 | ||
0d8b6a69 | 592 | /** |
593 | * Availability conditions for this course-module based on the completion of other | |
594 | * course-modules (array from other course-module id to required completion state for that | |
595 | * module) - from cached data in modinfo field | |
596 | * @var array | |
597 | */ | |
598 | public $conditionscompletion; | |
599 | ||
600 | /** | |
601 | * Availability conditions for this course-module based on course grades (array from | |
602 | * grade item id to object with ->min, ->max fields) - from cached data in modinfo field | |
603 | * @var array | |
604 | */ | |
605 | public $conditionsgrade; | |
606 | ||
607 | /** | |
608 | * Plural name of module type, e.g. 'Forums' - from lang file | |
609 | * @deprecated Do not use this value (you can obtain it by calling get_string instead); it | |
610 | * will be removed in a future version (see later TODO in this file) | |
611 | * @var string | |
612 | */ | |
613 | public $modplural; | |
614 | ||
615 | /** | |
616 | * True if this course-module is available to students i.e. if all availability conditions | |
617 | * are met - obtained dynamically | |
618 | * @var bool | |
619 | */ | |
620 | public $available; | |
621 | ||
622 | /** | |
623 | * If course-module is not available to students, this string gives information about | |
624 | * availability which can be displayed to students and/or staff (e.g. 'Available from 3 | |
625 | * January 2010') for display on main page - obtained dynamically | |
626 | * @var string | |
627 | */ | |
628 | public $availableinfo; | |
629 | ||
630 | /** | |
631 | * True if this course-module is available to the CURRENT user (for example, if current user | |
632 | * has viewhiddenactivities capability, they can access the course-module even if it is not | |
633 | * visible or not available, so this would be true in that case) | |
634 | * @var bool | |
635 | */ | |
636 | public $uservisible; | |
637 | ||
4478743c PS |
638 | /** |
639 | * Module context - hacky shortcut | |
640 | * @deprecated | |
641 | * @var stdClass | |
642 | */ | |
643 | public $context; | |
644 | ||
645 | ||
0d8b6a69 | 646 | // New data available only via functions |
647 | //////////////////////////////////////// | |
648 | ||
649 | /** | |
650 | * @var moodle_url | |
651 | */ | |
652 | private $url; | |
653 | ||
654 | /** | |
655 | * @var string | |
656 | */ | |
657 | private $content; | |
658 | ||
659 | /** | |
660 | * @var string | |
661 | */ | |
662 | private $extraclasses; | |
663 | ||
c443a1cd EL |
664 | /** |
665 | * @var moodle_url full external url pointing to icon image for activity | |
666 | */ | |
667 | private $iconurl; | |
668 | ||
0d8b6a69 | 669 | /** |
670 | * @var string | |
671 | */ | |
672 | private $onclick; | |
673 | ||
674 | /** | |
675 | * @var mixed | |
676 | */ | |
677 | private $customdata; | |
678 | ||
679 | /** | |
680 | * @var string | |
681 | */ | |
682 | private $afterlink; | |
683 | ||
684 | /** | |
685 | * @var string | |
686 | */ | |
687 | private $afterediticons; | |
688 | ||
689 | /** | |
690 | * @return bool True if this module has a 'view' page that should be linked to in navigation | |
691 | * etc (note: modules may still have a view.php file, but return false if this is not | |
692 | * intended to be linked to from 'normal' parts of the interface; this is what label does). | |
693 | */ | |
694 | public function has_view() { | |
695 | return !is_null($this->url); | |
696 | } | |
697 | ||
698 | /** | |
699 | * @return moodle_url URL to link to for this module, or null if it doesn't have a view page | |
700 | */ | |
701 | public function get_url() { | |
702 | return $this->url; | |
703 | } | |
704 | ||
705 | /** | |
706 | * Obtains content to display on main (view) page. | |
707 | * Note: Will collect view data, if not already obtained. | |
708 | * @return string Content to display on main page below link, or empty string if none | |
709 | */ | |
710 | public function get_content() { | |
711 | $this->obtain_view_data(); | |
712 | return $this->content; | |
713 | } | |
714 | ||
715 | /** | |
716 | * Note: Will collect view data, if not already obtained. | |
717 | * @return string Extra CSS classes to add to html output for this activity on main page | |
718 | */ | |
719 | public function get_extra_classes() { | |
720 | $this->obtain_view_data(); | |
721 | return $this->extraclasses; | |
722 | } | |
723 | ||
724 | /** | |
725 | * @return string Content of HTML on-click attribute. This string will be used literally | |
726 | * as a string so should be pre-escaped. | |
727 | */ | |
728 | public function get_on_click() { | |
729 | // Does not need view data; may be used by navigation | |
730 | return $this->onclick; | |
731 | } | |
732 | /** | |
733 | * @return mixed Optional custom data stored in modinfo cache for this activity, or null if none | |
734 | */ | |
735 | public function get_custom_data() { | |
736 | return $this->customdata; | |
737 | } | |
738 | ||
739 | /** | |
740 | * Note: Will collect view data, if not already obtained. | |
c443a1cd | 741 | * @return string Extra HTML code to display after link |
0d8b6a69 | 742 | */ |
743 | public function get_after_link() { | |
744 | $this->obtain_view_data(); | |
745 | return $this->afterlink; | |
746 | } | |
747 | ||
748 | /** | |
749 | * Note: Will collect view data, if not already obtained. | |
750 | * @return string Extra HTML code to display after editing icons (e.g. more icons) | |
751 | */ | |
752 | public function get_after_edit_icons() { | |
753 | $this->obtain_view_data(); | |
754 | return $this->afterediticons; | |
755 | } | |
756 | ||
757 | /** | |
758 | * @param moodle_core_renderer $output Output render to use, or null for default (global) | |
759 | * @return moodle_url Icon URL for a suitable icon to put beside this cm | |
760 | */ | |
761 | public function get_icon_url($output = null) { | |
762 | global $OUTPUT; | |
763 | if (!$output) { | |
764 | $output = $OUTPUT; | |
765 | } | |
c443a1cd EL |
766 | // Support modules setting their own, external, icon image |
767 | if (!empty($this->iconurl)) { | |
768 | $icon = $this->iconurl; | |
769 | ||
770 | // Fallback to normal local icon + component procesing | |
771 | } else if (!empty($this->icon)) { | |
0d8b6a69 | 772 | if (substr($this->icon, 0, 4) === 'mod/') { |
773 | list($modname, $iconname) = explode('/', substr($this->icon, 4), 2); | |
774 | $icon = $output->pix_url($iconname, $modname); | |
775 | } else { | |
776 | if (!empty($this->iconcomponent)) { | |
777 | // Icon has specified component | |
778 | $icon = $output->pix_url($this->icon, $this->iconcomponent); | |
779 | } else { | |
780 | // Icon does not have specified component, use default | |
781 | $icon = $output->pix_url($this->icon); | |
782 | } | |
783 | } | |
784 | } else { | |
785 | $icon = $output->pix_url('icon', $this->modname); | |
786 | } | |
787 | return $icon; | |
788 | } | |
789 | ||
790 | /** | |
791 | * @return course_modinfo Modinfo object that this came from | |
792 | */ | |
793 | public function get_modinfo() { | |
794 | return $this->modinfo; | |
795 | } | |
796 | ||
797 | /** | |
798 | * @return object Moodle course object that was used to construct this data | |
799 | */ | |
800 | public function get_course() { | |
801 | return $this->modinfo->get_course(); | |
802 | } | |
803 | ||
804 | // Set functions | |
805 | //////////////// | |
806 | ||
807 | /** | |
808 | * Sets content to display on course view page below link (if present). | |
809 | * @param string $content New content as HTML string (empty string if none) | |
810 | * @return void | |
811 | */ | |
812 | public function set_content($content) { | |
813 | $this->content = $content; | |
814 | } | |
815 | ||
816 | /** | |
817 | * Sets extra classes to include in CSS. | |
818 | * @param string $extraclasses Extra classes (empty string if none) | |
819 | * @return void | |
820 | */ | |
821 | public function set_extra_classes($extraclasses) { | |
822 | $this->extraclasses = $extraclasses; | |
823 | } | |
824 | ||
c443a1cd EL |
825 | /** |
826 | * Sets the external full url that points to the icon being used | |
827 | * by the activity. Useful for external-tool modules (lti...) | |
828 | * If set, takes precedence over $icon and $iconcomponent | |
829 | * | |
830 | * @param moodle_url $iconurl full external url pointing to icon image for activity | |
831 | * @return void | |
832 | */ | |
833 | public function set_icon_url(moodle_url $iconurl) { | |
834 | $this->iconurl = $iconurl; | |
835 | } | |
836 | ||
0d8b6a69 | 837 | /** |
838 | * Sets value of on-click attribute for JavaScript. | |
839 | * Note: May not be called from _cm_info_view (only _cm_info_dynamic). | |
840 | * @param string $onclick New onclick attribute which should be HTML-escaped | |
841 | * (empty string if none) | |
842 | * @return void | |
843 | */ | |
844 | public function set_on_click($onclick) { | |
845 | $this->check_not_view_only(); | |
846 | $this->onclick = $onclick; | |
847 | } | |
848 | ||
849 | /** | |
850 | * Sets HTML that displays after link on course view page. | |
851 | * @param string $afterlink HTML string (empty string if none) | |
852 | * @return void | |
853 | */ | |
854 | public function set_after_link($afterlink) { | |
855 | $this->afterlink = $afterlink; | |
856 | } | |
857 | ||
858 | /** | |
859 | * Sets HTML that displays after edit icons on course view page. | |
860 | * @param string $afterediticons HTML string (empty string if none) | |
861 | * @return void | |
862 | */ | |
863 | public function set_after_edit_icons($afterediticons) { | |
864 | $this->afterediticons = $afterediticons; | |
865 | } | |
866 | ||
867 | /** | |
868 | * Changes the name (text of link) for this module instance. | |
869 | * Note: May not be called from _cm_info_view (only _cm_info_dynamic). | |
870 | * @param string $name Name of activity / link text | |
871 | * @return void | |
872 | */ | |
873 | public function set_name($name) { | |
874 | $this->update_user_visible(); | |
875 | $this->name = $name; | |
876 | } | |
877 | ||
878 | /** | |
879 | * Turns off the view link for this module instance. | |
880 | * Note: May not be called from _cm_info_view (only _cm_info_dynamic). | |
881 | * @return void | |
882 | */ | |
883 | public function set_no_view_link() { | |
884 | $this->check_not_view_only(); | |
4478743c | 885 | $this->url = null; |
0d8b6a69 | 886 | } |
887 | ||
888 | /** | |
889 | * Sets the 'uservisible' flag. This can be used (by setting false) to prevent access and | |
890 | * display of this module link for the current user. | |
891 | * Note: May not be called from _cm_info_view (only _cm_info_dynamic). | |
892 | * @param bool $uservisible | |
893 | * @return void | |
894 | */ | |
895 | public function set_user_visible($uservisible) { | |
896 | $this->check_not_view_only(); | |
897 | $this->uservisible = $uservisible; | |
898 | } | |
899 | ||
900 | /** | |
901 | * Sets the 'available' flag and related details. This flag is normally used to make | |
902 | * course modules unavailable until a certain date or condition is met. (When a course | |
903 | * module is unavailable, it is still visible to users who have viewhiddenactivities | |
904 | * permission.) | |
905 | * | |
906 | * When this is function is called, user-visible status is recalculated automatically. | |
907 | * | |
908 | * Note: May not be called from _cm_info_view (only _cm_info_dynamic). | |
909 | * @param bool $available False if this item is not 'available' | |
910 | * @param int $showavailability 0 = do not show this item at all if it's not available, | |
911 | * 1 = show this item greyed out with the following message | |
912 | * @param string $availableinfo Information about why this is not available which displays | |
913 | * to those who have viewhiddenactivities, and to everyone if showavailability is set; | |
914 | * note that this function replaces the existing data (if any) | |
915 | * @return void | |
916 | */ | |
917 | public function set_available($available, $showavailability=0, $availableinfo='') { | |
918 | $this->check_not_view_only(); | |
919 | $this->available = $available; | |
920 | $this->showavailability = $showavailability; | |
921 | $this->availableinfo = $availableinfo; | |
922 | $this->update_user_visible(); | |
923 | } | |
924 | ||
925 | /** | |
926 | * Some set functions can only be called from _cm_info_dynamic and not _cm_info_view. | |
927 | * This is because they may affect parts of this object which are used on pages other | |
928 | * than the view page (e.g. in the navigation block, or when checking access on | |
929 | * module pages). | |
930 | * @return void | |
931 | */ | |
932 | private function check_not_view_only() { | |
933 | if ($this->state >= self::STATE_DYNAMIC) { | |
934 | throw new coding_exception('Cannot set this data from _cm_info_view because it may ' . | |
935 | 'affect other pages as well as view'); | |
936 | } | |
937 | } | |
938 | ||
939 | /** | |
940 | * Constructor should not be called directly; use get_fast_modinfo. | |
941 | * @param course_modinfo $modinfo Parent object | |
942 | * @param object $course Course row | |
943 | * @param object $mod Module object from the modinfo field of course table | |
944 | * @param object $info Entire object from modinfo field of course table | |
945 | */ | |
946 | public function __construct(course_modinfo $modinfo, $course, $mod, $info) { | |
947 | global $CFG; | |
948 | $this->modinfo = $modinfo; | |
949 | ||
950 | $this->id = $mod->cm; | |
951 | $this->instance = $mod->id; | |
952 | $this->course = $course->id; | |
953 | $this->modname = $mod->mod; | |
954 | $this->idnumber = isset($mod->idnumber) ? $mod->idnumber : ''; | |
955 | $this->name = $mod->name; | |
956 | $this->visible = $mod->visible; | |
adaeccb6 | 957 | $this->sectionnum = $mod->section; // Note weirdness with name here |
0d8b6a69 | 958 | $this->groupmode = isset($mod->groupmode) ? $mod->groupmode : 0; |
959 | $this->groupingid = isset($mod->groupingid) ? $mod->groupingid : 0; | |
960 | $this->groupmembersonly = isset($mod->groupmembersonly) ? $mod->groupmembersonly : 0; | |
961 | $this->indent = isset($mod->indent) ? $mod->indent : 0; | |
0d8b6a69 | 962 | $this->extra = isset($mod->extra) ? $mod->extra : ''; |
963 | $this->extraclasses = isset($mod->extraclasses) ? $mod->extraclasses : ''; | |
c443a1cd | 964 | $this->iconurl = isset($mod->iconurl) ? $mod->iconurl : ''; |
0d8b6a69 | 965 | $this->onclick = isset($mod->onclick) ? $mod->onclick : ''; |
966 | $this->content = isset($mod->content) ? $mod->content : ''; | |
967 | $this->icon = isset($mod->icon) ? $mod->icon : ''; | |
968 | $this->iconcomponent = isset($mod->iconcomponent) ? $mod->iconcomponent : ''; | |
969 | $this->customdata = isset($mod->customdata) ? $mod->customdata : ''; | |
4478743c | 970 | $this->context = get_context_instance(CONTEXT_MODULE, $mod->cm); |
8c40662e | 971 | $this->showdescription = isset($mod->showdescription) ? $mod->showdescription : 0; |
0d8b6a69 | 972 | $this->state = self::STATE_BASIC; |
973 | ||
974 | // This special case handles old label data. Labels used to use the 'name' field for | |
975 | // content | |
976 | if ($this->modname === 'label' && $this->content === '') { | |
977 | $this->content = $this->extra; | |
978 | $this->extra = ''; | |
979 | } | |
980 | ||
adaeccb6 | 981 | // Note: These fields from $cm were not present in cm_info in Moodle |
982 | // 2.0.2 and prior. They may not be available if course cache hasn't | |
983 | // been rebuilt since then. | |
984 | $this->section = isset($mod->sectionid) ? $mod->sectionid : 0; | |
985 | $this->module = isset($mod->module) ? $mod->module : 0; | |
986 | $this->added = isset($mod->added) ? $mod->added : 0; | |
987 | $this->score = isset($mod->score) ? $mod->score : 0; | |
988 | $this->visibleold = isset($mod->visibleold) ? $mod->visibleold : 0; | |
989 | ||
990 | // Note: it saves effort and database space to always include the | |
991 | // availability and completion fields, even if availability or completion | |
992 | // are actually disabled | |
993 | $this->completion = isset($mod->completion) ? $mod->completion : 0; | |
994 | $this->completiongradeitemnumber = isset($mod->completiongradeitemnumber) | |
995 | ? $mod->completiongradeitemnumber : null; | |
996 | $this->completionview = isset($mod->completionview) | |
997 | ? $mod->completionview : 0; | |
998 | $this->completionexpected = isset($mod->completionexpected) | |
999 | ? $mod->completionexpected : 0; | |
1000 | $this->showavailability = isset($mod->showavailability) ? $mod->showavailability : 0; | |
1001 | $this->availablefrom = isset($mod->availablefrom) ? $mod->availablefrom : 0; | |
1002 | $this->availableuntil = isset($mod->availableuntil) ? $mod->availableuntil : 0; | |
1003 | $this->conditionscompletion = isset($mod->conditionscompletion) | |
1004 | ? $mod->conditionscompletion : array(); | |
1005 | $this->conditionsgrade = isset($mod->conditionsgrade) | |
1006 | ? $mod->conditionsgrade : array(); | |
0d8b6a69 | 1007 | |
1008 | // Get module plural name. | |
1009 | // TODO This was a very old performance hack and should now be removed as the information | |
1010 | // certainly doesn't belong in modinfo. On a 'normal' page this is only used in the | |
1011 | // activity_modules block, so if it needs caching, it should be cached there. | |
1012 | static $modplurals; | |
1013 | if (!isset($modplurals[$this->modname])) { | |
1014 | $modplurals[$this->modname] = get_string('modulenameplural', $this->modname); | |
1015 | } | |
1016 | $this->modplural = $modplurals[$this->modname]; | |
1017 | ||
1018 | static $modviews; | |
1019 | if (!isset($modviews[$this->modname])) { | |
1020 | $modviews[$this->modname] = !plugin_supports('mod', $this->modname, | |
1021 | FEATURE_NO_VIEW_LINK); | |
1022 | } | |
1023 | $this->url = $modviews[$this->modname] | |
1024 | ? new moodle_url('/mod/' . $this->modname . '/view.php', array('id'=>$this->id)) | |
1025 | : null; | |
1026 | } | |
1027 | ||
1028 | /** | |
1029 | * If dynamic data for this course-module is not yet available, gets it. | |
1030 | * | |
1031 | * This function is automatically called when constructing course_modinfo, so users don't | |
1032 | * need to call it. | |
1033 | * | |
1034 | * Dynamic data is data which does not come directly from the cache but is calculated at | |
1035 | * runtime based on the current user. Primarily this concerns whether the user can access | |
1036 | * the module or not. | |
1037 | * | |
1038 | * As part of this function, the module's _cm_info_dynamic function from its lib.php will | |
1039 | * be called (if it exists). | |
1040 | * @return void | |
1041 | */ | |
1042 | public function obtain_dynamic_data() { | |
1043 | global $CFG; | |
1044 | if ($this->state >= self::STATE_DYNAMIC) { | |
1045 | return; | |
1046 | } | |
1047 | $userid = $this->modinfo->get_user_id(); | |
1048 | ||
1049 | if (!empty($CFG->enableavailability)) { | |
1050 | // Get availability information | |
1051 | $ci = new condition_info($this); | |
1052 | // Note that the modinfo currently available only includes minimal details (basic data) | |
1053 | // so passing it to this function is a bit dangerous as it would cause infinite | |
1054 | // recursion if it tried to get dynamic data, however we know that this function only | |
1055 | // uses basic data. | |
1056 | $this->available = $ci->is_available($this->availableinfo, true, | |
1057 | $userid, $this->modinfo); | |
ce4dfd27 | 1058 | |
1059 | // Check parent section | |
1060 | $parentsection = $this->modinfo->get_section_info($this->sectionnum); | |
1061 | if (!$parentsection->available) { | |
1062 | // Do not store info from section here, as that is already | |
1063 | // presented from the section (if appropriate) - just change | |
1064 | // the flag | |
1065 | $this->available = false; | |
1066 | } | |
0d8b6a69 | 1067 | } else { |
1068 | $this->available = true; | |
1069 | } | |
1070 | ||
1071 | // Update visible state for current user | |
1072 | $this->update_user_visible(); | |
1073 | ||
1074 | // Let module make dynamic changes at this point | |
1075 | $this->call_mod_function('cm_info_dynamic'); | |
1076 | $this->state = self::STATE_DYNAMIC; | |
1077 | } | |
1078 | ||
1079 | /** | |
84fad57f AD |
1080 | * Works out whether activity is available to the current user |
1081 | * | |
1082 | * If the activity is unavailable, additional checks are required to determine if its hidden or greyed out | |
1083 | * | |
1084 | * @see is_user_access_restricted_by_group() | |
1085 | * @see is_user_access_restricted_by_conditional_access() | |
0d8b6a69 | 1086 | * @return void |
1087 | */ | |
1088 | private function update_user_visible() { | |
1089 | global $CFG; | |
1090 | $modcontext = get_context_instance(CONTEXT_MODULE, $this->id); | |
1091 | $userid = $this->modinfo->get_user_id(); | |
1092 | $this->uservisible = true; | |
84fad57f AD |
1093 | |
1094 | // If the user cannot access the activity set the uservisible flag to false. | |
1095 | // Additional checks are required to determine whether the activity is entirely hidden or just greyed out. | |
0d8b6a69 | 1096 | if ((!$this->visible or !$this->available) and |
1097 | !has_capability('moodle/course:viewhiddenactivities', $modcontext, $userid)) { | |
84fad57f | 1098 | |
0d8b6a69 | 1099 | $this->uservisible = false; |
8530aac4 | 1100 | } |
84fad57f AD |
1101 | |
1102 | // Check group membership. | |
7765e94f | 1103 | if ($this->is_user_access_restricted_by_group()) { |
84fad57f AD |
1104 | |
1105 | $this->uservisible = false; | |
1106 | // Ensure activity is completely hidden from the user. | |
7765e94f FM |
1107 | $this->showavailability = 0; |
1108 | } | |
1109 | } | |
1110 | ||
1111 | /** | |
84fad57f AD |
1112 | * Checks whether the module's group settings restrict the current user's access |
1113 | * | |
1114 | * @return bool True if the user access is restricted | |
7765e94f FM |
1115 | */ |
1116 | public function is_user_access_restricted_by_group() { | |
1117 | global $CFG; | |
84fad57f AD |
1118 | |
1119 | if (!empty($CFG->enablegroupmembersonly) and !empty($this->groupmembersonly)) { | |
1120 | $modcontext = context_module::instance($this->id); | |
1121 | $userid = $this->modinfo->get_user_id(); | |
1122 | if (!has_capability('moodle/site:accessallgroups', $modcontext, $userid)) { | |
1123 | // If the activity has 'group members only' and you don't have accessallgroups... | |
1124 | $groups = $this->modinfo->get_groups($this->groupingid); | |
1125 | if (empty($groups)) { | |
1126 | // ...and you don't belong to a group, then set it so you can't see/access it | |
1127 | return true; | |
1128 | } | |
1129 | } | |
1130 | } | |
1131 | return false; | |
1132 | } | |
1133 | ||
1134 | /** | |
1135 | * Checks whether the module's conditional access settings mean that the user cannot see the activity at all | |
1136 | * | |
1137 | * @return bool True if the user cannot see the module. False if the activity is either available or should be greyed out. | |
1138 | */ | |
1139 | public function is_user_access_restricted_by_conditional_access() { | |
1140 | global $CFG, $USER; | |
1141 | ||
1142 | if (empty($CFG->enableavailability)) { | |
1143 | return false; | |
1144 | } | |
1145 | ||
1146 | // If module will always be visible anyway (but greyed out), don't bother checking anything else | |
1147 | if ($this->showavailability == CONDITION_STUDENTVIEW_SHOW) { | |
1148 | return false; | |
1149 | } | |
1150 | ||
1151 | // Can the user see hidden modules? | |
7765e94f FM |
1152 | $modcontext = context_module::instance($this->id); |
1153 | $userid = $this->modinfo->get_user_id(); | |
84fad57f AD |
1154 | if (has_capability('moodle/course:viewhiddenactivities', $modcontext, $userid)) { |
1155 | return false; | |
0d8b6a69 | 1156 | } |
84fad57f AD |
1157 | |
1158 | // Is the module hidden due to unmet conditions? | |
1159 | if (!$this->available) { | |
1160 | return true; | |
1161 | } | |
1162 | ||
7765e94f | 1163 | return false; |
0d8b6a69 | 1164 | } |
1165 | ||
1166 | /** | |
1167 | * Calls a module function (if exists), passing in one parameter: this object. | |
1168 | * @param string $type Name of function e.g. if this is 'grooblezorb' and the modname is | |
1169 | * 'forum' then it will try to call 'mod_forum_grooblezorb' or 'forum_grooblezorb' | |
1170 | * @return void | |
1171 | */ | |
1172 | private function call_mod_function($type) { | |
1173 | global $CFG; | |
1174 | $libfile = $CFG->dirroot . '/mod/' . $this->modname . '/lib.php'; | |
1175 | if (file_exists($libfile)) { | |
1176 | include_once($libfile); | |
1177 | $function = 'mod_' . $this->modname . '_' . $type; | |
1178 | if (function_exists($function)) { | |
1179 | $function($this); | |
1180 | } else { | |
1181 | $function = $this->modname . '_' . $type; | |
1182 | if (function_exists($function)) { | |
1183 | $function($this); | |
1184 | } | |
1185 | } | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | /** | |
1190 | * If view data for this course-module is not yet available, obtains it. | |
1191 | * | |
1192 | * This function is automatically called if any of the functions (marked) which require | |
1193 | * view data are called. | |
1194 | * | |
1195 | * View data is data which is needed only for displaying the course main page (& any similar | |
1196 | * functionality on other pages) but is not needed in general. Obtaining view data may have | |
1197 | * a performance cost. | |
1198 | * | |
1199 | * As part of this function, the module's _cm_info_view function from its lib.php will | |
1200 | * be called (if it exists). | |
1201 | * @return void | |
1202 | */ | |
1203 | private function obtain_view_data() { | |
1204 | if ($this->state >= self::STATE_VIEW) { | |
1205 | return; | |
1206 | } | |
1207 | ||
1208 | // Let module make changes at this point | |
1209 | $this->call_mod_function('cm_info_view'); | |
1210 | $this->state = self::STATE_VIEW; | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | ||
0d8b6a69 | 1215 | /** |
1216 | * Returns reference to full info about modules in course (including visibility). | |
1217 | * Cached and as fast as possible (0 or 1 db query). | |
1218 | * | |
1219 | * @global object | |
1220 | * @global object | |
1221 | * @global moodle_database | |
1222 | * @uses MAX_MODINFO_CACHE_SIZE | |
1223 | * @param mixed $course object or 'reset' string to reset caches, modinfo may be updated in db | |
1224 | * @param int $userid Defaults to current user id | |
1225 | * @return course_modinfo Module information for course, or null if resetting | |
1226 | */ | |
1227 | function get_fast_modinfo(&$course, $userid=0) { | |
1228 | global $CFG, $USER, $DB; | |
1229 | require_once($CFG->dirroot.'/course/lib.php'); | |
1230 | ||
1231 | if (!empty($CFG->enableavailability)) { | |
1232 | require_once($CFG->libdir.'/conditionlib.php'); | |
1233 | } | |
1234 | ||
1235 | static $cache = array(); | |
1236 | ||
1237 | if ($course === 'reset') { | |
1238 | $cache = array(); | |
1239 | return null; | |
1240 | } | |
1241 | ||
1242 | if (empty($userid)) { | |
1243 | $userid = $USER->id; | |
1244 | } | |
1245 | ||
1246 | if (array_key_exists($course->id, $cache) and $cache[$course->id]->userid == $userid) { | |
1247 | return $cache[$course->id]; | |
1248 | } | |
1249 | ||
1250 | if (!property_exists($course, 'modinfo')) { | |
1251 | debugging('Coding problem - missing course modinfo property in get_fast_modinfo() call'); | |
1252 | } | |
1253 | ||
409eab35 ARN |
1254 | if (!property_exists($course, 'sectioncache')) { |
1255 | debugging('Coding problem - missing course sectioncache property in get_fast_modinfo() call'); | |
1256 | } | |
1257 | ||
0d8b6a69 | 1258 | unset($cache[$course->id]); // prevent potential reference problems when switching users |
1259 | ||
adaeccb6 | 1260 | $cache[$course->id] = new course_modinfo($course, $userid); |
0d8b6a69 | 1261 | |
1262 | // Ensure cache does not use too much RAM | |
1263 | if (count($cache) > MAX_MODINFO_CACHE_SIZE) { | |
1264 | reset($cache); | |
1265 | $key = key($cache); | |
0420f8dc AO |
1266 | unset($cache[$key]->instances); |
1267 | unset($cache[$key]->cms); | |
0d8b6a69 | 1268 | unset($cache[$key]); |
1269 | } | |
1270 | ||
1271 | return $cache[$course->id]; | |
1272 | } | |
1273 | ||
112d3b49 PS |
1274 | /** |
1275 | * Rebuilds the cached list of course activities stored in the database | |
1276 | * @param int $courseid - id of course to rebuild, empty means all | |
1277 | * @param boolean $clearonly - only clear the modinfo fields, gets rebuild automatically on the fly | |
1278 | */ | |
1279 | function rebuild_course_cache($courseid=0, $clearonly=false) { | |
1280 | global $COURSE, $DB, $CFG; | |
1281 | ||
1d05ecfd MG |
1282 | if (!$clearonly && !empty($CFG->upgraderunning)) { |
1283 | debugging('Function rebuild_course_cache() should not be called from upgrade script unless with argument clearonly.', | |
1284 | DEBUG_DEVELOPER); | |
1285 | $clearonly = true; | |
1286 | } | |
1287 | ||
112d3b49 PS |
1288 | // Destroy navigation caches |
1289 | navigation_cache::destroy_volatile_caches(); | |
1290 | ||
1291 | if ($clearonly) { | |
1292 | if (empty($courseid)) { | |
ce4dfd27 | 1293 | $DB->set_field('course', 'modinfo', null); |
1294 | $DB->set_field('course', 'sectioncache', null); | |
112d3b49 | 1295 | } else { |
ce4dfd27 | 1296 | // Clear both fields in one update |
1297 | $resetobj = (object)array('id' => $courseid, 'modinfo' => null, 'sectioncache' => null); | |
1298 | $DB->update_record('course', $resetobj); | |
112d3b49 | 1299 | } |
112d3b49 PS |
1300 | // update cached global COURSE too ;-) |
1301 | if ($courseid == $COURSE->id or empty($courseid)) { | |
1302 | $COURSE->modinfo = null; | |
ce4dfd27 | 1303 | $COURSE->sectioncache = null; |
112d3b49 PS |
1304 | } |
1305 | // reset the fast modinfo cache | |
1306 | $reset = 'reset'; | |
1307 | get_fast_modinfo($reset); | |
1308 | return; | |
1309 | } | |
1310 | ||
1311 | require_once("$CFG->dirroot/course/lib.php"); | |
1312 | ||
1313 | if ($courseid) { | |
1314 | $select = array('id'=>$courseid); | |
1315 | } else { | |
1316 | $select = array(); | |
1317 | @set_time_limit(0); // this could take a while! MDL-10954 | |
1318 | } | |
1319 | ||
1320 | $rs = $DB->get_recordset("course", $select,'','id,fullname'); | |
1321 | foreach ($rs as $course) { | |
1322 | $modinfo = serialize(get_array_of_activities($course->id)); | |
ce4dfd27 | 1323 | $sectioncache = serialize(course_modinfo::build_section_cache($course->id)); |
1324 | $updateobj = (object)array('id' => $course->id, | |
1325 | 'modinfo' => $modinfo, 'sectioncache' => $sectioncache); | |
1326 | $DB->update_record("course", $updateobj); | |
112d3b49 PS |
1327 | // update cached global COURSE too ;-) |
1328 | if ($course->id == $COURSE->id) { | |
1329 | $COURSE->modinfo = $modinfo; | |
ce4dfd27 | 1330 | $COURSE->sectioncache = $sectioncache; |
112d3b49 PS |
1331 | } |
1332 | } | |
1333 | $rs->close(); | |
1334 | // reset the fast modinfo cache | |
1335 | $reset = 'reset'; | |
1336 | get_fast_modinfo($reset); | |
1337 | } | |
1338 | ||
0d8b6a69 | 1339 | |
1340 | /** | |
1341 | * Class that is the return value for the _get_coursemodule_info module API function. | |
1342 | * | |
1343 | * Note: For backward compatibility, you can also return a stdclass object from that function. | |
1344 | * The difference is that the stdclass object may contain an 'extra' field (deprecated because | |
1345 | * it was crazy, except for label which uses it differently). The stdclass object may not contain | |
1346 | * the new fields defined here (content, extraclasses, customdata). | |
1347 | */ | |
1348 | class cached_cm_info { | |
1349 | /** | |
1350 | * Name (text of link) for this activity; Leave unset to accept default name | |
1351 | * @var string | |
1352 | */ | |
1353 | public $name; | |
1354 | ||
1355 | /** | |
1356 | * Name of icon for this activity. Normally, this should be used together with $iconcomponent | |
1357 | * to define the icon, as per pix_url function. | |
1358 | * For backward compatibility, if this value is of the form 'mod/forum/icon' then an icon | |
1359 | * within that module will be used. | |
1360 | * @see cm_info::get_icon_url() | |
1361 | * @see renderer_base::pix_url() | |
1362 | * @var string | |
1363 | */ | |
1364 | public $icon; | |
1365 | ||
1366 | /** | |
1367 | * Component for icon for this activity, as per pix_url; leave blank to use default 'moodle' | |
1368 | * component | |
1369 | * @see renderer_base::pix_url() | |
1370 | * @var string | |
1371 | */ | |
1372 | public $iconcomponent; | |
1373 | ||
1374 | /** | |
1375 | * HTML content to be displayed on the main page below the link (if any) for this course-module | |
1376 | * @var string | |
1377 | */ | |
1378 | public $content; | |
1379 | ||
1380 | /** | |
1381 | * Custom data to be stored in modinfo for this activity; useful if there are cases when | |
1382 | * internal information for this activity type needs to be accessible from elsewhere on the | |
1383 | * course without making database queries. May be of any type but should be short. | |
1384 | * @var mixed | |
1385 | */ | |
1386 | public $customdata; | |
1387 | ||
1388 | /** | |
1389 | * Extra CSS class or classes to be added when this activity is displayed on the main page; | |
1390 | * space-separated string | |
1391 | * @var string | |
1392 | */ | |
1393 | public $extraclasses; | |
1394 | ||
c443a1cd EL |
1395 | /** |
1396 | * External URL image to be used by activity as icon, useful for some external-tool modules | |
1397 | * like lti. If set, takes precedence over $icon and $iconcomponent | |
1398 | * @var $moodle_url | |
1399 | */ | |
1400 | public $iconurl; | |
1401 | ||
0d8b6a69 | 1402 | /** |
1403 | * Content of onclick JavaScript; escaped HTML to be inserted as attribute value | |
1404 | * @var string | |
1405 | */ | |
1406 | public $onclick; | |
c443a1cd | 1407 | } |
ce4dfd27 | 1408 | |
1409 | ||
1410 | /** | |
1411 | * Data about a single section on a course. This contains the fields from the | |
1412 | * course_sections table, plus additional data when required. | |
1413 | */ | |
1414 | class section_info extends stdClass { | |
1415 | /** | |
1416 | * Section ID - from course_sections table | |
1417 | * @var int | |
1418 | */ | |
1419 | public $id; | |
1420 | ||
1421 | /** | |
1422 | * Course ID - from course_sections table | |
1423 | * @var int | |
1424 | */ | |
1425 | public $course; | |
1426 | ||
1427 | /** | |
1428 | * Section number - from course_sections table | |
1429 | * @var int | |
1430 | */ | |
1431 | public $section; | |
1432 | ||
1433 | /** | |
1434 | * Section name if specified - from course_sections table | |
1435 | * @var string | |
1436 | */ | |
1437 | public $name; | |
1438 | ||
1439 | /** | |
1440 | * Section visibility (1 = visible) - from course_sections table | |
1441 | * @var int | |
1442 | */ | |
1443 | public $visible; | |
1444 | ||
1445 | /** | |
1446 | * Section summary text if specified - from course_sections table | |
1447 | * @var string | |
1448 | */ | |
1449 | public $summary; | |
1450 | ||
1451 | /** | |
1452 | * Section summary text format (FORMAT_xx constant) - from course_sections table | |
1453 | * @var int | |
1454 | */ | |
1455 | public $summaryformat; | |
1456 | ||
1457 | /** | |
1458 | * When section is unavailable, this field controls whether it is shown to students (0 = | |
1459 | * hide completely, 1 = show greyed out with information about when it will be available) - | |
1460 | * from course_sections table | |
1461 | * @var int | |
1462 | */ | |
1463 | public $showavailability; | |
1464 | ||
1465 | /** | |
1466 | * Available date for this section (0 if not set, or set to seconds since epoch; before this | |
1467 | * date, section does not display to students) - from course_sections table | |
1468 | * @var int | |
1469 | */ | |
1470 | public $availablefrom; | |
1471 | ||
1472 | /** | |
1473 | * Available until date for this section (0 if not set, or set to seconds since epoch; from | |
1474 | * this date, section does not display to students) - from course_sections table | |
1475 | * @var int | |
1476 | */ | |
1477 | public $availableuntil; | |
1478 | ||
1479 | /** | |
1480 | * If section is restricted to users of a particular grouping, this is its id | |
1481 | * (0 if not set) - from course_sections table | |
1482 | * @var int | |
1483 | */ | |
1484 | public $groupingid; | |
1485 | ||
1486 | /** | |
1487 | * Availability conditions for this section based on the completion of | |
1488 | * course-modules (array from course-module id to required completion state | |
1489 | * for that module) - from cached data in sectioncache field | |
1490 | * @var array | |
1491 | */ | |
1492 | public $conditionscompletion; | |
1493 | ||
1494 | /** | |
1495 | * Availability conditions for this section based on course grades (array from | |
1496 | * grade item id to object with ->min, ->max fields) - from cached data in | |
1497 | * sectioncache field | |
1498 | * @var array | |
1499 | */ | |
1500 | public $conditionsgrade; | |
1501 | ||
1502 | /** | |
1503 | * True if this section is available to students i.e. if all availability conditions | |
1504 | * are met - obtained dynamically | |
1505 | * @var bool | |
1506 | */ | |
1507 | public $available; | |
1508 | ||
1509 | /** | |
1510 | * If section is not available to students, this string gives information about | |
1511 | * availability which can be displayed to students and/or staff (e.g. 'Available from 3 | |
1512 | * January 2010') for display on main page - obtained dynamically | |
1513 | * @var string | |
1514 | */ | |
1515 | public $availableinfo; | |
1516 | ||
1517 | /** | |
1518 | * True if this section is available to the CURRENT user (for example, if current user | |
1519 | * has viewhiddensections capability, they can access the section even if it is not | |
1520 | * visible or not available, so this would be true in that case) | |
1521 | * @var bool | |
1522 | */ | |
1523 | public $uservisible; | |
1524 | ||
1525 | /** | |
1526 | * Default values for sectioncache fields; if a field has this value, it won't | |
1527 | * be stored in the sectioncache cache, to save space. Checks are done by === | |
1528 | * which means values must all be strings. | |
1529 | * @var array | |
1530 | */ | |
1531 | private static $sectioncachedefaults = array( | |
1532 | 'name' => null, | |
1533 | 'summary' => '', | |
1534 | 'summaryformat' => '1', // FORMAT_HTML, but must be a string | |
1535 | 'visible' => '1', | |
1536 | 'showavailability' => '0', | |
1537 | 'availablefrom' => '0', | |
1538 | 'availableuntil' => '0', | |
1539 | 'groupingid' => '0', | |
1540 | ); | |
1541 | ||
1542 | /** | |
1543 | * Constructs object from database information plus extra required data. | |
1544 | * @param object $data Array entry from cached sectioncache | |
1545 | * @param int $number Section number (array key) | |
1546 | * @param int $courseid Course ID | |
1547 | * @param int $sequence Sequence of course-module ids contained within | |
1548 | * @param course_modinfo $modinfo Owner (needed for checking availability) | |
1549 | * @param int $userid User ID | |
1550 | */ | |
1551 | public function __construct($data, $number, $courseid, $sequence, $modinfo, $userid) { | |
1552 | global $CFG; | |
1553 | ||
1554 | // Data that is always present | |
1555 | $this->id = $data->id; | |
1556 | ||
1557 | // Data that may use default values to save cache size | |
1558 | foreach (self::$sectioncachedefaults as $field => $value) { | |
1559 | if (isset($data->{$field})) { | |
1560 | $this->{$field} = $data->{$field}; | |
1561 | } else { | |
1562 | $this->{$field} = $value; | |
1563 | } | |
1564 | } | |
1565 | ||
1566 | // Data with array defaults | |
1567 | $this->conditionscompletion = isset($data->conditionscompletion) | |
1568 | ? $data->conditionscompletion : array(); | |
1569 | $this->conditionsgrade = isset($data->conditionsgrade) | |
1570 | ? $data->conditionsgrade : array(); | |
1571 | ||
1572 | // Other data from other places | |
1573 | $this->course = $courseid; | |
1574 | $this->section = $number; | |
1575 | $this->sequence = $sequence; | |
1576 | ||
1577 | // Availability data | |
1578 | if (!empty($CFG->enableavailability)) { | |
1579 | // Get availability information | |
1580 | $ci = new condition_info_section($this); | |
1581 | $this->available = $ci->is_available($this->availableinfo, true, | |
1582 | $userid, $modinfo); | |
1583 | // Display grouping info if available & not already displaying | |
1584 | // (it would already display if current user doesn't have access) | |
1585 | // for people with managegroups - same logic/class as grouping label | |
1586 | // on individual activities. | |
1587 | $context = context_course::instance($courseid); | |
1588 | if ($this->availableinfo === '' && $this->groupingid && | |
1589 | has_capability('moodle/course:managegroups', $context)) { | |
1590 | $groupings = groups_get_all_groupings($courseid); | |
1591 | $this->availableinfo = html_writer::tag('span', '(' . format_string( | |
1592 | $groupings[$this->groupingid]->name, true, array('context' => $context)) . | |
1593 | ')', array('class' => 'groupinglabel')); | |
1594 | } | |
1595 | } else { | |
1596 | $this->available = true; | |
1597 | } | |
1598 | ||
1599 | // Update visibility for current user | |
1600 | $this->update_user_visible($userid); | |
1601 | } | |
1602 | ||
1603 | /** | |
1604 | * Works out whether activity is visible *for current user* - if this is false, they | |
1605 | * aren't allowed to access it. | |
1606 | * @param int $userid User ID | |
1607 | * @return void | |
1608 | */ | |
1609 | private function update_user_visible($userid) { | |
1610 | global $CFG; | |
1611 | $coursecontext = context_course::instance($this->course); | |
1612 | $this->uservisible = true; | |
1613 | if ((!$this->visible || !$this->available) && | |
1614 | !has_capability('moodle/course:viewhiddensections', $coursecontext, $userid)) { | |
1615 | $this->uservisible = false; | |
1616 | } | |
1617 | } | |
1618 | ||
1619 | /** | |
1620 | * Prepares section data for inclusion in sectioncache cache, removing items | |
1621 | * that are set to defaults, and adding availability data if required. | |
1622 | * | |
1623 | * Called by build_section_cache in course_modinfo only; do not use otherwise. | |
1624 | * @param object $section Raw section data object | |
1625 | */ | |
1626 | public static function convert_for_section_cache($section) { | |
1627 | global $CFG; | |
1628 | ||
1629 | // Course id stored in course table | |
1630 | unset($section->course); | |
1631 | // Section number stored in array key | |
1632 | unset($section->section); | |
1633 | // Sequence stored implicity in modinfo $sections array | |
1634 | unset($section->sequence); | |
1635 | ||
1636 | // Add availability data if turned on | |
1637 | if ($CFG->enableavailability) { | |
1638 | require_once($CFG->dirroot . '/lib/conditionlib.php'); | |
1639 | condition_info_section::fill_availability_conditions($section); | |
1640 | if (count($section->conditionscompletion) == 0) { | |
1641 | unset($section->conditionscompletion); | |
1642 | } | |
1643 | if (count($section->conditionsgrade) == 0) { | |
1644 | unset($section->conditionsgrade); | |
1645 | } | |
1646 | } | |
1647 | ||
1648 | // Remove default data | |
94dc3c7d | 1649 | foreach (self::$sectioncachedefaults as $field => $value) { |
ce4dfd27 | 1650 | // Exact compare as strings to avoid problems if some strings are set |
1651 | // to "0" etc. | |
1652 | if (isset($section->{$field}) && $section->{$field} === $value) { | |
1653 | unset($section->{$field}); | |
1654 | } | |
1655 | } | |
1656 | } | |
1657 | } |