MDL-78983 core: Preserve default behaviour of flipping question icon
[moodle.git] / question / classes / question_reference_manager.php
blobef1d3c69896cfdc7584ba65b9b8ee344947e465e
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.
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/>.
17 namespace core_question;
19 use core_question\local\bank\question_version_status;
21 /**
22 * This class should provide an API for managing question_references.
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.
28 * @package core_question
29 * @copyright 2023 The Open University
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 class question_reference_manager {
33 /**
34 * Return a list of those questions from the list passed in, which are referenced.
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.
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.
44 public static function questions_with_references(array $questionids): array {
45 global $DB;
47 if (empty($questionids)) {
48 return [];
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');
54 return $DB->get_fieldset_sql("
55 SELECT qv.questionid
57 FROM {question_versions} qv
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
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)
74 WHERE qv.questionid $qidtest
75 ", array_merge($params, $lparams, ['draft' => question_version_status::QUESTION_STATUS_DRAFT]));