See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 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 mod_quiz\question\bank; 18 19 use core_question\local\bank\question_version_status; 20 use core_question\local\bank\random_question_loader; 21 use qubaid_condition; 22 23 defined('MOODLE_INTERNAL') || die(); 24 25 require_once($CFG->dirroot . '/mod/quiz/accessmanager.php'); 26 require_once($CFG->dirroot . '/mod/quiz/attemptlib.php'); 27 28 /** 29 * Helper class for question bank and its associated data. 30 * 31 * @package mod_quiz 32 * @category question 33 * @copyright 2021 Catalyst IT Australia Pty Ltd 34 * @author Safat Shahin <safatshahin@catalyst-au.net> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class qbank_helper { 38 39 /** 40 * Get the available versions of a question where one of the version has the given question id. 41 * 42 * @param int $questionid id of a question. 43 * @return \stdClass[] other versions of this question. Each object has fields versionid, 44 * version and questionid. Array is returned most recent version first. 45 */ 46 public static function get_version_options(int $questionid): array { 47 global $DB; 48 49 return $DB->get_records_sql(" 50 SELECT allversions.id AS versionid, 51 allversions.version, 52 allversions.questionid 53 54 FROM {question_versions} allversions 55 56 WHERE allversions.questionbankentryid = ( 57 SELECT givenversion.questionbankentryid 58 FROM {question_versions} givenversion 59 WHERE givenversion.questionid = ? 60 ) 61 AND allversions.status <> ? 62 63 ORDER BY allversions.version DESC 64 ", [$questionid, question_version_status::QUESTION_STATUS_DRAFT]); 65 } 66 67 /** 68 * Get the information about which questions should be used to create a quiz attempt. 69 * 70 * Each element in the returned array is indexed by slot.slot (slot number) an each object hass: 71 * - All the field of the slot table. 72 * - contextid for where the question(s) come from. 73 * - category id for where the questions come from. 74 * - For non-random questions, All the fields of the question table (but id is in questionid). 75 * Also question version and question bankentryid. 76 * - For random questions, filtercondition, which is also unpacked into category, randomrecurse, 77 * randomtags, and note that these also have a ->name set and ->qtype set to 'random'. 78 * 79 * @param int $quizid the id of the quiz to load the data for. 80 * @param \context_module $quizcontext the context of this quiz. 81 * @param int|null $slotid optional, if passed only load the data for this one slot (if it is in this quiz). 82 * @return array indexed by slot, with information about the content of each slot. 83 */ 84 public static function get_question_structure(int $quizid, \context_module $quizcontext, 85 int $slotid = null): array { 86 global $DB; 87 88 $params = [ 89 'draft' => question_version_status::QUESTION_STATUS_DRAFT, 90 'quizcontextid' => $quizcontext->id, 91 'quizcontextid2' => $quizcontext->id, 92 'quizcontextid3' => $quizcontext->id, 93 'quizid' => $quizid, 94 'quizid2' => $quizid, 95 ]; 96 $slotidtest = ''; 97 $slotidtest2 = ''; 98 if ($slotid !== null) { 99 $params['slotid'] = $slotid; 100 $params['slotid2'] = $slotid; 101 $slotidtest = ' AND slot.id = :slotid'; 102 $slotidtest2 = ' AND lslot.id = :slotid2'; 103 } 104 105 // Load all the data about each slot. 106 $slotdata = $DB->get_records_sql(" 107 SELECT slot.slot, 108 slot.id AS slotid, 109 slot.page, 110 slot.maxmark, 111 slot.requireprevious, 112 qsr.filtercondition, 113 qv.status, 114 qv.id AS versionid, 115 qv.version, 116 qr.version AS requestedversion, 117 qv.questionbankentryid, 118 q.id AS questionid, 119 q.*, 120 qc.id AS category, 121 COALESCE(qc.contextid, qsr.questionscontextid) AS contextid 122 123 FROM {quiz_slots} slot 124 125 -- case where a particular question has been added to the quiz. 126 LEFT JOIN {question_references} qr ON qr.usingcontextid = :quizcontextid AND qr.component = 'mod_quiz' 127 AND qr.questionarea = 'slot' AND qr.itemid = slot.id 128 LEFT JOIN {question_bank_entries} qbe ON qbe.id = qr.questionbankentryid 129 130 -- This way of getting the latest version for each slot is a bit more complicated 131 -- than we would like, but the simpler SQL did not work in Oracle 11.2. 132 -- (It did work fine in Oracle 19.x, so once we have updated our min supported 133 -- version we could consider digging the old code out of git history from 134 -- just before the commit that added this comment. 135 -- For relevant question_bank_entries, this gets the latest non-draft slot number. 136 LEFT JOIN ( 137 SELECT lv.questionbankentryid, 138 MAX(CASE WHEN lv.status <> :draft THEN lv.version END) AS usableversion, 139 MAX(lv.version) AS anyversion 140 FROM {quiz_slots} lslot 141 JOIN {question_references} lqr ON lqr.usingcontextid = :quizcontextid2 AND lqr.component = 'mod_quiz' 142 AND lqr.questionarea = 'slot' AND lqr.itemid = lslot.id 143 JOIN {question_versions} lv ON lv.questionbankentryid = lqr.questionbankentryid 144 WHERE lslot.quizid = :quizid2 145 $slotidtest2 146 AND lqr.version IS NULL 147 GROUP BY lv.questionbankentryid 148 ) latestversions ON latestversions.questionbankentryid = qr.questionbankentryid 149 150 LEFT JOIN {question_versions} qv ON qv.questionbankentryid = qbe.id 151 -- Either specified version, or latest usable version, or a draft version. 152 AND qv.version = COALESCE(qr.version, 153 latestversions.usableversion, 154 latestversions.anyversion) 155 LEFT JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid 156 LEFT JOIN {question} q ON q.id = qv.questionid 157 158 -- Case where a random question has been added. 159 LEFT JOIN {question_set_references} qsr ON qsr.usingcontextid = :quizcontextid3 AND qsr.component = 'mod_quiz' 160 AND qsr.questionarea = 'slot' AND qsr.itemid = slot.id 161 162 WHERE slot.quizid = :quizid 163 $slotidtest 164 165 ORDER BY slot.slot 166 ", $params); 167 168 // Unpack the random info from question_set_reference. 169 foreach ($slotdata as $slot) { 170 // Ensure the right id is the id. 171 $slot->id = $slot->slotid; 172 173 if ($slot->filtercondition) { 174 // Unpack the information about a random question. 175 $filtercondition = json_decode($slot->filtercondition); 176 $slot->questionid = 's' . $slot->id; // Sometimes this is used as an array key, so needs to be unique. 177 $slot->category = $filtercondition->questioncategoryid; 178 $slot->randomrecurse = (bool) $filtercondition->includingsubcategories; 179 $slot->randomtags = isset($filtercondition->tags) ? (array) $filtercondition->tags : []; 180 $slot->qtype = 'random'; 181 $slot->name = get_string('random', 'quiz'); 182 $slot->length = 1; 183 } else if ($slot->qtype === null) { 184 // This question must have gone missing. Put in a placeholder. 185 $slot->questionid = 's' . $slot->id; // Sometimes this is used as an array key, so needs to be unique. 186 $slot->category = 0; 187 $slot->qtype = 'missingtype'; 188 $slot->name = get_string('missingquestion', 'quiz'); 189 $slot->questiontext = ' '; 190 $slot->questiontextformat = FORMAT_HTML; 191 $slot->length = 1; 192 } else if (!\question_bank::qtype_exists($slot->qtype)) { 193 // Question of unknown type found in the database. Set to placeholder question types instead. 194 $slot->qtype = 'missingtype'; 195 } else { 196 $slot->_partiallyloaded = 1; 197 } 198 } 199 200 return $slotdata; 201 } 202 203 /** 204 * Get this list of random selection tag ids from one of the slots returned by get_question_structure. 205 * 206 * @param \stdClass $slotdata one of the array elements returned by get_question_structure. 207 * @return array list of tag ids. 208 */ 209 public static function get_tag_ids_for_slot(\stdClass $slotdata): array { 210 $tagids = []; 211 foreach ($slotdata->randomtags as $taginfo) { 212 [$id] = explode(',', $taginfo, 2); 213 $tagids[] = $id; 214 } 215 return $tagids; 216 } 217 218 /** 219 * Given a slot from the array returned by get_question_structure, describe the random question it represents. 220 * 221 * @param \stdClass $slotdata one of the array elements returned by get_question_structure. 222 * @return string that can be used to display the random slot. 223 */ 224 public static function describe_random_question(\stdClass $slotdata): string { 225 global $DB; 226 $category = $DB->get_record('question_categories', ['id' => $slotdata->category]); 227 return \question_bank::get_qtype('random')->question_name( 228 $category, $slotdata->randomrecurse, $slotdata->randomtags); 229 } 230 231 /** 232 * Choose question for redo in a particular slot. 233 * 234 * @param int $quizid the id of the quiz to load the data for. 235 * @param \context_module $quizcontext the context of this quiz. 236 * @param int $slotid optional, if passed only load the data for this one slot (if it is in this quiz). 237 * @param qubaid_condition $qubaids attempts to consider when avoiding picking repeats of random questions. 238 * @return int the id of the question to use. 239 */ 240 public static function choose_question_for_redo(int $quizid, \context_module $quizcontext, 241 int $slotid, qubaid_condition $qubaids): int { 242 $slotdata = self::get_question_structure($quizid, $quizcontext, $slotid); 243 $slotdata = reset($slotdata); 244 245 // Non-random question. 246 if ($slotdata->qtype != 'random') { 247 return $slotdata->questionid; 248 } 249 250 // Random question. 251 $randomloader = new random_question_loader($qubaids, []); 252 $newqusetionid = $randomloader->get_next_question_id($slotdata->category, 253 $slotdata->randomrecurse, self::get_tag_ids_for_slot($slotdata)); 254 255 if ($newqusetionid === null) { 256 throw new \moodle_exception('notenoughrandomquestions', 'quiz'); 257 } 258 return $newqusetionid; 259 } 260 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body