Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 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 core_question;
  18  
  19  use core_question\local\bank\question_version_status;
  20  
  21  /**
  22   * This class should provide an API for managing question_references.
  23   *
  24   * Unfortunately, question_references were introduced in the DB structure
  25   * without an nice API. This class is being added later, and is currently
  26   * terribly incomplete, but hopefully it can be improved in time.
  27   *
  28   * @package    core_question
  29   * @copyright  2023 The Open University
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class question_reference_manager {
  33      /**
  34       * Return a list of those questions from the list passed in, which are referenced.
  35       *
  36       * A question is referenced if either:
  37       * - There is a question_reference pointing at exactly that version of that question; or
  38       * - There is an 'always latest' reference, and the question id is the latest non-draft version
  39       *   of that question_bank_entry.
  40       *
  41       * @param array $questionids a list of question ids to check.
  42       * @return array a list of the question ids from the input array which are referenced.
  43       */
  44      public static function questions_with_references(array $questionids): array {
  45          global $DB;
  46  
  47          if (empty($questionids)) {
  48              return [];
  49          }
  50  
  51          [$qidtest, $params] = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED, 'outerqid');
  52          [$lqidtest, $lparams] = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED, 'innerqid');
  53  
  54          return $DB->get_fieldset_sql("
  55              SELECT qv.questionid
  56  
  57                FROM {question_versions} qv
  58  
  59           -- This is a performant to get the latest non-draft version for each
  60           -- question_bank_entry that relates to one of our questionids.
  61           LEFT JOIN (
  62                         SELECT lqv.questionbankentryid,
  63                                MAX(lv.version) AS latestusableversion
  64                           FROM {question_versions} lqv
  65                           JOIN {question_versions} lv ON lv.questionbankentryid = lqv.questionbankentryid
  66                          WHERE lqv.questionid $lqidtest
  67                            AND lv.status <> :draft
  68                       GROUP BY lqv.questionbankentryid
  69                     ) latestversions ON latestversions.questionbankentryid = qv.questionbankentryid
  70  
  71                JOIN {question_references} qr ON qr.questionbankentryid = qv.questionbankentryid
  72                         AND (qr.version = qv.version OR qr.version IS NULL AND qv.version = latestversions.latestusableversion)
  73  
  74               WHERE qv.questionid $qidtest
  75              ", array_merge($params, $lparams, ['draft' => question_version_status::QUESTION_STATUS_DRAFT]));
  76      }
  77  }