-
Notifications
You must be signed in to change notification settings - Fork 58
/
correct.php
1221 lines (1051 loc) · 55.8 KB
/
correct.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/>.
/**
* The manual correction interface for offlinequizzes
*
* @package mod
* @subpackage offlinequiz
* @author Juergen Zimmer <zimmerj7@univie.ac.at>
* @copyright 2015 Academic Moodle Cooperation {@link http://www.academic-moodle-cooperation.org}
* @since Moodle 2.4
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
**/
require_once('../../config.php');
global $DB, $CFG;
require_once($CFG->dirroot . '/mod/offlinequiz/locallib.php');
require_once($CFG->dirroot . '/mod/offlinequiz/evallib.php');
require_once($CFG->dirroot . '/mod/offlinequiz/report/default.php');
require_once($CFG->dirroot . '/mod/offlinequiz/report/rimport/scanner.php');
$scannedpageid = optional_param('pageid', 0, PARAM_INT);
$overwrite = optional_param('overwrite', 0, PARAM_INT);
$action = optional_param('action', 'load', PARAM_TEXT);
$userchanged = optional_param('userchanged', 0, PARAM_INT);
if (!$scannedpage = $DB->get_record('offlinequiz_scanned_pages', array('id' => $scannedpageid))) {
throw new \moodle_exception('noscannedpage', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $scannedpageid);
}
if (!$offlinequiz = $DB->get_record('offlinequiz', array('id' => $scannedpage->offlinequizid))) {
throw new \moodle_exception('noofflinequiz', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $scannedpage->offlinequizid);
}
if (!$course = $DB->get_record('course', array('id' => $offlinequiz->course))) {
throw new \moodle_exception('nocourse', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id,
array('course' => $offlinequiz->course,
'offlinequiz' => $offlinequiz->id));
}
if (!$cm = get_coursemodule_from_instance("offlinequiz", $offlinequiz->id, $course->id)) {
throw new \moodle_exception('cmmissing', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $offlinequiz->id);
}
if (!$groups = $DB->get_records('offlinequiz_groups', array('offlinequizid' => $offlinequiz->id), 'groupnumber',
'*', 0, $offlinequiz->numgroups)) {
throw new \moodle_exception('nogroups', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $scannedpage->offlinequizid);
}
require_login($course->id, false, $cm);
$context = context_module::instance($cm->id);
$coursecontext = context_course::instance($course->id);
require_capability('mod/offlinequiz:viewreports', $context);
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
$url = new moodle_url('/mod/offlinequiz/correct.php', array('pageid' => $scannedpage->id));
$PAGE->set_url($url);
$PAGE->set_pagelayout('report');
offlinequiz_load_useridentification();
$offlinequizconfig = get_config('offlinequiz');
// Determine the maxanswers and maxquestions for the scannedpage.
$selectedgroups = $groups;
if ($overwrite && $scannedpage->resultid) {
$result = $DB->get_record('offlinequiz_results', array('id' => $scannedpage->resultid));
$resultgroup = $DB->get_record('offlinequiz_groups', array('id' => $result->offlinegroupid));
$selectedgroups = array($resultgroup);
}
list($maxquestions, $maxanswers, $formtype, $questionsperpage) = offlinequiz_get_question_numbers($offlinequiz, $selectedgroups);
// Get the corners either from the request parameters of from the offlinequiz_page_corners table.
$corners = array();
$nodbcorners = false;
if ($action == 'rotate') {
$corners[0] = new oq_point(853 - required_param('c-3-x', PARAM_INT), 1208 - required_param('c-3-y', PARAM_INT));
$corners[1] = new oq_point(853 - required_param('c-2-x', PARAM_INT), 1208 - required_param('c-2-y', PARAM_INT));
$corners[2] = new oq_point(853 - required_param('c-1-x', PARAM_INT), 1208 - required_param('c-1-y', PARAM_INT));
$corners[3] = new oq_point(853 - required_param('c-0-x', PARAM_INT), 1208 - required_param('c-0-y', PARAM_INT));
offlinequiz_save_page_corners($scannedpage, $corners);
} else if ($action == 'readjust') {
$corners[0] = new oq_point(required_param('c-0-x', PARAM_INT) + 7, required_param('c-0-y', PARAM_INT) + 7);
$corners[1] = new oq_point(required_param('c-1-x', PARAM_INT) + 7, required_param('c-1-y', PARAM_INT) + 7);
$corners[2] = new oq_point(required_param('c-2-x', PARAM_INT) + 7, required_param('c-2-y', PARAM_INT) + 7);
$corners[3] = new oq_point(required_param('c-3-x', PARAM_INT) + 7, required_param('c-3-y', PARAM_INT) + 7);
offlinequiz_save_page_corners($scannedpage, $corners);
} else if ($dbcorners = $DB->get_records('offlinequiz_page_corners', array('scannedpageid' => $scannedpage->id), 'position')) {
foreach ($dbcorners as $corner) {
$corners[] = new oq_point($corner->x, $corner->y);
}
} else {
// Define some default corners.
$corners[0] = new oq_point(55, 39);
$corners[1] = new oq_point(805, 49);
$corners[2] = new oq_point(44, 1160);
$corners[3] = new oq_point(805, 1160);
$nodbcorners = true;
}
// Initialize a page scanner.
$scanner = new offlinequiz_page_scanner($offlinequiz, $context->id, $maxquestions, $maxanswers);
// Load the stored image file.
if (property_exists($scannedpage, 'id')) {
// If we re-adjust, rotate, or changed the user we have to delete the stored hotspots.
if ($action == 'readjust' || $action == 'rotate' || $action == 'checkuser') {
$DB->delete_records('offlinequiz_hotspots', array('scannedpageid' => $scannedpage->id));
}
// Load the stored image and the hotspots from the DB if they have not been deleted.
$sheetloaded = $scanner->load_stored_image($scannedpage->filename, $corners, $scannedpage->id);
} else {
// Load the stored image and adjust the hotspots from scratch.
$sheetloaded = $scanner->load_stored_image($scannedpage->filename, $corners);
}
// Make a first check.
if (!$scanner->check_deleted()) {
$scannedpage->status = 'error';
$scannedpage->error = 'notadjusted';
}
// O=======================================.
// O Step 1. Get the data from the stored scanned page.
// O=======================================.
if ($action == 'load') {
$filename = $scannedpage->filename;
$userkey = $scannedpage->userkey;
$usernumber = substr($userkey, strlen($offlinequizconfig->ID_prefix), $offlinequizconfig->ID_digits);
$groupnumber = intval($scannedpage->groupnumber);
$pagenumber = intval($scannedpage->pagenumber);
// Remember initial data for cancel action.
$origfilename = $filename;
$origuserkey = $userkey;
$origgroupnumber = $groupnumber;
$origpagenumber = $pagenumber;
$origstatus = $scannedpage->status;
$origerror = $scannedpage->error;
$origtime = $scannedpage->time;
} else {
if ($submitfilename = optional_param('filename', '', PARAM_RAW)) {
$scannedpage->filename = $submitfilename;
$filename = $submitfilename;
}
$origfilename = required_param('origfilename', PARAM_FILE);
$origuserkey = required_param('origuserkey', PARAM_ALPHANUM);
$origgroupnumber = required_param('origgroupnumber', PARAM_INT);
$origpagenumber = required_param('origpagenumber', PARAM_INT);
$origstatus = required_param('origstatus', PARAM_ALPHA);
$origerror = required_param('origerror', PARAM_ALPHA);
$origtime = required_param('origtime', PARAM_INT);
}
// If we still don't have corners, get them from the scanner.
if ($nodbcorners) {
$corners = $scanner->get_corners();
offlinequiz_save_page_corners($scannedpage, $corners);
}
// O=======================================.
// O Step 2. The user might have submitted data.
// O=======================================.
// O=============================================.
// O Action cancel.
// O=============================================.
if ($action == 'cancel') {
$scannedpage->filename = $origfilename;
$scannedpage->userkey = $origuserkey;
$scannedpage->groupnumber = $origgroupnumber;
$scannedpage->pagenumber = $origpagenumber;
$scannedpage->status = $origstatus;
$scannedpage->error = $origerror;
$scannedpage->time = $origtime;
$DB->update_record('offlinequiz_scanned_pages', $scannedpage);
// Display a button to close the window and die.
echo '<html>';
echo '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
echo "<center><input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('closewindow', 'offlinequiz').
"\" name=\"submitbutton4\" onClick=\"self.close(); return false;\"></center>";
echo '</html>';
die;
// O=============================================.
// O Action checkuser.
// O=============================================.
} else if ($action == 'checkuser') {
if (!confirm_sesskey()) {
throw new \moodle_exception('invalidsesskey');
echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('cancel')."\" name=\"submitbutton4\"
onClick=\"self.close(); return false;\"><br />";
die;
}
// Maybe old errors have been fixed.
$scannedpage->status = 'ok';
$DB->set_field('offlinequiz_scanned_pages', 'status', 'ok', array('id' => $scannedpage->id));
$scannedpage->error = '';
$DB->set_field('offlinequiz_scanned_pages', 'error', '', array('id' => $scannedpage->id));
$groupnumber = required_param('groupnumber', PARAM_TEXT);
$groupnumber = intval($groupnumber);
$scanner->set_group($groupnumber);
$scannedpage->groupnumber = $groupnumber;
// O=======================================================.
// O Adjust the maxanswers of the scanner according to the offlinequiz group
// O=======================================================.
if ($newgroup = $DB->get_record('offlinequiz_groups', array('offlinequizid' => $offlinequiz->id,
'groupnumber' => $groupnumber))) {
$maxanswers = offlinequiz_get_maxanswers($offlinequiz, array($newgroup));
$scannedpage = $scanner->set_maxanswers($maxanswers, $scannedpage);
}
$usernumber = required_param('usernumber', PARAM_TEXT);
$xes = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// If we have only X's then we ignore the input.
$userkey = $offlinequizconfig->ID_prefix . $usernumber . $offlinequizconfig->ID_postfix;
if ($usernumber == substr($xes, 0, $offlinequizconfig->ID_digits)) {
$scannedpage->userkey = null;
$DB->set_field('offlinequiz_scanned_pages', 'userkey', null, array('id' => $scannedpage->id));
} else {
$scannedpage->userkey = $userkey;
}
// Now we check the scanned page with potentially updated information.
$oldresultid = $scannedpage->resultid;
$scannedpage = offlinequiz_check_for_changed_user($offlinequiz, $scanner, $scannedpage, $coursecontext,
$questionsperpage, $offlinequizconfig);
if ($scannedpage && property_exists($scannedpage, 'resultid') && $oldresultid != $scannedpage->resultid) {
// A new result has been linked to the scanned page.
// Already process the answers but don't submit them yet.
$scannedpage = offlinequiz_process_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id,
$questionsperpage, $coursecontext, false);
$userchanged = 1;
}
if (!$overwrite) {
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);
if ($scannedpage->status == 'error' && $scannedpage->error == 'resultexists') {
// Already process the answers but don't submit them.
$scannedpage = offlinequiz_process_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id,
$questionsperpage, $coursecontext, false);
// Compare the old and the new result wrt. the choices.
$scannedpage = offlinequiz_check_different_result($scannedpage);
}
}
$userkey = $scannedpage->userkey;
$usernumber = substr($userkey, strlen($offlinequizconfig->ID_prefix), $offlinequizconfig->ID_digits);
$groupnumber = intval($scannedpage->groupnumber);
$pagenumber = intval($scannedpage->pagenumber);
$scanner->set_page($scannedpage->pagenumber);
// O=============================================.
// O Action update.
// O=============================================.
} else if ($action == 'update') {
if (!confirm_sesskey()) {
throw new \moodle_exception('invalidsesskey');
echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('cancel')."\" name=\"submitbutton4\"
onClick=\"self.close(); return false;\"><br />";
die;
}
// Maybe old errors have been fixed.
$scannedpage->status = 'ok';
$scannedpage->error = '';
$scannedpage->warningfilename = '';
$groupnumber = required_param('groupnumber', PARAM_TEXT);
$scannedpage->groupnumber = $groupnumber;
$scanner->set_group($groupnumber);
$usernumber = required_param('usernumber', PARAM_TEXT);
// If we have only X's then we ignore the input.
if (!empty($usernumber)) {
$userkey = $offlinequizconfig->ID_prefix . $usernumber . $offlinequizconfig->ID_postfix;
$scannedpage->userkey = $userkey;
}
if ($overwrite && !$userchanged) {
// We want to overwrite an old result, so we have to create a new one.
// Don't delete the choices stored in the DB.
// Delete the old result and create a new one.
if ($scannedpage->resultid && $oldresult = $DB->get_record('offlinequiz_results', array('id' => $scannedpage->resultid))) {
$oldresultid = $scannedpage->resultid;
$scannedpage->resultid = 0;
$DB->set_field('offlinequiz_scanned_pages', 'resultid', 0, array('id' => $scannedpage->id));
}
// This should create a new result and set the resultid field of the scannedpage.
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);
if ($scannedpage->resultid) {
// Get all other pages of the old result and set their resultid number to the new one.
$sql = "SELECT *
FROM {offlinequiz_scanned_pages}
WHERE offlinequizid = :offlinequizid
AND resultid = :resultid
AND status = 'submitted'
AND id <> :currentpageid";
$params = array('offlinequizid' => $scannedpage->offlinequizid, 'resultid' => $oldresultid,
'currentpageid' => $scannedpage->id);
if ($oldpages = $DB->get_records_sql($sql, $params)) {
// Load the new result and the quba slots.
$newresult = $DB->get_record('offlinequiz_results', array('id' => $scannedpage->resultid));
$quba = question_engine::load_questions_usage_by_activity($newresult->usageid);
$pageslots = $quba->get_slots();
foreach ($oldpages as $page) {
$page->resultid = $scannedpage->resultid;
$DB->set_field('offlinequiz_scanned_pages', 'resultid', $scannedpage->resultid, array('id' => $page->id));
// Load the choices made before from the database. This might be empty.
$pagechoices = $DB->get_records('offlinequiz_choices', array('scannedpageid' => $page->id),
'slotnumber, choicenumber');
// Choicesdata contains the choices data from the DB indexed by slotnumber and choicenumber.
$pagechoicesdata = array();
if (!empty($pagechoices)) {
foreach ($pagechoices as $pagechoice) {
if (!isset($pagechoicesdata[$pagechoice->slotnumber]) ||
!is_array($pagechoicesdata[$pagechoice->slotnumber])) {
$pagechoicesdata[$pagechoice->slotnumber] = array();
}
$pagechoicesdata[$pagechoice->slotnumber][$pagechoice->choicenumber] = $pagechoice;
}
}
// Determine the slice of slots we are interested in.
// We start at the top of the page (e.g. 0, 96, etc).
$pagestartindex = min(($page->pagenumber - 1) * $questionsperpage, count($pageslots));
// We end on the bottom of the page or when the questions are gone (e.g., 95, 105).
$pageendindex = min($page->pagenumber * $questionsperpage, count($pageslots));
// Submit the choices of the other pages to the new result.
$page = offlinequiz_submit_scanned_page($offlinequiz, $page, $pagechoicesdata, $pagestartindex, $pageendindex);
}
}
// Finally, delete the old result.
offlinequiz_delete_result($oldresultid, $context);
} else {
// Otherwise keep the old resultid.
$scannedpage->resultid = $oldresultid;
$DB->update_record('offlinequiz_scanned_pages', $scannedpage);
}
// TODO we have to figure out what to do with the other pages in case of a multipage test.
}
if (!$overwrite) {
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);
if ($scannedpage->status == 'error' && $scannedpage->error == 'resultexists') {
// Already process the answers but don't submit them.
$scannedpage = offlinequiz_process_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id,
$questionsperpage, $coursecontext, false);
// Compare the old and the new result wrt. the choices.
$scannedpage = offlinequiz_check_different_result($scannedpage);
}
}
$userkey = $scannedpage->userkey;
$usernumber = substr($userkey, strlen($offlinequizconfig->ID_prefix), $offlinequizconfig->ID_digits);
$groupnumber = intval($scannedpage->groupnumber);
$pagenumber = intval($scannedpage->pagenumber);
$DB->update_record('offlinequiz_scanned_pages', $scannedpage);
// The updated item information (crosses), will be processed later.
$rawitemdata = required_param_array('item', PARAM_RAW);
// O=============================================.
// O Action rotate.
// O=============================================.
} else if ($action == 'rotate') {
if (!confirm_sesskey()) {
throw new \moodle_exception('invalidsesskey');
echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('cancel')."\" name=\"submitbutton4\"
onClick=\"self.close(); return false;\"><br />";
die;
}
if ($newfile = $scanner->rotate_180()) {
// Maybe old errors have been fixed.
$scannedpage->status = 'ok';
$scannedpage->error = '';
$scannedpage->userkey = null;
$scannedpage->pagenumber = null;
$scannedpage->groupnumber = null;
$scannedpage->filename = $newfile->get_filename();
$filename = $newfile->get_filename();
// Create a completely new scanner.
$scanner = new offlinequiz_page_scanner($offlinequiz, $context->id, $maxquestions, $maxanswers);
$sheetloaded = $scanner->load_stored_image($scannedpage->filename, array());
if ($sheetloaded && !$overwrite) {
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);
if ($scannedpage->status == 'error' && $scannedpage->error == 'resultexists') {
// Already process the answers but don't submit them.
$scannedpage = offlinequiz_process_scanned_page($offlinequiz, $scanner, $scannedpage, $job->importuserid,
$questionsperpage, $coursecontext, false);
// Compare the old and the new result wrt. the choices.
$scannedpage = offlinequiz_check_different_result($scannedpage);
}
}
$userkey = $scannedpage->userkey;
$usernumber = substr($userkey, strlen($offlinequizconfig->ID_prefix), $offlinequizconfig->ID_digits);
$groupnumber = intval($scannedpage->groupnumber);
$pagenumber = intval($scannedpage->pagenumber);
$DB->update_record('offlinequiz_scanned_pages', $scannedpage);
}
// O=============================================.
// O Action setpage.
// O=============================================.
} else if ($action == 'setpage') {
if (!confirm_sesskey()) {
throw new \moodle_exception('invalidsesskey');
echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('cancel')."\" name=\"submitbutton4\"
onClick=\"self.close(); return false;\"><br />";
die;
}
// If the page number was invalid and the user selected a pagenumber, take that and hope that page is OK now.
$scannedpage->pagenumber = optional_param('page', 0, PARAM_INT);
$pagenumber = $scannedpage->pagenumber;
$scannedpage->status = 'ok';
$scannedpage->error = '';
if (!$overwrite) {
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);
if ($scannedpage->status == 'error' && $scannedpage->error == 'resultexists') {
// Already process the answers but don't submit them.
$scannedpage = offlinequiz_process_scanned_page($offlinequiz, $scanner, $scannedpage, $job->importuserid,
$questionsperpage, $coursecontext, false);
// Compare the old and the new result wrt. the choices.
$scannedpage = offlinequiz_check_different_result($scannedpage);
}
}
$userkey = $scannedpage->userkey;
$usernumber = substr($userkey, strlen($offlinequizconfig->ID_prefix), $offlinequizconfig->ID_digits);
$groupnumber = intval($scannedpage->groupnumber);
$pagenumber = intval($scannedpage->pagenumber);
// O=============================================.
// O Action readjust.
// O=============================================.
} else if ($action == 'readjust') {
if (!confirm_sesskey()) {
throw new \moodle_exception('invalidsesskey');
echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('cancel')."\" name=\"submitbutton4\"
onClick=\"self.close(); return false;\"><br />";
die;
}
$scannedpage->status = 'ok';
$scannedpage->error = '';
// We reset all user information s.t. it is retrieved again from the scanner.
$scannedpage->userkey = null;
$scannedpage->pagenumber = null;
$scannedpage->groupnumber = null;
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);
$userkey = $scannedpage->userkey;
$usernumber = substr($userkey, strlen($offlinequizconfig->ID_prefix), $offlinequizconfig->ID_digits);
$groupnumber = intval($scannedpage->groupnumber);
$pagenumber = intval($scannedpage->pagenumber);
$DB->update_record('offlinequiz_scanned_pages', $scannedpage);
} else if ($action == 'enrol' && $offlinequizconfig->oneclickenrol) {
// O=============================================.
// O Action enrol.
// O=============================================.
if (!confirm_sesskey()) {
throw new \moodle_exception('invalidsesskey');
echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('cancel')."\" name=\"submitbutton4\"
onClick=\"self.close(); return false;\"><br />";
die;
}
require_once($CFG->libdir . '/enrollib.php');
require_once($CFG->dirroot.'/enrol/manual/locallib.php');
// Check that the user has the permission to manual enrol.
require_capability('enrol/manual:enrol', $coursecontext);
$userid = $DB->get_field('user', 'id', array($offlinequizconfig->ID_field => $scannedpage->userkey), MUST_EXIST);
// Get the manual enrolment plugin.
$enrol = enrol_get_plugin('manual');
if (empty($enrol)) {
throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual');
}
if (!is_enrolled($coursecontext, $userid)) {
// Now we need the correct instance of the manual enrolment plugin.
if (!$instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', IGNORE_MISSING)) {
if ($instanceid = $enrol->add_default_instance($course)) {
$instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
}
}
if ($instance != false) {
$enrol->enrol_user($instance, $userid, $offlinequizconfig->oneclickrole);
}
}
// Now we look for other pages with that user and reset their status.
$sql = "SELECT *
FROM {offlinequiz_scanned_pages}
WHERE offlinequizid = :offlinequizid
AND status = 'error'
AND error = 'usernotincourse'
AND userkey = :currentuserkey
AND id <> :currentpageid";
$params = array('offlinequizid' => $offlinequiz->id,
'currentuserkey' => $scannedpage->userkey,
'currentpageid' => $scannedpage->id);
$otherpages = $DB->get_records_sql($sql, $params);
foreach ($otherpages as $otherpage) {
$otherpage->status = 'ok';
$otherpage->error = '';
$tempscanner = new offlinequiz_page_scanner($offlinequiz, $context->id, $maxquestions, $maxanswers);
$tempcorners = array();
if ($dbcorners = $DB->get_records('offlinequiz_page_corners', array('scannedpageid' => $otherpage->id), 'position')) {
foreach ($dbcorners as $corner) {
$tempcorners[] = new oq_point($corner->x, $corner->y);
}
} else {
$tempcorners[0] = new oq_point(55, 39);
$tempcorners[1] = new oq_point(805, 49);
$tempcorners[2] = new oq_point(44, 1160);
$tempcorners[3] = new oq_point(805, 1160);
}
$tempscanner->load_stored_image($otherpage->filename, $tempcorners);
$otherpage = offlinequiz_check_scanned_page($offlinequiz, $tempscanner, $otherpage, $USER->id, $coursecontext);
if ($otherpage->status == 'ok') {
$otherpage = offlinequiz_process_scanned_page($offlinequiz, $tempscanner, $otherpage, $USER->id,
$questionsperpage, $coursecontext, true);
}
$DB->update_record('offlinequiz_scanned_pages', $otherpage);
}
// Now reset the status of the original page and check it again.
$scannedpage->status = 'ok';
$scannedpage->error = '';
// Now check the scanned page again. The user should be enrolled now.
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);
$userkey = $scannedpage->userkey;
$usernumber = substr($userkey, strlen($offlinequizconfig->ID_prefix), $offlinequizconfig->ID_digits);
$groupnumber = intval($scannedpage->groupnumber);
$pagenumber = intval($scannedpage->pagenumber);
}
// We count the old choices to decide whether to process the page again.
$oldchoices = $DB->count_records('offlinequiz_choices', array('scannedpageid' => $scannedpage->id));
// If we have an OK page and the action was checkuser, setpage, etc. we should process the page.
if (($scannedpage->status == 'ok' || $scannedpage->status == 'suspended') && ($action == 'readjust' ||
$action == 'checkuser' || $action == 'enrol' || $action == 'setpage' || $action == 'rotate' ||
($action == 'load' && !$oldchoices))) {
// Process the scanned page and write the answers in the offlinequiz_choices table.
$scannedpage = offlinequiz_process_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id,
$questionsperpage, $coursecontext);
}
// Load the choices made before from the database. This might be empty.
$choices = $DB->get_records('offlinequiz_choices', array('scannedpageid' => $scannedpage->id), 'slotnumber, choicenumber');
// Choicesdata contains the choices data from the DB indexed by slotnumber and choicenumber.
$choicesdata = array();
if (!empty($choices)) {
foreach ($choices as $choice) {
if (!isset($choicesdata[$choice->slotnumber]) || !is_array($choicesdata[$choice->slotnumber])) {
$choicesdata[$choice->slotnumber] = array();
}
$choicesdata[$choice->slotnumber][$choice->choicenumber] = $choice;
}
}
// Retrieve the offlinequiz group.
if (is_numeric($groupnumber) && $groupnumber > 0 && $groupnumber <= $offlinequiz->numgroups) {
if (!$group = $DB->get_record('offlinequiz_groups', array('offlinequizid' => $offlinequiz->id,
'groupnumber' => $groupnumber))) {
throw new \moodle_exception('nogroup', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $offlinequiz->id);
}
} else {
$group = null;
}
// Check whether the user exists in Moodle.
$user = null;
$useridcount = 0;
$userarray = $DB->get_records('user', array($offlinequizconfig->ID_field => $userkey));
// Multiple users with the same id are possible so we have to check if that's the case.
foreach ($userarray as $userelement) {
$sql = "SELECT ra.id
FROM {role_assignments} ra,
{role} r
WHERE r.id = ra.roleid
AND ra.userid = :userid
AND r.archetype = 'student'
AND ra.contextid = :contextid";
$userincourse = $DB->get_field_sql($sql, ['userid' => $userelement->id, 'contextid' => $coursecontext->id], IGNORE_MISSING);
if ($userincourse) {
$user = $userelement;
$useridcount++;
} else if (!$user) {
$user = $userelement;
}
}
// Retrieve the result from the database.
$result = null;
if ($group && $user && property_exists($scannedpage, 'resultid') &&
$result = $DB->get_record('offlinequiz_results', array('id' => $scannedpage->resultid))) {
$quba = question_engine::load_questions_usage_by_activity($result->usageid);
$slots = $quba->get_slots();
// Determine the slice of slots we are interested in.
// We start at the top of the page (e.g. 0, 96, etc).
$startindex = min(($pagenumber - 1) * $questionsperpage, count($slots));
// We end on the bottom of the page or when the questions are gone (e.g., 95, 105).
$endindex = min( $pagenumber * $questionsperpage, count($slots) );
if ($action == 'update') {
echo $OUTPUT->heading(get_string('resultimport', 'offlinequiz'));
$changed = array();
$questioncounter = 0;
$unknown = false;
// If we have the result slots, we update the choicesdata with the data sent by the user.
if (!empty($rawitemdata) && !empty($slots)) {
for ($slotindex = $startindex; $slotindex < $endindex; $slotindex++) {
$slot = $slots[$slotindex];
$slotquestion = $quba->get_question($slot);
$attempt = $quba->get_question_attempt($slot);
$order = $slotquestion->get_order($attempt); // Order of the answers.
foreach ($order as $key => $notused) {
if ($rawitemdata[$slot . '-' . $key] == -1) {
$unknown = true;
}
if ($choicesdata[$slot][$key]->value != $rawitemdata[$slot . '-' . $key]) {
$changed[] = array('question' => $questioncounter,
'answer' => $key);
}
if (is_array($choicesdata[$slot]) && is_object($choicesdata[$slot][$key])) {
// Remember the changed fields in case we want to display them to the user.
$choicesdata[$slot][$key]->value = $rawitemdata[$slot . '-' . $key];
$DB->set_field('offlinequiz_choices', 'value', $choicesdata[$slot][$key]->value,
array('id' => $choicesdata[$slot][$key]->id));
} else {
$choice = new stdClass();
$choice->scannedpageid = $scannedpage->id;
$choice->slotnumber = $slot;
$choice->choicenumber = $key;
$choice->value = $rawitemdata[$slot . '-' . $key];
$choice->id = $DB->insert_record('offlinequiz_choices', $choice);
if (!isset($choicesdata[$choice->slotnumber]) || !is_array($choicesdata[$choice->slotnumber])) {
$choicesdata[$choice->slotnumber] = array();
}
$choicesdata[$slot][$key] = $choice;
}
}
$questioncounter++;
}
}
if ($show = optional_param('show', false, PARAM_BOOL)) {
$scanner->create_warning_image(substr($origuserkey, strlen($offlinequizconfig->ID_prefix),
$offlinequizconfig->ID_digits),
substr($user->{$offlinequizconfig->ID_field},
strlen($offlinequizconfig->ID_prefix),
$offlinequizconfig->ID_digits),
$origgroupnumber,
$group->groupnumber,
$changed);
$filerecord = array(
'contextid' => $context->id,
'component' => 'mod_offlinequiz',
'filearea' => 'imagefiles',
'itemid' => 0,
'filepath' => '/',
'filename' => $scanner->filename . '_warning');
// Create a unique temp dir.
$unique = str_replace('.', '', microtime(true) . rand(0, 100000));
$dirname = "{$CFG->tempdir}/offlinequiz/import/$unique";
check_dir_exists($dirname, true, true);
$warningpathname = $dirname . '/' . $filerecord['filename'];
imagepng($scanner->image, $warningpathname);
$newfile = $scanner->save_image($filerecord, $warningpathname);
$scannedpage->warningfilename = $newfile->get_filename();
$DB->set_field('offlinequiz_scanned_pages', 'warningfilename', $newfile->get_filename(),
array('id' => $scannedpage->id));
unlink($warningpathname);
remove_dir($dirname);
}
if (!$unknown) {
$scannedpage = offlinequiz_submit_scanned_page($offlinequiz, $scannedpage, $choicesdata, $startindex, $endindex);
if ($scannedpage->status == 'submitted') {
$result = $DB->get_record('offlinequiz_results', array('id' => $scannedpage->resultid));
echo '<p>';
if (offlinequiz_check_result_completed($offlinequiz, $group, $result)) {
echo get_string('userimported', 'offlinequiz', fullname($user) . " (" .
$user->{$offlinequizconfig->ID_field}.")" );
} else {
echo get_string('userpageimported', 'offlinequiz', fullname($user) . " (" .
$user->{$offlinequizconfig->ID_field}.")");
}
echo '</p>';
echo '<html>';
echo '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
if ($overwrite) {
echo "<input type=\"button\" value=\"".get_string('closewindow') .
"\" onClick=\"window.opener.location.replace('" .
$CFG->wwwroot . '/mod/offlinequiz/review.php?q=' . $offlinequiz->id . '&resultid=' .
$scannedpage->resultid . "'); window.close(); return false;\">";
} else {
echo "<input type=\"button\" value=\"".get_string('closewindow') .
"\" onClick=\"window.opener.location.reload(1); self.close(); return false;\">";
}
echo '<html>';
return;
}
} else {
// We found some insecure markings, which is weird, because they should have been found earlier...
$scannedpage->status = 'error';
$scannedpage->error = 'insecuremarkings';
$scannedpage->time = time();
$DB->update_record('offlinequiz_scanned_pages', $scannedpage);
}
}
}
$itemdata = array();
// Itemdata contains the values of the HTML inputs indexed by slotnumber and choicenumber.
// Itemdata is always set, even if we don't have any choices data in the DB.
if (!empty($slots)) {
// We start at the top of the page (e.g. 0, 96, etc).
$startindex = min( ($pagenumber - 1) * $questionsperpage, count($slots));
// We end on the bottom of the page or when the questions are gone (e.g., 95, 105).
$endindex = min( $pagenumber * $questionsperpage, count($slots) );
for ($slotindex = $startindex; $slotindex < $endindex; $slotindex++) {
$slot = $slots[$slotindex];
if (!isset($itemdata[$slot]) || !is_array($itemdata[$slot])) {
$itemdata[$slot] = array();
}
$slotquestion = $quba->get_question($slot);
$attempt = $quba->get_question_attempt($slot);
$order = $slotquestion->get_order($attempt); // Order of the answers.
foreach ($order as $key => $notused) {
if (isset($choicesdata[$slot]) and is_object($choicesdata[$slot][$key])) {
// If we have choices in the DB, take them.
$itemdata[$slot][$key] = $choicesdata[$slot][$key]->value;
} else {
// Otherwise the choice is undetermined.
$itemdata[$slot][$key] = 0;
}
}
}
}
if ($group && $pagenumber > 0 and $pagenumber <= $group->numberofpages) {
$scanner->set_page($pagenumber);
}
// O=======================================================================.
// O OUTPUT THE PAGE HTML.
// O=======================================================================.
echo '<html>';
echo "<style>\n";
echo "body {margin:0px; font-family:Arial,Verdana,Helvetica,sans-serif;}\n";
echo ".imagebutton {width:250px; height:24px; text-align:left; margin-bottom:10px;}\n";
echo "</style>\n";
echo '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>';
echo html_writer::script('', $CFG->wwwroot.'/mod/offlinequiz/lib/jquery/jquery-1.4.3.min.js');
echo html_writer::script('', $CFG->wwwroot.'/mod/offlinequiz/lib/jquery/ui/jquery.ui.core.js');
echo html_writer::script('', $CFG->wwwroot.'/mod/offlinequiz/lib/jquery/ui/jquery.ui.widget.js');
echo html_writer::script('', $CFG->wwwroot.'/mod/offlinequiz/lib/jquery/ui/jquery.ui.mouse.js');
echo html_writer::script('', $CFG->wwwroot.'/mod/offlinequiz/lib/jquery/ui/jquery.ui.draggable.js');
$javascript = "<script language=\"JavaScript\">
function checkinput() {
// Get all item elements. We have jquery!
items = $('input[name^=\"item\"]');
for (i=0; i < items.length; i++) {
if (items[i].value == '-1') {
parts = items[i].name.split('[');
numbers = parts[1].split('-');
alert(\"" . get_string('insecuremarkingsforquestion', 'offlinequiz') . " \" + numbers[0]);
return false;
}
}
if (document.forms.cform.usernumber.value.indexOf('X') >= 0) {
alert(\"" . get_string('insecuremarkings', 'offlinequiz') . ": \"+document.forms.cform.usernumber.value+\" (userid)\");
return false;
}
if (document.forms.cform.groupnumber.value.indexOf('X') >= 0) {
alert(\"" . get_string('insecuremarkings', 'offlinequiz') . ": \"+document.forms.cform.groupnumber.value+\" (group)\");
return false;
}
}
function set_userid(image, x, y) {
for (i=0; i<=9; i++) {
key = 'u'+x+i;
document.images[key].src=\"$CFG->wwwroot/mod/offlinequiz/pix/spacer.gif\";
}
image.src=\"$CFG->wwwroot/mod/offlinequiz/pix/green.gif\";
document.forms.cform.usernumber.value = document.forms.cform.usernumber.value.substr(0,x) + y +
document.forms.cform.usernumber.value.substr(x+1);
}
function set_group(image, x) {
for (i=0; i<=5; i++) {
document.images['g'+i].src=\"$CFG->wwwroot/mod/offlinequiz/pix/spacer.gif\";
}
image.src=\"$CFG->wwwroot/mod/offlinequiz/pix/green.gif\"
document.forms.cform.groupnumber.value = x+1;
document.forms.cform.elements['action'].value='checkuser';
document.forms.cform.submit();
}
function set_item(image, x, y) {
key = 'item['+x+'-'+y+']';
if (document.forms.cform.elements[key].value == '1') {
image.src=\"$CFG->wwwroot/mod/offlinequiz/pix/spacer.gif\";
document.forms.cform.elements[key].value = 0;
} else {
image.src=\"$CFG->wwwroot/mod/offlinequiz/pix/green.gif\";
document.forms.cform.elements[key].value = 1;
}
}
function submitReadjust() {
changed = false;
for (i=0; i<=3; i++) {
corner = document.getElementById('c-'+i);
document.forms.cform.elements['c-'+i+'-x'].value = corner.style.left.replace('px','');
document.forms.cform.elements['c-'+i+'-y'].value = corner.style.top.replace('px','');
if (document.forms.cform.elements['c-'+i+'-x'].value != document.forms.cform.elements['c-old-'+i+'-x'].value) {
changed = true;
}
if (document.forms.cform.elements['c-'+i+'-y'].value != document.forms.cform.elements['c-old-'+i+'-y'].value) {
changed = true;
}
}
if (!changed) {
alert('" . get_string('movecorners', 'offlinequiz') . "');
} else {
document.forms.cform.elements['action'].value='readjust';
document.forms.cform.submit();
}
}
function submitCancel() {
document.forms.cform.elements['action'].value='cancel';
document.forms.cform.submit();
}
function submitPage() {
for (i=0; i<=3; i++) {
corner = document.getElementById('c-'+i);
document.forms.cform.elements['c-'+i+'-x'].value = corner.style.left.replace('px','');
document.forms.cform.elements['c-'+i+'-y'].value = corner.style.top.replace('px','');
}
document.forms.cform.elements['action'].value='setpage';
document.forms.cform.submit();
}
function submitCheckuser() {
for (i=0; i<=3; i++) {
// Reset possible readjustment.
document.forms.cform.elements['c-'+i+'-x'].value = document.forms.cform.elements['c-old-'+i+'-x'].value;
document.forms.cform.elements['c-'+i+'-y'].value = document.forms.cform.elements['c-old-'+i+'-y'].value;
}
document.forms.cform.elements['action'].value='checkuser';
document.forms.cform.submit();
}
function submitEnrol() {
document.forms.cform.elements['action'].value='enrol';
document.forms.cform.submit();
}
function submitRotated() {
document.forms.cform.elements['action'].value='rotate';
document.forms.cform.submit();
}
function toggleImage() {
img = $('#scannedimage').toggle();
}
</script>";
echo $javascript;
$fs = get_file_storage();
$imagefile = $fs->get_file($context->id, 'mod_offlinequiz', 'imagefiles', 0, '/', $scannedpage->filename);
// Print image of the form sheet.
$imageurl = moodle_url::make_pluginfile_url($context->id, 'mod_offlinequiz', 'imagefiles', 0, '/', $imagefile->get_filename());
echo '<img id="scannedimage" name="formimage" src="' . $imageurl . '"
border="1" width="' . OQ_IMAGE_WIDTH .
'" style="position:absolute; top:0px; left:0px; display: block;">';
// Print user name, number, and the page number.
echo "<div style=\"position:absolute; top: 20px; left: 130px\">\n";
if ($user && $user->firstname != '' and $user->lastname != '') {
echo "<strong style=\"color: green\">" . fullname($user) . " (" . $userkey . ")</strong>\n";
} else if (!empty($userkey)) {
echo "<strong style=\"color: red\">" . get_string('userdoesnotexist', 'offlinequiz', $userkey) . "</strong>\n";
}
if (!empty($pagenumber)) {
echo " " . get_string('page') . ": " . $pagenumber;
if ($group && $group->numberofpages) {
echo "/" . $group->numberofpages . "\n";
}
}
if ($scannedpage->status == 'error') {
echo " <strong style=\"color: red\">(" . get_string('error' .
$scannedpage->error, 'offlinequiz_rimport') . ")</strong>\n";
}
echo "</div>\n";
// Print action buttons and form.
echo "<form method=\"post\" action=\"correct.php?pageid=$scannedpage->id\" id=\"cform\">\n";
echo "<div style=\"position:absolute; top:10px; left:" . (OQ_IMAGE_WIDTH + 10) . "px; width:280px\">\n";
echo "<div style=\"margin:4px;margin-bottom:8px\"><u>";
print_string('actions');