-
Notifications
You must be signed in to change notification settings - Fork 8
/
lib.php
2295 lines (2005 loc) · 83.5 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
996
997
998
999
1000
<?php
// This file is part of mod_checkmark for Moodle - http://moodle.org/
//
// It 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.
//
// It 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/>.
/**
* lib.php - This file contains the basic checkmark functions
*
* @package mod_checkmark
* @author Philipp Hager
* @copyright 2014 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
/** GRADE ITEM */
define('CHECKMARK_GRADE_ITEM', 0);
/** ATTENDANCE ITEM */
define('CHECKMARK_ATTENDANCE_ITEM', 1);
/** PRESENTATIONGRADE ITEM */
define('CHECKMARK_PRESENTATION_ITEM', 2);
/** EVENT TYPE DUE - deadline for student's submissions */
define('CHECKMARK_EVENT_TYPE_DUE', 'due'); // Is backwards compatible to former events!
/** EVENT TYPE GRADINGDUE - reminder for teachers to grade */
define('CHECKMARK_EVENT_TYPE_GRADINGDUE', 'gradingdue');
define('CHECKMARK_INTROATTACHMENT_FILEAREA', 'introattachment');
/**
* Deletes a checkmark instance
*
* This is done by calling the delete_instance() method
*
* @param int $id ID of checkmark-instance to delete
* @return bool true if OK, else false
*/
function checkmark_delete_instance($id) {
global $DB, $OUTPUT;
// Bad practice, but we had some issues deleting corrupt checkmark instances with >200k examples!
core_php_time_limit::raise(600);
raise_memory_limit(MEMORY_UNLIMITED);
if (!$checkmark = $DB->get_record('checkmark', array('id' => $id))) {
return false;
}
if (!$cm = get_coursemodule_from_instance('checkmark', $checkmark->id)) {
echo $OUTPUT->notification('invalidinstance(CMID=' . $cm->id . ' CheckmarkID=' . $checkmark->id . ')', 'notifyproblem');
}
$result = true;
// Now get rid of all files!
$fs = get_file_storage();
if (!empty($cm)) {
$context = context_module::instance($cm->id);
$fs->delete_area_files($context->id);
}
if (!$DB->delete_records('checkmark_feedbacks', array('checkmarkid' => $checkmark->id))) {
$result = false;
}
$submissions = $DB->get_fieldset_select('checkmark_submissions', 'id',
'checkmarkid = :checkmarkid',
array('checkmarkid' => $checkmark->id));
if (!empty($submissions)) {
list($ssql, $sparams) = $DB->get_in_or_equal($submissions, SQL_PARAMS_NAMED);
} else {
// No dataset should have submissionid = NULL so we can use this for our OR to select whom do delete!
$ssql = ' = NULL';
$sparams = array();
}
if (!$DB->delete_records('checkmark_submissions', array('checkmarkid' => $checkmark->id))) {
$result = false;
}
$examples = $DB->get_fieldset_select('checkmark_examples', 'id',
'checkmarkid = :checkmarkid',
array('checkmarkid' => $checkmark->id));
if (!empty($examples)) {
list($esql, $eparams) = $DB->get_in_or_equal($examples, SQL_PARAMS_NAMED);
} else {
// No dataset should have exampleid = NULL so we can use this for our OR to select whom do delete!
$esql = ' = NULL';
$eparams = array();
}
if (!$DB->delete_records('checkmark_examples', array('checkmarkid' => $checkmark->id))) {
$result = false;
}
if (!empty($examples) || !empty($submissions)) {
$DB->delete_records_select('checkmark_checks', 'submissionid ' . $ssql . ' OR exampleid ' . $esql,
array_merge($sparams, $eparams));
}
if (!$DB->delete_records('event', array('modulename' => 'checkmark',
'instance' => $checkmark->id))) {
$result = false;
}
if (!$DB->delete_records('checkmark', array('id' => $checkmark->id))) {
$result = false;
}
checkmark_grade_item_delete($checkmark);
checkmark_attendance_item_delete($checkmark);
checkmark_presentation_item_delete($checkmark);
return $result;
}
/**
* Updates a checkmark instance
*
* @param object $checkmark Checkmark-data from form
* @return bool true if OK, else false
*/
function checkmark_update_instance($checkmark) {
global $CFG, $OUTPUT, $DB;
$checkmark->timemodified = time();
// Clean examplenames and examplegrades!
$checkmark->examplenames = preg_replace('#^,*|,*$#', '',
$checkmark->examplenames, -1);
$checkmark->examplenames = preg_replace('#,{2,}#', ',', $checkmark->examplenames, -1);
$checkmark->examplegrades = preg_replace('#^,*|,*$#', '',
$checkmark->examplegrades, -1);
$checkmark->examplegrades = preg_replace('#,{2,}#', ',', $checkmark->examplegrades, -1);
$checkmark->id = $checkmark->instance;
if (!empty($checkmark->presentationfeedbackpresent)) {
/* If there are presentation feedbacks present we won't change these settings,
* so get presentationgrade, everything else hasn't changed! */
$checkmark->presentationgrade = $DB->get_field('checkmark', 'presentationgrade', array('id' => $checkmark->instance));
} else if (empty($checkmark->presentationgrading)) {
$checkmark->presentationgrade = 0;
$checkmark->presentationgradebook = 0;
checkmark_presentation_item_delete($checkmark);
}
if (!empty($checkmark->flexiblenaming)) {
$checkmark->flexiblenaming = 1;
}
$DB->update_record('checkmark', $checkmark);
save_intro_draft_files($checkmark, $checkmark->coursemodule);
checkmark_update_examples($checkmark);
checkmark_refresh_events($checkmark->course, $checkmark);
if (!empty($examples)) {
if (empty($checkmark->flexiblenaming)) {
$examplecount = $checkmark->examplecount;
} else {
$examplecount = count(explode(checkmark::DELIMITER, $checkmark->examplenames));
}
if (!$DB->record_exists('checkmark_submissions', ['checkmarkid' => $checkmark->instance])
|| count($examples) == $examplecount) {
/* We won't change the grades after someone submitted already - otherwise he/she would
* have submitted with other informations than displayed
*
* Get existing grade item!
*/
checkmark_grade_item_update($checkmark);
}
} else {
checkmark_grade_item_update($checkmark);
}
if ($checkmark->trackattendance && $checkmark->attendancegradebook) {
checkmark_attendance_item_update($checkmark);
checkmark_update_attendances($checkmark);
} else {
checkmark_attendance_item_delete($checkmark);
}
if (empty($checkmark->presentationfeedbackpresent)) {
if (!empty($checkmark->presentationgrading) && !empty($checkmark->presentationgradebook)) {
checkmark_presentation_item_update($checkmark);
checkmark_update_presentation_grades($checkmark);
} else {
checkmark_presentation_item_delete($checkmark);
}
} else if (empty($checkmark->presentationgradebook)) {
// We have all the data save in our own table, so we can restore it anytime!
checkmark_presentation_item_delete($checkmark);
} else {
checkmark_presentation_item_update($checkmark);
checkmark_update_presentation_grades($checkmark);
}
if (!$cm = get_coursemodule_from_instance('checkmark', $checkmark->id)) {
echo $OUTPUT->notification('invalidinstance(' . $checkmark->id . ')', 'notifyproblem');
}
return true;
}
/**
* Adds a checkmark instance
*
* @param object $checkmark Checkmark-data from form
* @return int new checkmark id
*/
function checkmark_add_instance($checkmark) {
global $DB;
$checkmark->timemodified = time();
if (!empty($checkmark->flexiblenaming)) {
$checkmark->flexiblenaming = 1;
}
// Clean examplenames and examplegrades!
$checkmark->examplenames = preg_replace('#^,*|,*$#', '', $checkmark->examplenames, -1);
$checkmark->examplenames = preg_replace('#,{2,}#', ',', $checkmark->examplenames, -1);
$checkmark->examplegrades = preg_replace('#^,*|,*$#', '', $checkmark->examplegrades, -1);
$checkmark->examplegrades = preg_replace('#,{2,}#', ',', $checkmark->examplegrades, -1);
$returnid = $DB->insert_record('checkmark', $checkmark);
$checkmark->instance = $returnid;
save_intro_draft_files($checkmark, $checkmark->coursemodule);
checkmark_update_examples($checkmark, $checkmark->coursemodule);
checkmark_refresh_events($checkmark->course, $returnid);
checkmark_grade_item_update($checkmark);
if ($checkmark->trackattendance && $checkmark->attendancegradebook) {
checkmark_attendance_item_update($checkmark);
}
if (!empty($checkmark->presentationgrading) && !empty($checkmark->presentationgradebook)) {
checkmark_presentation_item_update($checkmark);
}
checkmark_grade_item_category_update($checkmark);
return $returnid;
}
/**
* Save the attachments in the draft areas.
*
* @param stdClass $formdata Formdata containing the introattachments file manager
* @param int $cmid Course module id of the checkmark
*/
function save_intro_draft_files($formdata, $cmid) {
if (isset($formdata->introattachments)) {
$context = context_module::instance($cmid);
file_save_draft_area_files($formdata->introattachments, $context->id,
'mod_checkmark', CHECKMARK_INTROATTACHMENT_FILEAREA, 0);
}
}
/**
* Serves intro checkmark files.
*
* @param mixed $course course or id of the course
* @param mixed $cm course module or id of the course module
* @param context $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @param array $options additional options affecting the file serving
* @return bool false if file not found, does not return if found - just send the file
*/
function mod_checkmark_pluginfile($course,
$cm,
context $context,
$filearea,
$args,
$forcedownload,
array $options=array()) {
global $CFG;
require_login($course, false, $cm);
if (!has_capability('mod/checkmark:view', $context)) {
return false;
}
require_once($CFG->dirroot . '/mod/checkmark/locallib.php');
$checkmark = new checkmark($cm->id);
if ($filearea !== CHECKMARK_INTROATTACHMENT_FILEAREA) {
return false;
}
$itemid = (int)array_shift($args);
if ($itemid != 0) {
return false;
}
$relativepath = implode('/', $args);
$fullpath = "/{$context->id}/mod_checkmark/$filearea/$itemid/$relativepath";
$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
send_stored_file($file, 0, 0, $forcedownload, $options);
}
/**
* Updates the examples in the DB for this checkmark
*
* @param object $checkmark containing data from checkmarks mod_form
* @param int $cmid (optional, if not set, we get it via get_coursemodule_from_instance())
* @since MOODLE 2.4
*/
function checkmark_update_examples($checkmark, $cmid = false) {
global $DB;
if (!is_object($checkmark)) {
// Something wrong happened, but this should never happen!
throw new coding_exception('The checkmark param to checkmark_update_examples() must be an' .
' object containing data from the mod_form.');
}
if (!$cmid) {
$cm = get_coursemodule_from_instance('checkmark', $checkmark->instance);
$cmid = $cm->id;
}
$examples = $DB->get_records('checkmark_examples', ['checkmarkid' => $checkmark->instance], 'id ASC');
if (!empty($examples)) {
if (empty($checkmark->flexiblenaming)) {
$examplecount = $checkmark->examplecount;
} else {
$examplecount = count(explode(checkmark::DELIMITER, $checkmark->examplenames));
}
if ($DB->record_exists('checkmark_submissions', ['checkmarkid' => $checkmark->instance])
&& count($examples) !== $examplecount) {
return;
}
if (checkmark::get_autograded_feedbacks($checkmark->instance)) {
\core\notification::info(get_string('remembertoupdategrades', 'checkmark'));
}
}
reset($examples);
if (empty($checkmark->flexiblenaming)) {
// Standard-naming.
$i = $checkmark->examplestart;
if ($checkmark->grade >= 0 && !empty($checkmark->examplecount)) {
$grade = $checkmark->grade / $checkmark->examplecount;
} else {
$grade = 0;
}
// First we go through the old examples.
while ($example = current($examples)) {
if ($i < $checkmark->examplestart + $checkmark->examplecount) {
// If there are more new examples replace the old ones with the new ones!
if (($i != $example->name) || ($grade != $example->grade)) {
$old = clone $example;
$example->name = $i;
$example->grade = $grade;
$DB->update_record('checkmark_examples', $example);
\mod_checkmark\event\example_updated::get($cmid, $old, $example)->trigger();
}
} else {
// If there are enough examples delete the rest of the old ones!
$DB->delete_records('checkmark_examples', ['id' => $example->id]);
\mod_checkmark\event\example_deleted::get($cmid, $example);
}
$i++;
next($examples);
}
// Add all the new examples if there haven't been enough old ones to update!
while ($i < $checkmark->examplestart + $checkmark->examplecount) {
$example = new stdClass();
$example->name = $i;
$example->grade = $grade;
$example->checkmarkid = $checkmark->instance;
$example->id = $DB->insert_record('checkmark_examples', $example);
\mod_checkmark\event\example_created::get($cmid, $example)->trigger();
$i++;
}
} else {
// Flexiblenaming!
$names = explode(checkmark::DELIMITER, $checkmark->examplenames);
$grades = explode(checkmark::DELIMITER, $checkmark->examplegrades);
reset($examples);
foreach (array_keys($names) as $key) {
if ($next = current($examples)) {
if (($next->name !== $names[$key]) || (empty($next->grade) && empty($grades[$key]))
|| ($next->grade !== $grades[$key])) {
$old = clone $next;
// If there's an old example to update, we reuse them!
$next->name = html_entity_decode($names[$key]);
$next->grade = null;
if (!empty($grades[$key])) {
$next->grade = $grades[$key];
}
if (empty($next->grade)) {
$next->grade = 0;
}
$DB->update_record('checkmark_examples', $next);
\mod_checkmark\event\example_updated::get($cmid, $old, $next)->trigger();
}
} else {
// Or we create new ones if there aren't any old ones left!
$example = new stdClass();
$example->checkmarkid = $checkmark->instance;
$example->name = html_entity_decode($names[$key]);
$example->grade = null;
if (!empty($grades[$key])) {
$example->grade = $grades[$key];
}
if (empty($example->grade)) {
$example->grade = 0;
}
$example->id = $DB->insert_record('checkmark_examples', $example);
\mod_checkmark\event\example_created::get($cmid, $example)->trigger();
}
next($examples);
}
while ($next = current($examples)) { // We delete the rest if there are any old left!
$DB->delete_records('checkmark_examples', ['id' => $next->id]);
\mod_checkmark\event\example_deleted::get($cmid, $next);
next($examples);
}
}
}
/**
* Returns an outline of a user interaction with an checkmark
*
* This is done by calling the user_outline() method
*
* @param object $course Course object
* @param object $user User object
* @param object $mod Course module object
* @param object $checkmark Checkmark object
* @return object
*/
function checkmark_user_outline($course, $user, $mod, $checkmark) {
global $CFG;
require_once($CFG->dirroot . '/mod/checkmark/locallib.php');
require_once('$CFG->libdir/gradelib.php');
$instance = new checkmark($mod->id, $checkmark, $mod, $course);
$grades = grade_get_grades($course->id, 'mod', 'checkmark', $checkmark->id, $user->id);
if (!empty($grades->items[CHECKMARK_GRADE_ITEM]->grades)) {
return $instance->user_outline(reset($grades->items[CHECKMARK_GRADE_ITEM]->grades));
} else {
return null;
}
}
/**
* Prints the complete info about a user's interaction with an checkmark
*
* This is done by calling the user_complete() method
*
* @param object $course Course object
* @param object $user User object
* @param object $mod course module object
* @param object $checkmark checkmark object
* @return void
*/
function checkmark_user_complete($course, $user, $mod, $checkmark) {
global $CFG;
require_once($CFG->dirroot . '/mod/checkmark/locallib.php');
require_once('$CFG->libdir/gradelib.php');
$instance = new checkmark($mod->id, $checkmark, $mod, $course);
$grades = grade_get_grades($course->id, 'mod', 'checkmark', $checkmark->id, $user->id);
if (empty($grades->items[CHECKMARK_GRADE_ITEM]->grades)) {
$grade = false;
} else {
$grade = reset($grades->items[CHECKMARK_GRADE_ITEM]->grades);
}
return $instance->user_complete($user, $grade);
}
/**
* Add a get_coursemodule_info function in case any checkmark type wants to add 'extra' information
* for the course (see resource).
*
* Given a course_module object, this function returns any "extra" information that may be needed
* when printing this activity in a course listing. See get_array_of_activities() in course/lib.php.
*
* @param stdClass $coursemodule The coursemodule object (record).
* @return cached_cm_info An object on information that the courses
* will know about (most noticeably, an icon).
*/
function checkmark_get_coursemodule_info($coursemodule) {
global $DB, $USER;
$dbparams = array('id' => $coursemodule->instance);
$fields = 'id, name, alwaysshowdescription, timeavailable, intro, introformat';
if (!$checkmark = $DB->get_record('checkmark', $dbparams, $fields)) {
return false;
}
if ($overridden = checkmark_get_overridden_dates($checkmark->id, $USER->id, $coursemodule->course)) {
if ($overridden->timeavailable !== null) {
$checkmark->timeavailable = $overridden->timeavailable;
}
}
$result = new cached_cm_info();
$result->name = $checkmark->name;
if ($coursemodule->showdescription) {
if ($checkmark->alwaysshowdescription || (time() > $checkmark->timeavailable)) {
// Convert intro to html. Do not filter cached version, filters run at display time.
$result->content = format_module_intro('checkmark', $checkmark, $coursemodule->id, false);
} else {
unset($result->content);
}
}
return $result;
}
/**
* This function returns the overridden values for timeavailable, timedue and cutoffdate or false!
* It checks for user and group overrides. A user override has priority over a group override.
* Otherwise, the override of the group with the highest priority is considered.
* It uses a static variable to cache the results and possibly lessen DB queries! TODO: examine if we need some other cache for it!
*
* @param int $checkmarkid The checkmark-ID to get the overridden dates for.
* @param int $userid (optional) 0 to get all user's overrides or a specific user's ID
* @param int $courseid If of the course the checkmark activity is located in. Required when group overrides should be considered
* @return stdClass|bool
* @throws coding_exception
* @throws dml_exception
*/
function checkmark_get_overridden_dates($checkmarkid, $userid = 0, $courseid = 0) {
global $USER, $DB;
static $cached = [];
if ($userid === 0) {
$userid = $USER->id;
}
if (key_exists($userid, $cached) && key_exists($checkmarkid, $cached[$userid])) {
return $cached[$userid][$checkmarkid];
}
// Retrieves all groupings and groups a user is part of.
$groups = groups_get_user_groups($courseid, $userid);
// Flattens groupings/groups array to one dimension.
$groups = call_user_func_array('array_merge', $groups);
$records = array();
if (!empty($groups) && is_array($groups)) {
list($insql, $params) = $DB->get_in_or_equal($groups);
array_push($params, $checkmarkid);
$sql = "SELECT id, timeavailable, timedue, cutoffdate, groupid FROM {checkmark_overrides}
WHERE groupid $insql AND checkmarkid = ? ORDER BY grouppriority DESC";
$records = $DB->get_records_sql($sql, $params, 0, 1);
}
$userrecords = $DB->get_records('checkmark_overrides', ['checkmarkid' => $checkmarkid, 'userid' => $userid], "timecreated DESC",
"id, timeavailable, timedue, cutoffdate, userid", 0, 1);
if (count($userrecords)) {
$records = $userrecords;
}
if (!key_exists($userid, $cached)) {
$cached[$userid] = [];
}
if (count($records)) {
$cached[$userid][$checkmarkid] = reset($records);
} else {
$cached[$userid][$checkmarkid] = false;
}
return $cached[$userid][$checkmarkid];
}
/**
* Return overridden dates for a given group in a given checkmark activity
*
* @param int $checkmarkid Id of the checkmark activity to be checked
* @param int $groupid Id of the group to be checked for
* @return stdClass|void Overridden dates if there are any. Empty object otherwise
* @throws dml_exception
*/
function checkmark_get_override_dates_for_group($checkmarkid, $groupid) {
global $DB;
if (empty($groupid)) {
return;
}
$records = $DB->get_records('checkmark_overrides', ['checkmarkid' => $checkmarkid, 'groupid' => $groupid],
'grouppriority DESC', 'id, timeavailable, timedue, cutoffdate', 0, 1);
return array_values($records)[0];
}
/**
* Return grade for given user or all users.
*
* @param object $checkmark checkmark object
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function checkmark_get_user_grades($checkmark, $userid = 0) {
global $DB;
if ($userid) {
$user = 'AND u.id = :userid';
$params = array('userid' => $userid);
} else {
$user = '';
}
$params['aid'] = $checkmark->id;
$sql = 'SELECT u.id, u.id AS userid, f.grade AS rawgrade, f.feedback AS feedback,
f.format AS feedbackformat, f.graderid AS usermodified,
f.timemodified AS dategraded
FROM {user} u, {checkmark_feedbacks} f
WHERE u.id = f.userid AND f.checkmarkid = :aid' .
$user;
return $DB->get_records_sql($sql, $params);
}
/**
* returns symbol for the attendance value (1, 0, other)
*
* @param bool|mixed $attendance 1, 0 or other value (incl. null)
* @return string HTML snippet with attendance symbol
*/
function checkmark_get_attendance_symbol($attendance = null) {
global $OUTPUT;
if ($attendance == 1) {
$attendantstr = strtolower(get_string('attendant', 'checkmark'));
$symbol = $OUTPUT->pix_icon('i/valid', $attendantstr, 'moodle', array('title' => $attendantstr));
} else if (($attendance == 0) && ($attendance != null)) {
$absentstr = strtolower(get_string('absent', 'checkmark'));
$symbol = $OUTPUT->pix_icon('i/invalid', $absentstr, 'moodle', array('title' => $absentstr));
} else {
$unknownstr = strtolower(get_string('unknown', 'checkmark'));
$symbol = $OUTPUT->pix_icon('questionmark', $unknownstr, 'checkmark', array('title' => $unknownstr));
}
return $symbol;
}
/**
* Return attendance for given user or all users.
*
* @param object $checkmark checkmark object
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function checkmark_get_user_attendances($checkmark, $userid = 0) {
global $DB;
if ($userid) {
$user = 'AND u.id = :userid';
$params = array('userid' => $userid);
} else {
$user = '';
}
$params['aid'] = $checkmark->id;
$sql = 'SELECT u.id, u.id AS userid, f.attendance AS rawgrade, f.graderid AS usermodified,
f.timemodified AS dategraded
FROM {user} u, {checkmark_feedbacks} f
WHERE u.id = f.userid AND f.checkmarkid = :aid' .
$user;
return $DB->get_records_sql($sql, $params);
}
/**
* Return presentation grade for given user or all users.
*
* @param object $checkmark checkmark object
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function checkmark_get_user_presentation_grades($checkmark, $userid = 0) {
global $DB;
if ($userid) {
$user = 'AND u.id = :userid';
$params = array('userid' => $userid);
} else {
$user = '';
}
$params['aid'] = $checkmark->id;
$sql = 'SELECT u.id, u.id AS userid, f.presentationgrade AS rawgrade, f.presentationfeedback AS feedback,
f.presentationformat AS format, f.graderid AS usermodified, f.timemodified AS dategraded
FROM {user} u, {checkmark_feedbacks} f
WHERE u.id = f.userid AND f.checkmarkid = :aid' .
$user;
return $DB->get_records_sql($sql, $params);
}
/**
* Update activity grades
*
* @param object $checkmark
* @param int $userid specific user only, 0 means all
* usual param bool $nullifnone (optional) not used here!
*/
function checkmark_update_grades($checkmark, $userid = 0) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
$grades = null;
if ($grades = checkmark_get_user_grades($checkmark, $userid)) {
foreach ($grades as $k => $v) {
if ($v->rawgrade == -1) {
$grades[$k]->rawgrade = null;
}
}
}
checkmark_grade_item_update($checkmark, $grades);
}
/**
* Update activity attendances
*
* @param object $checkmark
* @param int $userid specific user only, 0 means all
* usual param bool $nullifnone (optional) not used here!
*/
function checkmark_update_attendances($checkmark, $userid = 0) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
if (!$checkmark->trackattendance || !$checkmark->attendancegradebook) {
// If there's no gradeitem, we won't do anything!
return;
}
$attendances = null;
if ($attendances = checkmark_get_user_attendances($checkmark, $userid)) {
foreach ($attendances as $k => $v) {
if ($v->rawgrade == -1) {
$attendances[$k]->rawgrade = null;
}
}
}
checkmark_attendance_item_update($checkmark, $attendances);
}
/**
* Update activity grades
*
* @param object $checkmark
* @param int $userid specific user only, 0 means all
* usual param bool $nullifnone (optional) not used here!
*/
function checkmark_update_presentation_grades($checkmark, $userid = 0) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
if (!$checkmark->presentationgrading || !$checkmark->presentationgradebook) {
// If there's no gradeitem, we won't do anything!
return;
}
$grades = null;
if ($grades = checkmark_get_user_presentation_grades($checkmark, $userid)) {
foreach ($grades as $k => $v) {
if ($v->rawgrade == -1) {
$grades[$k]->rawgrade = null;
}
}
}
checkmark_presentation_item_update($checkmark, $grades);
}
/**
* Update all grades (including attendances and presentationgrades) in gradebook.
*
* Is this function still used anywhere? TODO: check that!
*/
function checkmark_upgrade_grades() {
global $DB;
$sql = 'SELECT COUNT(\'x\')
FROM {checkmark} a, {course_modules} cm, {modules} m
WHERE m.name=\'checkmark\' AND m.id=cm.module AND cm.instance=a.id';
$count = $DB->count_records_sql($sql);
$sql = 'SELECT a.*, cm.idnumber AS cmidnumber, a.course AS course
FROM {checkmark} a, {course_modules} cm, {modules} m
WHERE m.name=\'checkmark\' AND m.id=cm.module AND cm.instance=a.id';
$rs = $DB->get_recordset_sql($sql);
if ($rs->valid()) {
// Too much debug output!
$pbar = new progress_bar('checkmarkupgradegrades', 500, true);
$i = 0;
foreach ($rs as $checkmark) {
$i++;
upgrade_set_timeout(60 * 5); // Set up timeout, may also abort execution!
checkmark_update_grades($checkmark);
if ($checkmark->trackattendance && $checkmark->attendancegradebook) {
checkmark_update_attendances($checkmark);
}
if ($checkmark->presentationgrading && $checkmark->presentationgradebook) {
checkmark_update_presentation_grades($checkmark);
}
$pbar->update($i, $count, 'Updating checkmark grades (' . $i . '/' . $count . ')');
}
upgrade_set_timeout(); // Reset to default timeout!
}
$rs->close();
}
/**
* Create grade item for given checkmark
*
* @param object $checkmark object with extra cmidnumber
* @param object[]|object|string $grades (optional) array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function checkmark_grade_item_update($checkmark, $grades = null) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
$params = array('itemname' => $checkmark->name, 'idnumber' => $checkmark->cmidnumber);
if ($checkmark->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $checkmark->grade;
$params['grademin'] = 0;
} else if ($checkmark->grade < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$checkmark->grade;
} else {
$params['gradetype'] = GRADE_TYPE_TEXT; // Allow text comments only!
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
if (!isset($checkmark->id)) {
$checkmark->id = $checkmark->instance;
}
return grade_update('mod/checkmark', $checkmark->course, 'mod', 'checkmark', $checkmark->id, CHECKMARK_GRADE_ITEM, $grades,
$params);
}
/**
* Create attendance item for given checkmark
*
* @param object $checkmark object with extra cmidnumber
* @param object[]|object|string $grades (optional) array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function checkmark_attendance_item_update($checkmark, $grades = null) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
if (!$checkmark->trackattendance && !$checkmark->attendancegradebook) {
return;
}
if ($checkmark->cmidnumber) {
$idnumber = 'A' . $checkmark->cmidnumber;
} else {
$idnumber = null;
}
$params = array('itemname' => get_string('attendance', 'checkmark') . ' ' . $checkmark->name,
'idnumber' => 'A' . $idnumber);
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = 1;
$params['grademin'] = 0;
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
if (!isset($checkmark->id)) {
$checkmark->id = $checkmark->instance;
}
// We can't update the category ID because Moodle forces us to stay in 1 grade category!
if ($grades != null) {
// Normalize attendance values here. We should only put 0 or 1 through to gradebook!
foreach ($grades as $i => $grade) {
if (!empty($grade->rawgrade)) {
$grades[$i]->rawgrade = 1.00000;
} else if ($grade->rawgrade === null) {
$grades[$i]->rawgrade = null;
} else if ($grade->rawgrade == 0) {
$grades[$i]->rawgrade = 0.00000;
} else {
$grades[$i]->rawgrade = null;
}
}
}
$gradeupdate = grade_update('mod/checkmark', $checkmark->course, 'mod', 'checkmark', $checkmark->id, CHECKMARK_ATTENDANCE_ITEM,
$grades, $params);
// Move attendance item directly after grade item, if it exists in the same category!
$params = array('courseid' => $checkmark->course,
'itemtype' => 'mod',
'itemmodule' => 'checkmark',
'iteminstance' => $checkmark->id);
if ($attendanceitem = grade_item::fetch($params + array('itemnumber' => CHECKMARK_ATTENDANCE_ITEM))) {
if ($gradeitem = grade_item::fetch($params + array('itemnumber' => CHECKMARK_GRADE_ITEM))) {
if ($gradeitem->categoryid == $attendanceitem->categoryid) {
$attendanceitem->move_after_sortorder($gradeitem->get_sortorder());
}
}
}
return $gradeupdate;
}
/**
* Create presentation grade item for given checkmark
*
* @param object $checkmark object with extra cmidnumber
* @param object[]|object|string $grades (optional) array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function checkmark_presentation_item_update($checkmark, $grades = null) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
$params = array('itemname' => get_string('presentationgrade_short', 'checkmark') . ' ' . $checkmark->name,
'idnumber' => get_string('presentationgrade_short', 'checkmark') . $checkmark->cmidnumber);
if ($checkmark->presentationgrade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $checkmark->presentationgrade;
$params['grademin'] = 0;
} else if ($checkmark->presentationgrade < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$checkmark->presentationgrade;
} else {
$params['gradetype'] = GRADE_TYPE_TEXT; // Allow text comments only!
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
if (!isset($checkmark->id)) {
$checkmark->id = $checkmark->instance;
}
$gradeupdate = grade_update('mod/checkmark', $checkmark->course, 'mod', 'checkmark', $checkmark->id,
CHECKMARK_PRESENTATION_ITEM, $grades, $params);
// Move presentation item attendance item directly after attendance or grade item, if one of them exists!
$params = array('courseid' => $checkmark->course,
'itemtype' => 'mod',
'itemmodule' => 'checkmark',
'iteminstance' => $checkmark->id);
if ($presentationitem = grade_item::fetch($params + array('itemnumber' => CHECKMARK_PRESENTATION_ITEM))) {
if ($attendanceitem = grade_item::fetch($params + array('itemnumber' => CHECKMARK_ATTENDANCE_ITEM))) {
if ($attendanceitem->categoryid == $presentationitem->categoryid) {
$presentationitem->move_after_sortorder($attendanceitem->get_sortorder());
}
} else if ($gradeitem = grade_item::fetch($params + array('itemnumber' => CHECKMARK_GRADE_ITEM))) {
if ($presentationitem->categoryid == $gradeitem->categoryid) {
$presentationitem->move_after_sortorder($gradeitem->get_sortorder());
}
}
}
return $gradeupdate;
}
/**
* Update the grade items categories if they are changed via mod_form.php
*
* We must do it manually here in the checkmark module because modedit supports only