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.
   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   * The mod_quiz attempt auto-saved event.
  19   *
  20   * @package    mod_quiz
  21   * @copyright  2021 The Open University
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_quiz\event;
  26  
  27  /**
  28   * The mod_quiz attempt auto-saved event class.
  29   *
  30   * @property-read array $other {
  31   *      Extra information about event.
  32   *
  33   *      - int quizid: the id of the quiz.
  34   *      - int page: the page number of attempt.
  35   * }
  36   *
  37   * @package    mod_quiz
  38   * @copyright  2021 The Open University
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class attempt_autosaved extends \core\event\base {
  42  
  43      /**
  44       * Init method.
  45       */
  46      protected function init() {
  47          $this->data['objecttable'] = 'quiz_attempts';
  48          $this->data['crud'] = 'u';
  49          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
  50      }
  51  
  52      /**
  53       * Returns localised general event name.
  54       *
  55       * @return string
  56       */
  57      public static function get_name() {
  58          return get_string('eventattemptautosaved', 'mod_quiz');
  59      }
  60  
  61      /**
  62       * Returns description of what happened.
  63       *
  64       * @return string
  65       */
  66      public function get_description() {
  67          $pagenumber = $this->other['page'] + 1;
  68  
  69          return "The user with id '$this->userid' is working on page " .
  70              "'{$pagenumber}' of the attempt " .
  71              "with id '$this->objectid' for the quiz with course module id '$this->contextinstanceid', " .
  72              "and their latest responses have been saved automatically.";
  73      }
  74  
  75      /**
  76       * Returns relevant URL.
  77       *
  78       * @return \moodle_url
  79       */
  80      public function get_url() {
  81          return new \moodle_url('/mod/quiz/review.php', [
  82              'attempt' => $this->objectid,
  83              'page' => $this->other['page']
  84          ]);
  85      }
  86  
  87      /**
  88       * Custom validation.
  89       *
  90       * @throws \coding_exception
  91       * @return void
  92       */
  93      protected function validate_data() {
  94          parent::validate_data();
  95  
  96          if (!isset($this->relateduserid)) {
  97              throw new \coding_exception('The \'relateduserid\' must be set.');
  98          }
  99  
 100          if (!isset($this->other['quizid'])) {
 101              throw new \coding_exception('The \'quizid\' value must be set in other.');
 102          }
 103  
 104          if (!isset($this->other['page'])) {
 105              throw new \coding_exception('The \'page\' value must be set in other.');
 106          }
 107      }
 108  
 109      /**
 110       * This is used when restoring course logs where it is required that we
 111       * map the information in 'other' to it's new value in the new course.
 112       *
 113       * @return array List of mapping of other ids.
 114       */
 115      public static function get_objectid_mapping() {
 116          return ['db' => 'quiz_attempts', 'restore' => 'quiz_attempt'];
 117      }
 118  
 119      /**
 120       * This is used when restoring course logs where it is required that we
 121       * map the information in 'other' to it's new value in the new course.
 122       *
 123       * @return array List of mapping of other ids.
 124       */
 125      public static function get_other_mapping() {
 126          $othermapped = [];
 127          $othermapped['quizid'] = ['db' => 'quiz', 'restore' => 'quiz'];
 128  
 129          return $othermapped;
 130      }
 131  }