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