-
Notifications
You must be signed in to change notification settings - Fork 28
/
rule.php
441 lines (400 loc) · 17.2 KB
/
rule.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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Implementaton for the quizaccess_proctoring plugin.
*
* @package quizaccess_proctoring
* @copyright 2020 Brain Station 23
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* quizaccess_proctoring.
*/
class quizaccess_proctoring extends mod_quiz\local\access_rule_base {
/**
* Check is preflight check is required.
*
* @param mixed $attemptid
*
* @return bool
*/
public function is_preflight_check_required($attemptid) {
$script = $this->get_topmost_script();
$base = basename($script);
return $base == 'view.php';
}
/**
* Get topmost script path.
*
* @return string
*
* @throws coding_exception
*/
public function get_topmost_script() {
$backtrace = debug_backtrace(
defined('DEBUG_BACKTRACE_IGNORE_ARGS')
? DEBUG_BACKTRACE_IGNORE_ARGS
: false);
$topframe = array_pop($backtrace);
return $topframe['file'];
}
/**
* Get_courseid_cmid_from_preflight_form.
*
* @param mixed $quizform
* @return array
*
* @throws coding_exception
*/
public function get_courseid_cmid_from_preflight_form(mod_quiz\form\preflight_check_form $quizform) {
$response = [];
$response['courseid'] = $this->quiz->course;
$response['quizid'] = $this->quiz->id;
$response['cmid'] = $this->quiz->cmid;
return $response;
}
/**
* Makes the modal content
*
* @param $quizform
* @param $faceidcheck
* @return string
*
* @throws coding_exception
*/
public function make_modal_content($quizform, $faceidcheck) {
global $USER, $OUTPUT;
$headercontent = get_string('openwebcam', 'quizaccess_proctoring');
$header = "$headercontent";
$camhtml = get_string('camhtml', 'quizaccess_proctoring');
$proctoringstatement = get_string('proctoringstatement', 'quizaccess_proctoring');
if ($faceidcheck == '1') {
$html = "<div class='container'>
<div class='row'>
<div class='col'>$header</div>
</div>
<div class='row'>
<div class='col'>$proctoringstatement</div>
</div>
<div class='row'>
<div class='col' style='display: none'>$camhtml</div>
</div>
</div>";
} else {
$html = "<div class='container'>
<div class='row'>
<div class='col'>$header</div>
</div>
<div class='row'>
<div class='col'>$proctoringstatement</div>
</div>
<div class='row'>
<div class='col'>$camhtml</div>
</div>
</div>";
}
return $html;
}
/**
* add_preflight_check_form_fields.
*
* @param mixed $quizform
* @param mixed $mform
* @param mixed $attemptid
*
* @return void
*
* @throws coding_exception
*/
public function add_preflight_check_form_fields(mod_quiz\form\preflight_check_form $quizform,
MoodleQuickForm $mform, $attemptid) {
global $PAGE, $DB, $USER, $CFG;
$actionbtns = "";
$coursedata = $this->get_courseid_cmid_from_preflight_form($quizform);
// Get Screenshot Delay and Image Width.
$imagedelaysql = "SELECT * FROM {config_plugins}
WHERE plugin = 'quizaccess_proctoring'
AND name = 'autoreconfigurecamshotdelay'";
$delaydata = $DB->get_record_sql($imagedelaysql);
$camshotdelay = (int)$delaydata->value * 1000;
if ($camshotdelay == 0) {
$camshotdelay = 30 * 1000;
}
$faceidquery = "SELECT * FROM {config_plugins}
WHERE plugin = 'quizaccess_proctoring'
AND name = 'fcheckstartchk'";
$faceidrow = $DB->get_record_sql($faceidquery);
$faceidcheck = $faceidrow->value;
$imagewidth = get_config('quizaccess_proctoring', 'autoreconfigureimagewidth');
$examurl = new moodle_url('/mod/quiz/startattempt.php');
$record = [];
$record['id'] = 0;
$record['courseid'] = (int)$coursedata['courseid'];
$record['cmid'] = (int)$coursedata['cmid'];
$record['attemptid'] = $attemptid;
$record['imagewidth'] = $imagewidth;
$record['screenshotinterval'] = $camshotdelay;
$record['examurl'] = $examurl->__toString();
$modelurl = $CFG->wwwroot . '/mod/quiz/accessrule/proctoring/thirdpartylibs/models';
$PAGE->requires->js("/mod/quiz/accessrule/proctoring/amd/build/face-api.min.js", true);
$PAGE->requires->js_call_amd('quizaccess_proctoring/startAttempt', 'setup', [$record, $modelurl]);
$mform->addElement('html', "<div class='quiz-check-form'>");
$profileimageurl = '';
if ($USER->picture) {
$profileimageurl = new moodle_url('/user/pix.php/' . $USER->id . '/f1.jpg');
}
$coursedata = $this->get_courseid_cmid_from_preflight_form($quizform);
$hiddenvalue = '<input type="hidden" id="courseidval" value="' . $coursedata['courseid'] . '"/>
<input type="hidden" id="cmidval" value="' . $coursedata['cmid'] . '"/>
<input type="hidden" id="profileimage" value="' . $profileimageurl . '"/>';
$modalcontent = $this->make_modal_content($quizform, $faceidcheck);
$facevalidationlabel = get_string('modal:facevalidation', 'quizaccess_proctoring');
$pending = get_string('modal:pending', 'quizaccess_proctoring');
$validateface = get_string('modal:validateface', 'quizaccess_proctoring');
if ($faceidcheck == '1') {
$actionbtns = "$facevalidationlabel <span id='face_validation_result'>$pending</span>"
. "<button id='fcvalidate' style='height:50px; margin: 5px;"
. " display: flex; justify-content: center;align-items: center;'>
<div class='loadingspinner' id='loading_spinner'></div>
$validateface
</button>";
}
$actionbtnhtml = "<div class='container'><div class='row'><div class='col'>$actionbtns</div></div></div>";
$mform->addElement('html', $modalcontent);
$mform->addElement('static', 'actionbtns', '', $actionbtnhtml);
if ($faceidcheck == '1') {
$mform->addElement('html', '<div id="form_activate" style="visibility: hidden">');
}
$mform->addElement('checkbox', 'proctoring', '', get_string('proctoringlabel', 'quizaccess_proctoring'));
if ($faceidcheck == '1') {
$mform->addElement('html', '</div>');
}
$mform->addElement('html', $hiddenvalue);
$mform->addElement('html', '</div>');
}
/**
* Validate the preflight check.
*
* @param mixed $data
* @param mixed $files
* @param mixed $errors
* @param mixed $attemptid
*
* @return mixed $errors
*
* @throws coding_exception
*/
public function validate_preflight_check($data, $files, $errors, $attemptid) {
if (empty($data['proctoring'])) {
$errors['proctoring'] = get_string('youmustagree', 'quizaccess_proctoring');
}
return $errors;
}
/**
* * Information, such as might be shown on the quiz view page, relating to this restriction.
* There is no obligation to return anything. If it is not appropriate to tell students
* about this rule, then just return ''.
*
* @param mixed $quizobj
* @param int $timenow
* @param bool $canignoretimelimits
*
* @return quiz_access_rule_base|quizaccess_proctoring|null
*/
public static function make(mod_quiz\quiz_settings $quizobj, $timenow, $canignoretimelimits) {
if (empty($quizobj->get_quiz()->proctoringrequired)) {
return null;
}
return new self($quizobj, $timenow);
}
/**
* Add any fields that this rule requires to the quiz settings form. This
* method is called from mod_quiz_mod_form::definition(), while the
* security section is being built.
*
* @param mod_quiz_mod_form $quizform the quiz settings form that is being built
* @param MoodleQuickForm $mform the wrapped MoodleQuickForm
*
* @throws coding_exception
*/
public static function add_settings_form_fields(mod_quiz_mod_form $quizform, MoodleQuickForm $mform) {
$mform->addElement('select', 'proctoringrequired',
get_string('proctoringrequired', 'quizaccess_proctoring'),
[
0 => get_string('notrequired', 'quizaccess_proctoring'),
1 => get_string('proctoringrequiredoption', 'quizaccess_proctoring'),
]);
$mform->addHelpButton('proctoringrequired', 'proctoringrequired', 'quizaccess_proctoring');
}
/**
* Save any submitted settings when the quiz settings form is submitted. This
* is called from quiz_after_add_or_update() in lib.php.
*
* @param object $quiz the data from the quiz form, including $quiz->id
* which is the id of the quiz being saved
*
* @throws dml_exception
*/
public static function save_settings($quiz) {
global $DB;
if (empty($quiz->proctoringrequired)) {
$DB->delete_records('quizaccess_proctoring', ['quizid' => $quiz->id]);
} else {
if (!$DB->record_exists('quizaccess_proctoring', ['quizid' => $quiz->id])) {
$record = new stdClass();
$record->quizid = $quiz->id;
$record->proctoringrequired = 1;
$DB->insert_record('quizaccess_proctoring', $record);
}
}
}
/**
* Delete any rule-specific settings when the quiz is deleted. This is called
* from quiz_delete_instance() in lib.php.
*
* @param object $quiz the data from the database, including $quiz->id
* which is the id of the quiz being deleted
*
* @throws dml_exception
*/
public static function delete_settings($quiz) {
global $DB;
$DB->delete_records('quizaccess_proctoring', ['quizid' => $quiz->id]);
}
/**
* Return the bits of SQL needed to load all the settings from all the access
* plugins in one DB query. The easiest way to understand what you need to do
* here is probalby to read the code of quiz_access_manager::load_settings().
*
* If you have some settings that cannot be loaded in this way, then you can
* use the get_extra_settings() method instead, but that has
* performance implications.
*
* @param int $quizid the id of the quiz we are loading settings for. This
* can also be accessed as quiz.id in the SQL. (quiz is a table alisas for {quiz}.)
*
* @return array with three elements:
* 1. fields: any fields to add to the select list. These should be alised
* if neccessary so that the field name starts the name of the plugin.
* 2. joins: any joins (should probably be LEFT JOINS) with other tables that
* are needed.
* 3. params: array of placeholder values that are needed by the SQL. You must
* used named placeholders, and the placeholder names should start with the
* plugin name, to avoid collisions.
*/
public static function get_settings_sql($quizid) {
return [
'proctoringrequired',
'LEFT JOIN {quizaccess_proctoring} proctoring ON proctoring.quizid = quiz.id',
[], ];
}
/**
* Information, such as might be shown on the quiz view page, relating to this restriction.
* There is no obligation to return anything. If it is not appropriate to tell students
* about this rule, then just return ''.
*
* @return mixed a message, or array of messages, explaining the restriction
* (may be '' if no message is appropriate)
*
* @throws coding_exception
*/
public function description() {
global $PAGE;
$record = new stdClass();
$record->allowcamerawarning = get_string('warning:cameraallowwarning', 'quizaccess_proctoring');
$PAGE->requires->js_call_amd('quizaccess_proctoring/proctoring', 'init', [$record]);
$messages = [get_string('proctoringheader', 'quizaccess_proctoring')];
$messages[] = $this->get_download_config_button();
return $messages;
}
/**
* Sets up the attempt (review or summary) page with any special extra
* properties required by this rule.
*
* @param moodle_page $page the page object to initialise
*
* @throws coding_exception
* @throws dml_exception
*/
public function setup_attempt_page($page) {
$cmid = optional_param('cmid', '', PARAM_INT);
$attempt = optional_param('attempt', '', PARAM_INT);
$page->set_title($this->quizobj->get_course()->shortname . ': ' . $page->title);
$page->set_popup_notification_allowed(false); // Prevent message notifications.
$page->set_heading($page->title);
global $CFG, $DB, $COURSE, $USER;
if ($cmid) {
$contextquiz = $DB->get_record('course_modules', ['id' => $cmid]);
$record = new stdClass();
$record->courseid = $COURSE->id;
$record->quizid = $contextquiz->id;
$record->userid = $USER->id;
$record->webcampicture = '';
$record->status = $attempt;
$record->timemodified = time();
$record->id = $DB->insert_record('quizaccess_proctoring_logs', $record, true);
// Get Screenshot Delay and Image Width.
$imagedelaysql = "SELECT * FROM {config_plugins}
WHERE plugin = 'quizaccess_proctoring' AND name = 'autoreconfigurecamshotdelay'";
$delaydata = $DB->get_records_sql($imagedelaysql);
$camshotdelay = 30 * 1000;
if (count($delaydata) > 0) {
foreach ($delaydata as $row) {
$camshotdelay = (int)$row->value * 1000;
}
}
$imagesizesql = "SELECT * FROM {config_plugins}
WHERE plugin = 'quizaccess_proctoring' AND name = 'autoreconfigureimagewidth'";
$imagesizedata = $DB->get_records_sql($imagesizesql);
$imagewidth = 230;
if (count($imagesizedata) > 0) {
foreach ($imagesizedata as $row) {
$imagewidth = (int)$row->value;
}
}
$screensharesql = "SELECT * FROM {config_plugins}
WHERE plugin = 'quizaccess_proctoring'
AND name = 'screenshareenable'";
$screensharerow = $DB->get_record_sql($screensharesql);
$enablescreenshare = $screensharerow->value;
$quizurl = new moodle_url('/mod/quiz/view.php', ['id' => $cmid]);
$record->camshotdelay = $camshotdelay;
$record->image_width = $imagewidth;
$record->quizurl = $quizurl->__toString();
$record->enablescreenshare = $enablescreenshare;
$modelurl = $CFG->wwwroot . '/mod/quiz/accessrule/proctoring/thirdpartylibs/models';
$page->requires->js("/mod/quiz/accessrule/proctoring/amd/build/face-api.min.js", true);
$page->requires->js_call_amd('quizaccess_proctoring/proctoring', 'setup', [$record, $modelurl]);
}
}
/**
* Get a button to view the Proctoring report.
*
* @return string A link to view report
*
* @throws coding_exception
*/
private function get_download_config_button(): string {
global $OUTPUT, $USER;
$context = context_module::instance($this->quiz->cmid, MUST_EXIST);
if (has_capability('quizaccess/proctoring:viewreport', $context, $USER->id)) {
$httplink = \quizaccess_proctoring\LinkGenerator::get_link($this->quiz->course, $this->quiz->cmid, false, is_https());
return $OUTPUT->single_button($httplink, get_string('picturesreport', 'quizaccess_proctoring'), 'get');
} else {
return '';
}
}
}