Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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  use mod_quiz\form\preflight_check_form;
  18  use mod_quiz\local\access_rule_base;
  19  use mod_quiz\quiz_settings;
  20  
  21  /**
  22   * A rule implementing the password check.
  23   *
  24   * @package   quizaccess_password
  25   * @copyright 2009 Tim Hunt
  26   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class quizaccess_password extends access_rule_base {
  29  
  30      public static function make(quiz_settings $quizobj, $timenow, $canignoretimelimits) {
  31          if (empty($quizobj->get_quiz()->password)) {
  32              return null;
  33          }
  34  
  35          return new self($quizobj, $timenow);
  36      }
  37  
  38      public function description() {
  39          return get_string('requirepasswordmessage', 'quizaccess_password');
  40      }
  41  
  42      public function is_preflight_check_required($attemptid) {
  43          global $SESSION;
  44          return empty($SESSION->passwordcheckedquizzes[$this->quiz->id]);
  45      }
  46  
  47      public function add_preflight_check_form_fields(preflight_check_form $quizform,
  48              MoodleQuickForm $mform, $attemptid) {
  49  
  50          $mform->addElement('header', 'passwordheader', get_string('password'));
  51          $mform->addElement('static', 'passwordmessage', '',
  52                  get_string('requirepasswordmessage', 'quizaccess_password'));
  53  
  54          // Don't use the 'proper' field name of 'password' since that get's
  55          // Firefox's password auto-complete over-excited.
  56          $mform->addElement('passwordunmask', 'quizpassword',
  57                  get_string('quizpassword', 'quizaccess_password'), ['autofocus' => 'true']);
  58      }
  59  
  60      public function validate_preflight_check($data, $files, $errors, $attemptid) {
  61  
  62          $enteredpassword = $data['quizpassword'];
  63          if (strcmp($this->quiz->password, $enteredpassword) === 0) {
  64              return $errors; // Password is OK.
  65  
  66          } else if (isset($this->quiz->extrapasswords)) {
  67              // Group overrides may have additional passwords.
  68              foreach ($this->quiz->extrapasswords as $password) {
  69                  if (strcmp($password, $enteredpassword) === 0) {
  70                      return $errors; // Password is OK.
  71                  }
  72              }
  73          }
  74  
  75          $errors['quizpassword'] = get_string('passworderror', 'quizaccess_password');
  76          return $errors;
  77      }
  78  
  79      public function notify_preflight_check_passed($attemptid) {
  80          global $SESSION;
  81          $SESSION->passwordcheckedquizzes[$this->quiz->id] = true;
  82      }
  83  
  84      public function current_attempt_finished() {
  85          global $SESSION;
  86          // Clear the flag in the session that says that the user has already
  87          // entered the password for this quiz.
  88          if (!empty($SESSION->passwordcheckedquizzes[$this->quiz->id])) {
  89              unset($SESSION->passwordcheckedquizzes[$this->quiz->id]);
  90          }
  91      }
  92  }