-
Notifications
You must be signed in to change notification settings - Fork 58
/
lib.php
1601 lines (1386 loc) · 60.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_offlinequiz for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library of interface functions and constants for module offlinequiz
*
* All the core Moodle functions, neeeded to allow the module to work
* integrated in Moodle should be placed here.
* All the offlinequiz specific functions, needed to implement all the module
* logic, should go to locallib.php. This will help to save some memory when
* Moodle is performing actions across all modules.
*
* @package mod
* @subpackage offlinequiz
* @author Juergen Zimmer <[email protected]>
* @copyright 2015 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @since Moodle 2.2+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// If, for some reason, you need to use global variables instead of constants, do not forget to make them
// global as this file can be included inside a function scope. However, using the global variables
// at the module level is not recommended.
// CONSTANTS.
// The different review options are stored in the bits of $offlinequiz->review.
// These constants help to extract the options.
// Originally this method was copied from the Moodle 1.9 quiz module. We use:
// 111111100000000000.
define('OFFLINEQUIZ_REVIEW_ATTEMPT', 0x1000); // Show responses.
define('OFFLINEQUIZ_REVIEW_MARKS', 0x2000); // Show scores.
define('OFFLINEQUIZ_REVIEW_SPECIFICFEEDBACK', 0x4000); // Show feedback.
define('OFFLINEQUIZ_REVIEW_RIGHTANSWER', 0x8000); // Show correct answers.
define('OFFLINEQUIZ_REVIEW_GENERALFEEDBACK', 0x10000); // Show general feedback.
define('OFFLINEQUIZ_REVIEW_SHEET', 0x20000); // Show scanned sheet.
define('OFFLINEQUIZ_REVIEW_CORRECTNESS', 0x40000); // Show scanned sheet.
define('OFFLINEQUIZ_REVIEW_GRADEDSHEET', 0x800); // Show scanned sheet.
// Define constants for cron job status.
define('OQ_STATUS_PENDING', 1);
define('OQ_STATUS_OPERATING', 2);
define('OQ_STATUS_PROCESSED', 3);
define('OQ_STATUS_NEEDS_CORRECTION', 4);
define('OQ_STATUS_DOUBLE', 5);
// If start and end date for the offline quiz are more than this many seconds apart
// they will be represented by two separate events in the calendar.
define('OFFLINEQUIZ_MAX_EVENT_LENGTH', 5 * 24 * 60 * 60); // 5 days.
// FUNCTIONS.
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param object $offlinequiz An object from the form in mod_form.php
* @return int The id of the newly inserted offlinequiz record
*/
function offlinequiz_add_instance($offlinequiz) {
global $CFG, $DB;
// Process the options from the form.
$offlinequiz->timecreated = time();
$offlinequiz->questions = '';
$offlinequiz->grade = 100;
$result = offlinequiz_process_options($offlinequiz);
if ($result && is_string($result)) {
return $result;
}
if (!property_exists($offlinequiz, 'intro') || $offlinequiz->intro == null) {
$offlinequiz->intro = '';
}
if (!$course = $DB->get_record('course', array('id' => $offlinequiz->course))) {
throw new \moodle_exception('invalidcourseid', 'error');
}
$context = context_module::instance($offlinequiz->coursemodule);
// Process the HTML editor data in pdfintro.
if (is_array($offlinequiz->pdfintro) && array_key_exists('text', $offlinequiz->pdfintro)) {
if ($draftitemid = $offlinequiz->pdfintro['itemid']) {
$editoroptions = offlinequiz_get_editor_options();
$offlinequiz->pdfintro = file_save_draft_area_files($draftitemid, $context->id,
'mod_offlinequiz', 'pdfintro',
0, $editoroptions,
$offlinequiz->pdfintro['text']);
}
}
try {
if (!$offlinequiz->id = $DB->insert_record('offlinequiz', $offlinequiz)) {
throw new \moodle_exception('Could not create Offlinequiz object!');
return false;
}
} catch (Exception $e) {
throw new \moodle_exception("ERROR: " . $e->debuginfo);
}
// Do the processing required after an add or an update.
offlinequiz_after_add_or_update($offlinequiz);
return $offlinequiz->id;
}
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will update an existing instance with new data.
*
* @param object $offlinequiz An object from the form in mod_form.php
* @return boolean Success/Fail
*/
function offlinequiz_update_instance($offlinequiz) {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/offlinequiz/locallib.php');
$offlinequiz->timemodified = time();
$offlinequiz->id = $offlinequiz->instance;
// Process the options from the form.
$result = offlinequiz_process_options($offlinequiz);
if ($result && is_string($result)) {
return $result;
}
$context = context_module::instance($offlinequiz->coursemodule);
// Process the HTML editor data in pdfintro.
if (property_exists($offlinequiz, 'pdfintro') && is_array($offlinequiz->pdfintro)
&& array_key_exists('text', $offlinequiz->pdfintro)) {
if ($draftitemid = $offlinequiz->pdfintro['itemid']) {
$editoroptions = offlinequiz_get_editor_options();
$offlinequiz->pdfintro = file_save_draft_area_files($draftitemid, $context->id,
'mod_offlinequiz', 'pdfintro',
0, $editoroptions,
$offlinequiz->pdfintro['text']);
}
}
// Update the database.
if (! $DB->update_record('offlinequiz', $offlinequiz)) {
return false; // Some error occurred.
}
// Do the processing required after an add or an update.
offlinequiz_after_add_or_update($offlinequiz);
// We also need the docscreated and the numgroups field.
$offlinequiz = $DB->get_record('offlinequiz', array('id' => $offlinequiz->id));
// Delete the question usage templates if no documents have been created and no answer forms have been scanned.
if (!$offlinequiz->docscreated && !offlinequiz_has_scanned_pages($offlinequiz->id)) {
offlinequiz_delete_template_usages($offlinequiz);
}
return true;
}
/**
* Given an ID of an instance of this module,
* this function will permanently delete the instance
* and any data that depends on it.
*
* @param int $id Id of the module instance
* @return boolean Success/Failure
*/
function offlinequiz_delete_instance($id) {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/offlinequiz/locallib.php');
require_once($CFG->dirroot . '/calendar/lib.php');
if (! $offlinequiz = $DB->get_record('offlinequiz', array('id' => $id))) {
return false;
}
if (! $cm = get_coursemodule_from_instance("offlinequiz", $offlinequiz->id, $offlinequiz->course)) {
return false;
}
$context = context_module::instance($cm->id);
// Delete any dependent records here.
if ($results = $DB->get_records("offlinequiz_results", array('offlinequizid' => $offlinequiz->id))) {
foreach ($results as $result) {
offlinequiz_delete_result($result->id, $context);
}
}
if ($scannedpages = $DB->get_records('offlinequiz_scanned_pages', array('offlinequizid' => $offlinequiz->id))) {
foreach ($scannedpages as $page) {
offlinequiz_delete_scanned_page($page, $context);
}
}
if ($scannedppages = $DB->get_records('offlinequiz_scanned_p_pages', array('offlinequizid' => $offlinequiz->id))) {
foreach ($scannedppages as $page) {
offlinequiz_delete_scanned_p_page($page, $context);
}
}
$ctxid = $context->id;
$DB->delete_records('question_references',['usingcontextid' => $ctxid, 'component' => 'mod_offlinequiz',
'questionarea' => 'slot']);
if ($events = $DB->get_records('event', array('modulename' => 'offlinequiz', 'instance' => $offlinequiz->id))) {
foreach ($events as $event) {
$event = calendar_event::load($event);
$event->delete();
}
}
if ($plists = $DB->get_records('offlinequiz_p_lists', array('offlinequizid' => $offlinequiz->id))) {
foreach ($plists as $plist) {
$DB->delete_records('offlinequiz_participants', array('listid' => $plist->id));
$DB->delete_records('offlinequiz_p_lists', array('id' => $plist->id));
}
}
// Remove the grade item.
offlinequiz_grade_item_delete($offlinequiz);
// Delete template question usages of offlinequiz groups.
offlinequiz_delete_template_usages($offlinequiz);
// All the tables with no dependencies...
$tablestopurge = array(
'offlinequiz_groups' => 'offlinequizid',
'offlinequiz' => 'id',
'offlinequiz_group_questions' => 'offlinequizid'
);
foreach ($tablestopurge as $table => $keyfield) {
if (! $DB->delete_records($table, array($keyfield => $offlinequiz->id))) {
$result = false;
}
}
return true;
}
/**
* This gets an array with default options for the editor
*
* @return array the options
*/
function offlinequiz_get_editor_options($context = null) {
$options = array('maxfiles' => EDITOR_UNLIMITED_FILES,
'noclean' => true);
if ($context) {
$options['context'] = $context;
}
return $options;
}
/**
* Delete grade item for given offlinequiz
*
* @param object $offlinequiz object
* @return object offlinequiz
*/
function offlinequiz_grade_item_delete($offlinequiz) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
return grade_update('mod/offlinequiz', $offlinequiz->course, 'mod', 'offlinequiz', $offlinequiz->id, 0,
null, array('deleted' => 1));
}
/**
* Called via pluginfile.php -> question_pluginfile to serve files belonging to
* a question in a question_attempt when that attempt is an offlinequiz attempt.
*
* @package mod_offlinequiz
* @category files
* @param stdClass $course course settings object
* @param stdClass $context context object
* @param string $component the name of the component we are serving files for.
* @param string $filearea the name of the file area.
* @param int $qubaid the attempt usage id.
* @param int $slot the id of a question in this quiz attempt.
* @param array $args the remaining bits of the file path.
* @param bool $forcedownload whether the user must be forced to download the file.
* @param array $options additional options affecting the file serving
* @return bool false if file not found, does not return if found - justsend the file
*/
function offlinequiz_question_pluginfile($course, $context, $component,
$filearea, $qubaid, $slot, $args, $forcedownload, array $options=array()) {
global $CFG, $DB, $USER;
list($context, $course, $cm) = get_context_info_array($context->id);
require_login($course, false, $cm);
if (!has_capability('mod/offlinequiz:viewreports', $context)) {
// If the user is not a teacher then check whether a complete result exists.
if (!$result = $DB->get_record('offlinequiz_results', array('usageid' => $qubaid, 'status' => 'complete'))) {
send_file_not_found();
}
// If the user's ID is not the ID of the result we don't serve the file.
if ($result->userid != $USER->id) {
send_file_not_found();
}
}
$fs = get_file_storage();
$relativepath = implode('/', $args);
$fullpath = "/$context->id/$component/$filearea/$relativepath";
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
send_file_not_found();
}
send_stored_file($file, 0, 0, $forcedownload, $options);
}
/**
* Serve questiontext files in the question text when they are displayed in a report.
*
* @param context $previewcontext the quiz context
* @param int $questionid the question id.
* @param context $filecontext the file (question) context
* @param string $filecomponent the component the file belongs to.
* @param string $filearea the file area.
* @param array $args remaining file args.
* @param bool $forcedownload.
* @param array $options additional options affecting the file serving.
*/
function offlinequiz_question_preview_pluginfile($previewcontext, $questionid, $filecontext, $filecomponent, $filearea,
$args, $forcedownload, $options = array()) {
global $CFG;
require_once($CFG->dirroot . '/mod/offlinequiz/locallib.php');
require_once($CFG->dirroot . '/lib/questionlib.php');
list($context, $course, $cm) = get_context_info_array($previewcontext->id);
require_login($course, false, $cm);
// We assume that only trusted people can see this report. There is no real way to
// validate questionid, because of the complexity of random questions.
require_capability('mod/offlinequiz:viewreports', $context);
$fs = get_file_storage();
$relativepath = implode('/', $args);
$fullpath = "/{$filecontext->id}/{$filecomponent}/{$filearea}/{$relativepath}";
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
send_file_not_found();
}
send_stored_file($file, 0, 0, $forcedownload, $options);
}
/**
* Serve image files in the answer text when they are displayed in the preview
*
* @param context $context the context
* @param int $answerid the answer id
* @param array $args remaining file args
* @param bool $forcedownload
*/
function offlinequiz_answertext_preview_pluginfile($context, $answerid, $args, $forcedownload, array $options=array()) {
global $CFG;
require_once($CFG->dirroot . '/mod/offlinequiz/locallib.php');
require_once($CFG->dirroot . '/lib/questionlib.php');
list($context, $course, $cm) = get_context_info_array($context->id);
require_login($course, false, $cm);
// Assume only trusted people can see this report. There is no real way to
// validate questionid, becuase of the complexity of random quetsions.
require_capability('mod/offlinequiz:viewreports', $context);
offlinequiz_send_answertext_file($context, $answerid, $args, $forcedownload, $options);
}
/**
* Send a file in the text of an answer.
*
* @param int $questionid the question id
* @param array $args the remaining file arguments (file path).
* @param bool $forcedownload whether the user must be forced to download the file.
*/
function offlinequiz_send_answertext_file($context, $answerid, $args, $forcedownload) {
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/offlinequiz/locallib.php');
$fs = get_file_storage();
$fullpath = "/$context->id/question/answer/$answerid/" . implode('/', $args);
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
send_file_not_found();
}
send_stored_file($file, 0, 0, $forcedownload);
}
/**
* Serves the offlinequiz files.
*
* @param object $course
* @param object $cm
* @param object $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return bool false if file not found, does not return if found - justsend the file
*/
function offlinequiz_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
global $CFG, $DB, $USER;
require_once($CFG->dirroot . '/mod/offlinequiz/locallib.php');
require_once($CFG->libdir . '/questionlib.php');
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
require_login($course, false, $cm);
if (!$offlinequiz = $DB->get_record('offlinequiz', array('id' => $cm->instance))) {
return false;
}
// The file file areas served by this method.
$fileareas = array('pdfs', 'participants', 'imagefiles');
if (!in_array($filearea, $fileareas)) {
return false;
}
$fs = get_file_storage();
$relativepath = implode('/', $args);
$fullpath = '/' . $context->id . '/mod_offlinequiz/' . $filearea . '/' . $relativepath;
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// Teachers in this context are allowed to see all the files in the context.
if (has_capability('mod/offlinequiz:viewreports', $context)) {
if ($filearea == 'pdfs' || $filearea == 'participants') {
$filename = clean_filename($course->shortname . '_' . $offlinequiz->name . '_' . $file->get_filename());
$filename = str_replace(" ", "_", $filename);
send_stored_file($file, 86400, 0, $forcedownload, array('filename' => $filename));
} else {
send_stored_file($file, 86400, 0, $forcedownload);
}
} else {
// Get the corresponding scanned pages. There might be several in case an image file is used twice.
if (!$scannedpages = $DB->get_records('offlinequiz_scanned_pages',
array('offlinequizid' => $offlinequiz->id, 'warningfilename' => $file->get_filename()))) {
if (!$scannedpages = $DB->get_records('offlinequiz_scanned_pages', array('offlinequizid' => $offlinequiz->id,
'filename' => $file->get_filename()))) {
throw new \moodle_exception('scanned page not found');
return false;
}
}
// Actually, there should be only one scannedpage with that filename...
foreach ($scannedpages as $scannedpage) {
$sql = "SELECT *
FROM {offlinequiz_results}
WHERE id = :resultid
AND status = 'complete'";
if (!$result = $DB->get_record_sql($sql, array('resultid' => $scannedpage->resultid))) {
return false;
}
// Check whether the student is allowed to see scanned sheets.
$options = offlinequiz_get_review_options($offlinequiz, $result, $context);
if ($options->sheetfeedback == question_display_options::HIDDEN and
$options->gradedsheetfeedback == question_display_options::HIDDEN) {
return false;
}
// If we found a page of a complete result that belongs to the user, we can send the file.
if ($result->userid == $USER->id) {
send_stored_file($file, 86400, 0, $forcedownload);
return true;
}
}
}
}
/**
* Return a list of page types
*
* @param string $pagetype current page type
* @param stdClass $parentcontext Block's parent context
* @param stdClass $currentcontext Current context of block
*/
function offlinequiz_page_type_list($pagetype, $parentcontext, $currentcontext) {
$modulepagetype = array(
'mod-offlinequiz-*' => get_string('page-mod-offlinequiz-x', 'offlinequiz'),
'mod-offlinequiz-edit' => get_string('page-mod-offlinequiz-edit', 'offlinequiz'));
return $modulepagetype;
}
/**
* Return a textual summary of the number of attempts that have been made at a particular offlinequiz,
* returns '' if no attempts have been made yet, unless $returnzero is passed as true.
*
* @param object $offlinequiz the offlinequiz object. Only $offlinequiz->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 bool $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 offlinequiz_num_attempt_summary($offlinequiz, $cm, $returnzero = false, $currentgroup = 0) {
global $DB, $USER;
$sql = "SELECT COUNT(*)
FROM {offlinequiz_results}
WHERE offlinequizid = :offlinequizid
AND status = 'complete'";
$numattempts = $DB->count_records_sql($sql, array('offlinequizid' => $offlinequiz->id));
if ($numattempts || $returnzero) {
return get_string('attemptsnum', 'offlinequiz', $numattempts);
}
return '';
}
/**
* Returns the same as {@link offlinequiz_num_attempt_summary()} but wrapped in a link
* to the offlinequiz reports.
*
* @param object $offlinequiz the offlinequiz object. Only $offlinequiz->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 object $context the offlinequiz context.
* @param bool $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 HTML fragment for the link.
*/
function offlinequiz_attempt_summary_link_to_reports($offlinequiz, $cm, $context, $returnzero = false,
$currentgroup = 0) {
global $CFG;
$summary = offlinequiz_num_attempt_summary($offlinequiz, $cm, $returnzero, $currentgroup);
if (!$summary) {
return '';
}
$url = new moodle_url('/mod/offlinequiz/report.php', array(
'id' => $cm->id, 'mode' => 'overview'));
return html_writer::link($url, $summary);
}
/**
* Check for features supported by offlinequizzes.
*
* @param string $feature FEATURE_xx constant for requested feature
* @return bool True if offlinequiz supports feature
*/
function offlinequiz_supports($feature) {
switch($feature) {
case FEATURE_BACKUP_MOODLE2:
return true;
case FEATURE_COMPLETION_HAS_RULES:
return true;
case FEATURE_COMPLETION_TRACKS_VIEWS:
return true;
case FEATURE_GRADE_HAS_GRADE:
return true;
case FEATURE_GRADE_OUTCOMES:
return true;
case FEATURE_GROUPINGS:
return true;
case FEATURE_GROUPMEMBERSONLY:
return true;
case FEATURE_GROUPS:
return true;
case FEATURE_MOD_INTRO:
return true;
case FEATURE_MOD_PURPOSE:
return MOD_PURPOSE_ASSESSMENT;
case FEATURE_SHOW_DESCRIPTION:
return true;
case FEATURE_USES_QUESTIONS:
return true;
default:
return null;
}
}
/**
* Is this a graded offlinequiz? If this method returns true, you can assume that
* $offlinequiz->grade and $offlinequiz->sumgrades are non-zero (for example, if you want to
* divide by them).
*
* @param object $offlinequiz a row from the offlinequiz table.
* @return bool whether this is a graded offlinequiz.
*/
function offlinequiz_has_grades($offlinequiz) {
return $offlinequiz->grade >= 0.000005 && $offlinequiz->sumgrades >= 0.000005;
}
/**
* Pre-process the offlinequiz options form data, making any necessary adjustments.
* Called by add/update instance in this file, and the save code in admin/module.php.
*
* @param object $offlinequiz The variables set on the form.
*/
function offlinequiz_process_options(&$offlinequiz) {
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
$offlinequiz->timemodified = time();
// Offlinequiz name. (Make up a default if one was not given).
if (empty($offlinequiz->name)) {
if (empty($offlinequiz->intro)) {
$offlinequiz->name = get_string('modulename', 'offlinequiz');
} else {
$offlinequiz->name = shorten_text(strip_tags($offlinequiz->intro));
}
}
$offlinequiz->name = trim($offlinequiz->name);
// Settings that get combined to go into the optionflags column.
$offlinequiz->optionflags = 0;
if (!empty($offlinequiz->adaptive)) {
$offlinequiz->optionflags |= QUESTION_ADAPTIVE;
}
// Settings that get combined to go into the review column.
$review = 0;
if (isset($offlinequiz->attemptclosed)) {
$review += OFFLINEQUIZ_REVIEW_ATTEMPT;
unset($offlinequiz->attemptclosed);
}
if (isset($offlinequiz->marksclosed)) {
$review += OFFLINEQUIZ_REVIEW_MARKS;
unset($offlinequiz->marksclosed);
}
if (isset($offlinequiz->feedbackclosed)) {
$review += OFFLINEQUIZ_REVIEW_FEEDBACK;
unset($offlinequiz->feedbackclosed);
}
if (isset($offlinequiz->correctnessclosed)) {
$review += OFFLINEQUIZ_REVIEW_CORRECTNESS;
unset($offlinequiz->correctnessclosed);
}
if (isset($offlinequiz->rightanswerclosed)) {
$review += OFFLINEQUIZ_REVIEW_RIGHTANSWER;
unset($offlinequiz->rightanswerclosed);
}
if (isset($offlinequiz->generalfeedbackclosed)) {
$review += OFFLINEQUIZ_REVIEW_GENERALFEEDBACK;
unset($offlinequiz->generalfeedbackclosed);
}
if (isset($offlinequiz->specificfeedbackclosed)) {
$review += OFFLINEQUIZ_REVIEW_SPECIFICFEEDBACK;
unset($offlinequiz->specificfeedbackclosed);
}
if (isset($offlinequiz->sheetclosed)) {
$review += OFFLINEQUIZ_REVIEW_SHEET;
unset($offlinequiz->sheetclosed);
}
if (isset($offlinequiz->gradedsheetclosed)) {
$review += OFFLINEQUIZ_REVIEW_GRADEDSHEET;
unset($offlinequiz->gradedsheetclosed);
}
$offlinequiz->review = $review;
}
/**
* Return a small object with summary information about what a
* user has done with a given particular instance of this module
* Used for user activity reports.
* $return->time = the time they did it
* $return->info = a short text description
*
* @param unknown_type $course
* @param unknown_type $user
* @param unknown_type $mod
* @param unknown_type $offlinequiz
* @return stdClass|NULL
*/
function offlinequiz_user_outline($course, $user, $mod, $offlinequiz) {
global $DB;
$return = new stdClass;
$return->time = 0;
$return->info = '';
if ($grade = $DB->get_record('offlinequiz_results', array('userid' => $user->id, 'offlinequizid' => $offlinequiz->id))) {
if ((float) $grade->sumgrades) {
$return->info = get_string('grade', 'offlinequiz') . ': ' . round($grade->sumgrades, $offlinequiz->decimalpoints);
}
$return->time = $grade->timemodified;
return $return;
}
return null;
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param unknown_type $course
* @param unknown_type $user
* @param unknown_type $mod
* @param unknown_type $offlinequiz
* @return boolean
*/
function offlinequiz_user_complete($course, $user, $mod, $offlinequiz) {
global $DB;
if ($results = $DB->get_records('offlinequiz_results', array('userid' => $user->id, 'offlinequiz' => $offlinequiz->id))) {
if ($offlinequiz->grade && $offlinequiz->sumgrades &&
$grade = $DB->get_record('offlinequiz_results', array('userid' => $user->id, 'offlinequiz' => $offlinequiz->id))) {
echo get_string('grade', 'offlinequiz') . ': ' . round($grade->grade, $offlinequiz->decimalpoints) .
'/' . $offlinequiz->grade . '<br />';
}
foreach ($results as $result) {
echo get_string('result', 'offlinequiz') . ': ';
if ($result->timefinish == 0) {
print_string('unfinished');
} else {
echo round($result->sumgrades, $offlinequiz->decimalpoints) . '/' . $offlinequiz->sumgrades;
}
echo ' - ' . userdate($result->timemodified) . '<br />';
}
} else {
print_string('noresults', 'offlinequiz');
}
return true;
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in offlinequiz activities and print it out.
* Return true if there was output, or false is there was none.
*
* @param unknown_type $course
* @param unknown_type $viewfullnames
* @param unknown_type $timestart
* @return boolean
*/
function offlinequiz_print_recent_mod_activity($course, $viewfullnames, $timestart) {
return false; // True if anything was printed, otherwise false.
}
/**
* Function to be run periodically according to the moodle cron
* This function searches for things that need to be done, such
* as sending out mail, toggling flags etc ...
*
* Note: The evaluation of answer forms is done by a separate cron job using the script mod/offlinequiz/cron.php.
*
**/
function offlinequiz_cron() {
global $DB;
cron_execute_plugin_type('offlinequiz', 'offlinequiz reports');
// Remove all saved hotspot data that is older than 7 days.
$timenow = time();
// We have to make sure we do this atomic for each scanned page.
$sql = "SELECT DISTINCT(scannedpageid)
FROM {offlinequiz_hotspots}
WHERE time < :expiretime";
$params = array('expiretime' => (int) $timenow - 604800);
// First we get the different IDs.
$ids = $DB->get_fieldset_sql($sql, $params);
if (!empty($ids)) {
list($isql, $iparams) = $DB->get_in_or_equal($ids);
// Now we delete the records.
$DB->delete_records_select('offlinequiz_hotspots', 'scannedpageid ' . $isql, $iparams);
}
// Delete old temporary files not needed any longer.
$keepdays = get_config('offlinequiz', 'keepfilesfordays');
$keepseconds = $keepdays * 24 * 60 * 60;
$sql = "SELECT id
FROM {offlinequiz_queue}
WHERE timecreated < :expiretime";
$params = array('expiretime' => (int) $timenow - $keepseconds);
// First we get the IDs of cronjobs older than the configured number of days.
$jobids = $DB->get_fieldset_sql($sql, $params);
foreach ($jobids as $jobid) {
$dirname = null;
// Delete all temporary files and the database entries.
if ($files = $DB->get_records('offlinequiz_queue_data', array('queueid' => $jobid))) {
foreach ($files as $file) {
if (empty($dirname)) {
$pathparts = pathinfo($file->filename);
$dirname = $pathparts['dirname'];
}
$DB->delete_records('offlinequiz_queue_data', array('id' => $file->id));
}
// Remove the temporary directory.
echo "Removing dir " . $dirname . "\n";
remove_dir($dirname);
}
}
return true;
}
/**
* Must return an array of users who are participants for a given instance
* of offlinequiz. 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 $offlinequizid ID of an instance of this module
* @return boolean|array false if no participants, array of objects otherwise
*/
function offlinequiz_get_participants($offlinequizid) {
global $CFG, $DB;
// Get users from offlinequiz results.
$usattempts = $DB->get_records_sql("
SELECT DISTINCT u.id, u.id
FROM {user} u,
{offlinequiz_results} r
WHERE r.offlinequizid = '$offlinequizid'
AND (u.id = r.userid OR u.id = r.teacherid");
// Return us_attempts array (it contains an array of unique users).
return $usattempts;
}
/**
* This function returns if a scale is being used by one offlinequiz
* 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 $offlinequizid ID of an instance of this module
* @return mixed
*/
function offlinequiz_scale_used($offlinequizid, $scaleid) {
global $DB;
$return = false;
$rec = $DB->get_record('offlinequiz', array('id' => $offlinequizid, 'grade' => -$scaleid));
if (!empty($rec) && !empty($scaleid)) {
$return = true;
}
return $return;
}
/**
* Checks if scale is being used by any instance of offlinequiz.
* This function was added in 1.9
*
* This is used to find out if scale used anywhere
* @param $scaleid int
* @return boolean True if the scale is used by any offlinequiz
*/
function offlinequiz_scale_used_anywhere($scaleid) {
global $DB;
if ($scaleid and $DB->record_exists('offlinequiz', array('grade' => -$scaleid))) {
return true;
} else {
return false;
}
}
/**
* This function is called at the end of offlinequiz_add_instance
* and offlinequiz_update_instance, to do the common processing.
*
* @param object $offlinequiz the offlinequiz object.
*/
function offlinequiz_after_add_or_update($offlinequiz) {
global $DB;
// Create group entries if they don't exist.
if (property_exists($offlinequiz, 'numgroups')) {
for ($i = 1; $i <= $offlinequiz->numgroups; $i++) {
if (!$group = $DB->get_record('offlinequiz_groups', array('offlinequizid' => $offlinequiz->id, 'groupnumber' => $i))) {
$group = new stdClass();
$group->offlinequizid = $offlinequiz->id;
$group->groupnumber = $i;
$group->numberofpages = 1;
$DB->insert_record('offlinequiz_groups', $group);
}
}
}
offlinequiz_update_events($offlinequiz);
offlinequiz_grade_item_update($offlinequiz);
return;
}
/**
* This function updates the events associated to the offlinequiz.
* If $override is non-zero, then it updates only the events
* associated with the specified override.
*
* @uses OFFLINEQUIZ_MAX_EVENT_LENGTH
* @param object $offlinequiz the offlinequiz object.
* @param object optional $override limit to a specific override
*/
function offlinequiz_update_events($offlinequiz) {
global $DB, $CFG;
require_once($CFG->dirroot . '/calendar/lib.php');
// Load the old events relating to this offlinequiz.
$conds = array('modulename' => 'offlinequiz',
'instance' => $offlinequiz->id);
if (!empty($override)) {
// Only load events for this override.
$conds['groupid'] = isset($override->groupid) ? $override->groupid : 0;
$conds['userid'] = isset($override->userid) ? $override->userid : 0;
}
$oldevents = $DB->get_records('event', $conds);
$groupid = 0;
$userid = 0;
$timeopen = $offlinequiz->timeopen;
$timeclose = $offlinequiz->timeclose;
// Only add open/close events if they differ from the offlinequiz default.
if (!empty($offlinequiz->coursemodule)) {
$cmid = $offlinequiz->coursemodule;
} else {
$cmid = get_coursemodule_from_instance('offlinequiz', $offlinequiz->id, $offlinequiz->course)->id;
}
if (!empty($timeopen)) {
$event = new stdClass();
$event->name = $offlinequiz->name . ' (' . get_string('reportstarts', 'offlinequiz') . ')';
$event->description = format_module_intro('offlinequiz', $offlinequiz, $cmid);
// Events module won't show user events when the courseid is nonzero.
$event->courseid = ($userid) ? 0 : $offlinequiz->course;
$event->groupid = $groupid;
$event->userid = $userid;
$event->modulename = 'offlinequiz';
$event->instance = $offlinequiz->id;
$event->timestart = $timeopen;
$event->timeduration = 0;
$event->visible = instance_is_visible('offlinequiz', $offlinequiz);
calendar_event::create($event);
}
if (!empty($timeclose)) {
$event = new stdClass();
$event->name = $offlinequiz->name . ' (' . get_string('reportends', 'offlinequiz') . ')';
$event->description = format_module_intro('offlinequiz', $offlinequiz, $cmid);
// Events module won't show user events when the courseid is nonzero.
$event->courseid = ($userid) ? 0 : $offlinequiz->course;
$event->groupid = $groupid;
$event->userid = $userid;
$event->modulename = 'offlinequiz';
$event->instance = $offlinequiz->id;
$event->timestart = $timeclose;
$event->timeduration = 0;