-
Notifications
You must be signed in to change notification settings - Fork 70
/
lib.php
995 lines (861 loc) · 35.1 KB
/
lib.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
<?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/>.
/**
* Functions to link into the main Moodle API
* @copyright Davo Smith <[email protected]>
* @package mod_checklist
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/** Do not sent emails on completion */
define("CHECKLIST_EMAIL_NO", 0);
/** Send emails to students on completion */
define("CHECKLIST_EMAIL_STUDENT", 1);
/** Send emails to teachers on completion */
define("CHECKLIST_EMAIL_TEACHER", 2);
/** Send emails to students and teachers on completion */
define("CHECKLIST_EMAIL_BOTH", 3);
/** Teacher marked item as "No" */
define("CHECKLIST_TEACHERMARK_NO", 2);
/** Teacher marked item as "Yes" */
define("CHECKLIST_TEACHERMARK_YES", 1);
/** No teacher mark */
define("CHECKLIST_TEACHERMARK_UNDECIDED", 0);
/** Checklist updated by students */
define("CHECKLIST_MARKING_STUDENT", 0);
/** Checklist updated by teachers */
define("CHECKLIST_MARKING_TEACHER", 1);
/** Checklist updated by students and teachers */
define("CHECKLIST_MARKING_BOTH", 2);
/** Linked activities should not update item status */
define("CHECKLIST_AUTOUPDATE_NO", 0);
/** Linked activites should update item status */
define("CHECKLIST_AUTOUPDATE_YES", 2);
/** Linked activites should update items status, but can be overridden by students */
define("CHECKLIST_AUTOUPDATE_YES_OVERRIDE", 1);
/** Do not import activities into the checklist */
define("CHECKLIST_AUTOPOPULATE_NO", 0);
/** Import activities from the current section into the checklist */
define("CHECKLIST_AUTOPOPULATE_SECTION", 2);
/** Import all activities in the course into the checklist */
define("CHECKLIST_AUTOPOPULATE_COURSE", 1);
/** Maximum indend allowed */
define("CHECKLIST_MAX_INDENT", 10);
global $CFG;
require_once(__DIR__.'/locallib.php');
require_once($CFG->libdir.'/completionlib.php');
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param object $checklist An object from the form in mod_form.php
* @return int The id of the newly inserted checklist record
*/
function checklist_add_instance($checklist) {
global $DB;
$checklist->timecreated = time();
$checklist->id = $DB->insert_record('checklist', $checklist);
checklist_grade_item_update($checklist);
if (!empty($checklist->completionexpected)) {
\core_completion\api::update_completion_date_event($checklist->coursemodule, 'checklist', $checklist->id,
$checklist->completionexpected);
}
return $checklist->id;
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param object $checklist An object from the form in mod_form.php
* @return boolean Success/Fail
*/
function checklist_update_instance($checklist) {
global $DB;
$checklist->timemodified = time();
$checklist->id = $checklist->instance;
$oldrecord = $DB->get_record('checklist', ['id' => $checklist->id]);
$newmax = $checklist->maxgrade;
$oldmax = $oldrecord->maxgrade;
$oldcompletion = $oldrecord->completionpercent;
$newcompletion = $checklist->completionpercent ?? $oldcompletion;
$oldcompletiontype = $oldrecord->completionpercenttype;
$newcompletiontype = $checklist->completionpercenttype ?? $oldcompletiontype;
$newautoupdate = $checklist->autoupdate;
$oldautoupdate = $oldrecord->autoupdate;
$newteacheredit = $checklist->teacheredit;
$oldteacheredit = $oldrecord->teacheredit;
$DB->update_record('checklist', $checklist);
// Add or remove all calendar events, as needed.
$course = $DB->get_record('course', ['id' => $checklist->course]);
$cm = get_coursemodule_from_instance('checklist', $checklist->id, $course->id);
$chk = new checklist_class($cm->id, 0, $checklist, $cm, $course);
$chk->setallevents();
checklist_grade_item_update($checklist);
if ($newmax != $oldmax) {
checklist_update_grades($checklist);
} else if ($newcompletion && ($newcompletion != $oldcompletion || $newcompletiontype != $oldcompletiontype)) {
// This will already be updated if checklist_update_grades() is called.
$ci = new completion_info($course);
$context = context_module::instance($cm->id);
if (get_config('mod_checklist', 'onlyenrolled')) {
$users = get_enrolled_users($context, 'mod/checklist:updateown', 0, 'u.id', null, 0, 0, true);
} else {
$users = get_users_by_capability($context, 'mod/checklist:updateown', 'u.id');
}
foreach ($users as $user) {
$ci->update_state($cm, COMPLETION_UNKNOWN, $user->id);
}
}
if ($newautoupdate) {
if (!$oldautoupdate) {
$chk->update_all_autoupdate_checks();
} else {
$oldautoteacher = ($oldteacheredit == CHECKLIST_MARKING_TEACHER);
$newautoteacher = ($newteacheredit == CHECKLIST_MARKING_TEACHER);
if ($oldautoteacher != $newautoteacher) {
// Just switched to/from teacher-only marking => automatic checkmarks need updating
// (as they are updating a different value from before).
$chk->update_all_autoupdate_checks();
}
}
}
if (!empty($checklist->completionexpected)) {
\core_completion\api::update_completion_date_event($checklist->coursemodule, 'checklist', $checklist->id,
$checklist->completionexpected);
}
return true;
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
*/
function checklist_delete_instance($id) {
global $DB;
if (!$checklist = $DB->get_record('checklist', ['id' => $id])) {
return false;
}
// Remove all calendar events.
if ($checklist->duedatesoncalendar) {
$checklist->duedatesoncalendar = false;
$course = $DB->get_record('course', ['id' => $checklist->course]);
$cm = get_coursemodule_from_instance('checklist', $checklist->id, $course->id);
if ($cm) { // Should not be false, but check, just in case...
$chk = new checklist_class($cm->id, 0, $checklist, $cm, $course);
$chk->setallevents();
}
}
$DB->delete_records('checklist_comp_notification', ['checklistid' => $checklist->id]);
$items = $DB->get_records('checklist_item', ['checklist' => $checklist->id], '', 'id');
if (!empty($items)) {
$items = array_keys($items);
$DB->delete_records_list('checklist_check', 'item', $items);
$DB->delete_records_list('checklist_comment', 'itemid', $items);
$DB->delete_records_list('checklist_comment_student', 'itemid', $items);
$DB->delete_records('checklist_item', ['checklist' => $checklist->id]);
}
$DB->delete_records('checklist', ['id' => $checklist->id]);
checklist_grade_item_delete($checklist);
return true;
}
/**
* Update all checklist grades on the site
*/
function checklist_update_all_grades() {
global $DB;
\mod_checklist\local\previous_completions::set_bulk_update(true);
$checklists = $DB->get_recordset('checklist');
foreach ($checklists as $checklist) {
checklist_update_grades($checklist);
}
\mod_checklist\local\previous_completions::set_bulk_update(false);
}
/**
* Update the checklist grades
* @param object $checklist
* @param int $userid
*/
function checklist_update_grades($checklist, $userid = 0) {
global $DB;
$params = [
'checklist' => $checklist->id,
'userid' => 0,
'itemoptional' => CHECKLIST_OPTIONAL_NO,
'hidden' => CHECKLIST_HIDDEN_NO,
];
$items = \mod_checklist\local\checklist_item::fetch_all($params);
if (!$items) {
return;
}
if (!$course = $DB->get_record('course', ['id' => $checklist->course])) {
return;
}
if (!$cm = get_coursemodule_from_instance('checklist', $checklist->id, $course->id)) {
return;
}
$context = context_module::instance($cm->id);
$checkgroupings = false; // Don't check items against groupings unless we really have to.
$groupings = checklist_class::get_course_groupings($course->id);
foreach ($items as $item) {
if ($item->groupingid && isset($groupings[$item->groupingid])) {
$checkgroupings = true;
break;
}
}
if ($checklist->teacheredit == CHECKLIST_MARKING_STUDENT) {
$date = ', MAX(c.usertimestamp) AS datesubmitted';
$where = 'c.usertimestamp > 0';
} else {
$date = ', MAX(c.teachertimestamp) AS dategraded';
$where = 'c.teachermark = '.CHECKLIST_TEACHERMARK_YES;
}
if ($checkgroupings) {
if ($userid) {
$users = $DB->get_records('user', ['id' => $userid], null, 'id, firstname, lastname');
} else {
if (get_config('mod_checklist', 'onlyenrolled')) {
$users = get_enrolled_users($context, 'mod/checklist:updateown', 0, 'u.id, u.firstname, u.lastname',
null, 0, 0, true);
} else {
$users = get_users_by_capability($context, 'mod/checklist:updateown', 'u.id, u.firstname, u.lastname');
}
if (!$users) {
return;
}
}
$grades = [];
// With groupings, need to update each user individually (as each has different groupings).
foreach ($users as $uid => $user) {
$groupings = checklist_class::get_user_groupings($uid, $course->id);
$total = 0;
$itemlist = [];
foreach ($items as $item) {
if ($item->groupingid) {
if (!in_array($item->groupingid, $groupings)) {
continue;
}
}
$itemlist[] = $item->id;
$total++;
}
$itemlist = implode(',', $itemlist);
if (!$total) { // No items - set score to 0.
$ugrade = new stdClass;
$ugrade->userid = $uid;
$ugrade->rawgrade = 0;
$ugrade->date = time();
} else {
$sql = 'SELECT SUM(CASE WHEN '.$where.' THEN 1 ELSE 0 END) AS checked, ? AS total '.$date;
$sql .= " FROM {checklist_check} c ";
$sql .= " WHERE c.item IN ($itemlist)";
$sql .= ' AND c.userid = ? ';
$ugrade = $DB->get_record_sql($sql, [$total, $uid]);
if (!$ugrade) {
$ugrade = new stdClass;
$ugrade->rawgrade = 0;
$ugrade->date = time();
}
$ugrade->userid = $uid;
}
$ugrade->firstname = $user->firstname;
$ugrade->lastname = $user->lastname;
$grades[$uid] = $ugrade;
}
} else {
// No need to check groupings, so update all student grades at once.
if ($userid) {
$users = $userid;
} else {
if (get_config('mod_checklist', 'onlyenrolled')) {
$users = get_enrolled_users($context, 'mod/checklist:updateown', 0, 'u.id', null, 0, 0, true);
} else {
$users = get_users_by_capability($context, 'mod/checklist:updateown', 'u.id', '', '', '', '', '',
false);
}
if (!$users) {
return;
}
$users = array_keys($users);
}
$total = count($items);
[$usql, $uparams] = $DB->get_in_or_equal($users, SQL_PARAMS_NAMED);
[$isql, $iparams] = $DB->get_in_or_equal(array_keys($items), SQL_PARAMS_NAMED);
if (class_exists('\core_user\fields')) {
$namesql = \core_user\fields::for_name()->get_sql('u', true);
} else {
$namesql = (object)[
'selects' => ','.get_all_user_name_fields(true, 'u'),
'joins' => '',
'params' => [],
'mappings' => [],
];
}
$sql = "
SELECT u.id AS userid, SUM(CASE WHEN $where THEN 1 ELSE 0 END) AS checked, :total AS total $date
{$namesql->selects}
FROM {user} u
LEFT JOIN {checklist_check} c ON u.id = c.userid
{$namesql->joins}
WHERE u.id $usql
AND c.item $isql
GROUP BY u.id {$namesql->selects}
";
$params = array_merge(['total' => $total], $uparams, $iparams, $namesql->params);
$grades = $DB->get_records_sql($sql, $params);
}
// Get details of whether each user had previously completed the checklist.
$previouscomp = new \mod_checklist\local\previous_completions($checklist->id, array_keys($grades));
foreach ($grades as $grade) {
// Calculate the raw grade (done here, so that we can spot completion, even when the maxgrade is 0).
$grade->rawgrade = 0.0;
if ($grade->total && $checklist->maxgrade) {
$grade->rawgrade = (1.0 * $grade->checked / $grade->total) * $checklist->maxgrade;
}
// Log completion of checklist.
$iscomplete = $grade->total && ($grade->checked === $grade->total);
if ($iscomplete && !$previouscomp->was_complete($grade->userid) && !$previouscomp::is_bulk_update()) {
if ($checklist->emailoncomplete) {
// Do not send another email if this checklist was already 'completed' in the last hour.
if (!$previouscomp->notified_recently($grade->userid)) {
$previouscomp->mark_notified($grade->userid);
if (!isset($context)) {
$context = context_module::instance($cm->id);
}
// Prepare email content.
$details = new stdClass();
$details->user = fullname($grade);
$details->checklist = s($checklist->name);
$details->coursename = $course->fullname;
if ($checklist->emailoncomplete == CHECKLIST_EMAIL_TEACHER
|| $checklist->emailoncomplete == CHECKLIST_EMAIL_BOTH
) {
// Email will be sent to the all teachers who have capability.
$subj = get_string('emailoncompletesubject', 'checklist', $details);
$content = get_string('emailoncompletebody', 'checklist', $details);
$content .= new moodle_url('/mod/checklist/view.php', ['id' => $cm->id]);
$groups = groups_get_all_groups($course->id, $grade->userid, $cm->groupingid);
$groupmode = groups_get_activity_groupmode($cm, $course);
if (is_array($groups) && count($groups) > 0 && $groupmode != NOGROUPS) {
$groups = array_keys($groups);
} else if ($groupmode != NOGROUPS) {
// If the user is not in a group, and the checklist is set to group mode,
// then set $groups to a non-existant id so that only users with
// 'moodle/site:accessallgroups' get notified.
$groups = -1;
} else {
$groups = '';
}
if ($recipients = get_users_by_capability($context, 'mod/checklist:emailoncomplete',
'u.*', '', '', '', $groups)) {
foreach ($recipients as $recipient) {
email_to_user($recipient, $grade, $subj, $content, '', '', '', false);
}
}
}
if ($checklist->emailoncomplete == CHECKLIST_EMAIL_STUDENT
|| $checklist->emailoncomplete == CHECKLIST_EMAIL_BOTH
) {
// Email will be sent to the student who completes this checklist.
$subj = get_string('emailoncompletesubjectown', 'checklist', $details);
$content = get_string('emailoncompletebodyown', 'checklist', $details);
$content .= new moodle_url('/mod/checklist/view.php', ['id' => $cm->id]);
$recipientstudent = $DB->get_record('user', ['id' => $grade->userid]);
email_to_user($recipientstudent, $grade, $subj, $content, '', '', '', false);
}
}
}
$params = [
'contextid' => $context->id,
'objectid' => $checklist->id,
'userid' => $grade->userid,
];
$event = \mod_checklist\event\checklist_completed::create($params);
$event->trigger();
}
$previouscomp->save_completion($grade->userid, $iscomplete);
$ci = new completion_info($course);
if ($cm->completion == COMPLETION_TRACKING_AUTOMATIC) {
$ci->update_state($cm, COMPLETION_UNKNOWN, $grade->userid);
}
}
checklist_grade_item_update($checklist, $grades);
}
/**
* Delete the checklist grade item.
* @param object $checklist
* @return int
*/
function checklist_grade_item_delete($checklist) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
if (!isset($checklist->courseid)) {
$checklist->courseid = $checklist->course;
}
return grade_update('mod/checklist', $checklist->courseid, 'mod', 'checklist', $checklist->id, 0, null,
['deleted' => 1]);
}
/**
* Update the checklist grade items
* @param object $checklist
* @param null $grades
* @return int
*/
function checklist_grade_item_update($checklist, $grades = null) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
if (!isset($checklist->courseid)) {
$checklist->courseid = $checklist->course;
}
$params = ['itemname' => $checklist->name];
if ($checklist->maxgrade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $checklist->maxgrade;
$params['grademin'] = 0;
} else {
$params['gradetype'] = GRADE_TYPE_NONE;
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update('mod/checklist', $checklist->courseid, 'mod', 'checklist', $checklist->id, 0, $grades, $params);
}
/**
* Return a small object with summary information about what a
* user has done with a given particular instance of this module
* Used for user activity reports.
* $return->time = the time they did it
* $return->info = a short text description
*
* @param object $course
* @param object $user
* @param object $mod
* @param object $checklist
* @return null
*/
function checklist_user_outline($course, $user, $mod, $checklist) {
global $DB;
// Handle groupings.
$groupingsql = checklist_class::get_grouping_sql($user->id, $checklist->course);
$sel = 'checklist = ? AND userid = 0 AND itemoptional = '.CHECKLIST_OPTIONAL_NO;
$sel .= ' AND hidden = '.CHECKLIST_HIDDEN_NO." AND $groupingsql";
$items = $DB->get_records_select('checklist_item', $sel, [$checklist->id], '', 'id');
if (!$items) {
return null;
}
$total = count($items);
[$isql, $iparams] = $DB->get_in_or_equal(array_keys($items));
$sql = "userid = ? AND item $isql AND ";
if ($checklist->teacheredit == CHECKLIST_MARKING_STUDENT) {
$sql .= 'usertimestamp > 0';
$order = 'usertimestamp DESC';
} else {
$sql .= 'teachermark = '.CHECKLIST_TEACHERMARK_YES;
$order = 'teachertimestamp DESC';
}
$params = array_merge([$user->id], $iparams);
$checks = $DB->get_records_select('checklist_check', $sql, $params, $order);
$return = null;
if ($checks) {
$return = new stdClass;
$ticked = count($checks);
$check = reset($checks);
if ($checklist->teacheredit == CHECKLIST_MARKING_STUDENT) {
$return->time = $check->usertimestamp;
} else {
$return->time = $check->teachertimestamp;
}
$percent = sprintf('%0d', ($ticked * 100) / $total);
$return->info = get_string('progress', 'checklist').': '.$ticked.'/'.$total.' ('.$percent.'%)';
}
return $return;
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param object $course
* @param object $user
* @param object $mod
* @param object $checklist
* @return boolean
*/
function checklist_user_complete($course, $user, $mod, $checklist) {
$chk = new checklist_class($mod->id, $user->id, $checklist, $mod, $course);
echo $chk->user_complete();
return true;
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in checklist activities and print it out.
* Return true if there was output, or false is there was none.
*
* @param object $course
* @param bool $isteacher
* @param int $timestart
* @return boolean
*/
function checklist_print_recent_activity($course, $isteacher, $timestart) {
return false; // True if anything was printed, otherwise false.
}
/**
* Print an overview of the checklist
* @param array $courses
* @param array $htmlarray
*/
function checklist_print_overview($courses, &$htmlarray) {
global $USER, $CFG, $DB;
$config = get_config('checklist');
if (isset($config->showmymoodle) && !$config->showmymoodle) {
return; // Disabled via global config.
}
if (!isset($config->showcompletemymoodle)) {
$config->showcompletemymoodle = 1;
}
if (!isset($config->showupdateablemymoodle)) {
$config->showupdateablemymoodle = 1;
}
if (empty($courses) || !is_array($courses) || count($courses) == 0) {
return;
}
if (!$checklists = get_all_instances_in_courses('checklist', $courses)) {
return;
}
$strchecklist = get_string('modulename', 'checklist');
foreach ($checklists as $checklist) {
$showall = true;
$context = context_module::instance($checklist->coursemodule);
// If only the student is responsible for updating the checklist.
if ($checklist->teacheredit == CHECKLIST_MARKING_STUDENT) {
if ($showall = !has_capability('mod/checklist:updateown', $context, null, false)) {
if ($config->showupdateablemymoodle) {
continue;
}
}
} else { // If the teacher is involved with updating the checklist.
if ($config->showupdateablemymoodle) {
continue;
}
}
$progressbar = checklist_class::print_user_progressbar($checklist->id, $USER->id,
'270px', true, true,
!$config->showcompletemymoodle);
if (empty($progressbar)) {
continue;
}
if ($showall) { // Show all items whether or not they are checked off (as this user is unable to check them off).
$groupingsql = checklist_class::get_grouping_sql($USER->id, $checklist->course);
$dateitems = $DB->get_records_select('checklist_item',
"checklist = ? AND duetime > 0 AND $groupingsql",
[$checklist->id],
'duetime');
} else { // Show only items that have not been checked off.
$groupingsql = checklist_class::get_grouping_sql($USER->id, $checklist->course, 'i.');
$dateitems = $DB->get_records_sql("SELECT i.* FROM {checklist_item} i
JOIN {checklist_check} c ON c.item = i.id
WHERE i.checklist = ? AND i.duetime > 0 AND c.userid = ? AND usertimestamp = 0
AND $groupingsql
ORDER BY i.duetime", [$checklist->id, $USER->id]);
}
$str = '<div class="checklist overview"><div class="name">'.$strchecklist.': '.
'<a title="'.$strchecklist.'" href="'.$CFG->wwwroot.'/mod/checklist/view.php?id='.$checklist->coursemodule.'">'.
$checklist->name.'</a></div>';
$str .= '<div class="info">'.$progressbar.'</div>';
foreach ($dateitems as $item) {
$str .= '<div class="info">'.format_string($item->displaytext).': ';
if ($item->duetime > time()) {
$str .= '<span class="itemdue">';
} else {
$str .= '<span class="itemoverdue">';
}
$str .= date('j M Y', $item->duetime).'</span></div>';
}
$str .= '</div>';
if (empty($htmlarray[$checklist->course]['checklist'])) {
$htmlarray[$checklist->course]['checklist'] = $str;
} else {
$htmlarray[$checklist->course]['checklist'] .= $str;
}
}
}
/**
* Must return an array of user records (all data) who are participants
* for a given instance of newmodule. Must include every user involved
* in the instance, independient of his role (student, teacher, admin...)
* See other modules as example.
*
* @param int $checklistid ID of an instance of this module
* @return mixed boolean/array of students
*/
function checklist_get_participants($checklistid) {
global $DB;
$params = [$checklistid];
$sql = 'SELECT DISTINCT u.id
FROM {user} u
JOIN {checklist_item} i ON i.userid = u.id
WHERE i.checklist = ?';
$userids1 = $DB->get_records_sql($sql, $params);
$sql = 'SELECT DISTINCT u.id
FROM {user} u
JOIN {checklist_check} c ON c.userid = u.id
JOIN {checklist_item} i ON i.id = c.item
WHERE i.checklist = ?';
$userids2 = $DB->get_records_sql($sql, $params);
return $userids1 + $userids2;
}
/**
* This function returns if a scale is being used by one checklist
* if it has support for grading and scales. Commented code should be
* modified if necessary. See forum, glossary or journal modules
* as reference.
*
* @param int $checklistid ID of an instance of this module
* @param int $scaleid
* @return bool
*/
function checklist_scale_used($checklistid, $scaleid) {
return false;
}
/**
* Checks if scale is being used by any instance of checklist.
* This function was added in 1.9
*
* This is used to find out if scale used anywhere
* @param int $scaleid
* @return boolean True if the scale is used by any checklist
*/
function checklist_scale_used_anywhere($scaleid) {
return false;
}
/**
* Execute post-install custom actions for the module
* This function was added in 1.9
*
* @return boolean true if success, false on error
*/
function checklist_install() {
return true;
}
/**
* Execute post-uninstall custom actions for the module
* This function was added in 1.9
*
* @return boolean true if success, false on error
*/
function checklist_uninstall() {
return true;
}
/**
* Get the form elements for the reset form
* @param HTML_QuickForm $mform
*/
function checklist_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'checklistheader', get_string('modulenameplural', 'checklist'));
$mform->addElement('checkbox', 'reset_checklist_progress', get_string('resetchecklistprogress', 'checklist'));
}
/**
* Get the options for the reset form
* @param object $course
* @return array
*/
function checklist_reset_course_form_defaults($course) {
return ['reset_checklist_progress' => 1];
}
/**
* Reset the checklist userdata
* @param object $data
* @return array
*/
function checklist_reset_userdata($data) {
global $DB;
if (empty($data->reset_checklist_progress)) {
// Nothing to do. User did not request to reset checklist data.
return [];
}
$component = get_string('modulenameplural', 'checklist');
$typestr = get_string('resetchecklistprogress', 'checklist');
$status = [];
$status[] = ['component' => $component, 'item' => $typestr, 'error' => false];
$checklists = $DB->get_records('checklist', ['course' => $data->courseid]);
if (!$checklists) {
return $status;
}
[$csql, $cparams] = $DB->get_in_or_equal(array_keys($checklists));
$items = $DB->get_records_select('checklist_item', 'checklist '.$csql, $cparams);
if (!$items) {
return $status;
}
$DB->delete_records_list('checklist_comp_notification', 'checklistid', array_keys($checklists));
$itemids = array_keys($items);
$DB->delete_records_list('checklist_check', 'item', $itemids);
$DB->delete_records_list('checklist_comment', 'itemid', $itemids);
$sql = "checklist $csql AND userid <> 0";
$DB->delete_records_select('checklist_item', $sql, $cparams);
// Reset the grades.
foreach ($checklists as $checklist) {
checklist_grade_item_update($checklist, 'reset');
}
return $status;
}
/**
* Update calendar events
* @param int $courseid
* @param object|int|null $instance
* @param object|null $cm
* @return bool
*/
function checklist_refresh_events($courseid = 0, $instance = null, $cm = null) {
global $DB;
$course = null;
if ($instance) {
if (!is_object($instance)) {
$instance = $DB->get_record('checklist', ['id' => $instance], '*', MUST_EXIST);
}
$checklists = [$instance];
} else if ($courseid) {
$checklists = $DB->get_records('checklist', ['course' => $courseid]);
$course = $DB->get_record('course', ['id' => $courseid]);
} else {
$checklists = $DB->get_records('checklist');
}
foreach ($checklists as $checklist) {
if ($checklist->duedatesoncalendar) {
$cm = get_coursemodule_from_instance('checklist', $checklist->id, $checklist->course);
$chk = new checklist_class($cm->id, 0, $checklist, $cm, $course);
$chk->setallevents();
}
}
return true;
}
/**
* What features does the checklist support?
* @param string $feature
* @return bool|null
*/
function checklist_supports($feature) {
global $CFG;
if (!defined('FEATURE_SHOW_DESCRIPTION')) {
// For backwards compatibility.
define('FEATURE_SHOW_DESCRIPTION', 'showdescription');
}
if (defined('FEATURE_MOD_PURPOSE')) {
// Only defined in M4.0+.
if ($feature === FEATURE_MOD_PURPOSE) {
return MOD_PURPOSE_ASSESSMENT;
}
}
switch ($feature) {
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* Calculate the completion state of the checklist for the given user.
* Retained for compatibility with Moodle 3.10 and below.
* @param object $course
* @param object $cm
* @param int $userid
* @param int $type
* @return bool
*/
function checklist_get_completion_state($course, $cm, $userid, $type) {
global $DB;
if (!($checklist = $DB->get_record('checklist', ['id' => $cm->instance]))) {
throw new Exception("Can't find checklist {$cm->instance}");
}
$result = $type; // Default return value.
if ($checklist->completionpercent) {
[$ticked, $total] = checklist_class::get_user_progress($cm->instance, $userid);
if ($checklist->completionpercenttype === 'items') {
// Completionpercent is the actual number of items that need checking-off.
$value = $checklist->completionpercent <= $ticked;
} else {
// Completionpercent is the percentage of items that need checking-off.
$value = $checklist->completionpercent <= ($ticked * 100 / $total);
}
if ($type == COMPLETION_AND) {
$result = $result && $value;
} else {
$result = $result || $value;
}
}
return $result;
}
/**
* Add extra info needed to output activity onto course page
* @param object $coursemodule
* @return cached_cm_info|false
*/
function checklist_get_coursemodule_info($coursemodule) {
global $DB;
$fields = 'id, name, intro, introformat, completionpercent, completionpercenttype';
if (!$checklist = $DB->get_record('checklist', ['id' => $coursemodule->instance], $fields)) {
return false;
}
$result = new cached_cm_info();
$result->name = $checklist->name;
if ($coursemodule->showdescription) {
$result->content = format_module_intro('checklist', $checklist, $coursemodule->id, false);
}
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
// Needed by Moodle 3.11 and above - ignored by earlier versions.
$result->customdata['customcompletionrules']['completionpercent'] = [
$checklist->completionpercent, $checklist->completionpercenttype,
];
}
return $result;
}
/**
* Adds module specific settings to the settings block
*
* @param settings_navigation $settings The settings navigation object
* @param navigation_node $checklistnode The node to add module settings to
*/
function checklist_extend_settings_navigation(settings_navigation $settings, navigation_node $checklistnode) {
global $CFG;
$cm = $settings->get_page()->cm;
if (has_capability('mod/checklist:viewreports', $cm->context)
|| has_capability('mod/checklist:viewmenteereports', $cm->context)) {
$checklistnode->add(get_string('report', 'mod_checklist'),
new moodle_url('/mod/checklist/report.php', ['id' => $cm->id]),
navigation_node::TYPE_SETTING, null, 'progress');
}
if (has_capability('mod/checklist:edit', $cm->context)) {
$checklistnode->add(get_string('edit', 'mod_checklist'),
new moodle_url('/mod/checklist/edit.php', ['id' => $cm->id]),
navigation_node::TYPE_SETTING, null, 'edit');
}
}