Merge branch 'MDL-39029' of git://github.com/jacks92/moodle
[moodle.git] / mod / quiz / accessrule / password / rule.php
CommitLineData
a28a5d74
TH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Implementaton of the quizaccess_password plugin.
19 *
20 * @package quizaccess
21 * @subpackage password
22 * @copyright 2011 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
30
31
32/**
987c2d49 33 * A rule implementing the password check.
a28a5d74
TH
34 *
35 * @copyright 2009 Tim Hunt
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class quizaccess_password extends quiz_access_rule_base {
1d9e1a3c
TH
39
40 public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
41 if (empty($quizobj->get_quiz()->password)) {
42 return null;
43 }
44
45 return new self($quizobj, $timenow);
46 }
47
a28a5d74 48 public function description() {
ffe162d4 49 return get_string('requirepasswordmessage', 'quizaccess_password');
a28a5d74
TH
50 }
51
987c2d49 52 public function is_preflight_check_required($attemptid) {
a28a5d74 53 global $SESSION;
987c2d49 54 return empty($SESSION->passwordcheckedquizzes[$this->quiz->id]);
a28a5d74
TH
55 }
56
987c2d49
TH
57 public function add_preflight_check_form_fields(mod_quiz_preflight_check_form $quizform,
58 MoodleQuickForm $mform, $attemptid) {
a28a5d74 59
987c2d49
TH
60 $mform->addElement('header', 'passwordheader', get_string('password'));
61 $mform->addElement('static', 'passwordmessage', '',
ffe162d4 62 get_string('requirepasswordmessage', 'quizaccess_password'));
987c2d49 63
9e83f3d1 64 // Don't use the 'proper' field name of 'password' since that get's
987c2d49 65 // Firefox's password auto-complete over-excited.
ffe162d4 66 $mform->addElement('password', 'quizpassword',
6b7a020b 67 get_string('quizpassword', 'quizaccess_password'), array('autofocus' => 'true'));
987c2d49 68 }
a28a5d74 69
987c2d49
TH
70 public function validate_preflight_check($data, $files, $errors, $attemptid) {
71
72 $enteredpassword = $data['quizpassword'];
e5844040 73 if (strcmp($this->quiz->password, $enteredpassword) === 0) {
987c2d49
TH
74 return $errors; // Password is OK.
75
e5844040 76 } else if (isset($this->quiz->extrapasswords)) {
9e83f3d1 77 // Group overrides may have additional passwords.
e5844040 78 foreach ($this->quiz->extrapasswords as $password) {
a28a5d74 79 if (strcmp($password, $enteredpassword) === 0) {
987c2d49 80 return $errors; // Password is OK.
a28a5d74
TH
81 }
82 }
83 }
a28a5d74 84
ffe162d4 85 $errors['quizpassword'] = get_string('passworderror', 'quizaccess_password');
987c2d49
TH
86 return $errors;
87 }
a28a5d74 88
987c2d49
TH
89 public function notify_preflight_check_passed($attemptid) {
90 global $SESSION;
91 $SESSION->passwordcheckedquizzes[$this->quiz->id] = true;
92 }
a28a5d74 93
987c2d49
TH
94 public function current_attempt_finished() {
95 global $SESSION;
96 // Clear the flag in the session that says that the user has already
97 // entered the password for this quiz.
98 if (!empty($SESSION->passwordcheckedquizzes[$this->quiz->id])) {
99 unset($SESSION->passwordcheckedquizzes[$this->quiz->id]);
a28a5d74 100 }
a28a5d74
TH
101 }
102}