Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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  /**
  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  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
  30  
  31  
  32  /**
  33   * A rule implementing the password check.
  34   *
  35   * @copyright  2009 Tim Hunt
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class quizaccess_password extends quiz_access_rule_base {
  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  
  48      public function description() {
  49          return get_string('requirepasswordmessage', 'quizaccess_password');
  50      }
  51  
  52      public function is_preflight_check_required($attemptid) {
  53          global $SESSION;
  54          return empty($SESSION->passwordcheckedquizzes[$this->quiz->id]);
  55      }
  56  
  57      public function add_preflight_check_form_fields(mod_quiz_preflight_check_form $quizform,
  58              MoodleQuickForm $mform, $attemptid) {
  59  
  60          $mform->addElement('header', 'passwordheader', get_string('password'));
  61          $mform->addElement('static', 'passwordmessage', '',
  62                  get_string('requirepasswordmessage', 'quizaccess_password'));
  63  
  64          // Don't use the 'proper' field name of 'password' since that get's
  65          // Firefox's password auto-complete over-excited.
  66          $mform->addElement('password', 'quizpassword',
  67                  get_string('quizpassword', 'quizaccess_password'), array('autofocus' => 'true'));
  68      }
  69  
  70      public function validate_preflight_check($data, $files, $errors, $attemptid) {
  71  
  72          $enteredpassword = $data['quizpassword'];
  73          if (strcmp($this->quiz->password, $enteredpassword) === 0) {
  74              return $errors; // Password is OK.
  75  
  76          } else if (isset($this->quiz->extrapasswords)) {
  77              // Group overrides may have additional passwords.
  78              foreach ($this->quiz->extrapasswords as $password) {
  79                  if (strcmp($password, $enteredpassword) === 0) {
  80                      return $errors; // Password is OK.
  81                  }
  82              }
  83          }
  84  
  85          $errors['quizpassword'] = get_string('passworderror', 'quizaccess_password');
  86          return $errors;
  87      }
  88  
  89      public function notify_preflight_check_passed($attemptid) {
  90          global $SESSION;
  91          $SESSION->passwordcheckedquizzes[$this->quiz->id] = true;
  92      }
  93  
  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]);
 100          }
 101      }
 102  }