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  
  21  /**
  22   * Track and display question version information.
  23   *
  24   * This class handles rendering the question version information (the current version of the question, the total number of versions,
  25   * and if the current version is the latest). It also tracks loaded question definitions that don't yet have the latest version
  26   * loaded, and handles loading the latest version of all pending questions.
  27   *
  28   * @package   core_question
  29   * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
  30   * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class question_version_info implements \renderable, \templatable {
  34  
  35      /**
  36       * @var array List of definitions that don't know whether they are the latest version yet.
  37       */
  38      public static array $pendingdefinitions = [];
  39  
  40      /**
  41       * @var int $version The current version number.
  42       */
  43      public int $version;
  44  
  45      /**
  46       * @var ?int $latestversion The latest version number of this question.
  47       */
  48      public ?int $latestversion;
  49  
  50      /**
  51       * @var bool $shortversion Are we displaying an abbreviation for "version" rather than the full word?
  52       */
  53      protected bool $shortversion;
  54  
  55      /**
  56       * Store the current and latest versions of the question, and whether we want to abbreviate the output string.
  57       *
  58       * @param \question_definition $question
  59       * @param bool $shortversion
  60       */
  61      public function __construct(\question_definition $question, bool $shortversion = false) {
  62          $this->version = $question->version;
  63          $this->latestversion = $question->latestversion;
  64          $this->shortversion = $shortversion;
  65      }
  66  
  67      /**
  68       * Find and set the latest version of all pending question_definition objects.
  69       *
  70       * This will update all pending objects in one go, saving us having to do a query for each question.
  71       *
  72       * @return void
  73       */
  74      public static function populate_latest_versions(): void {
  75          global $DB;
  76          $pendingentryids = array_map(fn($definition) => $definition->questionbankentryid, self::$pendingdefinitions);
  77          [$insql, $params] = $DB->get_in_or_equal($pendingentryids);
  78  
  79          $sql = "SELECT questionbankentryid, MAX(version) AS latestversion
  80                        FROM {question_versions}
  81                       WHERE questionbankentryid $insql
  82                    GROUP BY questionbankentryid";
  83          $latestversions = $DB->get_records_sql_menu($sql, $params);
  84          array_walk(self::$pendingdefinitions, function($definition) use ($latestversions) {
  85              if (!isset($latestversions[$definition->questionbankentryid])) {
  86                  return;
  87              }
  88              $definition->set_latest_version($latestversions[$definition->questionbankentryid]);
  89              unset(self::$pendingdefinitions[$definition->id]);
  90          });
  91      }
  92  
  93      /**
  94       * Return the question version info as a string, including the version number and whether this is the latest version.
  95       *
  96       * @param renderer_base $output
  97       * @return array
  98       * @throws \coding_exception
  99       */
 100      public function export_for_template(renderer_base $output): array {
 101          if (is_null($this->latestversion)) {
 102              return [];
 103          }
 104          $identifier = 'versioninfo';
 105          if ($this->version === $this->latestversion) {
 106              $identifier .= 'latest';
 107          }
 108          if ($this->shortversion) {
 109              $identifier = 'short' . $identifier;
 110          }
 111          return [
 112              'versioninfo' => get_string($identifier, 'question', $this)
 113          ];
 114      }
 115  }