-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlib.php
executable file
·1341 lines (1211 loc) · 47 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 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/>.
/**
* Library of interface functions and constants for module mootyper
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
*
* @package mod_mootyper
* @copyright 2012 Jaka Luthar ([email protected])
* @copyright 2016 onwards AL Rachels ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
use mod_mootyper\local\lessons;
defined('MOODLE_INTERNAL') || die(); // phpcs:ignore
define('MOOTYPER_EVENT_TYPE_OPEN', 'open');
define('MOOTYPER_EVENT_TYPE_CLOSE', 'close');
/** Example constant.
* define('NEWMODULE_ULTIMATE_ANSWER', 42);
*/
// Moodle core API.
/**
* Returns the information on whether the module supports a feature.
*
* @uses FEATURE_MOD_PURPOSE:
* @uses FEATURE_BACKUP_MOODLE2
* @uses FEATURE_COMPLETION_TRACKS_VIEWS
* @uses FEATURE_COMPLETION_HAS_RULES
* @uses FEATURE_GRADE_HAS_GRADE
* @uses FEATURE_GRADE_OUTCOMES
* @uses FEATURE_GROUPS
* @uses FEATURE_GROUPINGS
* @uses FEATURE_GROUPMEMBERSONLY
* @uses FEATURE_MOD_INTRO
* @uses FEATURE_RATE
* @uses FEATURE_SHOW_DESCRIPTION
* @param string $feature
* @return mixed True if yes (some features may use other values)
*/
function mootyper_supports($feature) {
global $CFG;
if ((int)$CFG->branch > 311) {
if ($feature === FEATURE_MOD_PURPOSE) {
return MOD_PURPOSE_COLLABORATION;
}
}
switch ($feature) {
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GRADE_OUTCOMES:
return false;
case FEATURE_GROUPS;
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_GROUPMEMBERSONLY:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_RATE:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* Get users for this MooTyper.
*
* @param int $mootyperid
* @return array, false if none.
*/
function get_users_of_one_instance($mootyperid) {
global $DB, $CFG;
$params = [];
$toreturn = [];
$gradestblname = $CFG->prefix."mootyper_grades";
$userstblname = $CFG->prefix."user";
$sql = "SELECT DISTINCT ".$userstblname.".id, "
.$userstblname.".firstname, "
.$userstblname.".lastname".
" FROM ".$gradestblname.
" LEFT JOIN ".$userstblname." ON ".$gradestblname.".userid = ".$userstblname.".id".
" WHERE (mootyper=".$mootyperid.")";
if ($grades = $DB->get_records_sql($sql, $params)) {
return $grades;
}
return false;
}
/**
* Get grades for users for this MooTyper.
*
* @param int $mootyperid
* @param int $exerciseid
* @param int $userid
* @param int $orderby
* @param int $desc
* @return array, false if none.
*/
function get_typer_grades_adv($mootyperid, $exerciseid, $userid=0, $orderby=-1, $desc=false) {
global $DB, $CFG;
$params = [];
$toreturn = [];
$gradestblname = $CFG->prefix."mootyper_grades";
$userstblname = $CFG->prefix."user";
$exertblname = $CFG->prefix."mootyper_exercises";
$atttblname = $CFG->prefix."mootyper_attempts";
$sql = "SELECT ".$gradestblname.".id, "
.$userstblname.".firstname, "
.$userstblname.".lastname, "
.$userstblname.".id as u_id, "
.$gradestblname.".pass, "
.$gradestblname.".mistakes, "
.$gradestblname.".timeinseconds, "
.$gradestblname.".hitsperminute, "
.$atttblname.".suspicion, "
.$gradestblname.".fullhits, "
.$gradestblname.".precisionfield, "
.$gradestblname.".timetaken, "
.$gradestblname.".exercise, "
.$exertblname.".exercisename, "
.$gradestblname.".wpm,"
.$gradestblname.".grade,"
.$gradestblname.".mistakedetails".
" FROM ".$gradestblname.
" LEFT JOIN ".$userstblname." ON ".$gradestblname.".userid = ".$userstblname.".id".
" LEFT JOIN ".$exertblname." ON ".$gradestblname.".exercise = ".$exertblname.".id".
" LEFT JOIN ".$atttblname." ON ".$atttblname.".id = ".$gradestblname.".attemptid".
" WHERE (mootyper=".$mootyperid.") AND (exercise=".$exerciseid." OR ".$exerciseid."=0) AND".
" (".$gradestblname.".userid=".$userid." OR ".$userid."=0)";
if ($orderby == 0 || $orderby == -1) {
$oby = " ORDER BY ".$gradestblname.".id";
} else if ($orderby == 1) {
$oby = " ORDER BY ".$userstblname.".firstname";
} else if ($orderby == 2) {
$oby = " ORDER BY ".$userstblname.".lastname";
} else if ($orderby == 3) {
$oby = " ORDER BY ".$atttblname.".suspicion";
} else if ($orderby == 4) {
$oby = " ORDER BY ".$gradestblname.".mistakes";
} else if ($orderby == 5) {
$oby = " ORDER BY ".$gradestblname.".timeinseconds";
} else if ($orderby == 6) {
$oby = " ORDER BY ".$gradestblname.".hitsperminute";
} else if ($orderby == 7) {
$oby = " ORDER BY ".$gradestblname.".fullhits";
} else if ($orderby == 8) {
$oby = " ORDER BY ".$gradestblname.".precisionfield";
} else if ($orderby == 9) {
$oby = " ORDER BY ".$gradestblname.".timetaken";
} else if ($orderby == 10) {
$oby = " ORDER BY ".$exertblname.".exercisename";
} else if ($orderby == 11) {
$oby = " ORDER BY ".$gradestblname.".pass";
} else if ($orderby == 12) {
$oby = " ORDER BY ".$gradestblname.".wpm";
} else if ($orderby == 13) {
$oby = " ORDER BY ".$gradestblname.".grade";
} else {
$oby = "";
}
$sql .= $oby;
if ($desc) {
$sql .= " DESC";
}
if ($grades = $DB->get_records_sql($sql, $params)) {
return $grades;
}
return false;
}
/**
* Get grades for one user.
*
* @param int $sid
* @param int $uid
* @param int $orderby
* @param int $desc
* @return array, false if null.
*/
function get_typergradesuser($sid, $uid, $orderby=-1, $desc=false) {
global $DB, $CFG;
$params = [];
$toreturn = [];
$gradestblname = $CFG->prefix."mootyper_grades";
$userstblname = $CFG->prefix."user";
$exertblname = $CFG->prefix."mootyper_exercises";
$atttblname = $CFG->prefix."mootyper_attempts";
// 20231216 Added $gradestblname.".exercise a couple of lines down from here, for testing.
$sql = "SELECT ".$gradestblname.".id, "
.$gradestblname.".exercise, "
.$userstblname.".firstname, "
.$userstblname.".lastname, "
.$atttblname.".suspicion, "
.$gradestblname.".mistakes, "
.$gradestblname.".timeinseconds, "
.$gradestblname.".hitsperminute, "
.$gradestblname.".fullhits, "
.$gradestblname.".precisionfield, "
.$gradestblname.".pass, "
.$gradestblname.".timetaken, "
.$exertblname.".exercisename, "
.$gradestblname.".wpm,"
.$gradestblname.".grade,"
.$gradestblname.".mistakedetails".
" FROM ".$gradestblname.
" LEFT JOIN ".$userstblname." ON ".$gradestblname.".userid = ".$userstblname.".id".
" LEFT JOIN ".$exertblname." ON ".$gradestblname.".exercise = ".$exertblname.".id".
" LEFT JOIN ".$atttblname." ON ".$atttblname.".id = ".$gradestblname.".attemptid".
" WHERE mootyper=".$sid." AND ".$gradestblname.".userid=".$uid;
if ($orderby == 0 || $orderby == -1) {
$oby = " ORDER BY ".$gradestblname.".id";
} else if ($orderby == 1) {
$oby = " ORDER BY ".$userstblname.".firstname";
} else if ($orderby == 2) {
$oby = " ORDER BY ".$userstblname.".lastname";
} else if ($orderby == 3) {
$oby = " ORDER BY ".$atttblname.".suspicion";
} else if ($orderby == 4) {
$oby = " ORDER BY ".$gradestblname.".mistakes";
} else if ($orderby == 5) {
$oby = " ORDER BY ".$gradestblname.".timeinseconds";
} else if ($orderby == 6) {
$oby = " ORDER BY ".$gradestblname.".hitsperminute";
} else if ($orderby == 7) {
$oby = " ORDER BY ".$gradestblname.".fullhits";
} else if ($orderby == 8) {
$oby = " ORDER BY ".$gradestblname.".precisionfield";
} else if ($orderby == 9) {
$oby = " ORDER BY ".$gradestblname.".timetaken";
} else if ($orderby == 10) {
$oby = " ORDER BY ".$exertblname.".exercisename";
} else if ($orderby == 12) {
$oby = " ORDER BY ".$gradestblname.".wpm";
} else if ($orderby == 13) {
$oby = " ORDER BY ".$gradestblname.".grade";
} else {
$oby = "";
}
$sql .= $oby;
if ($desc) {
$sql .= " DESC";
}
if ($grades = $DB->get_records_sql($sql, $params)) {
return $grades;
}
return false;
}
/**
* Saves a new instance of the mootyper into the database.
*
* 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 stdClass $mootyper An object from the form in mod_form.php
* @param mod_mootyper_mod_form $mform
* @return int The id of the newly inserted mootyper record.
*/
function mootyper_add_instance($mootyper, $mform = null) {
global $CFG, $DB;
$mootyper->timecreated = time();
if (empty($mootyper->assessed)) {
$mootyper->assessed = 0;
}
if (empty($mootyper->ratingtime) || empty($mootyper->assessed)) {
$mootyper->assesstimestart = 0;
$mootyper->assesstimefinish = 0;
}
// Changed to add instance now instead of in the return, 02/15/19.
$mootyper->id = $DB->insert_record('mootyper', $mootyper);
// You may have to add extra stuff in here.
// Added next line for behat test 20190211.
$cmid = $mootyper->coursemodule;
mootyper_update_calendar($mootyper, $cmid);
mootyper_grade_item_update($mootyper);
$completiontimeexpected = !empty($mootyper->completionexpected) ? $mootyper->completionexpected : null;
\core_completion\api::update_completion_date_event($mootyper->coursemodule, 'mootyper', $mootyper->id, $completiontimeexpected);
return $mootyper->id;
}
/**
* Gets an instance of an exercise from the database.
*
* @param object $eid An exercise id.
* @return int The id of the exercise.
*/
function get_exercise_record($eid) {
global $DB;
return $DB->get_record('mootyper_exercises', ['id' => $eid]);
}
/**
* Checks to see if exam is already done.
*
* @param object $mootyper
* @param int $userid
* @return boolean.
*/
function exam_already_done($mootyper, $userid) {
global $DB;
$table = 'mootyper_grades';
$select = 'userid='.$userid.' AND mootyper='.$mootyper->id; // Is put into the where clause.
$result = $DB->get_records_select($table, $select);
if (!is_null($result) && count($result) > 0) {
return true;
}
return false;
}
/**
* Get next exercise to do.
*
* Called from view.php file, around line 218.
*
* @param object $mootyperid
* @param int $lessonid
* @param int $userid
* @return $lessonid
*/
function get_exercise_from_mootyper($mootyperid, $lessonid, $userid) {
global $DB;
$table = 'mootyper_grades';
$select = 'userid='.$userid.' AND mootyper='.$mootyperid.' AND pass=1'; // Is put into the where clause.
$result = $DB->get_records_select($table, $select);
// Process result if it is not empty.
if (!is_null($result) && count($result) > 0) {
$max = 0;
foreach ($result as $grd) {
$exrec = $DB->get_record('mootyper_exercises', ['id' => $grd->exercise]);
$zapst = $exrec->snumber;
if ($zapst > $max) {
$max = $zapst;
}
}
// Return with the lesson ID and the next exercise snumber.
return $DB->get_record('mootyper_exercises', ['snumber' => ($max + 1), 'lesson' => $lessonid]);
} else {
// Return with the lesson ID and the first exercise snumber.
return $DB->get_record('mootyper_exercises', ['snumber' => 1, 'lesson' => $lessonid]);
}
}
/**
* Get a record.
*
* @param int $sid
* @return $sid
*/
function jget_mootyper_record($sid) {
global $DB;
return $DB->get_record('mootyper', ['id' => $sid]);
}
/**
* Updates an instance of the mootyper in the database.
*
* 20200808 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 stdClass $mootyper An object from the form in mod_form.php
* @param mod_mootyper_mod_form $mform
* @return boolean Success/Fail
*/
function mootyper_update_instance($mootyper, $mform) {
global $CFG, $DB, $OUTPUT, $USER;
$mootyper->timemodified = time();
$mootyper->id = $mootyper->instance;
if (empty($mootyper->assessed)) {
$mootyper->assessed = 0;
}
if (empty($mootyper->assessed)) {
$mootyper->assesstimestart = 0;
$mootyper->assesstimefinish = 0;
}
if (empty($mootyper->timeopen)) {
$mootyper->timeopen = 0;
}
if (empty($mootyper->timeclose)) {
$mootyper->timeclose = 0;
}
$cmid = $mootyper->coursemodule;
$cmidnumber = $mootyper->cmidnumber;
$courseid = $mootyper->course;
$mootyper->id = $mootyper->instance;
$context = context_module::instance($cmid);
$mootyper->timemodified = time();
$mootyper->id = $mootyper->instance;
$oldmootyper = $DB->get_record('mootyper', ['id' => $mootyper->id]);
// MDL-3942 - if the aggregation type or scale (i.e. max grade) changes then
// recalculate the grades for the entire mootyper if scale changes - do we
// need to recheck the ratings, if ratings higher than scale how do we want
// to respond? For count and sum aggregation types the grade we check to make
// sure they do not exceed the scale (i.e. max score) when calculating the grade.
$updategrades = false;
if ($oldmootyper->assessed <> $mootyper->assessed) {
// Whether this mootyper is rated.
$updategrades = true;
}
if ($oldmootyper->scale <> $mootyper->scale) {
// The scale currently in use.
$updategrades = true;
}
// 20200907 Skip grading options for Moodle less than v3.8.
if ($CFG->branch > 37) {
// 20230117 Fixes whole grades MTs created prior to adding Moodle grading to MooTyper.
if (empty($oldmootyper->scale) && $oldmootyper->grade_mootyper > 0) {
$mootyper->scale = $mootyper->grade_mootyper;
// The whole mootyper grading.
$updategrades = true;
}
if (empty($oldmootyper->grade_mootyper) || $oldmootyper->grade_mootyper <> $mootyper->grade_mootyper) {
// The whole mootyper grading.
$updategrades = true;
}
if ($updategrades) {
mootyper_update_grades($mootyper); // Recalculate grades for the mootyper.
}
}
// You may have to add extra stuff in here.
mootyper_update_calendar($mootyper, $cmid);
mootyper_grade_item_update($mootyper);
$completiontimeexpected = !empty($mootyper->completionexpected) ? $mootyper->completionexpected : null;
\core_completion\api::update_completion_date_event($mootyper->coursemodule, 'mootyper', $mootyper->id, $completiontimeexpected);
return $DB->update_record('mootyper', $mootyper);
}
/**
* Removes an instance of the mootyper from the database.
*
* 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 mootyper_delete_instance($id) {
global $DB;
$mootyper = $DB->get_record('mootyper', ['id' => $id], '*', MUST_EXIST);
mootyper_delete_all_grades($mootyper);
if (! $mootyper = $DB->get_record('mootyper', ['id' => $id])) {
return false;
}
mootyper_delete_all_checks($id);
$DB->delete_records('mootyper_attempts', ['mootyperid' => $id]);
$DB->delete_records('mootyper', ['id' => $mootyper->id]);
return true;
}
/**
* Removes all checks from the database.
*
* 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 $mid Id of the module instance
*/
function mootyper_delete_all_checks($mid) {
global $DB;
$rcs = $DB->get_records('mootyper_attempts', ['mootyperid' => $mid]);
foreach ($rcs as $at) {
$DB->delete_records('mootyper_checks', ['attemptid' => $at->id]);
}
}
/**
* Removes all grades from the database.
*
* 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 $mootyper Id of the module instance
*/
function mootyper_delete_all_grades($mootyper) {
global $DB;
$DB->delete_records('mootyper_grades', ['mootyper' => $mootyper->id]);
}
/**
* Returns 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.
* @param int $course
* @param int $user
* @param int $mod
* @param int $mootyper
* $return->time = The time they did it.
* $return->info = A short text description.
*
* @return stdClass|null
*/
function mootyper_user_outline($course, $user, $mod, $mootyper) {
$return = new stdClass();
$return->time = 0;
$return->info = 'This is an outline.';
return $return;
}
/**
* Prints a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param stdClass $course the current course record
* @param stdClass $user the record of the user we are generating report for
* @param cm_info $mod course module info
* @param stdClass $mootyper the module instance record
* @return void, is supposed to echp directly
*/
function mootyper_user_complete($course, $user, $mod, $mootyper) {
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in mootyper activities and print it out.
* Return true if there was output, or false is there was none.
*
* @param int $course
* @param int $viewfullnames
* @param int $timestart
* @return boolean
*/
function mootyper_print_recent_activity($course, $viewfullnames, $timestart) {
global $CFG, $USER, $DB, $OUTPUT;
$dbparams = [$timestart, $course->id, 'mootyper'];
// 20212611 Added Moodle branch check.
if ($CFG->branch < 311) {
$namefields = user_picture::fields('u', null, 'userid');
} else {
$userfieldsapi = \core_user\fields::for_userpic();
$namefields = $userfieldsapi->get_sql('u', false, '', 'userid', false)->selects;;
}
$sql = "SELECT mtg.id, mtg.mootyper, mtg.timetaken, cm.id AS cmid, $namefields
FROM {mootyper_grades} mtg
JOIN {mootyper} mt ON mt.id = mtg.mootyper
JOIN {course_modules} cm ON cm.instance = mt.id
JOIN {modules} md ON md.id = cm.module
JOIN {user} u ON u.id = mtg.userid
WHERE mtg.timetaken > ? AND
mt.course = ? AND
md.name = ?
ORDER BY mtg.timetaken ASC
";
$newentries = $DB->get_records_sql($sql, $dbparams);
$modinfo = get_fast_modinfo($course);
$show = [];
$grader = [];
$showrecententries = get_config('mod_mootyper', 'showrecentactivity');
foreach ($newentries as $anentry) {
if (!array_key_exists($anentry->cmid, $modinfo->get_cms())) {
continue;
}
$cm = $modinfo->get_cm($anentry->cmid);
if (!$cm->uservisible) {
continue;
}
if ($anentry->userid == $USER->id) {
$show[] = $anentry;
continue;
}
$context = context_module::instance($anentry->cmid);
// The act of submitting of entries may be considered private -
// only graders will see it if specified.
if (empty($showrecententries)) {
if (!array_key_exists($cm->id, $grader)) {
$grader[$cm->id] = has_capability('moodle/grade:viewall', $context);
}
if (!$grader[$cm->id]) {
continue;
}
}
$groupmode = groups_get_activity_groupmode($cm, $course);
if ($groupmode == SEPARATEGROUPS &&
!has_capability('moodle/site:accessallgroups', $context)) {
if (isguestuser()) {
// Shortcut - guest user does not belong into any group.
continue;
}
// This will be slow - show only users that share group with me in this cm.
if (!$modinfo->get_groups($cm->groupingid)) {
continue;
}
$usersgroups = groups_get_all_groups($course->id, $anentry->userid, $cm->groupingid);
if (is_array($usersgroups)) {
$usersgroups = array_keys($usersgroups);
$intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid));
if (empty($intersect)) {
continue;
}
}
}
$show[] = $anentry;
}
if (empty($show)) {
return false;
}
echo $OUTPUT->heading(get_string('modulenameplural', 'mootyper').':', 3);
foreach ($show as $submission) {
$cm = $modinfo->get_cm($submission->cmid);
$context = context_module::instance($submission->cmid);
if (has_capability('mod/mootyper:viewgrades', $context)) {
$link = $CFG->wwwroot.'/mod/mootyper/gview.php?id='.$cm->id.'&n='.$submission->mootyper;
} else {
$link = $CFG->wwwroot.'/mod/mootyper/owngrades.php?id='.$cm->id.'&n='.$submission->mootyper;
}
$name = $cm->name;
print_recent_activity_note($submission->timetaken,
$submission,
$name,
$link,
false,
$viewfullnames);
}
return true;
}
/**
* Prepares the recent activity data.
*
* This callback function is supposed to populate the passed array with
* custom activity records. These records are then rendered into HTML via
* mootyper_print_recent_mod_activity().
*
* @param array $activities sequentially indexed array of objects with the 'cmid' property
* @param int $index the index in the $activities to use for the next record
* @param int $timestart append activity since this time
* @param int $courseid the id of the course we produce the report for
* @param int $cmid course module id
* @param int $userid check for a particular user's activity only, defaults to 0 (all users)
* @param int $groupid check for a particular group's activity only, defaults to 0 (all groups)
* @return void adds items into $activities and increases $index
*/
function mootyper_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
}
/**
* Prints single activity item prepared by {mootyper_get_recent_mod_activity()}.
* @param int $activity
* @param int $courseid
* @param int $detail
* @param int $modnames
* @param int $viewfullnames
* @return void
*/
function mootyper_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
}
/**
* Returns an array of users who are participanting in this mootyper.
*
* Must return an array of users who are participants for a given instance
* of mootyper. Must include every user involved in the instance,
* independient of his role (student, teacher, admin...). The returned
* objects must contain at least id property.
* See other modules as example.
*
* @param int $mootyperid ID of an instance of this module
* @return boolean|array false if no participants, array of objects otherwise
*/
function mootyper_get_participants($mootyperid) {
return false;
}
/**
* Returns all other caps used in the module.
*
* @return array
*/
function mootyper_get_extra_capabilities() {
return [];
}
// Gradebook API.
/**
* Is a given scale used by the instance of mootyper?
*
* This function returns if a scale is being used by one mootyper
* 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 $mootyperid ID of an instance of this module
* @param int $scaleid
* @return bool true if the scale is used by the given mootyper instance
*/
function mootyper_scale_used($mootyperid, $scaleid) {
return false;
$rec = $DB->get_record("mootyper", ["id" => $mootyperid, "grade" => -$scaleid]);
if (!empty($rec) && !empty($scaleid)) {
$return = true;
}
return $return;
}
/**
* Checks if scale is being used by any instance of mootyper.
*
* This is used to find out if scale used anywhere.
*
* @param int $scaleid
* @return boolean true if the scale is used by any mootyper instance
*/
function mootyper_scale_used_anywhere(int $scaleid): bool {
global $DB;
if (empty($scaleid)) {
return false;
}
return $DB->record_exists('mootyper', ['scale' => $scaleid * -1]);
}
/**
* Creates or updates grade item for the given mootyper instance.
*
* 20200808 Needed by grade_update_mod_grades() in lib/gradelib.php
*
* @category grade
* @uses GRADE_TYPE_NONE
* @uses GRADE_TYPE_VALUE
* @uses GRADE_TYPE_SCALE
* @param stdClass $mootyper Instance object with extra cmidnumber and modname property
* @param mixed $ratings
* @param mixed $mootypergrades
* @return int 0 if ok
*/
function mootyper_grade_item_update($mootyper, $ratings = null, $mootypergrades = null): void {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
// Update the rating.
$item = [
'itemname' => get_string('gradeitemnameforrating', 'mootyper', $mootyper),
'idnumber' => $mootyper->cmidnumber,
];
if (!$mootyper->assessed || $mootyper->scale == 0) {
$item['gradetype'] = GRADE_TYPE_NONE;
} else if ($mootyper->scale > 0) {
$item['gradetype'] = GRADE_TYPE_VALUE;
$item['grademax'] = $mootyper->scale;
$item['grademin'] = 0;
} else if ($mootyper->scale < 0) {
$item['gradetype'] = GRADE_TYPE_SCALE;
$item['scaleid'] = -$mootyper->scale;
}
if ($ratings === 'reset') {
$item['reset'] = true;
$ratings = null;
}
// Itemnumber 0 is the rating.
grade_update('mod/mootyper', $mootyper->course, 'mod', 'mootyper', $mootyper->id, 0, $ratings, $item);
// 20200907 Skip mootyper grading options for Moodle less than v3.8.
if ($CFG->branch > 37) {
// Whole mootyper grade.
$item = [
'itemname' => get_string('gradeitemnameforwholemootyper', 'mootyper', $mootyper),
// Note: We do not need to store the idnumber here.
];
if (empty($mootyper->grade_mootyper)) {
$item['gradetype'] = GRADE_TYPE_NONE;
} else if ($mootyper->grade_mootyper > 0) {
$item['gradetype'] = GRADE_TYPE_VALUE;
$item['grademax'] = $mootyper->grade_mootyper;
$item['grademin'] = 0;
} else if ($mootyper->grade_mootyper < 0) {
$item['gradetype'] = GRADE_TYPE_SCALE;
$item['scaleid'] = $mootyper->grade_mootyper * -1;
}
if ($mootypergrades === 'reset') {
$item['reset'] = true;
$mootypergrades = null;
}
// Itemnumber 1 is the whole mootyper grade.
grade_update('mod/mootyper', $mootyper->course, 'mod', 'mootyper', $mootyper->id, 1, $mootypergrades, $item);
}
}
/**
* Update mootyper grades in the gradebook.
*
* Needed by grade_update_mod_grades() in lib/gradelib.php.
* New version to replace the one above.
* @param stdClass $mootyper instance object with extra cmidnumber and modname property.
* @param int $userid update grade of specific user only, 0 means all participants.
* @return void
*/
function mootyper_update_grades($mootyper, $userid=0): void {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
$cm = get_coursemodule_from_instance('mootyper', $mootyper->id);
$mootyper->cmidnumber = $cm->idnumber;
$ratings = null;
if ($mootyper->assessed) {
// I think this is the Whole MooTyper grade stuff.
require_once($CFG->dirroot.'/rating/lib.php');
$rm = new rating_manager();
$ratings = $rm->get_user_grades((object) [
'component' => 'mod_mootyper',
'ratingarea' => 'exercises',
'contextid' => \context_module::instance($cm->id)->id,
'modulename' => 'mootyper',
'moduleid ' => $mootyper->id,
'userid' => $userid,
'aggregationmethod' => $mootyper->assessed,
'scaleid' => $mootyper->scale,
'itemtable' => 'mootyper_grades',
'itemtableusercolumn' => 'userid',
]);
}
$mootypergrades = null;
if (($mootyper->requiredgoal) || ($mootyper->requiredwpm)) {
$sql = "SELECT g.userid,
0 as datesubmitted,
g.grade as rawgrade,
g.timetaken as dategraded,
g.mistakedetails
FROM {mootyper} m
JOIN {mootyper_grades} g ON g.mootyper = m.id
WHERE m.id = :mootyperid";
$params = [
'mootyperid' => $mootyper->id,
];
if ($userid) {
$sql .= " AND g.userid = :userid";
$params['userid'] = $userid;
}
$mootypergrades = [];
if ($grades = $DB->get_recordset_sql($sql, $params)) {
foreach ($grades as $userid => $grade) {
if ($grade->rawgrade != -1) {
$grade->feedback = $grade->mistakedetails;
$mootypergrades[$userid] = $grade;
$course = $DB->get_record('course', ['id' => $mootyper->course]);
// 20240122 I think this needs to be $course.
$ci = new completion_info($course);
if ($cm->completion == COMPLETION_TRACKING_AUTOMATIC) {
$ci->update_state($cm, COMPLETION_UNKNOWN, $grade->userid);
}
}
}
$grades->close();
}
}
mootyper_grade_item_update($mootyper, $ratings, $mootypergrades);
}
/**
* Called by course/reset.php
*
* @param stdClass $mform
*/
function mootyper_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'mootyperheader', get_string('modulenameplural', 'mootyper'));
$mform->addElement('checkbox', 'reset_mootyper', get_string('resetmootyperall', 'mootyper'));
}
/**
* This function is used by the reset_course_userdata function in moodlelib.
*
*
* @param stdClass $data
* @return array
*/
function mootyper_reset_userdata($data) {
global $DB;
$status = [];
if (!empty($data->reset_mootyper)) {
$instances = $DB->get_records('mootyper', ['course' => $data->courseid]);
foreach ($instances as $instance) {
if (reset_mootyper_instance($instance->id)) {
$status[] = ['component' => get_string('modulenameplural', 'mootyper')
, 'item' => get_string('resetmootyperall', 'mootyper')
.': '.$instance->name, 'error' => false, ];
}
}
}
return $status;
}
/**
* Clear all attempts and grades.
*
* This function will remove all attempts and grades from the specified
* mootyper and clean up any related data.
* @param int $mootyperid
* @return boolean Success/Failure
*/
function reset_mootyper_instance($mootyperid) {
global $DB;
$attempts = $DB->get_records('mootyper_attempts', ['mootyperid' => $mootyperid]);
foreach ($attempts as $attempt) {
// 20230708 Added to delete any checks for this attempt.
$DB->delete_records('mootyper_checks', ['attemptid' => $attempt->id]);
if (!$DB->delete_records('mootyper_attempts', ['id' => $mootyperid])) {
return false;
}
}
if (!$DB->delete_records('mootyper_grades', ['mootyper' => $mootyperid])) {
return false;
}
if (!$DB->delete_records('mootyper_attempts', ['mootyperid' => $mootyperid])) {
return false;
}
return true;
}
// File API.