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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [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  namespace mod_quiz\local\structure;
  18  
  19  /**
  20   * Class slot_random, represents a random question slot type.
  21   *
  22   * @package    mod_quiz
  23   * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
  24   * @author     2021 Safat Shahin <safatshahin@catalyst-au.net>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class slot_random {
  28  
  29      /** @var \stdClass Slot's properties. A record retrieved from the quiz_slots table. */
  30      protected $record;
  31  
  32      /**
  33       * @var \stdClass set reference record
  34       */
  35      protected $referencerecord;
  36  
  37      /**
  38       * @var \stdClass The quiz this question slot belongs to.
  39       */
  40      protected $quiz = null;
  41  
  42      /**
  43       * @var \core_tag_tag[] List of tags for this slot.
  44       */
  45      protected $tags = [];
  46  
  47      /**
  48       * @var string filter condition
  49       */
  50      protected $filtercondition = null;
  51  
  52      /**
  53       * slot_random constructor.
  54       *
  55       * @param \stdClass $slotrecord Represents a record in the quiz_slots table.
  56       */
  57      public function __construct($slotrecord = null) {
  58          $this->record = new \stdClass();
  59          $this->referencerecord = new \stdClass();
  60  
  61          $slotproperties = ['id', 'slot', 'quizid', 'page', 'requireprevious', 'maxmark'];
  62          $setreferenceproperties = ['usingcontextid', 'questionscontextid'];
  63  
  64          foreach ($slotproperties as $property) {
  65              if (isset($slotrecord->$property)) {
  66                  $this->record->$property = $slotrecord->$property;
  67              }
  68          }
  69  
  70          foreach ($setreferenceproperties as $referenceproperty) {
  71              if (isset($slotrecord->$referenceproperty)) {
  72                  $this->referencerecord->$referenceproperty = $slotrecord->$referenceproperty;
  73              }
  74          }
  75      }
  76  
  77      /**
  78       * Returns the quiz for this question slot.
  79       * The quiz is fetched the first time it is requested and then stored in a member variable to be returned each subsequent time.
  80       *
  81       * @return mixed
  82       * @throws \coding_exception
  83       */
  84      public function get_quiz() {
  85          global $DB;
  86  
  87          if (empty($this->quiz)) {
  88              if (empty($this->record->quizid)) {
  89                  throw new \coding_exception('quizid is not set.');
  90              }
  91              $this->quiz = $DB->get_record('quiz', array('id' => $this->record->quizid));
  92          }
  93  
  94          return $this->quiz;
  95      }
  96  
  97      /**
  98       * Sets the quiz object for the quiz slot.
  99       * It is not mandatory to set the quiz as the quiz slot can fetch it the first time it is accessed,
 100       * however it helps with the performance to set the quiz if you already have it.
 101       *
 102       * @param \stdClass $quiz The qui object.
 103       */
 104      public function set_quiz($quiz) {
 105          $this->quiz = $quiz;
 106          $this->record->quizid = $quiz->id;
 107      }
 108  
 109      /**
 110       * Set some tags for this quiz slot.
 111       *
 112       * @param \core_tag_tag[] $tags
 113       */
 114      public function set_tags($tags) {
 115          $this->tags = [];
 116          foreach ($tags as $tag) {
 117              // We use $tag->id as the key for the array so not only it handles duplicates of the same tag being given,
 118              // but also it is consistent with the behaviour of set_tags_by_id() below.
 119              $this->tags[$tag->id] = $tag;
 120          }
 121      }
 122  
 123      /**
 124       * Set some tags for this quiz slot. This function uses tag ids to find tags.
 125       *
 126       * @param int[] $tagids
 127       */
 128      public function set_tags_by_id($tagids) {
 129          $this->tags = \core_tag_tag::get_bulk($tagids, 'id, name');
 130      }
 131  
 132      /**
 133       * Set filter condition.
 134       *
 135       * @param \stdClass $filters
 136       */
 137      public function set_filter_condition($filters) {
 138          if (!empty($this->tags)) {
 139              $filters->tags = $this->tags;
 140          }
 141  
 142          $this->filtercondition = json_encode($filters);
 143      }
 144  
 145      /**
 146       * Inserts the quiz slot at the $page page.
 147       * It is required to call this function if you are building a quiz slot object from scratch.
 148       *
 149       * @param int $page The page that this slot will be inserted at.
 150       */
 151      public function insert($page) {
 152          global $DB;
 153  
 154          $slots = $DB->get_records('quiz_slots', array('quizid' => $this->record->quizid),
 155                  'slot', 'id, slot, page');
 156          $quiz = $this->get_quiz();
 157  
 158          $trans = $DB->start_delegated_transaction();
 159  
 160          $maxpage = 1;
 161          $numonlastpage = 0;
 162          foreach ($slots as $slot) {
 163              if ($slot->page > $maxpage) {
 164                  $maxpage = $slot->page;
 165                  $numonlastpage = 1;
 166              } else {
 167                  $numonlastpage += 1;
 168              }
 169          }
 170  
 171          if (is_int($page) && $page >= 1) {
 172              // Adding on a given page.
 173              $lastslotbefore = 0;
 174              foreach (array_reverse($slots) as $otherslot) {
 175                  if ($otherslot->page > $page) {
 176                      $DB->set_field('quiz_slots', 'slot', $otherslot->slot + 1, array('id' => $otherslot->id));
 177                  } else {
 178                      $lastslotbefore = $otherslot->slot;
 179                      break;
 180                  }
 181              }
 182              $this->record->slot = $lastslotbefore + 1;
 183              $this->record->page = min($page, $maxpage + 1);
 184  
 185              quiz_update_section_firstslots($this->record->quizid, 1, max($lastslotbefore, 1));
 186          } else {
 187              $lastslot = end($slots);
 188              if ($lastslot) {
 189                  $this->record->slot = $lastslot->slot + 1;
 190              } else {
 191                  $this->record->slot = 1;
 192              }
 193              if ($quiz->questionsperpage && $numonlastpage >= $quiz->questionsperpage) {
 194                  $this->record->page = $maxpage + 1;
 195              } else {
 196                  $this->record->page = $maxpage;
 197              }
 198          }
 199  
 200          $this->record->id = $DB->insert_record('quiz_slots', $this->record);
 201  
 202          $this->referencerecord->component = 'mod_quiz';
 203          $this->referencerecord->questionarea = 'slot';
 204          $this->referencerecord->itemid = $this->record->id;
 205          $this->referencerecord->filtercondition = $this->filtercondition;
 206          $DB->insert_record('question_set_references', $this->referencerecord);
 207  
 208          $trans->allow_commit();
 209  
 210          // Log slot created event.
 211          $cm = get_coursemodule_from_instance('quiz', $quiz->id);
 212          $event = \mod_quiz\event\slot_created::create([
 213              'context' => \context_module::instance($cm->id),
 214              'objectid' => $this->record->id,
 215              'other' => [
 216                  'quizid' => $quiz->id,
 217                  'slotnumber' => $this->record->slot,
 218                  'page' => $this->record->page
 219              ]
 220          ]);
 221          $event->trigger();
 222      }
 223  }