-
Notifications
You must be signed in to change notification settings - Fork 41
/
lib.php
1703 lines (1489 loc) · 61 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 functions and constants for module game
*
* @package mod_game
* @copyright 2007 Vasilis Daloukas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Define CONSTANTS.
/*
* Options determining how the grades from individual attempts are combined to give
* the overall grade for a user
*/
define('GAME_GRADEHIGHEST', 1);
define('GAME_GRADEAVERAGE', 2);
define('GAME_ATTEMPTFIRST', 3);
define('GAME_ATTEMPTLAST', 4);
// The different review options are stored in the bits of $game->review.
// These constants help to extract the options.
define('GAME_REVIEW_IMMEDIATELY', 0x3f); // The first 6 bits refer to the time immediately after the attempt.
define('GAME_REVIEW_OPEN', 0xfc0); // The next 6 bits refer to the time after the attempt but while the game is open.
define('GAME_REVIEW_CLOSED', 0x3f000); // The final 6 bits refer to the time after the game closes.
// Within each group of 6 bits we determine what should be shown.
define('GAME_REVIEW_RESPONSES', 1 * 0x1041); // Show responses.
define('GAME_REVIEW_SCORES', 2 * 0x1041); // Show scores.
define('GAME_REVIEW_FEEDBACK', 4 * 0x1041); // Show feedback.
define('GAME_REVIEW_ANSWERS', 8 * 0x1041); // Show correct answers.
// Some handling of worked solutions is already in the code but not yet fully supported.
// and not switched on in the user interface.
define('GAME_REVIEW_SOLUTIONS', 16 * 0x1041); // Show solutions.
define('GAME_REVIEW_GENERALFEEDBACK', 32 * 0x1041); // Show general feedback.
/**
* Given an object containing all the necessary data, will create a new instance and return the id number of the new instance.
*
* @param object $game An object from the form in mod.html
*
* @return int The id of the newly inserted game record
**/
function game_add_instance($game) {
global $DB;
$game->timemodified = time();
game_before_add_or_update( $game);
// May have to add extra stuff in here.
$id = $DB->insert_record("game", $game);
$game = $DB->get_record_select( 'game', "id=$id");
// Do the processing required after an add or an update.
game_grade_item_update( $game);
return $id;
}
/**
* Given an object containing all the necessary data, this function will update an existing instance with new data.
*
* @param object $game An object from the form in mod.html
* @return boolean Success/Fail
**/
function game_update_instance($game) {
global $DB;
$game->timemodified = time();
$game->id = $game->instance;
if (!isset( $game->glossarycategoryid)) {
$game->glossarycategoryid = 0;
}
if (!isset( $game->glossarycategoryid2)) {
$game->glossarycategoryid2 = 0;
}
if ($game->grade == '') {
$game->grade = 0;
}
if (!isset( $game->param1)) {
$game->param1 = 0;
}
if ($game->param1 == 0) {
$game->param1 = 0;
}
if (!isset( $game->param2)) {
$game->param2 = 0;
}
if ($game->param2 == 0) {
$game->param2 = 0;
}
if (!isset( $game->questioncategoryid)) {
$game->questioncategoryid = 0;
}
game_before_add_or_update( $game);
if (!$DB->update_record("game", $game)) {
return false;
}
// Do the processing required after an add or an update.
game_grade_item_update( $game);
return true;
}
/**
* Updates some fields before writing to database.
*
* @param stdClass $game
*/
function game_before_add_or_update(&$game) {
if (isset( $game->toptext)) {
$game->toptext = $game->toptext['text'];
}
if (isset( $game->bottomtext)) {
$game->bottomtext = $game->bottomtext['text'];
}
if (isset( $game->questioncategoryid)) {
$pos = strpos( $game->questioncategoryid, ',');
if ($pos != false) {
$game->questioncategoryid = substr( $game->questioncategoryid, 0, $pos);
}
}
if ($game->gamekind == 'millionaire') {
$pos = strpos( '-'.$game->param8, '#');
if ($pos > 0) {
$game->param8 = hexdec(substr( $game->param8, $pos));
}
} else if ($game->gamekind == 'snakes') {
$s = '';
if ($game->param3 == 0) {
// Means user defined.
$draftitemid = $game->param4;
if (isset( $game->id)) {
$cmg = get_coursemodule_from_instance('game', $game->id, $game->course);
$modcontext = game_get_context_module_instance( $cmg->id);
$attachmentoptions = ['subdirs' => 0, 'maxbytes' => 9999999, 'maxfiles' => 1];
file_save_draft_area_files($draftitemid, $modcontext->id, 'mod_game', 'snakes_file', $game->id,
['subdirs' => 0, 'maxbytes' => 9999999, 'maxfiles' => 1]);
$game->param5 = 1;
}
if (isset( $_POST['snakes_cols'])) {
$fields = [ 'snakes_data', 'snakes_cols', 'snakes_rows', 'snakes_headerx', 'snakes_headery',
'snakes_footerx', 'snakes_footery', 'snakes_width', 'snakes_height'];
foreach ($fields as $f) {
$s .= '#'.$f.':'.$_POST[$f];
}
$s = substr( $s, 1);
}
}
$game->param9 = $s;
}
}
/**
* 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 $gameid Id of the module instance
* @return boolean Success/Failure
**/
function game_delete_instance($gameid) {
global $DB;
// Delete any dependent records here.
$aids = [];
if (($recs = $DB->get_records( 'game_attempts', [ 'gameid' => $gameid])) != false) {
$ids = '';
$count = 0;
foreach ($recs as $rec) {
$ids .= ( $ids == '' ? $rec->id : ','.$rec->id);
if (++$count > 10) {
$aids[] = $ids;
$count = 0;
$ids = '';
}
}
if ($ids != '') {
$aids[] = $ids;
}
}
foreach ($aids as $ids) {
$tables = [ 'game_hangman', 'game_cross', 'game_cryptex', 'game_millionaire',
'game_bookquiz', 'game_sudoku', 'game_snakes'];
foreach ($tables as $t) {
$sql = "DELETE FROM {".$t."} WHERE id IN (".$ids.')';
if (!$DB->execute( $sql)) {
return false;
}
}
}
$tables = [ 'game_attempts', 'game_grades', 'game_bookquiz_questions', 'game_queries', 'game_repetitions'];
foreach ($tables as $t) {
if (!$DB->delete_records( $t, [ 'gameid' => $gameid])) {
return false;
}
}
$tables = [ 'game_export_javame', 'game_export_html', 'game'];
foreach ($tables as $table) {
if (!$DB->delete_records( $table, [ 'id' => $gameid])) {
return false;
}
}
return true;
}
/**
* Return a small object with summary information about what a user has done
*
* @param stdClass $course
* @param stdClass $user
* @param string $mod
* @param stdClass $game
*
* $return->time = the time they did it
* $return->info = a short text description
**/
function game_user_outline($course, $user, $mod, $game) {
global $DB;
if ($grade = $DB->get_record_select('game_grades', "userid=$user->id AND gameid = $game->id", null, 'id,score,timemodified')) {
$result = new stdClass;
if ((float)$grade->score) {
$result->info = get_string('gradenoun').': '.round($grade->score * $game->grade, $game->decimalpoints).' '.
get_string('percent', 'game').': '.round(100 * $grade->score, $game->decimalpoints).' %';
}
$result->time = $grade->timemodified;
return $result;
}
return null;
}
/**
* Print a detailed representation of what a user has done with a given particular game,(user activity reports).
* @param stdClass $course
* @param stdClass $user
* @param string $mod
* @param stdClass $game
*/
function game_user_complete($course, $user, $mod, $game) {
global $DB;
if ($attempts = $DB->get_records_select('game_attempts', "userid='$user->id' AND gameid='$game->id'", null, 'attempt ASC')) {
if ($game->grade && $grade = $DB->get_record('game_grades', [ 'userid' => $user->id, 'gameid' => $game->id])) {
echo get_string('gradenoun').': '.game_format_score( $game, $grade->score).'/'.$game->grade.'<br />';
}
foreach ($attempts as $attempt) {
echo get_string('attempt', 'game').' '.$attempt->attempt.': ';
if ($attempt->timefinish == 0) {
print_string( 'unfinished');
} else {
echo game_format_score( $game, $attempt->score).'/'.$game->grade;
}
echo ' - '.userdate($attempt->timelastattempt).'<br />';
}
} else {
print_string('noattempts', 'game');
}
return true;
}
/**
* Given a course and a time, this module should find recent activity that has occurred in game activities and print it out.
*
* @uses $CFG
* @return boolean
* @todo Finish documenting this function
*
* @param stdClass $course
* @param int $isteacher
* @param int $timestart
*
* @return True if anything was printed, otherwise false.
*/
function game_print_recent_activity($course, $isteacher, $timestart) {
global $CFG;
return false;
}
/**
* Function to be run periodically according to the moodle cron
*
* @uses $CFG
* @return boolean
* @todo Finish documenting this function
**/
function game_cron() {
global $CFG;
return true;
}
/**
* Must return an array of grades for a given instance of this module, indexed by user.
*
* Example:
* $return->grades = array of grades;
* $return->maxgrade = maximum allowed grade;
*
* return $return;
*
* @param int $gameid ID of an instance of this module
* @return mixed Null or object with an array of grades and with the maximum grade
**/
function game_grades($gameid) {
// Must return an array of grades, indexed by user, and a max grade.
global $DB;
$game = $DB->get_record( 'game', [ 'id' => intval($gameid)]);
if (empty($game) || empty($game->grade)) {
return null;
}
$return = new stdClass;
$return->grades = $DB->get_records_menu('game_grades', 'gameid', $game->id, '', "userid, score * {$game->grade}");
$return->maxgrade = $game->grade;
return $return;
}
/**
* Return grade for given user or all users.
*
* @param stdClass $game
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function game_get_user_grades($game, $userid=0) {
global $DB;
$user = $userid ? "AND u.id = $userid" : "";
if (!isset( $game->grade)) {
$game->grade = 1;
}
$sql = 'SELECT u.id, u.id AS userid, '.$game->grade.
' * g.score AS rawgrade, g.timemodified AS dategraded, MAX(a.timefinish) AS datesubmitted
FROM {user} u, {game_grades} g, {game_attempts} a
WHERE u.id = g.userid AND g.gameid = '.$game->id.' AND a.gameid = g.gameid AND u.id = a.userid';
if ($userid != 0) {
$sql .= ' AND u.id='.$userid;
}
$sql .= ' GROUP BY u.id, g.score, g.timemodified';
return $DB->get_records_sql( $sql);
}
/**
* Must return an array of user records (all data) who are participants for a given instance of game.
*
* @param int $gameid ID of an instance of this module
* @return mixed boolean/array of students
**/
function game_get_participants($gameid) {
return false;
}
/**
* This function returns if a scale is being used by one game it it has support for grading and scales.
*
* @param int $gameid ID of an instance of this module
* @param int $scaleid
* @return mixed
* @todo Finish documenting this function
**/
function game_scale_used ($gameid, $scaleid) {
$return = false;
return $return;
}
/**
* Update grades in central gradebook
*
* @param object $game null means all games
* @param int $userid specific user only, 0 mean all
* @param boolean $nullifnone
*/
function game_update_grades($game=null, $userid=0, $nullifnone=true) {
global $CFG;
if (!function_exists('grade_update')) { // Workaround for buggy PHP versions.
if (file_exists( $CFG->libdir.'/gradelib.php')) {
require_once($CFG->libdir.'/gradelib.php');
} else {
return;
}
}
if ($game != null) {
$grades = game_get_user_grades($game, $userid);
if ( $grades != null) {
game_grade_item_update($game, $grades);
} else if ($userid && $nullifnone) {
$grade = new stdClass;
$grade->userid = $userid;
$grade->rawgrade = null;
game_grade_item_update( $game, $grade);
} else {
game_grade_item_update( $game);
}
} else {
$sql = "SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid
FROM {game} a, {course_modules} cm, {modules} m
WHERE m.name='game' AND m.id=cm.module AND cm.instance=a.id";
if ($rs = $DB->get_recordset_sql( $sql)) {
while ($game = $DB->rs_fetch_next_record( $rs)) {
if ($game->grade != 0) {
game_update_grades( $game, 0, false);
} else {
game_grade_item_update( $game);
}
}
$DB->rs_close( $rs);
}
}
}
/**
* Create grade item for given game
* Updates table grade_grades
*
* @param object $game object with extra cmidnumber
* @param stdClass $grades
* @return int 0 if ok, error code otherwise
*/
function game_grade_item_update($game, $grades=null) {
global $CFG;
if (!function_exists('grade_update')) { // Workaround for buggy PHP versions.
if (file_exists( $CFG->libdir.'/gradelib.php')) {
require_once($CFG->libdir.'/gradelib.php');
} else {
return;
}
}
if (isset($game->cmidnumber)) { // Tt may not be always present.
$params = ['itemname' => $game->name, 'idnumber' => $game->cmidnumber];
} else {
$params = ['itemname' => $game->name];
}
if ($game->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $game->grade;
$params['grademin'] = 0;
} else {
$params['gradetype'] = GRADE_TYPE_NONE;
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update('mod/game', $game->course, 'mod', 'game', $game->id, 0, $grades, $params);
}
/**
* Delete grade item for given game
*
* @param object $game object
* @return object game
*/
function game_grade_item_delete( $game) {
global $CFG;
if (file_exists( $CFG->libdir.'/gradelib.php')) {
require_once($CFG->libdir.'/gradelib.php');
} else {
return;
}
return grade_update('mod/game', $game->course, 'mod', 'game', $game->id, 0, null, ['deleted' => 1]);
}
/**
* Returns all game graded users since a given time for specified game
*
* @param stdClass $activities
* @param int $index
* @param int $timestart
* @param int $courseid
* @param int $cmid
* @param int $userid
* @param int $groupid
*/
function game_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
global $DB, $COURSE, $USER;
if ($COURSE->id == $courseid) {
$course = $COURSE;
} else {
$course = $DB->get_record('course', [ 'id' => $courseid]);
}
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->cms[$cmid];
if ($userid) {
$userselect = "AND u.id = $userid";
} else {
$userselect = "";
}
if ($groupid) {
$groupselect = "AND gm.groupid = $groupid";
$groupjoin = "JOIN {groups_members} gm ON gm.userid=u.id";
} else {
$groupselect = "";
$groupjoin = "";
}
$sql = "SELECT qa.*, qa.gameid, q.grade, u.lastname,u.firstname,u.picture ".
"FROM {game_attempts} qa JOIN {game} q ON q.id = qa.gameid JOIN {user} u ON u.id = qa.userid $groupjoin ".
"WHERE qa.timefinish > $timestart AND q.id = $cm->instance $userselect $groupselect ".
"ORDER BY qa.timefinish ASC";
if (!$attempts = $DB->get_records_sql( $sql)) {
return;
}
$cmcontext = game_get_context_module_instance( $cm->id);
$grader = has_capability('moodle/grade:viewall', $cmcontext);
$accessallgroups = has_capability('moodle/site:accessallgroups', $cmcontext);
$viewfullnames = has_capability('moodle/site:viewfullnames', $cmcontext);
$groupmode = groups_get_activity_groupmode($cm, $course);
if (is_null($modinfo->groups)) {
$modinfo->groups = groups_get_user_groups($course->id); // Load all my groups and cache it in modinfo.
}
$aname = format_string($cm->name, true);
foreach ($attempts as $attempt) {
if ($attempt->userid != $USER->id) {
if (!$grader) {
// Grade permission required.
continue;
}
if ($groupmode == SEPARATEGROUPS && !$accessallgroups) {
$usersgroups = groups_get_all_groups($course->id, $attempt->userid, $cm->groupingid);
if (!is_array($usersgroups)) {
continue;
}
$usersgroups = array_keys($usersgroups);
$interset = array_intersect($usersgroups, $modinfo->groups[$cm->id]);
if (empty($intersect)) {
continue;
}
}
}
$tmpactivity = new stdClass;
$tmpactivity->type = 'game';
$tmpactivity->gameid = $attempt->gameid;
$tmpactivity->cmid = $cm->id;
$tmpactivity->name = $aname;
$tmpactivity->sectionnum = $cm->sectionnum;
$tmpactivity->timestamp = $attempt->timefinish;
$tmpactivity->content = new stdClass;
$tmpactivity->content->attemptid = $attempt->id;
$tmpactivity->content->sumgrades = $attempt->score * $attempt->grade;
$tmpactivity->content->maxgrade = $attempt->grade;
$tmpactivity->content->attempt = $attempt->attempt;
$tmpactivity->user = new stdClass;
$tmpactivity->user->userid = $tmpactivity->user->id = $attempt->userid;
$tmpactivity->user->fullname = fullname($attempt, $viewfullnames);
$tmpactivity->user->firstname = $attempt->firstname;
$tmpactivity->user->lastname = $attempt->lastname;
$tmpactivity->user->picture = $attempt->picture;
$tmpactivity->user->imagealt = $attempt->imagealt;
$tmpactivity->user->email = $attempt->email;
$activities[$index++] = $tmpactivity;
}
}
/**
* Prints recent activity.
*
* @param stdClass $activity
* @param int $courseid
* @param stdClass $detail
* @param array $modnames
*/
function game_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
global $CFG, $OUTPUT;
echo '<table border="0" cellpadding="3" cellspacing="0" class="forum-recent">';
echo "<tr><td class=\"userpicture\" valign=\"top\">";
echo $OUTPUT->user_picture($activity->user, ['courseid' => $courseid]);
echo "</td><td>";
if ($detail) {
$modname = $modnames[$activity->type];
echo '<div class="title">';
echo "<img src=\"$CFG->modpixpath/{$activity->type}/icon.gif\" ".
"class=\"icon\" alt=\"$modname\" />";
echo "<a href=\"{$CFG->wwwroot}/mod/game/view.php?id={$activity->cmid}\">{$activity->name}</a>";
echo '</div>';
}
echo '<div class="grade">';
echo get_string("attempt", "game")." {$activity->content->attempt}: ";
$grades = "({$activity->content->sumgrades} / {$activity->content->maxgrade})";
echo "<a href=\"{$CFG->wwwroot}/mod/game/review.php".
"?attempt={$activity->content->attemptid}&q={$activity->gameid}\">$grades</a>";
echo '</div>';
echo '<div class="user">';
echo "<a href=\"{$CFG->wwwroot}/user/view.php?id={$activity->user->userid}&course=$courseid\">"
."{$activity->user->fullname}</a> - ".userdate($activity->timestamp);
echo '</div>';
echo "</td></tr></table>";
}
/**
* Removes all grades from gradebook
*
* @param int $courseid
* @param string $type
**/
function game_reset_gradebook($courseid, $type='') {
global $DB;
$sql = "SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid
FROM {game} q, {course_modules} cm, {modules} m
WHERE m.name='game' AND m.id=cm.module AND cm.instance=q.id AND q.course=$courseid";
if ($games = $DB->get_records_sql( $sql)) {
foreach ($games as $game) {
game_grade_item_update( $game, 'reset');
}
}
}
/**
* What supports.
*
* @uses FEATURE_GRADE_HAS_GRADE
* @param string $feature
* @return bool True if quiz supports feature
*/
function game_supports($feature) {
global $CFG;
if ($CFG->branch >= 400) {
if ($feature == FEATURE_MOD_PURPOSE) {
return MOD_PURPOSE_ASSESSMENT;
}
}
switch($feature) {
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GROUPS:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_GROUPMEMBERSONLY:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_GRADE_OUTCOMES:
return true;
case FEATURE_RATE:
return false;
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_SHOW_DESCRIPTION:
return true;
default:
return null;
}
}
/**
* get extra capabilities
*
* @return array all other caps used in module
*/
function game_get_extra_capabilities() {
global $DB, $CFG;
require_once($CFG->libdir.'/questionlib.php');
$caps = question_get_all_capabilities();
$reportcaps = $DB->get_records_select_menu('capabilities', 'name LIKE ?', ['quizreport/%'], 'id,name');
$caps = array_merge($caps, $reportcaps);
$caps[] = 'moodle/site:accessallgroups';
return $caps;
}
/**
* Return a textual summary of the number of attemtps that have been made at a particular game,
*
* @param object $game the game object. Only $game->id is used at the moment.
* @param object $cm the cm object. Only $cm->course, $cm->groupmode and $cm->groupingid fields are used at the moment.
* @param boolean $returnzero if false (default), when no attempts have been made '' is returned instead of 'Attempts: 0'.
* @param int $currentgroup if there is a concept of current group where this method is being called
* (e.g. a report) pass it in here. Default 0 which means no current group.
* @return string a string like "Attempts: 123", "Attemtps 123 (45 from your groups)" or
* "Attemtps 123 (45 from this group)".
*/
function game_num_attempt_summary($game, $cm, $returnzero = false, $currentgroup = 0) {
global $CFG, $USER, $DB;
$numattempts = $DB->count_records('game_attempts', ['gameid' => $game->id, 'preview' => 0]);
if ($numattempts || $returnzero) {
if (groups_get_activity_groupmode($cm)) {
$a = new stdClass();
$a->total = $numattempts;
if ($currentgroup) {
$a->group = $DB->count_records_sql('SELECT count(1) FROM ' .
'{game_attempts} qa JOIN ' .
'{groups_members} gm ON qa.userid = gm.userid ' .
'WHERE gameid = ? AND preview = 0 AND groupid = ?', [$game->id, $currentgroup]);
return get_string('attemptsnumthisgroup', 'quiz', $a);
} else if ($groups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) {
list($usql, $params) = $DB->get_in_or_equal(array_keys($groups));
$a->group = $DB->count_records_sql('SELECT count(1) FROM ' .
'{game_attempts} qa JOIN ' .
'{groups_members} gm ON qa.userid = gm.userid ' .
'WHERE gameid = ? AND preview = 0 AND ' .
"groupid $usql", array_merge([$game->id], $params));
return get_string('attemptsnumyourgroups', 'quiz', $a);
}
}
return get_string('attemptsnum', 'quiz', $numattempts);
}
return '';
}
/**
* Converts score of game to grade.
*
* @param stdClass $game
* @param float $score
*
* @return float the score
*/
function game_format_score($game, $score) {
return format_float($game->grade * $score / 100, $game->decimalpoints);
}
/**
* Converts grade to score.
*
* @param stdClass $game
* @param float $grade
*
* @return foat score
*/
function game_format_grade($game, $grade) {
return format_float($grade, $game->decimalpoints == null ? 2 : $game->decimalpoints);
}
/**
* get grading options
*
* @return the options for calculating the quiz grade from the individual attempt grades.
*/
function game_get_grading_options() {
return [
GAME_GRADEHIGHEST => get_string('gradehighest', 'quiz'),
GAME_GRADEAVERAGE => get_string('gradeaverage', 'quiz'),
GAME_ATTEMPTFIRST => get_string('attemptfirst', 'quiz'),
GAME_ATTEMPTLAST => get_string('attemptlast', 'quiz')];
}
/**
* This function extends the settings navigation block for the site.
*
* It is safe to rely on PAGE here as we will only ever be within the module
* context when this is called
*
* @param settings_navigation $settings
* @param navigation_node $gamenode
* @return void
*/
function game_extend_settings_navigation($settings, $gamenode) {
global $PAGE, $CFG, $DB;
$context = $PAGE->cm->context;
if (!has_capability('mod/game:viewreports', $context)) {
return;
}
if (has_capability('mod/game:view', $context)) {
$url = new moodle_url('/mod/game/view.php', ['id' => $PAGE->cm->id]);
$gamenode->add(get_string('info', 'game'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/info', ''));
}
if (has_capability('mod/game:manage', $context)) {
$url = new moodle_url('/course/modedit.php', ['update' => $PAGE->cm->id, 'return' => true, 'sesskey' => sesskey()]);
$gamenode->add(get_string('edit', 'moodle', ''), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('t/edit', ''));
}
if (has_capability('mod/game:manage', $context)) {
$gameid = $PAGE->cm->instance;
$sql = "SELECT id,gamekind,sourcemodule,bookid,course,glossaryid,quizid,questioncategoryid ".
"FROM {$CFG->prefix}game WHERE id=$gameid";
$game = $DB->get_record_sql( $sql);
if (($game->gamekind == 'bookquiz') && ($game->bookid != 0)) {
$book = $DB->get_record_sql( "SELECT id,name FROM {$CFG->prefix}book WHERE id={$game->bookid}");
$cmd = get_coursemodule_from_instance('book', $game->bookid, $game->course);
$url = new moodle_url('/mod/book/view.php', ['id' => $cmd->id]);
$gamenode->add(get_string('viewbook', 'game', $book->name), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('t/edit', ''));
}
if (($game->sourcemodule == 'glossary') && ($game->glossaryid != 0)) {
$glossary = $DB->get_record_sql( "SELECT id,name FROM {$CFG->prefix}glossary WHERE id={$game->glossaryid}");
$cmd = get_coursemodule_from_instance('glossary', $game->glossaryid, $game->course);
$url = new moodle_url('/mod/glossary/view.php', ['id' => $cmd->id]);
$gamenode->add(get_string('viewglossary', 'game', ' '.$glossary->name), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('t/edit', ''));
}
if (($game->sourcemodule == 'quiz') && ($game->quizid != 0)) {
$quiz = $DB->get_record_sql( "SELECT id,name FROM {$CFG->prefix}quiz WHERE id={$game->quizid}");
$cmd = get_coursemodule_from_instance('quiz', $game->quizid, $game->course);
$url = new moodle_url('/mod/quiz/view.php', ['id' => $cmd->id]);
$gamenode->add(get_string('viewquiz', 'game', $quiz->name), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('t/edit', ''));
}
if ($game->sourcemodule == 'question') {
$url = new moodle_url('/question/edit.php', ['courseid' => $game->course]);
$gamenode->add(get_string('viewquestions', 'game'), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('t/edit', ''));
}
}
if (has_capability('mod/game:viewreports', $context)) {
$url = new moodle_url('/mod/game/showanswers.php', ['q' => $PAGE->cm->instance]);
$reportnode = $gamenode->add(get_string('showanswers', 'game'), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('i/item', ''));
}
if (has_capability('mod/game:viewreports', $context)) {
$url = new moodle_url('/mod/game/showattempts.php', ['q' => $PAGE->cm->instance]);
$reportnode = $gamenode->add(get_string('showattempts', 'game'), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('f/explore', ''));
}
if (has_capability('mod/game:viewreports', $context)) {
$game = $DB->get_record('game', ["id" => $PAGE->cm->instance]);
$courseid = $game->course;
switch( $game->gamekind) {
case 'bookquiz':
$url = new moodle_url('/mod/game/bookquiz/questions.php', ['q' => $PAGE->cm->instance]);
$exportnode = $gamenode->add( get_string('bookquiz_questions', 'game'), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('i/item', ''));
break;
case 'hangman':
$url = new moodle_url('/mod/game/export.php', [ 'id' => $PAGE->cm->id,
'courseid' => $courseid, 'target' => 'html']);
$gamenode->add( get_string('export_to_html', 'game'), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('i/item', ''));
$url = new moodle_url('/mod/game/export.php', [ 'id' => $PAGE->cm->id,
'courseid' => $courseid, 'target' => 'javame']);
$gamenode->add( get_string('export_to_javame', 'game'), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('i/item', ''));
break;
case 'snakes':
case 'cross':
case 'millionaire':
$url = new moodle_url('/mod/game/export.php', [ 'q' => $game->id,
'courseid' => $courseid, 'target' => 'html']);
$gamenode->add(get_string('export_to_html', 'game'), $url, navigation_node::TYPE_SETTING,
null, null, new pix_icon('i/item', ''));
break;
}
}
$gamenode->make_active();
}
/* Returns an array of game type objects to construct menu list when adding new game */
require($CFG->dirroot.'/version.php');
if ($branch >= '31' && $branch < '401') {
define('USE_GET_SHORTCUTS', '1');
}
if ($branch >= '401') {
define('GAME_MOODLE_401', 1);
}
if (!defined('USE_GET_SHORTCUTS')) {
/**
* Shows kind of games
*/
function game_get_types() {
global $DB;
$config = get_config('game');
$types = [];
$type = new stdClass;
$type->modclass = MOD_CLASS_ACTIVITY;
$type->type = "game_group_start";
$type->typestr = '--'.get_string( 'modulenameplural', 'game');
$types[] = $type;
$hide = ( isset( $config->hidehangman) ? ($config->hidehangman != 0) : false);
if ($hide == false) {
$type = new stdClass;
$type->modclass = MOD_CLASS_ACTIVITY;
$type->type = "game&type=hangman";
$type->typestr = get_string('game_hangman', 'game');
$types[] = $type;
}
if (isset( $config->hidecross)) {
$hide = ($config->hidecross != 0);
} else {
$hide = false;
}
if ($hide == false) {
$type = new stdClass;
$type->modclass = MOD_CLASS_ACTIVITY;
$type->type = "game&type=cross";
$type->typestr = get_string('game_cross', 'game');
$types[] = $type;
}
if (isset( $config->hidecryptex)) {
$hide = ($config->hidecryptex != 0);
} else {
$hide = false;
}