-
Notifications
You must be signed in to change notification settings - Fork 70
/
locallib.php
3319 lines (2946 loc) · 125 KB
/
locallib.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 the Checklist plugin for 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/>.
/**
* Stores all the functions for manipulating a checklist
*
* @copyright Davo Smith <[email protected]>
* @package mod_checklist
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_checklist\local\checklist_check;
use mod_checklist\local\checklist_comment;
use mod_checklist\local\checklist_comment_student;
use mod_checklist\local\checklist_item;
use mod_checklist\local\output_status;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/mod/checklist/lib.php');
/** Width of text input */
define("CHECKLIST_TEXT_INPUT_WIDTH", 45);
/** Item is not optional */
define("CHECKLIST_OPTIONAL_NO", 0);
/** Item is optional */
define("CHECKLIST_OPTIONAL_YES", 1);
/** Item is a heading */
define("CHECKLIST_OPTIONAL_HEADING", 2);
/** Item is not hidden */
define("CHECKLIST_HIDDEN_NO", 0);
/** Item is manually hidden */
define("CHECKLIST_HIDDEN_MANUAL", 1);
/** Item is linked to a hidden activity */
define("CHECKLIST_HIDDEN_BYMODULE", 2);
/**
* Class checklist_class
*/
class checklist_class {
/** @var stdClass */
protected $cm;
/** @var stdClass */
protected $course;
/** @var stdClass */
protected $checklist;
/** @var string */
protected $strchecklists;
/** @var string */
protected $pagetitle;
/** @var string */
protected $strchecklist;
/** @var context_module */
protected $context;
/** @var int */
protected $userid;
/** @var checklist_item[] */
protected $items;
/** @var checklist_item[] */
protected $useritems;
/** @var bool */
protected $useredit;
/** @var int|false */
protected $additemafter;
/** @var bool */
protected $editdates;
/** @var bool|int[] */
protected $groupings;
/** @var mod_checklist_renderer */
protected $output;
/** @var bool */
protected $canlinkcourses;
/**
* Class constructor
* @param int|string $cmid optional
* @param int $userid optional
* @param object|null $checklist optional
* @param object|null $cm optional
* @param object|null $course optional
*/
public function __construct($cmid = 'staticonly', $userid = 0, $checklist = null, $cm = null, $course = null) {
global $COURSE, $DB;
if ($cmid === 'staticonly') {
// Use static functions only!
return;
}
$this->output = self::get_renderer();
$this->userid = $userid;
if ($cm) {
$this->cm = $cm;
} else {
$this->cm = get_coursemodule_from_id('checklist', $cmid, 0, false, MUST_EXIST);
}
$this->context = context_module::instance($this->cm->id);
if ($course) {
$this->course = $course;
} else if ($this->cm->course == $COURSE->id) {
$this->course = $COURSE;
} else {
$this->course = $DB->get_record('course', ['id' => $this->cm->course], '*', MUST_EXIST);
}
checklist_comment::set_courseid($this->course->id);
if ($checklist) {
$this->checklist = $checklist;
} else {
$this->checklist = $DB->get_record('checklist', ['id' => $this->cm->instance], '*', MUST_EXIST);
}
if ($userid) {
$this->groupings = self::get_user_groupings($userid, $this->course->id);
} else {
$this->groupings = false;
}
$this->strchecklist = get_string('modulename', 'checklist');
$this->strchecklists = get_string('modulenameplural', 'checklist');
$this->pagetitle = strip_tags($this->course->shortname.': '.$this->strchecklist.': '.
format_string($this->checklist->name, true));
$this->get_items();
if ($this->checklist->autopopulate) {
$this->update_items_from_course();
}
}
/**
* Get the checklist renderer
* @return mod_checklist_renderer
*/
private static function get_renderer() {
global $PAGE;
return $PAGE->get_renderer('mod_checklist');
}
/**
* Is linking items to courses enabled on the site?
* @return bool
*/
protected function can_link_courses() {
if ($this->canlinkcourses === null) {
$this->canlinkcourses = (bool)get_config('mod_checklist', 'linkcourses');
}
return $this->canlinkcourses;
}
/**
* Force checklist into 'edit dates' mode (really only needed by behat generator).
* @param bool $edit
*/
public function set_editing_dates($edit) {
$this->editdates = (bool)$edit;
}
/**
* Get an array of the items in a checklist
*/
protected function get_items() {
global $DB;
// Load all shared checklist items.
$this->items = checklist_item::fetch_all(['checklist' => $this->checklist->id, 'userid' => 0], true);
// Makes sure all items are numbered sequentially, starting at 1.
$this->update_item_positions();
// Load student's own checklist items.
if ($this->userid && $this->canaddown()) {
$this->useritems = checklist_item::fetch_all(['checklist' => $this->checklist->id, 'userid' => $this->userid],
true);
} else {
$this->useritems = false;
}
// Load the currently checked-off items.
if ($this->userid) {
$sql = 'SELECT i.id, c.usertimestamp, c.teachermark, c.teachertimestamp, c.teacherid
FROM {checklist_item} i
LEFT JOIN {checklist_check} c ';
$sql .= 'ON (i.id = c.item AND c.userid = ?) WHERE i.checklist = ? ';
$checks = $DB->get_records_sql($sql, [$this->userid, $this->checklist->id]);
foreach ($checks as $check) {
$id = $check->id;
if (isset($this->items[$id])) {
$this->items[$id]->store_status($check->usertimestamp, $check->teachermark,
$check->teachertimestamp, $check->teacherid);
} else if ($this->useritems && isset($this->useritems[$id])) {
$this->useritems[$id]->store_status($check->usertimestamp);
// User items never have a teacher mark to go with them.
}
}
}
}
/**
* Get the items to output in a template.
* @return object
*/
public function get_items_for_template() {
global $DB;
$checklist = clone($this->checklist);
$checklist->name = format_string($checklist->name);
if (class_exists(\core_external\util::class)) {
[$checklist->intro, $checklist->introformat] = \core_external\util::format_text($checklist->intro,
$checklist->introformat,
$this->context,
'mod_checklist',
'intro');
} else {
$formattedintro = external_format_text($checklist->intro,
$checklist->introformat,
$this->context,
'mod_checklist',
'intro'
);
$checklist->intro = $formattedintro['text'];
$checklist->introformat = $formattedintro['format'];
}
// Configure the status of the checklist output.
$status = new output_status();
$status->set_viewother(false);
$status->set_userreport(false);
$status->set_teachercomments($checklist->teachercomments);
$status->set_studentcomments($checklist->studentcomments);
$status->set_canupdateown($this->canupdateown());
$status->set_canaddown($this->canaddown());
$status->set_courseid($this->course->id);
$progressinfo = $this->get_progress();
$progress = 0.0;
$progressall = 0.0;
$showrequired = false;
if ($progressinfo) {
if ($progressinfo->totalitems !== $progressinfo->requireditems) {
$showrequired = true;
if ($progressinfo->requireditems) {
$progress = 100.0 * $progressinfo->requiredcompleteitems / $progressinfo->requireditems;
}
}
if ($progressinfo->totalitems) {
$progressall = 100.0 * $progressinfo->allcompleteitems / $progressinfo->totalitems;
}
}
$isoverrideauto = ($this->checklist->autoupdate != CHECKLIST_AUTOUPDATE_YES);
// TODO Allow adding/editing items, adding/editing comments.
$data = (object)[
'intro' => $checklist->intro,
'cmid' => $this->cm->id,
'courseid' => $checklist->course,
'items' => [],
'showteachermark' => in_array($this->checklist->teacheredit,
[CHECKLIST_MARKING_TEACHER, CHECKLIST_MARKING_BOTH]),
'showcheckbox' => in_array($this->checklist->teacheredit,
[CHECKLIST_MARKING_STUDENT, CHECKLIST_MARKING_BOTH]),
'progress' => $progress,
'progressall' => $progressall,
'showrequired' => $showrequired,
];
// Load comments.
if ($status->is_teachercomments()) {
$comments = checklist_comment::fetch_by_userid_itemids($this->userid, array_keys($this->items));
checklist_comment::add_commentby_names($comments);
checklist_item::add_comments($this->items, $comments);
}
if ($status->is_studentcomments()) {
$studentcomments = checklist_comment_student::get_student_comments_indexed($this->userid,
array_keys($this->items));
checklist_comment_student::add_student_names($studentcomments);
checklist_item::add_student_comments($this->items, $studentcomments);
}
foreach ($this->items as $item) {
if ($item->hidden) {
continue;
}
$disabled = false;
if (!$isoverrideauto && $item->is_auto_item()) {
$disabled = true;
}
$itemfortemplate = (object)[
'itemid' => $item->id,
'text' => $item->displaytext,
'indent' => $item->indent,
'colour' => $item->colour,
'duetime' => $item->duetime ? $item->duetime : false,
'showitemmark' => $data->showteachermark && !$item->is_heading(),
'showitemcheckbox' => $data->showcheckbox && !$item->is_heading(),
'isoverdue' => $item->duetime && $item->duetime <= time(),
'isheading' => $item->is_heading(),
'isoptional' => $item->is_optional(),
'checkedstudent' => $item->is_checked_student(),
'teachermarktext' => $item->get_teachermark_text(),
'teachermarkimage' => $item->get_teachermark_image_url()->out(),
'url' => $item->get_link_url(),
'disabled' => $disabled,
];
if ($status->is_teachercomments()) {
$comment = $item->get_comment();
if ($comment) {
$itemfortemplate->comment = (object)[
'commentby' => $comment->commentby,
'text' => s($comment->text),
];
if ($comment->commentby) {
$itemfortemplate->comment->commentbyurl = $comment->get_commentby_url();
$itemfortemplate->comment->commentbyname = $comment->get_commentby_name();
}
}
}
if ($status->is_studentcomments()) {
// Right now you can only see your own list in the app, not other people's lists.
$studentcomment = $item->get_student_comment();
if ($this->userid && $studentcomment) {
$currentuser = $DB->get_record('user', ['id' => $this->userid], '*', MUST_EXIST);
$itemfortemplate->studentcomment = (object)[
'text' => s($studentcomment->get('text')),
'studentid' => $this->userid,
'studenturl' => new moodle_url('/user/view.php',
['id' => $this->userid, 'course' => $this->course->id]),
'studentname' => fullname($currentuser),
];
}
}
$data->items[] = $itemfortemplate;
}
return $data;
}
/**
* Loop through all activities / resources in course and check they
* are in the current checklist (in the right order)
*/
protected function update_items_from_course() {
global $DB, $CFG;
$mods = get_fast_modinfo($this->course);
$section = 1;
$nextpos = 1;
$changes = false;
reset($this->items);
$importsection = -1;
if ($this->checklist->autopopulate == CHECKLIST_AUTOPOPULATE_SECTION) {
foreach ($mods->get_sections() as $num => $sectioncms) {
if (in_array($this->cm->id, $sectioncms)) {
$importsection = $num;
$section = $importsection;
break;
}
}
}
$groupmembersonly = false;
$numsections = 1;
$courseformat = course_get_format($this->course);
if (method_exists($courseformat, 'get_last_section_number')) {
$numsections = $courseformat->get_last_section_number();
} else {
$opts = $courseformat->get_format_options();
if (isset($opts['numsections'])) {
$numsections = $opts['numsections'];
}
}
$sections = $mods->get_sections();
while ($section <= $numsections || $section == $importsection) {
if (!array_key_exists($section, $sections)) {
$section++;
continue;
}
if ($importsection >= 0 && $importsection != $section) {
$section++; // Only importing the section with the checklist in it.
continue;
}
$sectionheading = 0;
while ($item = current($this->items)) {
// Search from current position.
if (($item->moduleid == $section) && ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING)) {
$sectionheading = $item->id;
break;
}
next($this->items);
}
if (!$sectionheading) {
// Search again from the start.
foreach ($this->items as $item) {
if (($item->moduleid == $section) && ($item->itemoptional == CHECKLIST_OPTIONAL_HEADING)) {
$sectionheading = $item->id;
break;
}
}
reset($this->items);
}
$sectionname = $courseformat->get_section_name($section);
if (trim($sectionname) == '') {
$sectionname = get_string('section').' '.$section;
}
if (!$sectionheading) {
$sectionheading = $this->additem($sectionname, 0, 0, false, false, $section,
CHECKLIST_OPTIONAL_HEADING);
reset($this->items);
} else {
if ($this->items[$sectionheading]->displaytext != $sectionname) {
$this->updateitem($sectionheading, $sectionname, false, null, null, null, false, true);
}
}
if ($sectionheading) {
$this->items[$sectionheading]->stillexists = true;
if ($this->items[$sectionheading]->position < $nextpos) {
$this->moveitemto($sectionheading, $nextpos, true);
reset($this->items);
}
$nextpos = $this->items[$sectionheading]->position + 1;
}
foreach ($sections[$section] as $cmid) {
if ($this->cm->id == $cmid) {
continue; // Do not include this checklist in the list of modules.
}
if ($mods->get_cm($cmid)->modname === 'label') {
continue; // Ignore any labels.
}
if (isset($mods->get_cm($cmid)->deletioninprogress) && $mods->get_cm($cmid)->deletioninprogress) {
continue; // M3.2 onwards - if cm is in the recycle bin, being deleted, then skip it.
}
$foundit = false;
while ($item = current($this->items)) {
// Search list from current position (will usually be the next item).
if (($item->moduleid == $cmid) && ($item->itemoptional != CHECKLIST_OPTIONAL_HEADING)) {
$foundit = $item;
break;
}
if (($item->moduleid == 0) && ($item->position == $nextpos)) {
// Skip any items that are not linked to modules.
$nextpos++;
}
next($this->items);
}
if (!$foundit) {
// Search list again from the start (just in case).
foreach ($this->items as $item) {
if (($item->moduleid == $cmid) && ($item->itemoptional != CHECKLIST_OPTIONAL_HEADING)) {
$foundit = $item;
break;
}
}
reset($this->items);
}
$modname = $mods->get_cm($cmid)->name;
if ($foundit) {
$item->stillexists = true;
if ($item->position != $nextpos) {
$this->moveitemto($item->id, $nextpos, true);
reset($this->items);
}
if ($item->displaytext != $modname) {
$this->updateitem($item->id, $modname, false, null, null, null, false, true);
}
if (($item->hidden == CHECKLIST_HIDDEN_BYMODULE) && $mods->get_cm($cmid)->visible) {
// Course module was hidden and now is not.
$item->hidden = CHECKLIST_HIDDEN_NO;
$upd = new stdClass;
$upd->id = $item->id;
$upd->hidden = $item->hidden;
$DB->update_record('checklist_item', $upd);
$changes = true;
} else if (($item->hidden == CHECKLIST_HIDDEN_NO) && !$mods->get_cm($cmid)->visible) {
// Course module is now hidden.
$item->hidden = CHECKLIST_HIDDEN_BYMODULE;
$upd = new stdClass;
$upd->id = $item->id;
$upd->hidden = $item->hidden;
$DB->update_record('checklist_item', $upd);
$changes = true;
}
if ($item->groupingid) {
$item->groupingid = 0;
$upd = new stdClass;
$upd->id = $item->id;
$upd->groupingid = 0;
$DB->update_record('checklist_item', $upd);
$changes = true;
}
} else {
$hidden = $mods->get_cm($cmid)->visible ? CHECKLIST_HIDDEN_NO : CHECKLIST_HIDDEN_BYMODULE;
$itemid = $this->additem($modname, 0, 0, $nextpos, false, $cmid, CHECKLIST_OPTIONAL_NO, $hidden);
$changes = true;
reset($this->items);
$this->items[$itemid]->stillexists = true;
$usegrouping = $groupmembersonly && $mods->get_cm($cmid)->groupmembersonly;
$this->items[$itemid]->groupingid = $usegrouping ? $mods->get_cm($cmid)->groupingid : 0;
$item = $this->items[$itemid];
}
$item->set_modulelink(new moodle_url('/mod/'.$mods->get_cm($cmid)->modname.'/view.php',
['id' => $cmid]));
$nextpos++;
}
$section++;
}
// Delete any items that are related to activities / resources that have been deleted.
if ($this->items) {
foreach ($this->items as $item) {
if ($item->moduleid && !$item->stillexists) {
$this->deleteitem($item->id, true);
$changes = true;
}
}
}
if ($changes) {
$this->update_all_autoupdate_checks(true, false);
}
}
/**
* Remove automatically-added items from the course activities (if no longer required).
*/
protected function removeauto() {
if ($this->checklist->autopopulate) {
return; // Still automatically populating the checklist, so don't remove the items.
}
if (!$this->canedit()) {
return;
}
if ($this->items) {
foreach ($this->items as $item) {
if ($item->moduleid) {
$this->deleteitem($item->id);
}
}
}
}
/**
* Check all items are numbered sequentially from 1
* then, move any items between $start and $end
* the number of places indicated by $move
*
* @param int $move (optional) - how far to offset the current positions
* @param int $start (optional) - where to start offsetting positions
* @param bool $end (optional) - where to stop offsetting positions
*/
protected function update_item_positions($move = 0, $start = 1, $end = false) {
$pos = 1;
if (!$this->items) {
return;
}
foreach ($this->items as $item) {
if ($pos == $start) {
$pos += $move;
$start = -1;
}
if ($item->position != $pos) {
$oldpos = $item->position;
$item->position = $pos;
$item->update();
if ($oldpos == $end) {
break;
}
}
$pos++;
}
}
/**
* Get checklist item at a particular position.
* @param int $position
* @return bool|object
*/
protected function get_item_at_position($position) {
if (!$this->items) {
return false;
}
foreach ($this->items as $item) {
if ($item->position == $position) {
return $item;
}
}
return false;
}
/**
* Can the current user update student mark for items on this list?
* @return bool
*/
protected function canupdateown() {
global $USER;
return (!$this->userid || ($this->userid == $USER->id)) && has_capability('mod/checklist:updateown',
$this->context);
}
/**
* Can the current user add their own items to this list?
* @return bool
*/
protected function canaddown() {
global $USER;
return $this->checklist->useritemsallowed
&& (!$this->userid || ($this->userid == $USER->id)) && has_capability('mod/checklist:updateown',
$this->context);
}
/**
* Can the current user add student comments to this checklist?
* - Checklist must have student comments enabled
* - Must have capability mod/checklist:updateown
* - Must be viewing your own checklist, no for teachers viewing a student's checklist (unless previewing).
* @return bool
*/
public function canaddstudentcomments(): bool {
global $USER;
return $this->checklist->studentcomments &&
(!$this->userid || ($this->userid == $USER->id)) &&
has_capability('mod/checklist:updateown', $this->context);
}
/**
* Can the current user preview this checklist?
* @return bool
*/
protected function canpreview() {
return has_capability('mod/checklist:preview', $this->context);
}
/**
* Can the current user edit this checklist?
* @return bool
*/
protected function canedit() {
return has_capability('mod/checklist:edit', $this->context);
}
/**
* Can the current user update the 'teacher marks' against other users?
* @return bool
*/
protected function caneditother() {
return has_capability('mod/checklist:updateother', $this->context);
}
/**
* Can the current user view reports?
* @return bool
*/
protected function canviewreports() {
return has_capability('mod/checklist:viewreports', $this->context)
|| has_capability('mod/checklist:viewmenteereports', $this->context);
}
/**
* Is the current user limited to only viewing reports on their mentees?
* @return bool
*/
protected function only_view_mentee_reports() {
return has_capability('mod/checklist:viewmenteereports', $this->context)
&& !has_capability('mod/checklist:viewreports', $this->context);
}
/**
* Test if the current user is a mentor of the passed in user id.
*
* @param int $userid
* @return bool
*/
public static function is_mentor($userid) {
global $USER, $DB;
$sql = 'SELECT c.instanceid
FROM {role_assignments} ra
JOIN {context} c ON ra.contextid = c.id
WHERE c.contextlevel = '.CONTEXT_USER.'
AND ra.userid = ?
AND c.instanceid = ?';
return $DB->record_exists_sql($sql, [$USER->id, $userid]);
}
/**
* Takes a list of userids and returns only those that the current user
* is a mentor for (ones where the current user is assigned a role in their
* user context)
*
* @param int[] $userids
* @return int[]
*/
public static function filter_mentee_users($userids) {
global $DB, $USER;
[$usql, $uparams] = $DB->get_in_or_equal($userids);
$sql = 'SELECT c.instanceid
FROM {role_assignments} ra
JOIN {context} c ON ra.contextid = c.id
WHERE c.contextlevel = '.CONTEXT_USER.'
AND ra.userid = ?
AND c.instanceid '.$usql;
$params = array_merge([$USER->id], $uparams);
return $DB->get_fieldset_sql($sql, $params);
}
/**
* View the checklist items
*
* @param bool $embedded Hides header, tabs, footer
*/
public function view($embedded = false) {
global $OUTPUT, $CFG;
if ((!$this->items) && $this->canedit()) {
if ($embedded) {
return '';
}
redirect(new moodle_url('/mod/checklist/edit.php', ['id' => $this->cm->id]));
}
if ($this->canupdateown()) {
$currenttab = 'view';
} else if ($this->canpreview()) {
$currenttab = 'preview';
} else {
if ($this->canviewreports()) { // No editing, but can view reports.
redirect(new moodle_url('/mod/checklist/report.php', ['id' => $this->cm->id]));
} else {
$output = $this->view_header();
$ref = get_local_referer(false);
$output .= $OUTPUT->heading(format_string($this->checklist->name));
$output .= $OUTPUT->confirm('<p>'.get_string('guestsno',
'checklist')."</p>\n\n<p>".
get_string('liketologin')."</p>\n",
get_login_url(), $ref);
$output .= $OUTPUT->footer();
return $output;
}
$currenttab = '';
}
$output = '';
if (!$embedded) {
$output .= $this->view_header();
}
$params = [
'contextid' => $this->context->id,
'objectid' => $this->checklist->id,
];
$event = \mod_checklist\event\course_module_viewed::create($params);
$event->trigger();
if ($this->canupdateown()) {
$this->process_view_actions();
}
$output .= $this->view_items();
if (!$embedded) {
$output .= $this->view_footer();
}
return $output;
}
/**
* View the edit items interface.
*/
public function edit() {
global $CFG;
if (!$this->canedit()) {
redirect(new moodle_url('/mod/checklist/view.php', ['id' => $this->cm->id]));
}
$params = [
'contextid' => $this->context->id,
'objectid' => $this->checklist->id,
];
$event = \mod_checklist\event\edit_page_viewed::create($params);
$event->trigger();
$output = $this->view_header();
$this->process_edit_actions();
if ($this->checklist->autopopulate) {
// Needs to be done again, just in case the edit actions have changed something.
$this->update_items_from_course();
}
$output .= $this->view_import_export();
$output .= $this->view_edit_items();
$output .= $this->view_footer();
return $output;
}
/**
* View the report on user's checkmarks.
*/
public function report() {
global $CFG;
if ((!$this->items) && $this->canedit()) {
redirect(new moodle_url('/mod/checklist/edit.php', ['id' => $this->cm->id]));
}
if (!$this->canviewreports()) {
redirect(new moodle_url('/mod/checklist/view.php', ['id' => $this->cm->id]));
}
if ($this->userid && $this->only_view_mentee_reports()) {
// Check this user is a mentee of the logged in user.
if (!self::is_mentor($this->userid)) {
$this->userid = false;
}
} else if (!$this->caneditother()) {
$this->userid = false;
}
checklist_item::add_grouping_names($this->items, $this->course->id);
$output = $this->view_header();
$this->process_report_actions();
$params = [
'contextid' => $this->context->id,
'objectid' => $this->checklist->id,
];
if ($this->userid) {
$params['relateduserid'] = $this->userid;
}
$event = \mod_checklist\event\report_viewed::create($params);
$event->trigger();
if ($this->userid) {
$output .= $this->view_items(true);
} else {
$output .= $this->view_report();
}
$output .= $this->view_footer();
return $output;
}
/**
* Fill in the content of the user activity report.
*/
public function user_complete(): string {
return $this->view_items(false, true);
}
/**
* Returns the output of the header for the page.
*/
protected function view_header(): string {
global $PAGE, $OUTPUT;
$PAGE->set_title($this->pagetitle);
$PAGE->set_heading($this->course->fullname);
return $OUTPUT->header();
}
/**
* Returns the output of the checklist name along with completion info.
* @deprecated
*/
protected function view_name_info(): string {
global $OUTPUT, $USER;
$output = $OUTPUT->heading(format_string($this->checklist->name));
if (class_exists('\core_completion\activity_custom_completion')) {
// Render the activity information.
$modinfo = get_fast_modinfo($this->course);
$cm = $modinfo->get_cm($this->cm->id);
$completiondetails = \core_completion\cm_completion_details::get_instance($cm, $USER->id);
$activitydates = \core\activity_dates::get_dates_for_module($cm, $USER->id);
$output .= $OUTPUT->activity_information($cm, $completiondetails, $activitydates);
}
return $output;
}
/**
* Returns the output of the view/report/edit tabs.
* @param string $currenttab
* @deprecated
*/
protected function view_tabs($currenttab): string {
$tabs = [];
$row = [];
$inactive = [];
$activated = [];
if ($this->canupdateown()) {
$row[] = new tabobject('view', new moodle_url('/mod/checklist/view.php', ['id' => $this->cm->id]),
get_string('view', 'checklist'));
} else if ($this->canpreview()) {
$row[] = new tabobject('preview', new moodle_url('/mod/checklist/view.php', ['id' => $this->cm->id]),
get_string('preview', 'checklist'));
}
if ($this->canviewreports()) {
$row[] = new tabobject('report', new moodle_url('/mod/checklist/report.php', ['id' => $this->cm->id]),
get_string('report', 'checklist'));
}
if ($this->canedit()) {
$row[] = new tabobject('edit', new moodle_url('/mod/checklist/edit.php', ['id' => $this->cm->id]),
get_string('edit', 'checklist'));
}
if (count($row) > 1) { // No tabs for students.
$tabs[] = $row;
}
if ($currenttab == 'report') {
$activated[] = 'report';
}
if ($currenttab == 'edit') {
$activated[] = 'edit';
if (!$this->items) {
$inactive = ['view', 'report', 'preview'];
}
}
if ($currenttab == 'preview') {
$activated[] = 'preview';
}
return print_tabs($tabs, $currenttab, $inactive, $activated, true);
}
/**
* Get details of the overall progress for the checklist being viewed
* @return \mod_checklist\local\progress_info|null
*/
protected function get_progress() {
if (!$this->items) {
return null;
}
$teacherprogress = ($this->checklist->teacheredit != CHECKLIST_MARKING_STUDENT);
$totalitems = 0;
$requireditems = 0;