|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php'); 00030 00031 00038 class quizaccess_password extends quiz_access_rule_base { 00039 00040 public static function make(quiz $quizobj, $timenow, $canignoretimelimits) { 00041 if (empty($quizobj->get_quiz()->password)) { 00042 return null; 00043 } 00044 00045 return new self($quizobj, $timenow); 00046 } 00047 00048 public function description() { 00049 return get_string('requirepasswordmessage', 'quizaccess_password'); 00050 } 00051 00052 public function is_preflight_check_required($attemptid) { 00053 global $SESSION; 00054 return empty($SESSION->passwordcheckedquizzes[$this->quiz->id]); 00055 } 00056 00057 public function add_preflight_check_form_fields(mod_quiz_preflight_check_form $quizform, 00058 MoodleQuickForm $mform, $attemptid) { 00059 00060 $mform->addElement('header', 'passwordheader', get_string('password')); 00061 $mform->addElement('static', 'passwordmessage', '', 00062 get_string('requirepasswordmessage', 'quizaccess_password')); 00063 00064 // don't use the 'proper' field name of 'password' since that get's 00065 // Firefox's password auto-complete over-excited. 00066 $mform->addElement('password', 'quizpassword', 00067 get_string('quizpassword', 'quizaccess_password')); 00068 } 00069 00070 public function validate_preflight_check($data, $files, $errors, $attemptid) { 00071 00072 $enteredpassword = $data['quizpassword']; 00073 if (strcmp($this->quiz->password, $enteredpassword) === 0) { 00074 return $errors; // Password is OK. 00075 00076 } else if (isset($this->quiz->extrapasswords)) { 00077 // Group overrides may have additional passwords 00078 foreach ($this->quiz->extrapasswords as $password) { 00079 if (strcmp($password, $enteredpassword) === 0) { 00080 return $errors; // Password is OK. 00081 } 00082 } 00083 } 00084 00085 $errors['quizpassword'] = get_string('passworderror', 'quizaccess_password'); 00086 return $errors; 00087 } 00088 00089 public function notify_preflight_check_passed($attemptid) { 00090 global $SESSION; 00091 $SESSION->passwordcheckedquizzes[$this->quiz->id] = true; 00092 } 00093 00094 public function current_attempt_finished() { 00095 global $SESSION; 00096 // Clear the flag in the session that says that the user has already 00097 // entered the password for this quiz. 00098 if (!empty($SESSION->passwordcheckedquizzes[$this->quiz->id])) { 00099 unset($SESSION->passwordcheckedquizzes[$this->quiz->id]); 00100 } 00101 } 00102 }