/
accessrules.php
560 lines (512 loc) · 22 KB
/
accessrules.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
<?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/>.
/**
* Classes to enforce the various access rules that can apply to a quiz.
*
* @package mod
* @subpackage quiz
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* This class keeps track of the various access rules that apply to a particular
* quiz, with convinient methods for seeing whether access is allowed.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class reader_access_manager {
private $_readerobj;
private $_timenow;
private $_passwordrule = null;
private $_securewindowrule = null;
private $_safebrowserrule = null;
private $_rules = array();
/**
* Create an instance for a particular quiz.
* @param object $quizobj An instance of the class quiz from attemptlib.php.
* The quiz we will be controlling access to.
* @param int $timenow The time to use as 'now'.
* @param bool $canignoretimelimits Whether this user is exempt from time
* limits (has_capability('mod/quiz:ignoretimelimits', ...)).
*/
public function __construct($quizobj, $timenow, $canignoretimelimits) {
$this->_readerobj = $quizobj;
$this->_timenow = $timenow;
$this->create_standard_rules($canignoretimelimits);
}
private function create_standard_rules($canignoretimelimits) {
$quiz = $this->_readerobj->get_reader();
if ($quiz->attempts > 0) {
$this->_rules[] = new num_attempts_access_rule($this->_readerobj, $this->_timenow);
}
$this->_rules[] = new open_close_date_access_rule1($this->_readerobj, $this->_timenow);
if (!empty($quiz->timelimit) && !$canignoretimelimits) {
$this->_rules[] = new time_limit_access_rule($this->_readerobj, $this->_timenow);
}
if (!empty($quiz->delay1) || !empty($quiz->delay2)) {
$this->_rules[] = new inter_attempt_delay_access_rule($this->_readerobj, $this->_timenow);
}
if (!empty($quiz->subnet)) {
$this->_rules[] = new ipaddress_access_rule($this->_readerobj, $this->_timenow);
}
if (!empty($quiz->password)) {
$this->_passwordrule = new password_access_rule($this->_readerobj, $this->_timenow);
$this->_rules[] = $this->_passwordrule;
}
if (!empty($quiz->popup)) {
if ($quiz->popup == 1) {
$this->_securewindowrule = new securewindow_access_rule(
$this->_readerobj, $this->_timenow);
$this->_rules[] = $this->_securewindowrule;
} else if ($quiz->popup == 2) {
$this->_safebrowserrule = new safebrowser_access_rule(
$this->_readerobj, $this->_timenow);
$this->_rules[] = $this->_safebrowserrule;
}
}
}
private function accumulate_messages(&$messages, $new) {
if (is_array($new)) {
$messages = array_merge($messages, $new);
} else if (is_string($new) && $new) {
$messages[] = $new;
}
}
/**
* Provide a description of the rules that apply to this quiz, such
* as is shown at the top of the quiz view page. Note that not all
* rules consider themselves important enough to output a description.
*
* @return array an array of description messages which may be empty. It
* would be sensible to output each one surrounded by <p> tags.
*/
public function describe_rules() {
$result = array();
foreach ($this->_rules as $rule) {
$this->accumulate_messages($result, $rule->description());
}
return $result;
}
/**
* Is it OK to let the current user start a new attempt now? If there are
* any restrictions in force now, return an array of reasons why access
* should be blocked. If access is OK, return false.
*
* @param int $numattempts the number of previous attempts this user has made.
* @param object|false $lastattempt information about the user's last completed attempt.
* if there is not a previous attempt, the false is passed.
* @return mixed An array of reason why access is not allowed, or an empty array
* (== false) if access should be allowed.
*/
public function prevent_new_attempt($numprevattempts, $lastattempt) {
$reasons = array();
foreach ($this->_rules as $rule) {
$this->accumulate_messages($reasons,
$rule->prevent_new_attempt($numprevattempts, $lastattempt));
}
return $reasons;
}
/**
* Is it OK to let the current user start a new attempt now? If there are
* any restrictions in force now, return an array of reasons why access
* should be blocked. If access is OK, return false.
*
* @return mixed An array of reason why access is not allowed, or an empty array
* (== false) if access should be allowed.
*/
public function prevent_access() {
$reasons = array();
foreach ($this->_rules as $rule) {
$this->accumulate_messages($reasons, $rule->prevent_access());
}
return $reasons;
}
/**
* Do any of the rules mean that this student will no be allowed any further attempts at this
* quiz. Used, for example, to change the label by the grade displayed on the view page from
* 'your current grade is' to 'your final grade is'.
*
* @param int $numattempts the number of previous attempts this user has made.
* @param object $lastattempt information about the user's last completed attempt.
* @return bool true if there is no way the user will ever be allowed to attempt
* this quiz again.
*/
public function is_finished($numprevattempts, $lastattempt) {
foreach ($this->_rules as $rule) {
if ($rule->is_finished($numprevattempts, $lastattempt)) {
return true;
}
}
return false;
}
/**
* Do the printheader call, etc. required for a secure page, including the necessary JS.
*
* @param string $title HTML title tag content, passed to printheader.
* @param string $headtags extra stuff to go in the HTML head tag, passed to printheader.
*/
public function setup_secure_page($title, $headtags = null) {
$this->_securewindowrule->setup_secure_page($title, $headtags);
}
public function show_attempt_timer_if_needed($attempt, $timenow) {
global $PAGE;
$timeleft = false;
foreach ($this->_rules as $rule) {
$ruletimeleft = $rule->time_left($attempt, $timenow);
if ($ruletimeleft !== false && ($timeleft === false || $ruletimeleft < $timeleft)) {
$timeleft = $ruletimeleft;
}
}
if ($timeleft !== false) {
// Make sure the timer starts just above zero. If $timeleft was <= 0, then
// this will just have the effect of causing the quiz to be submitted immediately.
$timerstartvalue = max($timeleft, 1);
$PAGE->requires->js_init_call('M.mod_quiz.timer.init',
array($timerstartvalue), false, reader_get_js_module());
}
}
/**
* @return bolean if this quiz should only be shown to students in a secure window.
*/
public function securewindow_required($canpreview) {
return !$canpreview && !is_null($this->_securewindowrule);
}
/**
* @return bolean if this quiz should only be shown to students with safe browser.
*/
public function safebrowser_required($canpreview) {
return !$canpreview && !is_null($this->_safebrowserrule);
}
/**
* Print a button to start a quiz attempt, with an appropriate javascript warning,
* depending on the access restrictions. The link will pop up a 'secure' window, if
* necessary.
*
* @param bool $canpreview whether this user can preview. This affects whether they must
* use a secure window.
* @param string $buttontext the label to put on the button.
* @param bool $unfinished whether the button is to continue an existing attempt,
* or start a new one. This affects whether a javascript alert is shown.
*/
//TODO: Add this function to renderer
public function print_start_attempt_button($canpreview, $buttontext, $unfinished) {
global $OUTPUT;
$url = $this->_readerobj->start_attempt_url();
$button = new single_button($url, $buttontext);
$button->class .= ' quizstartbuttondiv';
if (!$unfinished) {
$strconfirmstartattempt = $this->confirm_start_attempt_message();
if ($strconfirmstartattempt) {
$button->add_action(new confirm_action($strconfirmstartattempt, null,
get_string('startattempt', 'quiz')));
}
}
$warning = '';
if ($this->securewindow_required($canpreview)) {
$button->class .= ' quizsecuremoderequired';
$button->add_action(new popup_action('click', $url, 'quizpopup',
securewindow_access_rule::$popupoptions));
$warning = html_writer::tag('noscript',
$OUTPUT->heading(get_string('noscript', 'quiz')));
}
return $OUTPUT->render($button) . $warning;
}
/**
* Send the user back to the quiz view page. Normally this is just a redirect, but
* If we were in a secure window, we close this window, and reload the view window we came from.
*
* @param bool $canpreview This affects whether we have to worry about secure window stuff.
*/
public function back_to_view_page($canpreview, $message = '') {
global $CFG, $OUTPUT, $PAGE;
$url = $this->_readerobj->view_url();
if ($this->securewindow_required($canpreview)) {
$PAGE->set_pagelayout('popup');
echo $OUTPUT->header();
echo $OUTPUT->box_start();
if ($message) {
echo '<p>' . $message . '</p><p>' . get_string('windowclosing', 'quiz') . '</p>';
$delay = 5;
} else {
echo '<p>' . get_string('pleaseclose', 'quiz') . '</p>';
$delay = 0;
}
$PAGE->requires->js_function_call('M.mod_quiz.secure_window.close',
array($url, $delay));
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
die();
} else {
redirect($url, $message);
}
}
/**
* Print a control to finish the review. Normally this is just a link, but if we are
* in a secure window, it needs to be a button that does M.mod_quiz.secure_window.close.
*
* @param bool $canpreview This affects whether we have to worry about secure window stuff.
*/
public function print_finish_review_link($canpreview, $return = false) {
global $CFG;
$output = '';
$url = $this->_readerobj->view_url();
$output .= '<div class="finishreview">';
if ($this->securewindow_required($canpreview)) {
$url = addslashes_js(htmlspecialchars($url));
$output .= '<input type="button" value="' . get_string('finishreview', 'quiz') . '" ' .
"onclick=\"M.mod_quiz.secure_window.close('$url', 0)\" />\n";
} else {
$output .= '<a href="' . $url . '">' . get_string('finishreview', 'quiz') . "</a>\n";
}
$output .= "</div>\n";
if ($return) {
return $output;
} else {
echo $output;
}
}
/**
* @return bolean if this quiz is password public.
*/
public function password_required() {
return !is_null($this->_passwordrule);
}
/**
* Clear the flag in the session that says that the current user is allowed to do this quiz.
*/
public function clear_password_access() {
if (!is_null($this->_passwordrule)) {
$this->_passwordrule->clear_access_allowed();
}
}
/**
* Actually ask the user for the password, if they have not already given it this session.
* This function only returns is access is OK.
*
* @param bool $canpreview used to enfore securewindow stuff.
*/
public function do_password_check($canpreview) {
if (!is_null($this->_passwordrule)) {
$this->_passwordrule->do_password_check($canpreview, $this);
}
}
/**
* @return string if the quiz policies merit it, return a warning string to be displayed
* in a javascript alert on the start attempt button.
*/
public function confirm_start_attempt_message() {
$quiz = $this->_readerobj->get_reader();
if ($quiz->timelimit && $quiz->attempts) {
return get_string('confirmstartattempttimelimit', 'quiz', $quiz->attempts);
} else if ($quiz->timelimit) {
return get_string('confirmstarttimelimit', 'quiz');
} else if ($quiz->attempts) {
return get_string('confirmstartattemptlimit', 'quiz', $quiz->attempts);
}
return '';
}
/**
* Make some text into a link to review the quiz, if that is appropriate.
*
* @param string $linktext some text.
* @param object $attempt the attempt object
* @return string some HTML, the $linktext either unmodified or wrapped in a
* link to the review page.
*/
public function make_review_link($attempt, $canpreview, $reviewoptions) {
global $CFG;
// If review of responses is not allowed, or the attempt is still open, don't link.
if (!$attempt->timefinish) {
return '';
}
$when = quiz_attempt_state($this->_readerobj->get_reader(), $attempt);
$reviewoptions = mod_reader_display_options::make_from_quiz(
$this->_readerobj->get_reader(), $when);
if (!$reviewoptions->attempt) {
$message = $this->cannot_review_message($when, true);
if ($message) {
return '<span class="noreviewmessage">' . $message . '</span>';
} else {
return '';
}
}
$linktext = get_string('review', 'quiz');
// It is OK to link, does it need to be in a secure window?
if ($this->securewindow_required($canpreview)) {
return $this->_securewindowrule->make_review_link($linktext, $attempt->id);
} else {
return '<a href="' . $this->_readerobj->review_url($attempt->id) . '" title="' .
get_string('reviewthisattempt', 'quiz') . '">' . $linktext . '</a>';
}
}
/**
* If $reviewoptions->attempt is false, meaning that students can't review this
* attempt at the moment, return an appropriate string explaining why.
*
* @param int $when One of the mod_reader_display_options::DURING,
* IMMEDIATELY_AFTER, LATER_WHILE_OPEN or AFTER_CLOSE constants.
* @param bool $short if true, return a shorter string.
* @return string an appropraite message.
*/
public function cannot_review_message($when, $short = false) {
$quiz = $this->_readerobj->get_reader();
if ($short) {
$langstrsuffix = 'short';
$dateformat = get_string('strftimedatetimeshort', 'langconfig');
} else {
$langstrsuffix = '';
$dateformat = '';
}
if ($when == mod_reader_display_options::DURING ||
$when == mod_reader_display_options::IMMEDIATELY_AFTER) {
return '';
} else if ($when == mod_reader_display_options::LATER_WHILE_OPEN && $quiz->timeclose &&
$quiz->reviewattempt & mod_reader_display_options::AFTER_CLOSE) {
return get_string('noreviewuntil' . $langstrsuffix, 'quiz',
userdate($quiz->timeclose, $dateformat));
} else {
return get_string('noreview' . $langstrsuffix, 'quiz');
}
}
}
/**
* A base class that defines the interface for the various quiz access rules.
* Most of the methods are defined in a slightly unnatural way because we either
* want to say that access is allowed, or explain the reason why it is block.
* Therefore instead of is_access_allowed(...) we have prevent_access(...) that
* return false if access is permitted, or a string explanation (which is treated
* as true) if access should be blocked. Slighly unnatural, but acutally the easist
* way to implement this.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class reader_access_rule_base {
public $_quiz;
public $_readerobj;
public $_timenow;
/**
* Create an instance of this rule for a particular quiz.
* @param object $quiz the quiz we will be controlling access to.
*/
public function __construct($quizobj, $timenow) {
$this->_readerobj = $quizobj;
$this->_quiz = $quizobj->get_reader();
$this->_timenow = $timenow;
}
/**
* Whether or not a user should be allowed to start a new attempt at this quiz now.
* @param int $numattempts the number of previous attempts this user has made.
* @param object $lastattempt information about the user's last completed attempt.
* @return string false if access should be allowed, a message explaining the
* reason if access should be prevented.
*/
public function prevent_new_attempt($numprevattempts, $lastattempt) {
return false;
}
/**
* Whether or not a user should be allowed to start a new attempt at this quiz now.
* @return string false if access should be allowed, a message explaining the
* reason if access should be prevented.
*/
public function prevent_access() {
return false;
}
/**
* 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).
*/
public function description() {
return '';
}
/**
* If this rule can determine that this user will never be allowed another attempt at
* this quiz, then return true. This is used so we can know whether to display a
* final grade on the view page. This will only be called if there is not a currently
* active attempt for this user.
* @param int $numattempts the number of previous attempts this user has made.
* @param object $lastattempt information about the user's last completed attempt.
* @return bool true if this rule means that this user will never be allowed another
* attempt at this quiz.
*/
public function is_finished($numprevattempts, $lastattempt) {
return false;
}
/**
* If, because of this rule, the user has to finish their attempt by a certain time,
* you should override this method to return the amount of time left in seconds.
* @param object $attempt the current attempt
* @param int $timenow the time now. We don't use $this->_timenow, so we can
* give the user a more accurate indication of how much time is left.
* @return mixed false if there is no deadline, of the time left in seconds if there is one.
*/
public function time_left($attempt, $timenow) {
return false;
}
}
/**
* A rule enforcing open and close dates.
*
* @copyright 2009 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class open_close_date_access_rule1 extends reader_access_rule_base {
public function description() {
$result = array();
if ($this->_timenow < $this->_quiz->timeopen) {
$result[] = get_string('quiznotavailable', 'quiz', userdate($this->_quiz->timeopen));
} else if ($this->_quiz->timeclose && $this->_timenow > $this->_quiz->timeclose) {
$result[] = get_string("quizclosed", "quiz", userdate($this->_quiz->timeclose));
} else {
if ($this->_quiz->timeopen) {
$result[] = get_string('quizopenedon', 'quiz', userdate($this->_quiz->timeopen));
}
if ($this->_quiz->timeclose) {
$result[] = get_string('quizcloseson', 'quiz', userdate($this->_quiz->timeclose));
}
}
return $result;
}
public function prevent_access() {
if ($this->_timenow < $this->_quiz->timeopen ||
($this->_quiz->timeclose && $this->_timenow > $this->_quiz->timeclose)) {
return get_string('notavailable', 'quiz');
}
return false;
}
public function is_finished($numprevattempts, $lastattempt) {
return $this->_quiz->timeclose && $this->_timenow > $this->_quiz->timeclose;
}
public function time_left($attempt, $timenow) {
if ($attempt->preview && $timenow > $this->_quiz->timeclose) {
return false;
}
// Otherwise, return to the time left until the close date, providing
// that is less than QUIZ_SHOW_TIME_BEFORE_DEADLINE
if ($this->_quiz->timeclose) {
$timeleft = $this->_quiz->timeclose - $timenow;
if ($timeleft < QUIZ_SHOW_TIME_BEFORE_DEADLINE) {
return $timeleft;
}
}
return false;
}
}