MDL-20636 More progress.
[moodle.git] / question / engine / bank.php
CommitLineData
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 */
41abstract 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
06f8ed54
TH
80 /**
81 * @param string $qtypename the internal name of a question type. For example multichoice.
82 * @return boolean whether users are allowed to create questions of this type.
83 */
84 public static function qtype_enabled($qtypename) {
85 ;
86 }
87
d1b7e03d
TH
88 /**
89 * @param $qtypename the internal name of a question type, for example multichoice.
90 * @return string the human_readable name of this question type, from the language pack.
91 */
92 public static function get_qtype_name($qtypename) {
93 return self::get_qtype($qtypename)->menu_name();
94 }
95
96 /**
97 * @return array all the installed question types.
98 */
99 public static function get_all_qtypes() {
100 $qtypes = array();
101 $plugins = get_list_of_plugins('question/type', 'datasetdependent');
102 foreach ($plugins as $plugin) {
103 $qtypes[$plugin] = self::get_qtype($plugin);
104 }
105 return $qtypes;
106 }
107
108 /**
109 * Load the question definition class(es) belonging to a question type. That is,
110 * include_once('/question/type/' . $qtypename . '/question.php'), with a bit
111 * of checking.
112 * @param string $qtypename the question type name. For example 'multichoice' or 'shortanswer'.
113 */
114 public static function load_question_definition_classes($qtypename) {
115 global $CFG;
116 if (isset(self::$loadedqdefs[$qtypename])) {
117 return;
118 }
119 $file = $CFG->dirroot . '/question/type/' . $qtypename . '/question.php';
120 if (!is_readable($file)) {
121 throw new Exception('Unknown question type (no definition) ' . $qtypename);
122 }
123 include_once($file);
124 self::$loadedqdefs[$qtypename] = 1;
125 }
126
127 /**
128 * Load a question definition from the database. The object returned
129 * will actually be of an appropriate {@link question_definition} subclass.
130 * @param integer $questionid the id of the question to load.
131 * @return question_definition loaded from the database.
132 */
133 public static function load_question($questionid) {
134 if (self::$testmode) {
135 // Evil, test code in production, but now way round it.
136 return self::return_test_question_data($questionid);
137 }
138
139 $questiondata = get_record('question', 'id', $questionid);
140 if (empty($questiondata)) {
141 throw new Exception('Unknown question id ' . $questionid);
142 }
143 get_question_options($questiondata);
144 return self::make_question($questiondata);
145 }
146
147 /**
148 * Convert the question information loaded with {@link get_question_options()}
149 * to a question_definintion object.
150 * @param object $questiondata raw data loaded from the database.
151 * @return question_definition loaded from the database.
152 */
153 public static function make_question($questiondata) {
154 return self::get_qtype($questiondata->qtype, false)->make_question($questiondata, false);
155 }
156
157 /**
158 * @return question_finder a question finder.
159 */
160 public static function get_finder() {
161 if (is_null(self::$questionfinder)) {
162 self::$questionfinder = new question_finder();
163 }
164 return self::$questionfinder;
165 }
166
167 /**
168 * Only to be called from unit tests. Allows {@link load_test_data()} to be used.
169 */
170 public static function start_unit_test() {
171 self::$testmode = true;
172 }
173
174 /**
175 * Only to be called from unit tests. Allows {@link load_test_data()} to be used.
176 */
177 public static function end_unit_test() {
178 self::$testmode = false;
179 self::$testdata = array();
180 }
181
182 private static function return_test_question_data($questionid) {
183 if (!isset(self::$testdata[$questionid])) {
184 throw new Exception('question_bank::return_test_data(' . $questionid .
185 ') called, but no matching question has been loaded by load_test_data.');
186 }
187 return self::$testdata[$questionid];
188 }
189
190 /**
191 * To be used for unit testing only. Will throw an exception if
192 * {@link start_unit_test()} has not been called first.
193 * @param object $questiondata a question data object to put in the test data store.
194 */
195 public static function load_test_question_data(question_definition $question) {
196 if (!self::$testmode) {
197 throw new Exception('question_bank::load_test_data called when not in test mode.');
198 }
199 self::$testdata[$question->id] = $question;
200 }
201}
202
203class question_finder {
204 /**
205 * Get the ids of all the questions in a list of categoryies.
206 * @param integer|string|array $categoryids either a categoryid, or a comma-separated list
207 * category ids, or an array of them.
208 * @param string $extraconditions extra conditions to AND with the rest of the where clause.
209 * @return array questionid => questionid.
210 */
211 public function get_questions_from_categories($categoryids, $extraconditions) {
212 if (is_array($categoryids)) {
213 $categoryids = implode(',', $categoryids);
214 }
215
216 if ($extraconditions) {
217 $extraconditions = ' AND (' . $extraconditions . ')';
218 }
219 $questionids = get_records_select_menu('question',
220 "category IN ($categoryids)
221 AND parent = 0
222 AND hidden = 0
223 $extraconditions", '', 'id,id AS id2');
224 if (!$questionids) {
225 $questionids = array();
226 }
227 return $questionids;
228 }
229}