forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodinfolib.php
2627 lines (2383 loc) · 99.1 KB
/
modinfolib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* modinfolib.php - Functions/classes relating to cached information about module instances on
* a course.
* @package core
* @subpackage lib
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author sam marshall
*/
// Maximum number of modinfo items to keep in memory cache. Do not increase this to a large
// number because:
// a) modinfo can be big (megabyte range) for some courses
// b) performance of cache will deteriorate if there are very many items in it
if (!defined('MAX_MODINFO_CACHE_SIZE')) {
define('MAX_MODINFO_CACHE_SIZE', 10);
}
/**
* Information about a course that is cached in the course table 'modinfo' field (and then in
* memory) in order to reduce the need for other database queries.
*
* This includes information about the course-modules and the sections on the course. It can also
* include dynamic data that has been updated for the current user.
*
* Use {@link get_fast_modinfo()} to retrieve the instance of the object for particular course
* and particular user.
*
* @property-read int $courseid Course ID
* @property-read int $userid User ID
* @property-read array $sections Array from section number (e.g. 0) to array of course-module IDs in that
* section; this only includes sections that contain at least one course-module
* @property-read cm_info[] $cms Array from course-module instance to cm_info object within this course, in
* order of appearance
* @property-read cm_info[][] $instances Array from string (modname) => int (instance id) => cm_info object
* @property-read array $groups Groups that the current user belongs to. Calculated on the first request.
* Is an array of grouping id => array of group id => group id. Includes grouping id 0 for 'all groups'
*/
class course_modinfo {
/**
* List of fields from DB table 'course' that are cached in MUC and are always present in course_modinfo::$course
* @var array
*/
public static $cachedfields = array('shortname', 'fullname', 'format',
'enablecompletion', 'groupmode', 'groupmodeforce', 'cacherev');
/**
* For convenience we store the course object here as it is needed in other parts of code
* @var stdClass
*/
private $course;
/**
* Array of section data from cache
* @var section_info[]
*/
private $sectioninfo;
/**
* User ID
* @var int
*/
private $userid;
/**
* Array from int (section num, e.g. 0) => array of int (course-module id); this list only
* includes sections that actually contain at least one course-module
* @var array
*/
private $sections;
/**
* Array from int (cm id) => cm_info object
* @var cm_info[]
*/
private $cms;
/**
* Array from string (modname) => int (instance id) => cm_info object
* @var cm_info[][]
*/
private $instances;
/**
* Groups that the current user belongs to. This value is usually not available (set to null)
* unless the course has activities set to groupmembersonly. When set, it is an array of
* grouping id => array of group id => group id. Includes grouping id 0 for 'all groups'.
* @var int[][]
*/
private $groups;
/**
* List of class read-only properties and their getter methods.
* Used by magic functions __get(), __isset(), __empty()
* @var array
*/
private static $standardproperties = array(
'courseid' => 'get_course_id',
'userid' => 'get_user_id',
'sections' => 'get_sections',
'cms' => 'get_cms',
'instances' => 'get_instances',
'groups' => 'get_groups_all',
);
/**
* Magic method getter
*
* @param string $name
* @return mixed
*/
public function __get($name) {
if (isset(self::$standardproperties[$name])) {
$method = self::$standardproperties[$name];
return $this->$method();
} else {
debugging('Invalid course_modinfo property accessed: '.$name);
return null;
}
}
/**
* Magic method for function isset()
*
* @param string $name
* @return bool
*/
public function __isset($name) {
if (isset(self::$standardproperties[$name])) {
$value = $this->__get($name);
return isset($value);
}
return false;
}
/**
* Magic method for function empty()
*
* @param string $name
* @return bool
*/
public function __empty($name) {
if (isset(self::$standardproperties[$name])) {
$value = $this->__get($name);
return empty($value);
}
return true;
}
/**
* Magic method setter
*
* Will display the developer warning when trying to set/overwrite existing property.
*
* @param string $name
* @param mixed $value
*/
public function __set($name, $value) {
debugging("It is not allowed to set the property course_modinfo::\${$name}", DEBUG_DEVELOPER);
}
/**
* Returns course object that was used in the first {@link get_fast_modinfo()} call.
*
* It may not contain all fields from DB table {course} but always has at least the following:
* id,shortname,fullname,format,enablecompletion,groupmode,groupmodeforce,cacherev
*
* @return stdClass
*/
public function get_course() {
return $this->course;
}
/**
* @return int Course ID
*/
public function get_course_id() {
return $this->course->id;
}
/**
* @return int User ID
*/
public function get_user_id() {
return $this->userid;
}
/**
* @return array Array from section number (e.g. 0) to array of course-module IDs in that
* section; this only includes sections that contain at least one course-module
*/
public function get_sections() {
return $this->sections;
}
/**
* @return cm_info[] Array from course-module instance to cm_info object within this course, in
* order of appearance
*/
public function get_cms() {
return $this->cms;
}
/**
* Obtains a single course-module object (for a course-module that is on this course).
* @param int $cmid Course-module ID
* @return cm_info Information about that course-module
* @throws moodle_exception If the course-module does not exist
*/
public function get_cm($cmid) {
if (empty($this->cms[$cmid])) {
throw new moodle_exception('invalidcoursemodule', 'error');
}
return $this->cms[$cmid];
}
/**
* Obtains all module instances on this course.
* @return cm_info[][] Array from module name => array from instance id => cm_info
*/
public function get_instances() {
return $this->instances;
}
/**
* Returns array of localised human-readable module names used in this course
*
* @param bool $plural if true returns the plural form of modules names
* @return array
*/
public function get_used_module_names($plural = false) {
$modnames = get_module_types_names($plural);
$modnamesused = array();
foreach ($this->get_cms() as $cmid => $mod) {
if (isset($modnames[$mod->modname]) && $mod->uservisible) {
$modnamesused[$mod->modname] = $modnames[$mod->modname];
}
}
core_collator::asort($modnamesused);
return $modnamesused;
}
/**
* Obtains all instances of a particular module on this course.
* @param $modname Name of module (not full frankenstyle) e.g. 'label'
* @return cm_info[] Array from instance id => cm_info for modules on this course; empty if none
*/
public function get_instances_of($modname) {
if (empty($this->instances[$modname])) {
return array();
}
return $this->instances[$modname];
}
/**
* Groups that the current user belongs to organised by grouping id. Calculated on the first request.
* @return int[][] array of grouping id => array of group id => group id. Includes grouping id 0 for 'all groups'
*/
private function get_groups_all() {
if (is_null($this->groups)) {
// NOTE: Performance could be improved here. The system caches user groups
// in $USER->groupmember[$courseid] => array of groupid=>groupid. Unfortunately this
// structure does not include grouping information. It probably could be changed to
// do so, without a significant performance hit on login, thus saving this one query
// each request.
$this->groups = groups_get_user_groups($this->course->id, $this->userid);
}
return $this->groups;
}
/**
* Returns groups that the current user belongs to on the course. Note: If not already
* available, this may make a database query.
* @param int $groupingid Grouping ID or 0 (default) for all groups
* @return int[] Array of int (group id) => int (same group id again); empty array if none
*/
public function get_groups($groupingid = 0) {
$allgroups = $this->get_groups_all();
if (!isset($allgroups[$groupingid])) {
return array();
}
return $allgroups[$groupingid];
}
/**
* Gets all sections as array from section number => data about section.
* @return section_info[] Array of section_info objects organised by section number
*/
public function get_section_info_all() {
return $this->sectioninfo;
}
/**
* Gets data about specific numbered section.
* @param int $sectionnumber Number (not id) of section
* @param int $strictness Use MUST_EXIST to throw exception if it doesn't
* @return section_info Information for numbered section or null if not found
*/
public function get_section_info($sectionnumber, $strictness = IGNORE_MISSING) {
if (!array_key_exists($sectionnumber, $this->sectioninfo)) {
if ($strictness === MUST_EXIST) {
throw new moodle_exception('sectionnotexist');
} else {
return null;
}
}
return $this->sectioninfo[$sectionnumber];
}
/**
* Static cache for generated course_modinfo instances
*
* @see course_modinfo::instance()
* @see course_modinfo::clear_instance_cache()
* @var course_modinfo[]
*/
protected static $instancecache = array();
/**
* Timestamps (microtime) when the course_modinfo instances were last accessed
*
* It is used to remove the least recent accessed instances when static cache is full
*
* @var float[]
*/
protected static $cacheaccessed = array();
/**
* Clears the cache used in course_modinfo::instance()
*
* Used in {@link get_fast_modinfo()} when called with argument $reset = true
* and in {@link rebuild_course_cache()}
*
* @param null|int|stdClass $courseorid if specified removes only cached value for this course
*/
public static function clear_instance_cache($courseorid = null) {
if (empty($courseorid)) {
self::$instancecache = array();
self::$cacheaccessed = array();
return;
}
if (is_object($courseorid)) {
$courseorid = $courseorid->id;
}
if (isset(self::$instancecache[$courseorid])) {
// Unsetting static variable in PHP is peculiar, it removes the reference,
// but data remain in memory. Prior to unsetting, the varable needs to be
// set to empty to remove its remains from memory.
self::$instancecache[$courseorid] = '';
unset(self::$instancecache[$courseorid]);
unset(self::$cacheaccessed[$courseorid]);
}
}
/**
* Returns the instance of course_modinfo for the specified course and specified user
*
* This function uses static cache for the retrieved instances. The cache
* size is limited by MAX_MODINFO_CACHE_SIZE. If instance is not found in
* the static cache or it was created for another user or the cacherev validation
* failed - a new instance is constructed and returned.
*
* Used in {@link get_fast_modinfo()}
*
* @param int|stdClass $courseorid object from DB table 'course' (must have field 'id'
* and recommended to have field 'cacherev') or just a course id
* @param int $userid User id to populate 'availble' and 'uservisible' attributes of modules and sections.
* Set to 0 for current user (default). Set to -1 to avoid calculation of dynamic user-depended data.
* @return course_modinfo
*/
public static function instance($courseorid, $userid = 0) {
global $USER;
if (is_object($courseorid)) {
$course = $courseorid;
} else {
$course = (object)array('id' => $courseorid);
}
if (empty($userid)) {
$userid = $USER->id;
}
if (!empty(self::$instancecache[$course->id])) {
if (self::$instancecache[$course->id]->userid == $userid &&
(!isset($course->cacherev) ||
$course->cacherev == self::$instancecache[$course->id]->get_course()->cacherev)) {
// This course's modinfo for the same user was recently retrieved, return cached.
self::$cacheaccessed[$course->id] = microtime(true);
return self::$instancecache[$course->id];
} else {
// Prevent potential reference problems when switching users.
self::clear_instance_cache($course->id);
}
}
$modinfo = new course_modinfo($course, $userid);
// We have a limit of MAX_MODINFO_CACHE_SIZE entries to store in static variable.
if (count(self::$instancecache) >= MAX_MODINFO_CACHE_SIZE) {
// Find the course that was the least recently accessed.
asort(self::$cacheaccessed, SORT_NUMERIC);
$courseidtoremove = key(array_reverse(self::$cacheaccessed, true));
self::clear_instance_cache($courseidtoremove);
}
// Add modinfo to the static cache.
self::$instancecache[$course->id] = $modinfo;
self::$cacheaccessed[$course->id] = microtime(true);
return $modinfo;
}
/**
* Constructs based on course.
* Note: This constructor should not usually be called directly.
* Use get_fast_modinfo($course) instead as this maintains a cache.
* @param stdClass $course course object, only property id is required.
* @param int $userid User ID
* @throws moodle_exception if course is not found
*/
public function __construct($course, $userid) {
global $CFG, $COURSE, $SITE, $DB;
if (!isset($course->cacherev)) {
// We require presence of property cacherev to validate the course cache.
// No need to clone the $COURSE or $SITE object here because we clone it below anyway.
$course = get_course($course->id, false);
}
$cachecoursemodinfo = cache::make('core', 'coursemodinfo');
// Retrieve modinfo from cache. If not present or cacherev mismatches, call rebuild and retrieve again.
$coursemodinfo = $cachecoursemodinfo->get($course->id);
if ($coursemodinfo === false || ($course->cacherev != $coursemodinfo->cacherev)) {
$coursemodinfo = self::build_course_cache($course);
}
// Set initial values
$this->userid = $userid;
$this->sections = array();
$this->cms = array();
$this->instances = array();
$this->groups = null;
// If we haven't already preloaded contexts for the course, do it now
// Modules are also cached here as long as it's the first time this course has been preloaded.
context_helper::preload_course($course->id);
// Quick integrity check: as a result of race conditions modinfo may not be regenerated after the change.
// It is especially dangerous if modinfo contains the deleted course module, as it results in fatal error.
// We can check it very cheap by validating the existence of module context.
if ($course->id == $COURSE->id || $course->id == $SITE->id) {
// Only verify current course (or frontpage) as pages with many courses may not have module contexts cached.
// (Uncached modules will result in a very slow verification).
foreach ($coursemodinfo->modinfo as $mod) {
if (!context_module::instance($mod->cm, IGNORE_MISSING)) {
debugging('Course cache integrity check failed: course module with id '. $mod->cm.
' does not have context. Rebuilding cache for course '. $course->id);
// Re-request the course record from DB as well, don't use get_course() here.
$course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
$coursemodinfo = self::build_course_cache($course);
break;
}
}
}
// Overwrite unset fields in $course object with cached values, store the course object.
$this->course = fullclone($course);
foreach ($coursemodinfo as $key => $value) {
if ($key !== 'modinfo' && $key !== 'sectioncache' &&
(!isset($this->course->$key) || $key === 'cacherev')) {
$this->course->$key = $value;
}
}
// Loop through each piece of module data, constructing it
static $modexists = array();
foreach ($coursemodinfo->modinfo as $mod) {
if (empty($mod->name)) {
// something is wrong here
continue;
}
// Skip modules which don't exist
if (!array_key_exists($mod->mod, $modexists)) {
$modexists[$mod->mod] = file_exists("$CFG->dirroot/mod/$mod->mod/lib.php");
}
if (!$modexists[$mod->mod]) {
continue;
}
// Construct info for this module
$cm = new cm_info($this, null, $mod, null);
// Store module in instances and cms array
if (!isset($this->instances[$cm->modname])) {
$this->instances[$cm->modname] = array();
}
$this->instances[$cm->modname][$cm->instance] = $cm;
$this->cms[$cm->id] = $cm;
// Reconstruct sections. This works because modules are stored in order
if (!isset($this->sections[$cm->sectionnum])) {
$this->sections[$cm->sectionnum] = array();
}
$this->sections[$cm->sectionnum][] = $cm->id;
}
// Expand section objects
$this->sectioninfo = array();
foreach ($coursemodinfo->sectioncache as $number => $data) {
$this->sectioninfo[$number] = new section_info($data, $number, null, null,
$this, null);
}
}
/**
* Builds a list of information about sections on a course to be stored in
* the course cache. (Does not include information that is already cached
* in some other way.)
*
* This function will be removed in 2.7
*
* @deprecated since 2.6
* @param int $courseid Course ID
* @return array Information about sections, indexed by section number (not id)
*/
public static function build_section_cache($courseid) {
global $DB;
debugging('Function course_modinfo::build_section_cache() is deprecated. It can only be used internally to build course cache.');
$course = $DB->get_record('course', array('id' => $course->id),
array_merge(array('id'), self::$cachedfields), MUST_EXIST);
return self::build_course_section_cache($course);
}
/**
* Builds a list of information about sections on a course to be stored in
* the course cache. (Does not include information that is already cached
* in some other way.)
*
* @param stdClass $course Course object (must contain fields
* @return array Information about sections, indexed by section number (not id)
*/
protected static function build_course_section_cache($course) {
global $DB;
// Get section data
$sections = $DB->get_records('course_sections', array('course' => $course->id), 'section',
'section, id, course, name, summary, summaryformat, sequence, visible, ' .
'availablefrom, availableuntil, showavailability, groupingid');
$compressedsections = array();
$formatoptionsdef = course_get_format($course)->section_format_options();
// Remove unnecessary data and add availability
foreach ($sections as $number => $section) {
// Add cached options from course format to $section object
foreach ($formatoptionsdef as $key => $option) {
if (!empty($option['cache'])) {
$formatoptions = course_get_format($course)->get_format_options($section);
if (!array_key_exists('cachedefault', $option) || $option['cachedefault'] !== $formatoptions[$key]) {
$section->$key = $formatoptions[$key];
}
}
}
// Clone just in case it is reused elsewhere
$compressedsections[$number] = clone($section);
section_info::convert_for_section_cache($compressedsections[$number]);
}
return $compressedsections;
}
/**
* Builds and stores in MUC object containing information about course
* modules and sections together with cached fields from table course.
*
* @param stdClass $course object from DB table course. Must have property 'id'
* but preferably should have all cached fields.
* @return stdClass object with all cached keys of the course plus fields modinfo and sectioncache.
* The same object is stored in MUC
* @throws moodle_exception if course is not found (if $course object misses some of the
* necessary fields it is re-requested from database)
*/
public static function build_course_cache($course) {
global $DB, $CFG;
require_once("$CFG->dirroot/course/lib.php");
if (empty($course->id)) {
throw new coding_exception('Object $course is missing required property \id\'');
}
// Ensure object has all necessary fields.
foreach (self::$cachedfields as $key) {
if (!isset($course->$key)) {
$course = $DB->get_record('course', array('id' => $course->id),
implode(',', array_merge(array('id'), self::$cachedfields)), MUST_EXIST);
break;
}
}
// Retrieve all information about activities and sections.
// This may take time on large courses and it is possible that another user modifies the same course during this process.
// Field cacherev stored in both DB and cache will ensure that cached data matches the current course state.
$coursemodinfo = new stdClass();
$coursemodinfo->modinfo = get_array_of_activities($course->id);
$coursemodinfo->sectioncache = self::build_course_section_cache($course);
foreach (self::$cachedfields as $key) {
$coursemodinfo->$key = $course->$key;
}
// Set the accumulated activities and sections information in cache, together with cacherev.
$cachecoursemodinfo = cache::make('core', 'coursemodinfo');
$cachecoursemodinfo->set($course->id, $coursemodinfo);
return $coursemodinfo;
}
}
/**
* Data about a single module on a course. This contains most of the fields in the course_modules
* table, plus additional data when required.
*
* The object can be accessed by core or any plugin (i.e. course format, block, filter, etc.) as
* get_fast_modinfo($courseorid)->cms[$coursemoduleid]
* or
* get_fast_modinfo($courseorid)->instances[$moduletype][$instanceid]
*
* There are three stages when activity module can add/modify data in this object:
*
* <b>Stage 1 - during building the cache.</b>
* Allows to add to the course cache static user-independent information about the module.
* Modules should try to include only absolutely necessary information that may be required
* when displaying course view page. The information is stored in application-level cache
* and reset when {@link rebuild_course_cache()} is called or cache is purged by admin.
*
* Modules can implement callback XXX_get_coursemodule_info() returning instance of object
* {@link cached_cm_info}
*
* <b>Stage 2 - dynamic data.</b>
* Dynamic data is user-dependend, it is stored in request-level cache. To reset this cache
* {@link get_fast_modinfo()} with $reset argument may be called.
*
* Dynamic data is obtained when any of the following properties/methods is requested:
* - {@link cm_info::$url}
* - {@link cm_info::$name}
* - {@link cm_info::$onclick}
* - {@link cm_info::get_icon_url()}
* - {@link cm_info::$uservisible}
* - {@link cm_info::$available}
* - {@link cm_info::$showavailability}
* - {@link cm_info::$availableinfo}
* - plus any of the properties listed in Stage 3.
*
* Modules can implement callback <b>XXX_cm_info_dynamic()</b> and inside this callback they
* are allowed to use any of the following set methods:
* - {@link cm_info::set_available()}
* - {@link cm_info::set_name()}
* - {@link cm_info::set_no_view_link()}
* - {@link cm_info::set_user_visible()}
* - {@link cm_info::set_on_click()}
* - {@link cm_info::set_icon_url()}
* Any methods affecting view elements can also be set in this callback.
*
* <b>Stage 3 (view data).</b>
* Also user-dependend data stored in request-level cache. Second stage is created
* because populating the view data can be expensive as it may access much more
* Moodle APIs such as filters, user information, output renderers and we
* don't want to request it until necessary.
* View data is obtained when any of the following properties/methods is requested:
* - {@link cm_info::$afterediticons}
* - {@link cm_info::$content}
* - {@link cm_info::get_formatted_content()}
* - {@link cm_info::$extraclasses}
* - {@link cm_info::$afterlink}
*
* Modules can implement callback <b>XXX_cm_info_view()</b> and inside this callback they
* are allowed to use any of the following set methods:
* - {@link cm_info::set_after_edit_icons()}
* - {@link cm_info::set_after_link()}
* - {@link cm_info::set_content()}
* - {@link cm_info::set_extra_classes()}
*
* @property-read int $id Course-module ID - from course_modules table
* @property-read int $instance Module instance (ID within module table) - from course_modules table
* @property-read int $course Course ID - from course_modules table
* @property-read string $idnumber 'ID number' from course-modules table (arbitrary text set by user) - from
* course_modules table
* @property-read int $added Time that this course-module was added (unix time) - from course_modules table
* @property-read int $visible Visible setting (0 or 1; if this is 0, students cannot see/access the activity) - from
* course_modules table
* @property-read int $visibleold Old visible setting (if the entire section is hidden, the previous value for
* visible is stored in this field) - from course_modules table
* @property-read int $groupmode Group mode (one of the constants NOGROUPS, SEPARATEGROUPS, or VISIBLEGROUPS) - from
* course_modules table. Use {@link cm_info::$effectivegroupmode} to find the actual group mode that may be forced by course.
* @property-read int $groupingid Grouping ID (0 = all groupings)
* @property-read int $groupmembersonly Group members only (if set to 1, only members of a suitable group see this link on the
* course page; 0 = everyone sees it even if they don't belong to a suitable group) - from
* course_modules table
* @property-read bool $coursegroupmodeforce Indicates whether the course containing the module has forced the groupmode
* This means that cm_info::$groupmode should be ignored and cm_info::$coursegroupmode be used instead
* @property-read int $coursegroupmode Group mode (one of the constants NOGROUPS, SEPARATEGROUPS, or VISIBLEGROUPS) - from
* course table - as specified for the course containing the module
* Effective only if {@link cm_info::$coursegroupmodeforce} is set
* @property-read int $effectivegroupmode Effective group mode for this module (one of the constants NOGROUPS, SEPARATEGROUPS,
* or VISIBLEGROUPS). This can be different from groupmode set for the module if the groupmode is forced for the course.
* This value will always be NOGROUPS if module type does not support group mode.
* @property-read int $indent Indent level on course page (0 = no indent) - from course_modules table
* @property-read int $completion Activity completion setting for this activity, COMPLETION_TRACKING_xx constant - from
* course_modules table
* @property-read mixed $completiongradeitemnumber Set to the item number (usually 0) if completion depends on a particular
* grade of this activity, or null if completion does not depend on a grade - from course_modules table
* @property-read int $completionview 1 if 'on view' completion is enabled, 0 otherwise - from course_modules table
* @property-read int $completionexpected Set to a unix time if completion of this activity is expected at a
* particular time, 0 if no time set - from course_modules table
* @property-read int $availablefrom Available date for this activity (0 if not set, or set to seconds since epoch; before this
* date, activity does not display to students) - from course_modules table
* @property-read int $availableuntil Available until date for this activity (0 if not set, or set to seconds since epoch; from
* this date, activity does not display to students) - from course_modules table
* @property-read int $showavailability When activity is unavailable, this field controls whether it is shown to students (0 =
* hide completely, 1 = show greyed out with information about when it will be available) -
* from course_modules table
* @property-read int $showdescription Controls whether the description of the activity displays on the course main page (in
* addition to anywhere it might display within the activity itself). 0 = do not show
* on main page, 1 = show on main page.
* @property-read string $extra (deprecated) Extra HTML that is put in an unhelpful part of the HTML when displaying this module in
* course page - from cached data in modinfo field. Deprecated, replaced by ->extraclasses and ->onclick
* @property-read string $icon Name of icon to use - from cached data in modinfo field
* @property-read string $iconcomponent Component that contains icon - from cached data in modinfo field
* @property-read string $modname Name of module e.g. 'forum' (this is the same name as the module's main database
* table) - from cached data in modinfo field
* @property-read int $module ID of module type - from course_modules table
* @property-read string $name Name of module instance for display on page e.g. 'General discussion forum' - from cached
* data in modinfo field
* @property-read int $sectionnum Section number that this course-module is in (section 0 = above the calendar, section 1
* = week/topic 1, etc) - from cached data in modinfo field
* @property-read int $section Section id - from course_modules table
* @property-read array $conditionscompletion Availability conditions for this course-module based on the completion of other
* course-modules (array from other course-module id to required completion state for that
* module) - from cached data in modinfo field
* @property-read array $conditionsgrade Availability conditions for this course-module based on course grades (array from
* grade item id to object with ->min, ->max fields) - from cached data in modinfo field
* @property-read array $conditionsfield Availability conditions for this course-module based on user fields
* @property-read bool $available True if this course-module is available to students i.e. if all availability conditions
* are met - obtained dynamically
* @property-read string $availableinfo If course-module is not available to students, this string gives information about
* availability which can be displayed to students and/or staff (e.g. 'Available from 3
* January 2010') for display on main page - obtained dynamically
* @property-read bool $uservisible True if this course-module is available to the CURRENT user (for example, if current user
* has viewhiddenactivities capability, they can access the course-module even if it is not
* visible or not available, so this would be true in that case)
* @property-read context_module $context Module context
* @property-read string $modfullname Returns a localised human-readable name of the module type - calculated on request
* @property-read string $modplural Returns a localised human-readable name of the module type in plural form - calculated on request
* @property-read string $content Content to display on main (view) page - calculated on request
* @property-read moodle_url $url URL to link to for this module, or null if it doesn't have a view page - calculated on request
* @property-read string $extraclasses Extra CSS classes to add to html output for this activity on main page - calculated on request
* @property-read string $onclick Content of HTML on-click attribute already escaped - calculated on request
* @property-read mixed $customdata Optional custom data stored in modinfo cache for this activity, or null if none
* @property-read string $afterlink Extra HTML code to display after link - calculated on request
* @property-read string $afterediticons Extra HTML code to display after editing icons (e.g. more icons) - calculated on request
*/
class cm_info implements IteratorAggregate {
/**
* State: Only basic data from modinfo cache is available.
*/
const STATE_BASIC = 0;
/**
* State: In the process of building dynamic data (to avoid recursive calls to obtain_dynamic_data())
*/
const STATE_BUILDING_DYNAMIC = 1;
/**
* State: Dynamic data is available too.
*/
const STATE_DYNAMIC = 2;
/**
* State: In the process of building view data (to avoid recursive calls to obtain_view_data())
*/
const STATE_BUILDING_VIEW = 3;
/**
* State: View data (for course page) is available.
*/
const STATE_VIEW = 4;
/**
* Parent object
* @var course_modinfo
*/
private $modinfo;
/**
* Level of information stored inside this object (STATE_xx constant)
* @var int
*/
private $state;
/**
* Course-module ID - from course_modules table
* @var int
*/
private $id;
/**
* Module instance (ID within module table) - from course_modules table
* @var int
*/
private $instance;
/**
* 'ID number' from course-modules table (arbitrary text set by user) - from
* course_modules table
* @var string
*/
private $idnumber;
/**
* Time that this course-module was added (unix time) - from course_modules table
* @var int
*/
private $added;
/**
* This variable is not used and is included here only so it can be documented.
* Once the database entry is removed from course_modules, it should be deleted
* here too.
* @var int
* @deprecated Do not use this variable
*/
private $score;
/**
* Visible setting (0 or 1; if this is 0, students cannot see/access the activity) - from
* course_modules table
* @var int
*/
private $visible;
/**
* Old visible setting (if the entire section is hidden, the previous value for
* visible is stored in this field) - from course_modules table
* @var int
*/
private $visibleold;
/**
* Group mode (one of the constants NONE, SEPARATEGROUPS, or VISIBLEGROUPS) - from
* course_modules table
* @var int
*/
private $groupmode;
/**
* Grouping ID (0 = all groupings)
* @var int
*/
private $groupingid;
/**
* Group members only (if set to 1, only members of a suitable group see this link on the
* course page; 0 = everyone sees it even if they don't belong to a suitable group) - from
* course_modules table
* @var int
*/
private $groupmembersonly;
/**
* Indent level on course page (0 = no indent) - from course_modules table
* @var int
*/
private $indent;
/**
* Activity completion setting for this activity, COMPLETION_TRACKING_xx constant - from
* course_modules table
* @var int
*/
private $completion;
/**
* Set to the item number (usually 0) if completion depends on a particular
* grade of this activity, or null if completion does not depend on a grade - from
* course_modules table
* @var mixed
*/
private $completiongradeitemnumber;
/**
* 1 if 'on view' completion is enabled, 0 otherwise - from course_modules table
* @var int
*/
private $completionview;
/**
* Set to a unix time if completion of this activity is expected at a
* particular time, 0 if no time set - from course_modules table
* @var int
*/
private $completionexpected;
/**
* Available date for this activity (0 if not set, or set to seconds since epoch; before this
* date, activity does not display to students) - from course_modules table
* @var int
*/
private $availablefrom;
/**
* Available until date for this activity (0 if not set, or set to seconds since epoch; from
* this date, activity does not display to students) - from course_modules table
* @var int
*/
private $availableuntil;
/**
* When activity is unavailable, this field controls whether it is shown to students (0 =
* hide completely, 1 = show greyed out with information about when it will be available) -
* from course_modules table
* @var int
*/
private $showavailability;
/**
* Controls whether the description of the activity displays on the course main page (in
* addition to anywhere it might display within the activity itself). 0 = do not show
* on main page, 1 = show on main page.
* @var int
*/
private $showdescription;
/**
* Extra HTML that is put in an unhelpful part of the HTML when displaying this module in
* course page - from cached data in modinfo field
* @deprecated This is crazy, don't use it. Replaced by ->extraclasses and ->onclick
* @var string
*/
private $extra;
/**
* Name of icon to use - from cached data in modinfo field
* @var string
*/
private $icon;
/**
* Component that contains icon - from cached data in modinfo field
* @var string
*/
private $iconcomponent;
/**
* Name of module e.g. 'forum' (this is the same name as the module's main database
* table) - from cached data in modinfo field
* @var string
*/
private $modname;
/**
* ID of module - from course_modules table
* @var int
*/
private $module;
/**
* Name of module instance for display on page e.g. 'General discussion forum' - from cached
* data in modinfo field
* @var string
*/
private $name;
/**
* Section number that this course-module is in (section 0 = above the calendar, section 1
* = week/topic 1, etc) - from cached data in modinfo field
* @var int
*/
private $sectionnum;
/**
* Section id - from course_modules table
* @var int
*/
private $section;
/**
* Availability conditions for this course-module based on the completion of other
* course-modules (array from other course-module id to required completion state for that
* module) - from cached data in modinfo field