-
Notifications
You must be signed in to change notification settings - Fork 0
/
participants.php
665 lines (599 loc) · 30.5 KB
/
participants.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
<?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/>.
/**
* Defines the interface for participant lists
*
* @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
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once('locallib.php');
require_once(__DIR__ . '/pdflib.php');
require_once(__DIR__ . '/evallib.php');
require_once(__DIR__ . '/participants/participants_listform.php');
require_once(__DIR__ . '/participants/participants_uploadform.php');
require_once(__DIR__ . '/participants/participants_report.php');
require_once(__DIR__ . '/participants/participants_scanner.php');
define("MAX_USERS_PER_PAGE", 5000);
$id = optional_param('id', 0, PARAM_INT); // Course Module ID.
$q = optional_param('q', 0, PARAM_INT); // Or offlinequiz ID.
$forcenew = optional_param('forcenew', 0, PARAM_INT);
$mode = optional_param('mode', 'editparticipants', PARAM_ALPHA);
$action = optional_param('action', '', PARAM_ALPHA);
$download = optional_param('download', false, PARAM_ALPHA);
list($offlinequiz, $course, $cm) = get_course_objects($id, $q);
require_login($course->id, false, $cm);
if (!offlinequiz_partlist_created($offlinequiz) and $mode != 'editlists') {
$mode = 'editparticipants';
}
$context = context_module::instance($cm->id);
$coursecontext = context_course::instance($cm->course);
$systemcontext = context_system::instance();
// We redirect students to info.
if (!has_capability('mod/offlinequiz:createofflinequiz', $context)) {
redirect('view.php?q='.$offlinequiz->id);
}
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
$thispageurl = new moodle_url('/mod/offlinequiz/participants.php',
array('id' => $id, 'q' => $q, 'mode' => $mode, 'forcenew' => $forcenew));
$PAGE->set_url($thispageurl);
$PAGE->set_pagelayout('admin');
$node = $PAGE->settingsnav->find('mod_offlinequiz_participants', navigation_node::TYPE_SETTING);
$PAGE->force_settings_menu(true);
if ($node) {
$node->make_active();
}
$pagetitle = get_string('editparticipants', 'offlinequiz');
$PAGE->set_title($pagetitle);
$PAGE->set_heading($course->fullname);
$PAGE->requires->yui_module('moodle-mod_offlinequiz-toolboxes',
'M.mod_offlinequiz.init_resource_toolbox',
array(array(
'courseid' => $course->id,
'offlinequizid' => $offlinequiz->id
))
);
offlinequiz_load_useridentification();
$offlinequizconfig = get_config('offlinequiz');
function find_pdf_file($contextid, $listfilename) {
$fs = get_file_storage();
if ($pdffile = $fs->get_file($contextid, 'mod_offlinequiz', 'participants', 0, '/', $listfilename)) {
return $pdffile;
} else {
return $fs->get_file($contextid, 'mod_offlinequiz', 'pdfs', 0, '/', $listfilename);
}
}
switch($mode) {
case 'editlists':
// Only print headers and tabs if not asked to download data.
if (!$download && $action != 'savelist') {
echo $OUTPUT->header();
$currenttab = 'participants';
// Print the tabs.
offlinequiz_print_tabs($offlinequiz, 'tabeditparticipants', $cm);
}
switch ($action) {
case 'edit':
// Print the heading.
echo $OUTPUT->heading_with_help(get_string('editlists', 'offlinequiz'), 'participants', 'offlinequiz');
$list = $DB->get_record('offlinequiz_p_lists', array('id' => required_param('listid', PARAM_INT)));
$list->listid = $list->id;
$myform = new offlinequiz_participantslistform($offlinequiz->id, get_string('editlist', 'offlinequiz'));
$myform->set_data($list);
break;
case 'delete':
// Print the heading.
echo $OUTPUT->heading_with_help(get_string('editlists', 'offlinequiz'), 'participants', 'offlinequiz');
if (confirm_sesskey()) {
if ($list = $DB->get_record('offlinequiz_p_lists', array('id' => required_param('listid', PARAM_INT)))) {
$DB->delete_records('offlinequiz_participants', array('listid' => $list->id));
$DB->delete_records('offlinequiz_p_lists', array('id' => $list->id));
}
}
$myform = new offlinequiz_participantslistform($offlinequiz->id, get_string('addlist', 'offlinequiz'));
break;
case 'savelist':
$myform = new offlinequiz_participantslistform($offlinequiz->id, get_string('addlist', 'offlinequiz'));
if ($data = $myform->get_data()) {
if (empty($data->listid)) {
$record = new stdClass();
$record->offlinequizid = $offlinequiz->id;
$record->name = $data->name;
$sql = "SELECT max(listnumber) as maxlist
FROM {offlinequiz_p_lists}
WHERE offlinequizid = :offlinequizid";
$last = $DB->get_record_sql($sql, array('offlinequizid' => $offlinequiz->id));
$record->listnumber = $last->maxlist + 1;
$DB->insert_record('offlinequiz_p_lists', $record);
} else {
$DB->set_field('offlinequiz_p_lists', 'name', $data->name, array('id' => $data->listid));
}
}
redirect('participants.php?mode=editlists&q='.$offlinequiz->id, get_string('changessaved'));
break;
default:
// Print the heading.
echo $OUTPUT->heading_with_help(get_string('editlists', 'offlinequiz'), 'participants', 'offlinequiz');
$myform = new offlinequiz_participantslistform($offlinequiz->id, get_string('addlist', 'offlinequiz'));
break;
}
echo $OUTPUT->box_start('boxaligncenter generalbox boxwidthnormal');
$lists = $DB->get_records('offlinequiz_p_lists', array('offlinequizid' => $offlinequiz->id));
echo '<ul>';
foreach ($lists as $list) {
$sql = "SELECT COUNT(*)
FROM {offlinequiz_participants} p, {offlinequiz_p_lists} pl
WHERE p.listid = pl.id
AND pl.offlinequizid = :offlinequizid
AND p.listid = :listid";
$params = array('offlinequizid' => $offlinequiz->id, 'listid' => $list->id);
$numusers = $DB->count_records_sql($sql, $params);
echo '<li>';
$listname = '<b>' . $list->name . '(' . $numusers . ')</b>';
$listurl = new moodle_url($CFG->wwwroot . '/mod/offlinequiz/participants.php',
array('mode' => 'editparticipants', 'q' => $offlinequiz->id , 'listid' => $list->id));
echo html_writer::link($listurl, $listname);
$streditlist = get_string('editthislist', 'offlinequiz');
$imagehtml = $OUTPUT->pix_icon('i/edit', $streditlist);
$editurl = new moodle_url($CFG->wwwroot . '/mod/offlinequiz/participants.php',
array('mode' => 'editlists', 'action' => 'edit' , 'q' => $offlinequiz->id , 'listid' => $list->id));
echo html_writer::link($editurl, $imagehtml, array('class' => 'editlistlink'));
$strdeletelist = get_string('deletethislist', 'offlinequiz');
$imagehtml = $OUTPUT->pix_icon('t/delete', $strdeletelist);
$deleteurl = new moodle_url($CFG->wwwroot . '/mod/offlinequiz/participants.php',
array('mode' => 'editlists',
'action' => 'delete',
'q' => $offlinequiz->id,
'listid' => $list->id,
'sesskey' => sesskey()));
echo html_writer::link($deleteurl, $imagehtml,
array('onClick' => 'return confirm(\'' . addslashes(get_string('deletelistcheck', 'offlinequiz'))
. '\');', 'class' => 'deletelistlink'));
echo '</li>';
}
echo '</ul>';
echo $OUTPUT->box_end();
echo $OUTPUT->box_start('boxaligncenter generalbox boxwidthnormal');
$myform->display();
echo $OUTPUT->box_end();
break;
case 'editparticipants':
// Print the heading.
$strsearch = get_string('search');
$strshowall = get_string('showall');
$listid = optional_param('listid', 0, PARAM_INT);
$group = optional_param('group', 0, PARAM_INT);
$addselect = optional_param_array('addselect', array(), PARAM_INT);
$removeselect = optional_param_array('removeselect', array(), PARAM_RAW);
if (!$list = $DB->get_record('offlinequiz_p_lists', array('id' => $listid))) {
if (!$lists = $DB->get_records('offlinequiz_p_lists', array('offlinequizid' => $offlinequiz->id), ' listnumber ASC ')) {
$url = new moodle_url($CFG->wwwroot . '/mod/offlinequiz/participants.php',
array('q' => $offlinequiz->id, 'mode' => 'editlists'));
redirect($url);
} else {
$list = array_pop($lists);
}
}
// Only print headers and tabs if not asked to download data.
if (!$download) {
echo $OUTPUT->header();
// Print the tabs.
offlinequiz_print_tabs($offlinequiz, 'tabeditparticipants', $cm);
}
echo $OUTPUT->heading_with_help(get_string('participantsinlists', 'offlinequiz'), 'participants', 'offlinequiz');
if (!empty($addselect) && confirm_sesskey()) {
$record = new stdClass();
$record->listid = $list->id;
$record->checked = 0;
foreach ($addselect as $userid) {
$record->userid = $userid;
if (!$DB->get_record('offlinequiz_participants',
array('listid' => $record->listid, 'userid' => $record->userid))) {
$DB->insert_record('offlinequiz_participants', $record);
}
}
}
if (!empty($removeselect) && confirm_sesskey()) {
list($dsql, $dparams) = $DB->get_in_or_equal($removeselect, SQL_PARAMS_NAMED, 'remove');
$sql = "SELECT p.*
FROM {offlinequiz_participants} p,
{offlinequiz_p_lists} pl
WHERE p.listid = pl.id
AND p.userid $dsql
AND pl.offlinequizid = :offlinequizid
AND p.listid = :listid";
$dparams['offlinequizid'] = $offlinequiz->id;
$dparams['listid'] = $list->id;
$todelete = $DB->get_records_sql($sql, $dparams);
foreach ($todelete as $deleteuser) {
$DB->delete_records('offlinequiz_participants',
array('listid' => $list->id,
'userid' => $deleteuser->userid));
}
}
$groups = groups_get_all_groups($course->id);
$groupoptions = '';
if (!empty($groups)) {
foreach ($groups as $item) {
$groupoptions .= '<option value="' . $item->id . '"';
if ($group == $item->id) {
$groupoptions .= ' selected="selected"';
}
$groupoptions .= '>'.$item->name.'</option>';
}
}
$listoptions = '';
if ($lists = $DB->get_records('offlinequiz_p_lists', array('offlinequizid' => $offlinequiz->id), 'name ASC')) {
foreach ($lists as $item) {
$listoptions .= '<option value="' . $item->id . '"';
if ($list->id == $item->id) {
$listoptions .= ' selected="selected"';
}
$listoptions .= '>'.$item->name.'</option>';
}
}
// First get roleids for students from leagcy.
if (!$roles = get_roles_with_capability('mod/offlinequiz:attempt', CAP_ALLOW, $systemcontext)) {
print_error("No roles with capability 'mod/offlinequiz:attempt' defined in system context");
}
$roleids = array();
foreach ($roles as $role) {
$roleids[] = $role->id;
}
$rolelist = implode(',', $roleids);
list($csql, $cparams) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'ctx');
$membersoptions = '';
list($rsql, $rparams) = $DB->get_in_or_equal($roleids, SQL_PARAMS_NAMED, 'role');
$params = array_merge($cparams, $rparams);
$sql = "SELECT DISTINCT u.id, u." . $offlinequizconfig->ID_field . ", u.firstname, u.lastname,
u.alternatename, u.middlename, u.firstnamephonetic, u.lastnamephonetic
FROM {user} u, {offlinequiz_participants} p, {role_assignments} ra, {offlinequiz_p_lists} pl
WHERE ra.userid = u.id
AND p.listid = :listid
AND p.listid = pl.id
AND pl.offlinequizid = :offlinequizid
AND p.userid = u.id
AND ra.roleid $rsql AND ra.contextid $csql
ORDER BY u.lastname, u.firstname";
$params['offlinequizid'] = $offlinequiz->id;
$params['listid'] = $list->id;
$memberids = array();
if ($members = $DB->get_records_sql($sql, $params)) {
$memberscount = count($members);
foreach ($members as $member) {
$membersoptions .= '<option value="' . $member->id.'">' . fullname($member) .
' (' . $member->{$offlinequizconfig->ID_field} . ')</option>';
$memberids[] = $member->id;
}
} else {
$memberscount = 0;
}
$potentialmembersoptions = '';
$memberlist = implode(',', $memberids);
if (!empty($group)) {
$groupmembers = groups_get_members($group);
}
$potentialmemberscount = 0;
$params = array_merge($rparams, $cparams);
if ($potentialmembers = get_enrolled_users($coursecontext, 'mod/offlinequiz:attempt')) {
foreach ($potentialmembers as $member) {
if (empty($members[$member->id]) and (empty($group) or !empty($groupmembers[$member->id]))) {
$potentialmembersoptions .= '<option value="' . $member->id . '">' . fullname($member) .
' (' . $member->{$offlinequizconfig->ID_field} . ')</option>';
$potentialmemberscount++;
}
}
}
$strpreview = get_string('participantslist', 'offlinequiz');
include('participants/members.html');
break;
case 'attendances':
// Only print headers and tabs if not asked to download data.
if (!$download) {
echo $OUTPUT->header();
// Print the tabs.
offlinequiz_print_tabs($offlinequiz, 'tabattendancesoverview', $cm);
echo $OUTPUT->heading_with_help(get_string('attendances', 'offlinequiz'), 'participants', 'offlinequiz');
if (!$lists = $DB->get_records('offlinequiz_p_lists', array('offlinequizid' => $offlinequiz->id), 'name ASC')) {
error('No list created for offlinequiz');
}
$options = array('0' => get_string('alllists', 'offlinequiz'));
foreach ($lists as $option) {
$options[$option->id] = $option->name;
}
$listid = optional_param('listid', 0, PARAM_INT);
echo '<div align="center">' . get_string('participantslist', 'offlinequiz') . ': ';
$url = new moodle_url($CFG->wwwroot . '/mod/offlinequiz/participants.php',
array('q' => $offlinequiz->id, 'mode' => 'attendances'));
echo $OUTPUT->single_select($url, 'listid', $options, $listid);
echo '<br /> <br /></div>';
}
if ($action == 'uncheck' and $participantids = optional_param_array('participantid', array(), PARAM_INT)) {
foreach ($participantids as $participantid) {
if ($participantid) {
$DB->set_field('offlinequiz_participants', 'checked', 0, array('id' => $participantid));
}
// Log this event.
$userid = $DB->get_field('offlinequiz_participants', 'userid', array('id' => $participantid));
$params = array (
'objectid' => $userid,
'context' => context_module::instance( $cm->id ),
'other' => array (
'mode' => 'attendances',
'offlinequizid' => $offlinequiz->id,
'type' => 'absent from'
)
);
$event = \mod_offlinequiz\event\participant_manually_marked::create($params);
$event->trigger();
}
}
if ($action == 'check' and $participantids = optional_param_array('participantid', array(), PARAM_INT)) {
foreach ($participantids as $participantid) {
if ($participantid) {
$DB->set_field('offlinequiz_participants', 'checked', 1, array('id' => $participantid));
}
// Log this event.
$userid = $DB->get_field('offlinequiz_participants', 'userid', array('id' => $participantid));
$params = array (
'objectid' => $userid,
'context' => context_module::instance( $cm->id ),
'other' => array (
'mode' => 'attendances',
'offlinequizid' => $offlinequiz->id,
'type' => 'present at'
)
);
$event = \mod_offlinequiz\event\participant_manually_marked::create($params);
$event->trigger();
}
}
// We redirect if no list has been created.
if (!offlinequiz_partlist_created($offlinequiz)) {
redirect('participants.php?q='.$offlinequiz->id, get_string('createlistfirst', 'offlinequiz'));
} else {
if ($download) {
offlinequiz_download_partlist($offlinequiz, $download, $coursecontext, $systemcontext);
} else {
offlinequiz_print_partlist($offlinequiz, $coursecontext, $systemcontext);
}
}
break;
case 'createpdfs':
// We redirect if no list has been created.
if (!offlinequiz_partlist_created($offlinequiz)) {
redirect('participants.php?q='.$offlinequiz->id, get_string('createlistfirst', 'offlinequiz'));
}
// Only print headers and tabs if not asked to download data.
if (!$download) {
echo $OUTPUT->header();
// Print the tabs.
offlinequiz_print_tabs($offlinequiz,'tabdownloadparticipantsforms', $cm);
echo $OUTPUT->heading_with_help(get_string('createpdfsparticipants', 'offlinequiz'), 'participants', 'offlinequiz');
}
// Show update button.
?>
<div class="singlebutton" align="center">
<form action="<?php echo "$CFG->wwwroot/mod/offlinequiz/participants.php" ?>" method="post">
<div>
<input type="hidden" name="q" value="<?php echo $offlinequiz->id ?>" />
<input type="hidden" name="forcenew" value="1" />
<input type="hidden" name="mode" value="createpdfs" />
<button type="submit"
onClick='return confirm("<?php echo get_string('reallydeleteupdatepdf', 'offlinequiz') ?>")'
class="btn btn-secondary">
<?php echo get_string('deleteupdatepdf', 'offlinequiz') ?>
</button>
</div>
</form>
<br> <br>
</div>
<?php
echo $OUTPUT->box_start('boxaligncenter generalbox boxwidthnormal');
$sql = "SELECT id, name, listnumber, filename
FROM {offlinequiz_p_lists}
WHERE offlinequizid = :offlinequizid
ORDER BY name ASC";
$lists = $DB->get_records_sql($sql, array('offlinequizid' => $offlinequiz->id));
foreach ($lists as $list) {
$fs = get_file_storage();
// Delete existing pdf if forcenew.
if ($forcenew && property_exists($list, 'filename') && $list->filename
&& $file = find_pdf_file($context->id, $list->filename)) {
$file->delete();
$list->filename = null;
}
$pdffile = null;
// Create PDF file if necessary.
if (!property_exists($list, 'filename') || !$list->filename ||
!$pdffile = find_pdf_file($context->id, $list->filename)) {
$pdffile = offlinequiz_create_pdf_participants($offlinequiz, $course->id, $list, $context);
if (!empty($pdffile)) {
$list->filename = $pdffile->get_filename();
}
$DB->update_record('offlinequiz_p_lists', $list);
}
// Show downloadlink.
if ($pdffile) {
$url = "$CFG->wwwroot/pluginfile.php/" . $pdffile->get_contextid() . '/' . $pdffile->get_component() . '/' .
$pdffile->get_filearea() . '/' . $pdffile->get_itemid() . '/' . $pdffile->get_filename() .
'?forcedownload=1';
echo $OUTPUT->action_link($url, trim(format_text(get_string('downloadpartpdf', 'offlinequiz', $list->name))));
$list->filename = $pdffile->get_filename();
$DB->update_record('offlinequiz_p_lists', $list);
} else {
echo $OUTPUT->notification(format_text(get_string('createpartpdferror', 'offlinequiz', $list->name)));
}
echo '<br /> <br />';
}
echo $OUTPUT->box_end();
break;
case 'upload':
// We redirect if no list created.
if (!offlinequiz_partlist_created($offlinequiz)) {
redirect('participants.php?q='.$offlinequiz->id, get_string('createlistfirst', 'offlinequiz'));
}
$lists = $DB->get_records_sql("
SELECT *
FROM {offlinequiz_p_lists}
WHERE offlinequizid = :offlinequizid
ORDER BY name ASC",
array('offlinequizid' => $offlinequiz->id));
$fs = get_file_storage();
// We redirect if all pdf files are missing.
$redirect = true;
foreach ($lists as $list) {
if ($list->filename && $file = find_pdf_file($context->id, $list->filename)) {
$redirect = false;
}
}
if ($redirect) {
redirect('participants.php?mode=createpdfs&q=' . $offlinequiz->id, get_string('createpdffirst', 'offlinequiz'));
}
// Only print headers and tabs if not asked to download data.
if (!$download) {
echo $OUTPUT->header();
// Print the tabs.
offlinequiz_print_tabs($offlinequiz, 'tabparticipantsupload', $cm);
echo $OUTPUT->heading_with_help(get_string('uploadpart', 'offlinequiz'), 'partimportnew', 'offlinequiz');
}
$report = new participants_report();
$importform = new offlinequiz_participants_upload_form($thispageurl);
$first = optional_param('first', 0, PARAM_INT); // Index of the last imported student.
$numimports = optional_param('numimports', 0, PARAM_INT);
$tempdir = optional_param('tempdir', 0, PARAM_PATH);
if ($newfile = optional_param('newfile', '', PARAM_INT)) {
if ($fromform = $importform->get_data()) {
@raise_memory_limit('128M');
$offlinequizconfig->papergray = $offlinequiz->papergray;
$fileisgood = false;
// Work out if this is an uploaded file
// or one from the filesarea.
$realfilename = $importform->get_new_filename('newfile');
// Create a unique temp dir.
$unique = str_replace('.', '', microtime(true) . rand(0, 100000));
$tempdir = "{$CFG->tempdir}/offlinequiz/import/$unique";
check_dir_exists($tempdir, true, true);
$importfile = $tempdir . '/' . $realfilename;
if (!$result = $importform->save_file('newfile', $importfile, true)) {
throw new moodle_exception('uploadproblem');
}
$files = array();
$mimetype = mimeinfo('type', $importfile);
if ($mimetype == 'application/zip') {
$fp = get_file_packer('application/zip');
$files = $fp->extract_to_pathname($importfile, $tempdir);
if ($files) {
unlink($importfile);
$files = get_directory_list($tempdir);
} else {
echo $OUTPUT->notification(get_string('couldnotunzip', 'offlinequiz_rimport', $realfilename),
'notifyproblem');
}
} else if (preg_match('/^image/' , $mimetype)) {
$files[] = $realfilename;
}
}
if (empty($files)) {
$files = get_directory_list($tempdir);
}
$numpages = count($files);
$last = $first + OFFLINEQUIZ_IMPORT_NUMUSERS - 1;
if ($last > $numpages - 1) {
$last = $numpages - 1;
}
$a = new stdClass();
$a->from = $first + 1;
$a->to = $last + 1;
$a->total = $numpages;
echo $OUTPUT->box_start();
print_string('importfromto', 'offlinequiz', $a);
echo "<br />";
echo $OUTPUT->box_end();
echo $OUTPUT->box_start();
$offlinequizconfig->papergray = $offlinequiz->papergray;
for ($j = $first; $j <= $last; $j++) {
$file = $files[$j];
$filename = $tempdir . '/' . $file;
set_time_limit(120);
$scanner = new offlinequiz_participants_scanner($offlinequiz, $context->id, 0, 0);
if ($scannedpage = $scanner->load_image($filename)) {
if ($scannedpage->status == 'ok') {
list($scanner, $scannedpage) = offlinequiz_check_scanned_participants_page(
$offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext, true);
}
if ($scannedpage->status == 'ok') {
$scannedpage = offlinequiz_process_scanned_participants_page($offlinequiz, $scanner, $scannedpage,
$USER->id, $coursecontext);
}
if ($scannedpage->status == 'ok') {
$choicesdata = $DB->get_records('offlinequiz_p_choices', array('scannedppageid' => $scannedpage->id));
$scannedpage = $scannedpage = offlinequiz_submit_scanned_participants_page(
$offlinequiz, $scannedpage, $choicesdata);
if ($scannedpage->status == 'submitted') {
echo get_string('pagenumberimported', 'offlinequiz', $j)."<br /><br />";
}
}
} else {
if ($scanner->ontop) {
$scannedpage->status = 'error';
$scannedpage->error = 'upsidedown';
}
}
}
echo $OUTPUT->box_end();
if ($last == $numpages - 1 or $numpages == 0) {
if ($numimports) {
$OUTPUT->notification(get_string('numpages', 'offlinequiz', $numimports), 'notifysuccess');
} else {
$OUTPUT->notification(get_string('nopages', 'offlinequiz'));
}
remove_dir($tempdir);
echo $OUTPUT->continue_button("$CFG->wwwroot/mod/offlinequiz/participants.php?q=$offlinequiz->id&mode=upload");
$OUTPUT->footer();
die;
} else {
$first = $last + 1;
redirect("$CFG->wwwroot/mod/offlinequiz/participants.php?q=$offlinequiz->id&mode=upload&" .
"action=upload&tempdir=$tempdir&first=$first&numimports=$numimports&sesskey=".sesskey());
}
$importform->display();
} else if ($action == 'delete') {
// Some pages need to be deleted.
$pageids = optional_param_array('pageid', array(), PARAM_INT);
foreach ($pageids as $pageid) {
if ($pageid && $todelete = $DB->get_record('offlinequiz_scanned_p_pages', array('id' => $pageid))) {
$DB->delete_records('offlinequiz_scanned_p_pages', array('id' => $pageid));
$DB->delete_records('offlinequiz_p_choices', array('scannedppageid' => $pageid));
}
}
$report->error_report($offlinequiz, $course->id);
$importform->display();
} else {
$report->error_report($offlinequiz, $course->id);
$importform->display();
}
break;
}
// Finish the page.
if (!$download) {
echo $OUTPUT->footer();
}