Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [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  /**
  18   * Implementaton of the quizaccess_delaybetweenattempts plugin.
  19   *
  20   * @package    quizaccess
  21   * @subpackage delaybetweenattempts
  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 imposing the delay between attempts settings.
  34   *
  35   * @copyright  2009 Tim Hunt
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class quizaccess_delaybetweenattempts extends quiz_access_rule_base {
  39  
  40      public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
  41          if (empty($quizobj->get_quiz()->delay1) && empty($quizobj->get_quiz()->delay2)) {
  42              return null;
  43          }
  44  
  45          return new self($quizobj, $timenow);
  46      }
  47  
  48      public function prevent_new_attempt($numprevattempts, $lastattempt) {
  49          if ($this->quiz->attempts > 0 && $numprevattempts >= $this->quiz->attempts) {
  50              // No more attempts allowed anyway.
  51              return false;
  52          }
  53          if ($this->quiz->timeclose != 0 && $this->timenow > $this->quiz->timeclose) {
  54              // No more attempts allowed anyway.
  55              return false;
  56          }
  57          $nextstarttime = $this->compute_next_start_time($numprevattempts, $lastattempt);
  58          if ($this->timenow < $nextstarttime) {
  59              if ($this->quiz->timeclose == 0 || $nextstarttime <= $this->quiz->timeclose) {
  60                  return get_string('youmustwait', 'quizaccess_delaybetweenattempts',
  61                          userdate($nextstarttime));
  62              } else {
  63                  return get_string('youcannotwait', 'quizaccess_delaybetweenattempts');
  64              }
  65          }
  66          return false;
  67      }
  68  
  69      /**
  70       * Compute the next time a student would be allowed to start an attempt,
  71       * according to this rule.
  72       * @param int $numprevattempts number of previous attempts.
  73       * @param object $lastattempt information about the previous attempt.
  74       * @return number the time.
  75       */
  76      protected function compute_next_start_time($numprevattempts, $lastattempt) {
  77          if ($numprevattempts == 0) {
  78              return 0;
  79          }
  80  
  81          $lastattemptfinish = $lastattempt->timefinish;
  82          if ($this->quiz->timelimit > 0) {
  83              $lastattemptfinish = min($lastattemptfinish,
  84                      $lastattempt->timestart + $this->quiz->timelimit);
  85          }
  86  
  87          if ($numprevattempts == 1 && $this->quiz->delay1) {
  88              return $lastattemptfinish + $this->quiz->delay1;
  89          } else if ($numprevattempts > 1 && $this->quiz->delay2) {
  90              return $lastattemptfinish + $this->quiz->delay2;
  91          }
  92          return 0;
  93      }
  94  
  95      public function is_finished($numprevattempts, $lastattempt) {
  96          $nextstarttime = $this->compute_next_start_time($numprevattempts, $lastattempt);
  97          return $this->timenow <= $nextstarttime &&
  98          $this->quiz->timeclose != 0 && $nextstarttime >= $this->quiz->timeclose;
  99      }
 100  }