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 |
namespace mod_quiz;
|
|
|
18 |
|
|
|
19 |
use question_engine;
|
|
|
20 |
use mod_quiz\quiz_settings;
|
|
|
21 |
|
|
|
22 |
defined('MOODLE_INTERNAL') || die();
|
|
|
23 |
|
|
|
24 |
global $CFG;
|
|
|
25 |
require_once($CFG->dirroot . '/mod/quiz/locallib.php');
|
|
|
26 |
require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Quiz attempt walk through using data from csv file.
|
|
|
30 |
*
|
|
|
31 |
* @package mod_quiz
|
|
|
32 |
* @category test
|
|
|
33 |
* @copyright 2013 The Open University
|
|
|
34 |
* @author Jamie Pratt <me@jamiep.org>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class attempt_walkthrough_from_csv_test extends \advanced_testcase {
|
|
|
38 |
|
|
|
39 |
use \quiz_question_helper_test_trait;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* @var string[] names of the files which contain the test data.
|
|
|
43 |
*/
|
|
|
44 |
protected $files = ['questions', 'steps', 'results'];
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* @var stdClass the quiz record we create.
|
|
|
48 |
*/
|
|
|
49 |
protected $quiz;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* @var array with slot no => question name => questionid. Question ids of questions created in the same category as random q.
|
|
|
53 |
*/
|
|
|
54 |
protected $randqids;
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* The only test in this class. This is run multiple times depending on how many sets of files there are in fixtures/
|
|
|
58 |
* directory.
|
|
|
59 |
*
|
|
|
60 |
* @param array $quizsettings of settings read from csv file quizzes.csv
|
|
|
61 |
* @param array $csvdata of data read from csv file "questionsXX.csv", "stepsXX.csv" and "resultsXX.csv".
|
|
|
62 |
* @dataProvider get_data_for_walkthrough
|
|
|
63 |
*/
|
11 |
efrain |
64 |
public function test_walkthrough_from_csv($quizsettings, $csvdata): void {
|
1 |
efrain |
65 |
|
|
|
66 |
// CSV data files for these tests were generated using :
|
|
|
67 |
// https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
|
|
|
68 |
|
|
|
69 |
$this->create_quiz_simulate_attempts_and_check_results($quizsettings, $csvdata);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public function create_quiz($quizsettings, $qs) {
|
|
|
73 |
global $SITE, $DB;
|
|
|
74 |
$this->setAdminUser();
|
|
|
75 |
|
|
|
76 |
/** @var core_question_generator $questiongenerator */
|
|
|
77 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
78 |
$slots = [];
|
|
|
79 |
$qidsbycat = [];
|
|
|
80 |
$sumofgrades = 0;
|
|
|
81 |
foreach ($qs as $qsrow) {
|
|
|
82 |
$q = $this->explode_dot_separated_keys_to_make_subindexs($qsrow);
|
|
|
83 |
|
|
|
84 |
$catname = ['name' => $q['cat']];
|
|
|
85 |
if (!$cat = $DB->get_record('question_categories', ['name' => $q['cat']])) {
|
|
|
86 |
$cat = $questiongenerator->create_question_category($catname);
|
|
|
87 |
}
|
|
|
88 |
$q['catid'] = $cat->id;
|
|
|
89 |
foreach (['which' => null, 'overrides' => []] as $key => $default) {
|
|
|
90 |
if (empty($q[$key])) {
|
|
|
91 |
$q[$key] = $default;
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
if ($q['type'] !== 'random') {
|
|
|
96 |
// Don't actually create random questions here.
|
|
|
97 |
$overrides = ['category' => $cat->id, 'defaultmark' => $q['mark']] + $q['overrides'];
|
|
|
98 |
if ($q['type'] === 'truefalse') {
|
|
|
99 |
// True/false question can never have hints, but sometimes we need to put them
|
|
|
100 |
// in the CSV file, to keep it rectangular.
|
|
|
101 |
unset($overrides['hint']);
|
|
|
102 |
}
|
|
|
103 |
$question = $questiongenerator->create_question($q['type'], $q['which'], $overrides);
|
|
|
104 |
$q['id'] = $question->id;
|
|
|
105 |
|
|
|
106 |
if (!isset($qidsbycat[$q['cat']])) {
|
|
|
107 |
$qidsbycat[$q['cat']] = [];
|
|
|
108 |
}
|
|
|
109 |
if (!empty($q['which'])) {
|
|
|
110 |
$name = $q['type'].'_'.$q['which'];
|
|
|
111 |
} else {
|
|
|
112 |
$name = $q['type'];
|
|
|
113 |
}
|
|
|
114 |
$qidsbycat[$q['catid']][$name] = $q['id'];
|
|
|
115 |
}
|
|
|
116 |
if (!empty($q['slot'])) {
|
|
|
117 |
$slots[$q['slot']] = $q;
|
|
|
118 |
$sumofgrades += $q['mark'];
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
ksort($slots);
|
|
|
123 |
|
|
|
124 |
// Make a quiz.
|
|
|
125 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
126 |
|
|
|
127 |
// Settings from param override defaults.
|
|
|
128 |
$aggregratedsettings = $quizsettings + ['course' => $SITE->id,
|
|
|
129 |
'questionsperpage' => 0,
|
|
|
130 |
'grade' => 100.0,
|
|
|
131 |
'sumgrades' => $sumofgrades];
|
|
|
132 |
|
|
|
133 |
$this->quiz = $quizgenerator->create_instance($aggregratedsettings);
|
|
|
134 |
|
|
|
135 |
$this->randqids = [];
|
|
|
136 |
foreach ($slots as $slotno => $slotquestion) {
|
|
|
137 |
if ($slotquestion['type'] !== 'random') {
|
|
|
138 |
quiz_add_quiz_question($slotquestion['id'], $this->quiz, 0, $slotquestion['mark']);
|
|
|
139 |
} else {
|
|
|
140 |
$this->add_random_questions($this->quiz->id, 0, $slotquestion['catid'], 1);
|
|
|
141 |
$this->randqids[$slotno] = $qidsbycat[$slotquestion['catid']];
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Create quiz, simulate attempts and check results (if resultsXX.csv exists).
|
|
|
148 |
*
|
|
|
149 |
* @param array $quizsettings Quiz overrides for this quiz.
|
|
|
150 |
* @param array $csvdata Data loaded from csv files for this test.
|
|
|
151 |
*/
|
|
|
152 |
protected function create_quiz_simulate_attempts_and_check_results(array $quizsettings, array $csvdata) {
|
|
|
153 |
$this->resetAfterTest();
|
|
|
154 |
|
|
|
155 |
$this->create_quiz($quizsettings, $csvdata['questions']);
|
|
|
156 |
|
|
|
157 |
$attemptids = $this->walkthrough_attempts($csvdata['steps']);
|
|
|
158 |
|
|
|
159 |
if (isset($csvdata['results'])) {
|
|
|
160 |
$this->check_attempts_results($csvdata['results'], $attemptids);
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Get full path of CSV file.
|
|
|
166 |
*
|
|
|
167 |
* @param string $setname
|
|
|
168 |
* @param string $test
|
|
|
169 |
* @return string full path of file.
|
|
|
170 |
*/
|
|
|
171 |
protected function get_full_path_of_csv_file(string $setname, string $test): string {
|
|
|
172 |
return __DIR__."/fixtures/{$setname}{$test}.csv";
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Load dataset from CSV file "{$setname}{$test}.csv".
|
|
|
177 |
*
|
|
|
178 |
* @param string $setname
|
|
|
179 |
* @param string $test
|
|
|
180 |
* @return array
|
|
|
181 |
*/
|
|
|
182 |
protected function load_csv_data_file(string $setname, string $test = ''): array {
|
|
|
183 |
$files = [$setname => $this->get_full_path_of_csv_file($setname, $test)];
|
|
|
184 |
return $this->dataset_from_files($files)->get_rows([$setname]);
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Break down row of csv data into sub arrays, according to column names.
|
|
|
189 |
*
|
|
|
190 |
* @param array $row from csv file with field names with parts separate by '.'.
|
|
|
191 |
* @return array the row with each part of the field name following a '.' being a separate sub array's index.
|
|
|
192 |
*/
|
|
|
193 |
protected function explode_dot_separated_keys_to_make_subindexs(array $row): array {
|
|
|
194 |
$parts = [];
|
|
|
195 |
foreach ($row as $columnkey => $value) {
|
|
|
196 |
$newkeys = explode('.', trim($columnkey));
|
|
|
197 |
$placetoputvalue =& $parts;
|
|
|
198 |
foreach ($newkeys as $newkeydepth => $newkey) {
|
|
|
199 |
if ($newkeydepth + 1 === count($newkeys)) {
|
|
|
200 |
$placetoputvalue[$newkey] = $value;
|
|
|
201 |
} else {
|
|
|
202 |
// Going deeper down.
|
|
|
203 |
if (!isset($placetoputvalue[$newkey])) {
|
|
|
204 |
$placetoputvalue[$newkey] = [];
|
|
|
205 |
}
|
|
|
206 |
$placetoputvalue =& $placetoputvalue[$newkey];
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
return $parts;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/**
|
|
|
214 |
* Data provider method for test_walkthrough_from_csv. Called by PHPUnit.
|
|
|
215 |
*
|
|
|
216 |
* @return array One array element for each run of the test. Each element contains an array with the params for
|
|
|
217 |
* test_walkthrough_from_csv.
|
|
|
218 |
*/
|
|
|
219 |
public function get_data_for_walkthrough(): array {
|
|
|
220 |
$quizzes = $this->load_csv_data_file('quizzes')['quizzes'];
|
|
|
221 |
$datasets = [];
|
|
|
222 |
foreach ($quizzes as $quizsettings) {
|
|
|
223 |
$dataset = [];
|
|
|
224 |
foreach ($this->files as $file) {
|
|
|
225 |
if (file_exists($this->get_full_path_of_csv_file($file, $quizsettings['testnumber']))) {
|
|
|
226 |
$dataset[$file] = $this->load_csv_data_file($file, $quizsettings['testnumber'])[$file];
|
|
|
227 |
}
|
|
|
228 |
}
|
|
|
229 |
$datasets[] = [$quizsettings, $dataset];
|
|
|
230 |
}
|
|
|
231 |
return $datasets;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* @param array $steps the step data from the csv file.
|
|
|
236 |
* @return array attempt no as in csv file => the id of the quiz_attempt as stored in the db.
|
|
|
237 |
*/
|
|
|
238 |
protected function walkthrough_attempts(array $steps): array {
|
|
|
239 |
global $DB;
|
|
|
240 |
$attemptids = [];
|
|
|
241 |
foreach ($steps as $steprow) {
|
|
|
242 |
|
|
|
243 |
$step = $this->explode_dot_separated_keys_to_make_subindexs($steprow);
|
|
|
244 |
// Find existing user or make a new user to do the quiz.
|
|
|
245 |
$username = ['firstname' => $step['firstname'],
|
|
|
246 |
'lastname' => $step['lastname']];
|
|
|
247 |
|
|
|
248 |
if (!$user = $DB->get_record('user', $username)) {
|
|
|
249 |
$user = $this->getDataGenerator()->create_user($username);
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
if (!isset($attemptids[$step['quizattempt']])) {
|
|
|
253 |
// Start the attempt.
|
|
|
254 |
$quizobj = quiz_settings::create($this->quiz->id, $user->id);
|
|
|
255 |
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
|
|
|
256 |
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
|
|
|
257 |
|
|
|
258 |
$prevattempts = quiz_get_user_attempts($this->quiz->id, $user->id, 'all', true);
|
|
|
259 |
$attemptnumber = count($prevattempts) + 1;
|
|
|
260 |
$timenow = time();
|
|
|
261 |
$attempt = quiz_create_attempt($quizobj, $attemptnumber, null, $timenow, false, $user->id);
|
|
|
262 |
// Select variant and / or random sub question.
|
|
|
263 |
if (!isset($step['variants'])) {
|
|
|
264 |
$step['variants'] = [];
|
|
|
265 |
}
|
|
|
266 |
if (isset($step['randqs'])) {
|
|
|
267 |
// Replace 'names' with ids.
|
|
|
268 |
foreach ($step['randqs'] as $slotno => $randqname) {
|
|
|
269 |
$step['randqs'][$slotno] = $this->randqids[$slotno][$randqname];
|
|
|
270 |
}
|
|
|
271 |
} else {
|
|
|
272 |
$step['randqs'] = [];
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow, $step['randqs'], $step['variants']);
|
|
|
276 |
quiz_attempt_save_started($quizobj, $quba, $attempt);
|
|
|
277 |
$attemptid = $attemptids[$step['quizattempt']] = $attempt->id;
|
|
|
278 |
} else {
|
|
|
279 |
$attemptid = $attemptids[$step['quizattempt']];
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
// Process some responses from the student.
|
|
|
283 |
$attemptobj = quiz_attempt::create($attemptid);
|
|
|
284 |
$attemptobj->process_submitted_actions($timenow, false, $step['responses']);
|
|
|
285 |
|
|
|
286 |
// Finish the attempt.
|
|
|
287 |
if (!isset($step['finished']) || ($step['finished'] == 1)) {
|
|
|
288 |
$attemptobj = quiz_attempt::create($attemptid);
|
|
|
289 |
$attemptobj->process_finish($timenow, false);
|
|
|
290 |
}
|
|
|
291 |
}
|
|
|
292 |
return $attemptids;
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* @param array $results the results data from the csv file.
|
|
|
297 |
* @param array $attemptids attempt no as in csv file => the id of the quiz_attempt as stored in the db.
|
|
|
298 |
*/
|
|
|
299 |
protected function check_attempts_results(array $results, array $attemptids) {
|
|
|
300 |
foreach ($results as $resultrow) {
|
|
|
301 |
$result = $this->explode_dot_separated_keys_to_make_subindexs($resultrow);
|
|
|
302 |
// Re-load quiz attempt data.
|
|
|
303 |
$attemptobj = quiz_attempt::create($attemptids[$result['quizattempt']]);
|
|
|
304 |
$this->check_attempt_results($result, $attemptobj);
|
|
|
305 |
}
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
* Check that attempt results are as specified in $result.
|
|
|
310 |
*
|
|
|
311 |
* @param array $result row of data read from csv file.
|
|
|
312 |
* @param quiz_attempt $attemptobj the attempt object loaded from db.
|
|
|
313 |
*/
|
|
|
314 |
protected function check_attempt_results(array $result, quiz_attempt $attemptobj) {
|
|
|
315 |
foreach ($result as $fieldname => $value) {
|
|
|
316 |
if ($value === '!NULL!') {
|
|
|
317 |
$value = null;
|
|
|
318 |
}
|
|
|
319 |
switch ($fieldname) {
|
|
|
320 |
case 'quizattempt' :
|
|
|
321 |
break;
|
|
|
322 |
case 'attemptnumber' :
|
|
|
323 |
$this->assertEquals($value, $attemptobj->get_attempt_number());
|
|
|
324 |
break;
|
|
|
325 |
case 'slots' :
|
|
|
326 |
foreach ($value as $slotno => $slottests) {
|
|
|
327 |
foreach ($slottests as $slotfieldname => $slotvalue) {
|
|
|
328 |
switch ($slotfieldname) {
|
|
|
329 |
case 'mark' :
|
|
|
330 |
$this->assertEquals(round($slotvalue, 2), $attemptobj->get_question_mark($slotno),
|
|
|
331 |
"Mark for slot $slotno of attempt {$result['quizattempt']}.");
|
|
|
332 |
break;
|
|
|
333 |
default :
|
|
|
334 |
throw new \coding_exception('Unknown slots sub field column in csv file '
|
|
|
335 |
.s($slotfieldname));
|
|
|
336 |
}
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
break;
|
|
|
340 |
case 'finished' :
|
|
|
341 |
$this->assertEquals((bool)$value, $attemptobj->is_finished());
|
|
|
342 |
break;
|
|
|
343 |
case 'summarks' :
|
|
|
344 |
$this->assertEquals((float)$value, $attemptobj->get_sum_marks(),
|
|
|
345 |
"Sum of marks of attempt {$result['quizattempt']}.");
|
|
|
346 |
break;
|
|
|
347 |
case 'quizgrade' :
|
|
|
348 |
// Check quiz grades.
|
|
|
349 |
$grades = quiz_get_user_grades($attemptobj->get_quiz(), $attemptobj->get_userid());
|
|
|
350 |
$grade = array_shift($grades);
|
|
|
351 |
$this->assertEquals($value, $grade->rawgrade, "Quiz grade for attempt {$result['quizattempt']}.");
|
|
|
352 |
break;
|
|
|
353 |
case 'gradebookgrade' :
|
|
|
354 |
// Check grade book.
|
|
|
355 |
$gradebookgrades = grade_get_grades($attemptobj->get_courseid(),
|
|
|
356 |
'mod', 'quiz',
|
|
|
357 |
$attemptobj->get_quizid(),
|
|
|
358 |
$attemptobj->get_userid());
|
|
|
359 |
$gradebookitem = array_shift($gradebookgrades->items);
|
|
|
360 |
$gradebookgrade = array_shift($gradebookitem->grades);
|
|
|
361 |
$this->assertEquals($value, $gradebookgrade->grade, "Gradebook grade for attempt {$result['quizattempt']}.");
|
|
|
362 |
break;
|
|
|
363 |
default :
|
|
|
364 |
throw new \coding_exception('Unknown column in csv file '.s($fieldname));
|
|
|
365 |
}
|
|
|
366 |
}
|
|
|
367 |
}
|
|
|
368 |
}
|