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