| 1 |
efrain |
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 |
/**
|
|
|
18 |
* Quiz module test data generator.
|
|
|
19 |
*
|
|
|
20 |
* @package core_question
|
|
|
21 |
* @copyright 2013 The Open University
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
use core_question\local\bank\question_version_status;
|
| 1441 |
ariadna |
26 |
use core\exception\coding_exception;
|
| 1 |
efrain |
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Class core_question_generator for generating question data.
|
|
|
30 |
*
|
|
|
31 |
* @package core_question
|
|
|
32 |
* @copyright 2013 The Open University
|
|
|
33 |
* @author 2021 Safat Shahin <safatshahin@catalyst-au.net>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class core_question_generator extends component_generator_base {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* @var number of created instances
|
|
|
40 |
*/
|
|
|
41 |
protected $categorycount = 0;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Make the category count to zero.
|
|
|
45 |
*/
|
|
|
46 |
public function reset() {
|
|
|
47 |
$this->categorycount = 0;
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Create a new question category.
|
|
|
52 |
*
|
|
|
53 |
* @param array|stdClass $record
|
|
|
54 |
* @return stdClass question_categories record.
|
|
|
55 |
*/
|
|
|
56 |
public function create_question_category($record = null) {
|
|
|
57 |
global $DB;
|
|
|
58 |
|
|
|
59 |
$this->categorycount ++;
|
|
|
60 |
|
|
|
61 |
$defaults = [
|
|
|
62 |
'name' => 'Test question category ' . $this->categorycount,
|
|
|
63 |
'info' => '',
|
|
|
64 |
'infoformat' => FORMAT_HTML,
|
|
|
65 |
'stamp' => make_unique_id_code(),
|
| 1441 |
ariadna |
66 |
'idnumber' => null,
|
| 1 |
efrain |
67 |
];
|
|
|
68 |
|
|
|
69 |
$record = $this->datagenerator->combine_defaults_and_record($defaults, $record);
|
|
|
70 |
|
|
|
71 |
if (!isset($record['contextid'])) {
|
|
|
72 |
if (isset($record['parent'])) {
|
|
|
73 |
$record['contextid'] = $DB->get_field('question_categories', 'contextid', ['id' => $record['parent']]);
|
|
|
74 |
} else {
|
| 1441 |
ariadna |
75 |
$qbank = $this->datagenerator->create_module('qbank', ['course' => SITEID]);
|
|
|
76 |
$record['contextid'] = context_module::instance($qbank->cmid)->id;
|
| 1 |
efrain |
77 |
}
|
| 1441 |
ariadna |
78 |
} else {
|
|
|
79 |
// Any requests for a question category in a contextlevel that is no longer supported
|
|
|
80 |
// will have a qbank instance created on the associated context and then the category
|
|
|
81 |
// will be made for that context instead.
|
|
|
82 |
$context = context::instance_by_id($record['contextid']);
|
|
|
83 |
if ($context->contextlevel !== CONTEXT_MODULE) {
|
|
|
84 |
$course = match ($context->contextlevel) {
|
|
|
85 |
CONTEXT_COURSE => get_course($context->instanceid),
|
|
|
86 |
CONTEXT_SYSTEM => get_site(),
|
|
|
87 |
CONTEXT_COURSECAT => $this->datagenerator->create_course(['category' => $context->instanceid]),
|
|
|
88 |
default => throw new \Exception('Invalid context to infer a question bank from.'),
|
|
|
89 |
};
|
|
|
90 |
$qbank = \core_question\local\bank\question_bank_helper::get_default_open_instance_system_type($course, true);
|
|
|
91 |
$bankcontext = context_module::instance($qbank->id);
|
|
|
92 |
} else {
|
|
|
93 |
$bankcontext = $context;
|
|
|
94 |
}
|
|
|
95 |
$record['contextid'] = $bankcontext->id;
|
| 1 |
efrain |
96 |
}
|
| 1441 |
ariadna |
97 |
|
| 1 |
efrain |
98 |
if (!isset($record['parent'])) {
|
|
|
99 |
$record['parent'] = question_get_top_category($record['contextid'], true)->id;
|
|
|
100 |
}
|
| 1441 |
ariadna |
101 |
if (!isset($record['sortorder'])) {
|
|
|
102 |
$manager = new \core_question\category_manager();
|
|
|
103 |
$record['sortorder'] = $manager->get_max_sortorder($record['parent']) + 1;
|
|
|
104 |
}
|
| 1 |
efrain |
105 |
$record['id'] = $DB->insert_record('question_categories', $record);
|
|
|
106 |
return (object) $record;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Create a new question. The question is initialised using one of the
|
|
|
111 |
* examples from the appropriate {@link question_test_helper} subclass.
|
|
|
112 |
* Then, any files you want to change from the value in the base example you
|
|
|
113 |
* can override using $overrides.
|
|
|
114 |
*
|
|
|
115 |
* @param string $qtype the question type to create an example of.
|
|
|
116 |
* @param string $which as for the corresponding argument of
|
|
|
117 |
* {@link question_test_helper::get_question_form_data}. null for the default one.
|
|
|
118 |
* @param array|stdClass $overrides any fields that should be different from the base example.
|
|
|
119 |
* @return stdClass the question data.
|
|
|
120 |
*/
|
|
|
121 |
public function create_question($qtype, $which = null, $overrides = null) {
|
|
|
122 |
$question = new stdClass();
|
|
|
123 |
$question->qtype = $qtype;
|
|
|
124 |
$question->createdby = 0;
|
|
|
125 |
$question->idnumber = null;
|
|
|
126 |
$question->status = question_version_status::QUESTION_STATUS_READY;
|
|
|
127 |
|
|
|
128 |
return $this->update_question($question, $which, $overrides);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Create a tag on a question.
|
|
|
133 |
*
|
|
|
134 |
* @param array $data with two elements ['questionid' => 123, 'tag' => 'mytag'].
|
|
|
135 |
*/
|
|
|
136 |
public function create_question_tag(array $data): void {
|
|
|
137 |
$question = question_bank::load_question($data['questionid']);
|
|
|
138 |
core_tag_tag::add_item_tag('core_question', 'question', $question->id,
|
|
|
139 |
context::instance_by_id($question->contextid), $data['tag'], 0);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Update an existing question.
|
|
|
144 |
*
|
|
|
145 |
* @param stdClass $question the question data to update.
|
|
|
146 |
* @param string $which as for the corresponding argument of
|
|
|
147 |
* {@link question_test_helper::get_question_form_data}. null for the default one.
|
|
|
148 |
* @param array|stdClass $overrides any fields that should be different from the base example.
|
|
|
149 |
* @return stdClass the question data, including version info and questionbankentryid
|
|
|
150 |
*/
|
|
|
151 |
public function update_question($question, $which = null, $overrides = null) {
|
|
|
152 |
global $CFG, $DB;
|
|
|
153 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
154 |
$question = clone($question);
|
|
|
155 |
|
|
|
156 |
$qtype = $question->qtype;
|
|
|
157 |
|
|
|
158 |
$fromform = test_question_maker::get_question_form_data($qtype, $which);
|
|
|
159 |
$fromform = (object) $this->datagenerator->combine_defaults_and_record((array) $question, $fromform);
|
|
|
160 |
$fromform = (object) $this->datagenerator->combine_defaults_and_record((array) $fromform, $overrides);
|
|
|
161 |
$fromform->status = $fromform->status ?? $question->status;
|
|
|
162 |
|
|
|
163 |
$question = question_bank::get_qtype($qtype)->save_question($question, $fromform);
|
|
|
164 |
|
| 1441 |
ariadna |
165 |
$validoverrides = ['createdby', 'modifiedby', 'timemodified'];
|
|
|
166 |
if ($overrides && !empty(array_intersect($validoverrides, array_keys($overrides)))) {
|
|
|
167 |
// Manually update the createdby, modifiedby and timemodified because questiontypebase forces
|
|
|
168 |
// current user and time and some tests require a specific user or time.
|
|
|
169 |
foreach ($validoverrides as $validoverride) {
|
|
|
170 |
if (array_key_exists($validoverride, $overrides)) {
|
|
|
171 |
$question->{$validoverride} = $overrides[$validoverride];
|
|
|
172 |
}
|
| 1 |
efrain |
173 |
}
|
|
|
174 |
$DB->update_record('question', $question);
|
|
|
175 |
}
|
|
|
176 |
$questionversion = $DB->get_record('question_versions', ['questionid' => $question->id], '*', MUST_EXIST);
|
|
|
177 |
$question->versionid = $questionversion->id;
|
|
|
178 |
$question->questionbankentryid = $questionversion->questionbankentryid;
|
|
|
179 |
$question->version = $questionversion->version;
|
|
|
180 |
$question->status = $questionversion->status;
|
|
|
181 |
|
|
|
182 |
return $question;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
| 1441 |
ariadna |
186 |
* Set up a course category, a course, a mod_qbank instance, a question category for the mod_qbank instance,
|
|
|
187 |
* and 2 questions for testing.
|
| 1 |
efrain |
188 |
*
|
| 1441 |
ariadna |
189 |
* @return array of the data objects mentioned above
|
| 1 |
efrain |
190 |
*/
|
| 1441 |
ariadna |
191 |
public function setup_course_and_questions() {
|
| 1 |
efrain |
192 |
$datagenerator = $this->datagenerator;
|
|
|
193 |
$category = $datagenerator->create_category();
|
|
|
194 |
$course = $datagenerator->create_course([
|
|
|
195 |
'numsections' => 5,
|
|
|
196 |
'category' => $category->id
|
|
|
197 |
]);
|
| 1441 |
ariadna |
198 |
$qbank = $datagenerator->create_module('qbank', ['course' => $course->id]);
|
|
|
199 |
$context = context_module::instance($qbank->cmid);
|
| 1 |
efrain |
200 |
|
|
|
201 |
$qcat = $this->create_question_category(['contextid' => $context->id]);
|
|
|
202 |
|
|
|
203 |
$questions = [
|
|
|
204 |
$this->create_question('shortanswer', null, ['category' => $qcat->id]),
|
|
|
205 |
$this->create_question('shortanswer', null, ['category' => $qcat->id]),
|
|
|
206 |
];
|
|
|
207 |
|
| 1441 |
ariadna |
208 |
return [$category, $course, $qcat, $questions, $qbank];
|
| 1 |
efrain |
209 |
}
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* This method can construct what the post data would be to simulate a user submitting
|
|
|
213 |
* responses to a number of questions within a question usage.
|
|
|
214 |
*
|
|
|
215 |
* In the responses array, the array keys are the slot numbers for which a response will
|
|
|
216 |
* be submitted. You can submit a response to any number of questions within the usage.
|
|
|
217 |
* There is no need to do them all. The values are a string representation of the response.
|
|
|
218 |
* The exact meaning of that depends on the particular question type. These strings
|
|
|
219 |
* are passed to the un_summarise_response method of the question to decode.
|
|
|
220 |
*
|
|
|
221 |
* @param question_usage_by_activity $quba the question usage.
|
|
|
222 |
* @param array $responses the responses to submit, in the format described above.
|
|
|
223 |
* @param bool $checkbutton if simulate a click on the check button for each question, else simulate save.
|
|
|
224 |
* This should only be used with behaviours that have a check button.
|
|
|
225 |
* @return array that can be passed to methods like $quba->process_all_actions as simulated POST data.
|
|
|
226 |
*/
|
|
|
227 |
public function get_simulated_post_data_for_questions_in_usage(
|
|
|
228 |
question_usage_by_activity $quba, array $responses, $checkbutton) {
|
|
|
229 |
$postdata = [];
|
|
|
230 |
|
|
|
231 |
foreach ($responses as $slot => $responsesummary) {
|
|
|
232 |
$postdata += $this->get_simulated_post_data_for_question_attempt(
|
|
|
233 |
$quba->get_question_attempt($slot), $responsesummary, $checkbutton);
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
return $postdata;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* This method can construct what the post data would be to simulate a user submitting
|
|
|
241 |
* responses to one particular question attempt.
|
|
|
242 |
*
|
|
|
243 |
* The $responsesummary is a string representation of the response to be submitted.
|
|
|
244 |
* The exact meaning of that depends on the particular question type. These strings
|
|
|
245 |
* are passed to the un_summarise_response method of the question to decode.
|
|
|
246 |
*
|
|
|
247 |
* @param question_attempt $qa the question attempt for which we are generating POST data.
|
|
|
248 |
* @param string $responsesummary a textual summary of the response, as described above.
|
|
|
249 |
* @param bool $checkbutton if simulate a click on the check button, else simulate save.
|
|
|
250 |
* This should only be used with behaviours that have a check button.
|
|
|
251 |
* @return array the simulated post data that can be passed to $quba->process_all_actions.
|
|
|
252 |
*/
|
|
|
253 |
public function get_simulated_post_data_for_question_attempt(
|
|
|
254 |
question_attempt $qa, $responsesummary, $checkbutton) {
|
|
|
255 |
|
|
|
256 |
$question = $qa->get_question();
|
|
|
257 |
if (!$question instanceof question_with_responses) {
|
|
|
258 |
return [];
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
$postdata = [];
|
|
|
262 |
$postdata[$qa->get_control_field_name('sequencecheck')] = (string)$qa->get_sequence_check_count();
|
|
|
263 |
$postdata[$qa->get_flag_field_name()] = (string)(int)$qa->is_flagged();
|
|
|
264 |
|
|
|
265 |
$response = $question->un_summarise_response($responsesummary);
|
|
|
266 |
foreach ($response as $name => $value) {
|
|
|
267 |
$postdata[$qa->get_qt_field_name($name)] = (string)$value;
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
// TODO handle behaviour variables better than this.
|
|
|
271 |
if ($checkbutton) {
|
|
|
272 |
$postdata[$qa->get_behaviour_field_name('submit')] = 1;
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
return $postdata;
|
|
|
276 |
}
|
| 1441 |
ariadna |
277 |
|
|
|
278 |
/**
|
|
|
279 |
* Given a context and a structure of categories and questions, generate that structure.
|
|
|
280 |
*
|
|
|
281 |
* The $structure parameter takes a multi-dimensional array of categories and questions, like this:
|
|
|
282 |
* [
|
|
|
283 |
* 'categoryname' => [
|
|
|
284 |
* 'question1name' => 'questiontype',
|
|
|
285 |
* 'question2name' => 'questiontype',
|
|
|
286 |
* 'subcategoryname' => [
|
|
|
287 |
* 'subquestion1name' => 'questiontype',
|
|
|
288 |
* ],
|
|
|
289 |
* ],
|
|
|
290 |
* ]
|
|
|
291 |
* Arrays are treated as categories, strings are treated as questions. The key in each case is used for the name of the category
|
|
|
292 |
* or question. For subcategories, the method is called recursively to create all descendants.
|
|
|
293 |
* The 'questiontype' string is the type of question to be generated, and will be passed to create_question.
|
|
|
294 |
*
|
|
|
295 |
* @param context $context The context to create the structure in.
|
|
|
296 |
* @param array $structure The array of categories and questions, see above.
|
|
|
297 |
* @param ?int $parentid The category to create the category or question within.
|
|
|
298 |
* @return array The input structure, with the generated questions in place of the question types.
|
|
|
299 |
*/
|
|
|
300 |
public function create_categories_and_questions(context $context, array $structure, ?int $parentid = null) {
|
|
|
301 |
$createdcategories = [];
|
|
|
302 |
foreach ($structure as $name => $item) {
|
|
|
303 |
if (is_array($item)) {
|
|
|
304 |
$categorydata = [
|
|
|
305 |
'name' => $name,
|
|
|
306 |
'contextid' => $context->id,
|
|
|
307 |
];
|
|
|
308 |
if ($parentid) {
|
|
|
309 |
$categorydata['parent'] = $parentid;
|
|
|
310 |
}
|
|
|
311 |
$category = $this->create_question_category($categorydata);
|
|
|
312 |
$createdcategories[$name] = $this->create_categories_and_questions($context, $item, $category->id);
|
|
|
313 |
} else if (is_string($item)) {
|
|
|
314 |
if (!$parentid) {
|
|
|
315 |
throw new coding_exception('You cannot create questions in a top-level category.');
|
|
|
316 |
}
|
|
|
317 |
$createdcategories[$name] = $this->create_question($item, null, ['name' => $name, 'category' => $parentid]);
|
|
|
318 |
} else {
|
|
|
319 |
throw new coding_exception('Structure items must be arrays or strings, ' . gettype($item) . ' found.');
|
|
|
320 |
}
|
|
|
321 |
}
|
|
|
322 |
return $createdcategories;
|
|
|
323 |
}
|
| 1 |
efrain |
324 |
}
|