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