Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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  namespace core_question\output;
  18  
  19  use renderer_base;
  20  use templatable;
  21  use renderable;
  22  use question_bank;
  23  
  24  require_once($CFG->dirroot . '/question/engine/bank.php');
  25  
  26  /**
  27   * A UI widget to select other versions of a particular question.
  28   *
  29   * It will help plugins to enable version selection in locations like modal, page etc.
  30   *
  31   * @package    core_question
  32   * @copyright  2022 Catalyst IT Australia Pty Ltd
  33   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class question_version_selection implements templatable, renderable {
  37  
  38      /** @var string */
  39      private $uniqueidentifier;
  40  
  41      /** @var int */
  42      private $currentselectedquestionid = null;
  43  
  44      /**
  45       * Constructor.
  46       *
  47       * @param string $uniqueidentifier unique identifier for the api usage.
  48       * @param int $currentlyselectedquestionid selected question id in dropdown.
  49       */
  50      protected function __construct(string $uniqueidentifier, int $currentlyselectedquestionid) {
  51          $this->uniqueidentifier = $uniqueidentifier;
  52          $this->currentselectedquestionid = $currentlyselectedquestionid;
  53      }
  54  
  55      /**
  56       * Set the selected question id for the currently selected question.
  57       *
  58       * @param string $uniqueidentifier unique identifier for the api usage.
  59       * @param int $currentlyselectedquestionid selected question id in dropdown.
  60       * @return self an instance of this UI widget for the given question.
  61       */
  62      public static function make_for_question(string $uniqueidentifier, int $currentlyselectedquestionid): self {
  63          return new self($uniqueidentifier, $currentlyselectedquestionid);
  64      }
  65  
  66      /**
  67       * Export the data for version selection mustache.
  68       *
  69       * @param renderer_base $output renderer of the output
  70       * @return array
  71       */
  72      public function export_for_template(renderer_base $output): array {
  73          $displaydata = [];
  74          $versionsoptions = question_bank::get_all_versions_of_question($this->currentselectedquestionid);
  75          foreach ($versionsoptions as $versionsoption) {
  76              $versionsoption->selected = false;
  77              $a = new \stdClass();
  78              $a->version = $versionsoption->version;
  79              $versionsoption->name = get_string('version_selection', 'core_question', $a);
  80              if ($versionsoption->questionid == $this->currentselectedquestionid) {
  81                  $versionsoption->selected = true;
  82              }
  83              $displaydata[] = $versionsoption;
  84          }
  85  
  86          return [
  87              'options' => $displaydata,
  88              'uniqueidentifier' => $this->uniqueidentifier,
  89          ];
  90      }
  91  }