-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrule.php
99 lines (89 loc) · 3.88 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
<?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/>.
/**
* Implementation of the quizaccess_ratelimit plugin.
*
* @package quizaccess_ratelimit
* @copyright 2021 Martin Gauk, TU Berlin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use mod_quiz\form\preflight_check_form;
/**
* Implementation of the quizaccess_ratelimit plugin.
*
* @copyright 2021 Martin Gauk, TU Berlin <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quizaccess_ratelimit extends \mod_quiz\local\access_rule_base {
/**
* This is the maximum possible delay (created by this plugin) before a quiz attempt can be started.
*/
const MAX_DELAY = 15 * 60;
/**
* Return an appropriately configured instance of this rule, if it is applicable
* to the given quiz, otherwise return null.
*
* @param \mod_quiz\quiz_settings $quizobj information about the quiz in question.
* @param int $timenow the time that should be considered as 'now'.
* @param bool $canignoretimelimits whether the current user is exempt from
* time limits by the mod/quiz:ignoretimelimits capability.
* @return self|null the rule, if applicable, else null.
*/
public static function make(
\mod_quiz\quiz_settings $quizobj,
$timenow,
$canignoretimelimits,
): self|null {
return new self($quizobj, $timenow);
}
/**
* Does this rule require a UI check with the user before an attempt is started?
*
* @param int|null $attemptid the id of the current attempt, if there is one,
* otherwise null.
* @return bool whether a check is required before the user starts/continues
* their attempt.
*/
public function is_preflight_check_required($attemptid) {
global $PAGE;
if (isset($PAGE)) {
// It is a bit hacky to insert the amd call at this point, but there is no better place. We have used the description
// method before, but it is not called at all places where the preflight form is displayed.
// When there is a $PAGE object, $PAGE->requires should also exist as it is dynamically created when accessed.
if ($this->quizobj->has_capability('quizaccess/ratelimit:exempt')) {
$maxdelay = 0;
} else if ($this->quiz->timeclose && $this->quiz->timelimit) {
// The user should have enough time to take the quiz including a short safety margin.
$maxdelay = max(0, $this->quiz->timeclose - $this->quiz->timelimit - $this->timenow - 2);
$maxdelay = min(self::MAX_DELAY, $maxdelay);
} else {
$maxdelay = self::MAX_DELAY;
}
$PAGE->requires->js_call_amd('quizaccess_ratelimit/ratelimit', 'init', [$maxdelay]);
}
return false;
}
/**
* The pre-flight check has passed. This is a chance to record that fact in
* some way.
* @param int|null $attemptid the id of the current attempt, if there is one,
* otherwise null.
*/
public function notify_preflight_check_passed($attemptid): void {
global $SESSION;
unset($SESSION->quizaccess_ratelimit_time);
}
}