Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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