Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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_openclosedate plugin.
  19   *
  20   * @package    quizaccess
  21   * @subpackage openclosedate
  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 enforcing open and close dates.
  34   *
  35   * @copyright  2009 Tim Hunt
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class quizaccess_openclosedate extends quiz_access_rule_base {
  39  
  40      public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
  41          // This rule is always used, even if the quiz has no open or close date.
  42          return new self($quizobj, $timenow);
  43      }
  44  
  45      public function description() {
  46          $result = array();
  47          if ($this->timenow < $this->quiz->timeopen) {
  48              $result[] = get_string('quiznotavailable', 'quizaccess_openclosedate',
  49                      userdate($this->quiz->timeopen));
  50              if ($this->quiz->timeclose) {
  51                  $result[] = get_string('quizcloseson', 'quiz', userdate($this->quiz->timeclose));
  52              }
  53  
  54          } else if ($this->quiz->timeclose && $this->timenow > $this->quiz->timeclose) {
  55              $result[] = get_string('quizclosed', 'quiz', userdate($this->quiz->timeclose));
  56  
  57          } else {
  58              if ($this->quiz->timeopen) {
  59                  $result[] = get_string('quizopenedon', 'quiz', userdate($this->quiz->timeopen));
  60              }
  61              if ($this->quiz->timeclose) {
  62                  $result[] = get_string('quizcloseson', 'quiz', userdate($this->quiz->timeclose));
  63              }
  64          }
  65  
  66          return $result;
  67      }
  68  
  69      public function prevent_access() {
  70          $message = get_string('notavailable', 'quizaccess_openclosedate');
  71  
  72          if ($this->timenow < $this->quiz->timeopen) {
  73              return $message;
  74          }
  75  
  76          if (!$this->quiz->timeclose) {
  77              return false;
  78          }
  79  
  80          if ($this->timenow <= $this->quiz->timeclose) {
  81              return false;
  82          }
  83  
  84          if ($this->quiz->overduehandling != 'graceperiod') {
  85              return $message;
  86          }
  87  
  88          if ($this->timenow <= $this->quiz->timeclose + $this->quiz->graceperiod) {
  89              return false;
  90          }
  91  
  92          return $message;
  93      }
  94  
  95      public function is_finished($numprevattempts, $lastattempt) {
  96          return $this->quiz->timeclose && $this->timenow > $this->quiz->timeclose;
  97      }
  98  
  99      public function end_time($attempt) {
 100          if ($this->quiz->timeclose) {
 101              return $this->quiz->timeclose;
 102          }
 103          return false;
 104      }
 105  
 106      public function time_left_display($attempt, $timenow) {
 107          // If this is a teacher preview after the close date, do not show
 108          // the time.
 109          if ($attempt->preview && $timenow > $this->quiz->timeclose) {
 110              return false;
 111          }
 112          // Otherwise, return to the time left until the close date, providing that is
 113          // less than QUIZ_SHOW_TIME_BEFORE_DEADLINE.
 114          $endtime = $this->end_time($attempt);
 115          if ($endtime !== false && $timenow > $endtime - QUIZ_SHOW_TIME_BEFORE_DEADLINE) {
 116              return $endtime - $timenow;
 117          }
 118          return false;
 119      }
 120  }