mirrored from git://git.moodle.org/moodle.git
/
questiontype.php
265 lines (232 loc) · 10.5 KB
/
questiontype.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Question type class for the random question type.
*
* @package qtype
* @subpackage random
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/question/type/questiontypebase.php');
/**
* The random question type.
*
* This question type does not have a question definition class, nor any
* renderers. When you load a question of this type, it actually loads a
* question chosen randomly from a particular category in the question bank.
*
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_random extends question_type {
/** @var string comma-separated list of qytpe names not to select, can be used in SQL. */
protected $excludedqtypes = null;
/** @var string comma-separated list of manually graded qytpe names, can be used in SQL. */
protected $manualqtypes = null;
/**
* Cache of availabe question ids from a particular category.
* @var array two-dimensional array. The first key is a category id, the
* second key is wether subcategories should be included.
*/
private $availablequestionsbycategory = array();
public function menu_name() {
// Don't include this question type in the 'add new question' menu.
return false;
}
public function is_manual_graded() {
return true;
}
public function is_usable_by_random() {
return false;
}
public function is_question_manual_graded($question, $otherquestionsinuse) {
global $DB;
// We take our best shot at working whether a particular question is manually
// graded follows: We look to see if any of the questions that this random
// question might select if of a manually graded type. If a category contains
// a mixture of manual and non-manual questions, and if all the attempts so
// far selected non-manual ones, this will give the wrong answer, but we
// don't care. Even so, this is an expensive calculation!
$this->init_qtype_lists();
if (!$this->manualqtypes) {
return false;
}
if ($question->questiontext) {
$categorylist = question_categorylist($question->category);
} else {
$categorylist = array($question->category);
}
list($qcsql, $qcparams) = $DB->get_in_or_equal($categorylist);
// TODO use in_or_equal for $otherquestionsinuse and $this->manualqtypes.
return $DB->record_exists_select('question',
"category $qcsql
AND parent = 0
AND hidden = 0
AND id NOT IN ($otherquestionsinuse)
AND qtype IN ($this->manualqtypes)", $qcparams);
}
/**
* This method needs to be called before the ->excludedqtypes and
* ->manualqtypes fields can be used.
*/
protected function init_qtype_lists() {
if (!is_null($this->excludedqtypes)) {
return; // Already done.
}
$excludedqtypes = array();
$manualqtypes = array();
foreach (question_bank::get_all_qtypes() as $qtype) {
$quotedname = "'" . $qtype->name() . "'";
if (!$qtype->is_usable_by_random()) {
$excludedqtypes[] = $quotedname;
} else if ($qtype->is_manual_graded()) {
$manualqtypes[] = $quotedname;
}
}
$this->excludedqtypes = implode(',', $excludedqtypes);
$this->manualqtypes = implode(',', $manualqtypes);
}
public function display_question_editing_page($mform, $question, $wizardnow) {
global $OUTPUT;
$heading = $this->get_heading(empty($question->id));
echo $OUTPUT->heading_with_help($heading, 'pluginname', $this->plugin_name());
$mform->display();
}
public function get_question_options($question) {
return true;
}
/**
* Random questions always get a question name that is Random (cateogryname).
* This function is a centralised place to calculate that, given the category.
* @param object $category the category this question picks from. (Only ->name is used.)
* @param bool $includesubcategories whether this question also picks from subcategories.
* @return string the name this question should have.
*/
public function question_name($category, $includesubcategories) {
if ($includesubcategories) {
$string = 'randomqplusname';
} else {
$string = 'randomqname';
}
return get_string($string, 'qtype_random', shorten_text($category->name, 100));
}
protected function set_selected_question_name($question, $randomname) {
$a = new stdClass();
$a->randomname = $randomname;
$a->questionname = $question->name;
$question->name = get_string('selectedby', 'qtype_random', $a);
}
public function save_question($question, $form) {
$form->name = '';
// In case someone set the question text to true/false in the old style, set it properly.
if ($form->questiontext['text']) {
$form->questiontext['text'] = '1';
} else {
$form->questiontext['text'] = '0';
}
$form->tags = array();
// Name is not a required field for random questions, but
// parent::save_question Assumes that it is.
return parent::save_question($question, $form);
}
public function save_question_options($question) {
global $DB;
// No options, as such, but we set the parent field to the question's
// own id. Setting the parent field has the effect of hiding this
// question in various places.
$updateobject = new stdClass();
$updateobject->id = $question->id;
$updateobject->parent = $question->id;
// We also force the question name to be 'Random (categoryname)'.
$category = $DB->get_record('question_categories',
array('id' => $question->category), '*', MUST_EXIST);
$updateobject->name = $this->question_name($category, !empty($question->questiontext));
return $DB->update_record('question', $updateobject);
}
/**
* During unit tests we need to be able to reset all caches so that each new test starts in a known state.
* Intended for use only for testing. This is a stop gap until we start using the MUC caching api here.
* You need to call this before every test that loads one or more random questions.
*/
public function clear_caches_before_testing() {
$this->availablequestionsbycategory = array();
}
/**
* Get all the usable questions from a particular question category.
*
* @param int $categoryid the id of a question category.
* @param bool whether to include questions from subcategories.
* @param string $questionsinuse comma-separated list of question ids to
* exclude from consideration.
* @return array of question records.
*/
public function get_available_questions_from_category($categoryid, $subcategories) {
if (isset($this->availablequestionsbycategory[$categoryid][$subcategories])) {
return $this->availablequestionsbycategory[$categoryid][$subcategories];
}
$this->init_qtype_lists();
if ($subcategories) {
$categoryids = question_categorylist($categoryid);
} else {
$categoryids = array($categoryid);
}
$questionids = question_bank::get_finder()->get_questions_from_categories(
$categoryids, 'qtype NOT IN (' . $this->excludedqtypes . ')');
$this->availablequestionsbycategory[$categoryid][$subcategories] = $questionids;
return $questionids;
}
public function make_question($questiondata) {
return $this->choose_other_question($questiondata, array());
}
/**
* Load the definition of another question picked randomly by this question.
* @param object $questiondata the data defining a random question.
* @param array $excludedquestions of question ids. We will no pick any question whose id is in this list.
* @param bool $allowshuffle if false, then any shuffle option on the selected quetsion is disabled.
* @param null|integer $forcequestionid if not null then force the picking of question with id $forcequestionid.
* @throws coding_exception
* @return question_definition|null the definition of the question that was
* selected, or null if no suitable question could be found.
*/
public function choose_other_question($questiondata, $excludedquestions, $allowshuffle = true, $forcequestionid = null) {
$available = $this->get_available_questions_from_category($questiondata->category,
!empty($questiondata->questiontext));
shuffle($available);
if ($forcequestionid !== null) {
$forcedquestionkey = array_search($forcequestionid, $available);
if ($forcedquestionkey !== false) {
unset($available[$forcedquestionkey]);
array_unshift($available, $forcequestionid);
} else {
throw new coding_exception('thisquestionidisnotavailable', $forcequestionid);
}
}
foreach ($available as $questionid) {
if (in_array($questionid, $excludedquestions)) {
continue;
}
$question = question_bank::load_question($questionid, $allowshuffle);
$this->set_selected_question_name($question, $questiondata->name);
return $question;
}
return null;
}
public function get_random_guess_score($questiondata) {
return null;
}
}