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 representing the time limit.
  23   *
  24   * It does not actually restrict access, but we use this
  25   * class to encapsulate some of the relevant code.
  26   *
  27   * @package   quizaccess_timelimit
  28   * @copyright 2009 Tim Hunt
  29   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class quizaccess_timelimit extends access_rule_base {
  32  
  33      public static function make(quiz_settings $quizobj, $timenow, $canignoretimelimits) {
  34  
  35          if (empty($quizobj->get_quiz()->timelimit) || $canignoretimelimits) {
  36              return null;
  37          }
  38  
  39          return new self($quizobj, $timenow);
  40      }
  41  
  42      public function description() {
  43          return get_string('quiztimelimit', 'quizaccess_timelimit',
  44                  format_time($this->quiz->timelimit));
  45      }
  46  
  47      public function end_time($attempt) {
  48          $timedue = $attempt->timestart + $this->quiz->timelimit;
  49          if ($this->quiz->timeclose) {
  50              $timedue = min($timedue, $this->quiz->timeclose);
  51          }
  52          return $timedue;
  53      }
  54  
  55      public function time_left_display($attempt, $timenow) {
  56          // If this is a teacher preview after the time limit expires, don't show the time_left
  57          $endtime = $this->end_time($attempt);
  58          if ($attempt->preview && $timenow > $endtime) {
  59              return false;
  60          }
  61          return $endtime - $timenow;
  62      }
  63  
  64      public function is_preflight_check_required($attemptid) {
  65          // Warning only required if the attempt is not already started.
  66          return $attemptid === null;
  67      }
  68  
  69      public function add_preflight_check_form_fields(preflight_check_form $quizform,
  70              MoodleQuickForm $mform, $attemptid) {
  71          $mform->addElement('header', 'honestycheckheader',
  72                  get_string('confirmstartheader', 'quizaccess_timelimit'));
  73          $mform->addElement('static', 'honestycheckmessage', '',
  74                  get_string('confirmstart', 'quizaccess_timelimit', format_time($this->quiz->timelimit)));
  75      }
  76  }