Commit | Line | Data |
---|---|---|
d1b7e03d TH |
1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | ||
19 | /** | |
20 | * More object oriented wrappers around parts of the Moodle question bank. | |
21 | * | |
22 | * In due course, I expect that the question bank will be converted to a | |
23 | * fully object oriented structure, at which point this file can be a | |
24 | * starting point. | |
25 | * | |
26 | * @package moodlecore | |
27 | * @subpackage questionbank | |
28 | * @copyright 2009 The Open University | |
29 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
30 | */ | |
31 | ||
32 | ||
33 | /** | |
34 | * This static class provides access to the other question bank. | |
35 | * | |
36 | * It provides functions for managing question types and question definitions. | |
37 | * | |
38 | * @copyright 2009 The Open University | |
39 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
40 | */ | |
41 | abstract class question_bank { | |
42 | /** @var array question type name => question_type subclass. */ | |
43 | private static $questiontypes = array(); | |
44 | ||
45 | /** @var array question type name => 1. Records which question definitions have been loaded. */ | |
46 | private static $loadedqdefs = array(); | |
47 | ||
48 | protected static $questionfinder = null; | |
49 | ||
50 | /** @var boolean nasty hack to allow unit tests to call {@link load_question()}. */ | |
51 | private static $testmode = false; | |
52 | private static $testdata = array(); | |
53 | ||
54 | /** | |
55 | * Get the question type class for a particular question type. | |
56 | * @param string $qtypename the question type name. For example 'multichoice' or 'shortanswer'. | |
57 | * @param boolean $mustexist if false, the missing question type is returned when | |
58 | * the requested question type is not installed. | |
59 | * @return question_type the corresponding question type class. | |
60 | */ | |
61 | public static function get_qtype($qtypename, $mustexist = true) { | |
62 | global $CFG; | |
63 | if (isset(self::$questiontypes[$qtypename])) { | |
64 | return self::$questiontypes[$qtypename]; | |
65 | } | |
66 | $file = $CFG->dirroot . '/question/type/' . $qtypename . '/questiontype.php'; | |
67 | if (!is_readable($file)) { | |
68 | if ($mustexist || $qtypename == 'missingtype') { | |
69 | throw new Exception('Unknown question type ' . $qtypename); | |
70 | } else { | |
71 | return self::get_qtype('missingtype'); | |
72 | } | |
73 | } | |
74 | include_once($file); | |
75 | $class = 'qtype_' . $qtypename; | |
76 | self::$questiontypes[$qtypename] = new $class(); | |
77 | return self::$questiontypes[$qtypename]; | |
78 | } | |
79 | ||
80 | /** | |
81 | * @param $qtypename the internal name of a question type, for example multichoice. | |
82 | * @return string the human_readable name of this question type, from the language pack. | |
83 | */ | |
84 | public static function get_qtype_name($qtypename) { | |
85 | return self::get_qtype($qtypename)->menu_name(); | |
86 | } | |
87 | ||
88 | /** | |
89 | * @return array all the installed question types. | |
90 | */ | |
91 | public static function get_all_qtypes() { | |
92 | $qtypes = array(); | |
93 | $plugins = get_list_of_plugins('question/type', 'datasetdependent'); | |
94 | foreach ($plugins as $plugin) { | |
95 | $qtypes[$plugin] = self::get_qtype($plugin); | |
96 | } | |
97 | return $qtypes; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Load the question definition class(es) belonging to a question type. That is, | |
102 | * include_once('/question/type/' . $qtypename . '/question.php'), with a bit | |
103 | * of checking. | |
104 | * @param string $qtypename the question type name. For example 'multichoice' or 'shortanswer'. | |
105 | */ | |
106 | public static function load_question_definition_classes($qtypename) { | |
107 | global $CFG; | |
108 | if (isset(self::$loadedqdefs[$qtypename])) { | |
109 | return; | |
110 | } | |
111 | $file = $CFG->dirroot . '/question/type/' . $qtypename . '/question.php'; | |
112 | if (!is_readable($file)) { | |
113 | throw new Exception('Unknown question type (no definition) ' . $qtypename); | |
114 | } | |
115 | include_once($file); | |
116 | self::$loadedqdefs[$qtypename] = 1; | |
117 | } | |
118 | ||
119 | /** | |
120 | * Load a question definition from the database. The object returned | |
121 | * will actually be of an appropriate {@link question_definition} subclass. | |
122 | * @param integer $questionid the id of the question to load. | |
123 | * @return question_definition loaded from the database. | |
124 | */ | |
125 | public static function load_question($questionid) { | |
126 | if (self::$testmode) { | |
127 | // Evil, test code in production, but now way round it. | |
128 | return self::return_test_question_data($questionid); | |
129 | } | |
130 | ||
131 | $questiondata = get_record('question', 'id', $questionid); | |
132 | if (empty($questiondata)) { | |
133 | throw new Exception('Unknown question id ' . $questionid); | |
134 | } | |
135 | get_question_options($questiondata); | |
136 | return self::make_question($questiondata); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Convert the question information loaded with {@link get_question_options()} | |
141 | * to a question_definintion object. | |
142 | * @param object $questiondata raw data loaded from the database. | |
143 | * @return question_definition loaded from the database. | |
144 | */ | |
145 | public static function make_question($questiondata) { | |
146 | return self::get_qtype($questiondata->qtype, false)->make_question($questiondata, false); | |
147 | } | |
148 | ||
149 | /** | |
150 | * @return question_finder a question finder. | |
151 | */ | |
152 | public static function get_finder() { | |
153 | if (is_null(self::$questionfinder)) { | |
154 | self::$questionfinder = new question_finder(); | |
155 | } | |
156 | return self::$questionfinder; | |
157 | } | |
158 | ||
159 | /** | |
160 | * Only to be called from unit tests. Allows {@link load_test_data()} to be used. | |
161 | */ | |
162 | public static function start_unit_test() { | |
163 | self::$testmode = true; | |
164 | } | |
165 | ||
166 | /** | |
167 | * Only to be called from unit tests. Allows {@link load_test_data()} to be used. | |
168 | */ | |
169 | public static function end_unit_test() { | |
170 | self::$testmode = false; | |
171 | self::$testdata = array(); | |
172 | } | |
173 | ||
174 | private static function return_test_question_data($questionid) { | |
175 | if (!isset(self::$testdata[$questionid])) { | |
176 | throw new Exception('question_bank::return_test_data(' . $questionid . | |
177 | ') called, but no matching question has been loaded by load_test_data.'); | |
178 | } | |
179 | return self::$testdata[$questionid]; | |
180 | } | |
181 | ||
182 | /** | |
183 | * To be used for unit testing only. Will throw an exception if | |
184 | * {@link start_unit_test()} has not been called first. | |
185 | * @param object $questiondata a question data object to put in the test data store. | |
186 | */ | |
187 | public static function load_test_question_data(question_definition $question) { | |
188 | if (!self::$testmode) { | |
189 | throw new Exception('question_bank::load_test_data called when not in test mode.'); | |
190 | } | |
191 | self::$testdata[$question->id] = $question; | |
192 | } | |
193 | } | |
194 | ||
195 | class question_finder { | |
196 | /** | |
197 | * Get the ids of all the questions in a list of categoryies. | |
198 | * @param integer|string|array $categoryids either a categoryid, or a comma-separated list | |
199 | * category ids, or an array of them. | |
200 | * @param string $extraconditions extra conditions to AND with the rest of the where clause. | |
201 | * @return array questionid => questionid. | |
202 | */ | |
203 | public function get_questions_from_categories($categoryids, $extraconditions) { | |
204 | if (is_array($categoryids)) { | |
205 | $categoryids = implode(',', $categoryids); | |
206 | } | |
207 | ||
208 | if ($extraconditions) { | |
209 | $extraconditions = ' AND (' . $extraconditions . ')'; | |
210 | } | |
211 | $questionids = get_records_select_menu('question', | |
212 | "category IN ($categoryids) | |
213 | AND parent = 0 | |
214 | AND hidden = 0 | |
215 | $extraconditions", '', 'id,id AS id2'); | |
216 | if (!$questionids) { | |
217 | $questionids = array(); | |
218 | } | |
219 | return $questionids; | |
220 | } | |
221 | } |