2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Quiz attempt walk through using data from csv file.
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
27 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/mod/quiz/editlib.php');
31 require_once($CFG->dirroot
. '/mod/quiz/locallib.php');
34 * Quiz attempt walk through using data from csv file.
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
42 class mod_quiz_attempt_walkthrough_from_csv_testcase
extends advanced_testcase
{
44 protected $files = array('questions', 'steps', 'results');
47 * @var stdClass the quiz record we create.
52 * @var array with slot no => question name => questionid. Question ids of questions created in the same category as random q.
57 * The only test in this class. This is run multiple times depending on how many sets of files there are in fixtures/
60 * @param array $quizsettings of settings read from csv file quizzes.csv
61 * @param PHPUnit_Extensions_Database_DataSet_ITable[] $csvdata of data read from csv file "questionsXX.csv",
62 * "stepsXX.csv" and "resultsXX.csv".
63 * @dataProvider get_data_for_walkthrough
65 public function test_walkthrough_from_csv($quizsettings, $csvdata) {
67 // CSV data files for these tests were generated using :
68 // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
70 $this->create_quiz_simulate_attempts_and_check_results($quizsettings, $csvdata);
73 public function create_quiz($quizsettings, $qs) {
75 $this->setAdminUser();
77 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
81 for ($rowno = 0; $rowno < $qs->getRowCount(); $rowno++
) {
82 $q = $this->explode_dot_separated_keys_to_make_subindexs($qs->getRow($rowno));
84 $catname = array('name' => $q['cat']);
85 if (!$cat = $DB->get_record('question_categories', array('name' => $q['cat']))) {
86 $cat = $questiongenerator->create_question_category($catname);
88 $q['catid'] = $cat->id
;
89 foreach (array('which' => null, 'overrides' => array()) as $key => $default) {
90 if (empty($q[$key])) {
95 if ($q['type'] !== 'random') {
96 // Don't actually create random questions here.
97 $overrides = array('category' => $cat->id
, 'defaultmark' => $q['mark']) +
$q['overrides'];
98 $question = $questiongenerator->create_question($q['type'], $q['which'], $overrides);
99 $q['id'] = $question->id
;
101 if (!isset($qidsbycat[$q['cat']])) {
102 $qidsbycat[$q['cat']] = array();
104 if (!empty($q['which'])) {
105 $name = $q['type'].'_'.$q['which'];
109 $qidsbycat[$q['catid']][$name] = $q['id'];
111 if (!empty($q['slot'])) {
112 $slots[$q['slot']] = $q;
113 $sumofgrades +
= $q['mark'];
120 $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
122 // Settings from param override defaults.
123 $aggregratedsettings = $quizsettings +
array('course' => $SITE->id
,
124 'questionsperpage' => 0,
126 'sumgrades' => $sumofgrades);
128 $this->quiz
= $quizgenerator->create_instance($aggregratedsettings);
130 $this->randqids
= array();
131 foreach ($slots as $slotno => $slotquestion) {
132 if ($slotquestion['type'] !== 'random') {
133 quiz_add_quiz_question($slotquestion['id'], $this->quiz
, 0, $slotquestion['mark']);
135 quiz_add_random_questions($this->quiz
, 0, $slotquestion['catid'], 1, 0);
136 $this->randqids
[$slotno] = $qidsbycat[$slotquestion['catid']];
142 * Create quiz, simulate attempts and check results (if resultsXX.csv exists).
144 * @param array $quizsettings Quiz overrides for this quiz.
145 * @param PHPUnit_Extensions_Database_DataSet_ITable[] $csvdata Data loaded from csv files for this test.
147 protected function create_quiz_simulate_attempts_and_check_results($quizsettings, $csvdata) {
148 $this->resetAfterTest(true);
149 question_bank
::get_qtype('random')->clear_caches_before_testing();
151 $this->create_quiz($quizsettings, $csvdata['questions']);
153 $attemptids = $this->walkthrough_attempts($csvdata['steps']);
155 if (isset($csvdata['results'])) {
156 $this->check_attempts_results($csvdata['results'], $attemptids);
161 * Get full path of CSV file.
163 * @param string $setname
164 * @param string $test
165 * @return string full path of file.
167 protected function get_full_path_of_csv_file($setname, $test) {
168 return __DIR__
."/fixtures/{$setname}{$test}.csv";
172 * Load dataset from CSV file "{$setname}{$test}.csv".
174 * @param string $setname
175 * @param string $test
176 * @return \PHPUnit_Extensions_Database_DataSet_ITable
178 protected function load_csv_data_file($setname, $test='') {
179 $files = array($setname => $this->get_full_path_of_csv_file($setname, $test));
180 return $this->createCsvDataSet($files)->getTable($setname);
184 * Break down row of csv data into sub arrays, according to column names.
186 * @param array $row from csv file with field names with parts separate by '.'.
187 * @return array the row with each part of the field name following a '.' being a separate sub array's index.
189 protected function explode_dot_separated_keys_to_make_subindexs(array $row) {
191 foreach ($row as $columnkey => $value) {
192 $newkeys = explode('.', trim($columnkey));
193 $placetoputvalue =& $parts;
194 foreach ($newkeys as $newkeydepth => $newkey) {
195 if ($newkeydepth +
1 === count($newkeys)) {
196 $placetoputvalue[$newkey] = $value;
198 // Going deeper down.
199 if (!isset($placetoputvalue[$newkey])) {
200 $placetoputvalue[$newkey] = array();
202 $placetoputvalue =& $placetoputvalue[$newkey];
210 * Data provider method for test_walkthrough_from_csv. Called by PHPUnit.
212 * @return array One array element for each run of the test. Each element contains an array with the params for
213 * test_walkthrough_from_csv.
215 public function get_data_for_walkthrough() {
216 $quizzes = $this->load_csv_data_file('quizzes');
218 for ($rowno = 0; $rowno < $quizzes->getRowCount(); $rowno++
) {
219 $quizsettings = $quizzes->getRow($rowno);
221 foreach ($this->files
as $file) {
222 if (file_exists($this->get_full_path_of_csv_file($file, $quizsettings['testnumber']))) {
223 $dataset[$file] = $this->load_csv_data_file($file, $quizsettings['testnumber']);
226 $datasets[] = array($quizsettings, $dataset);
232 * @param $steps PHPUnit_Extensions_Database_DataSet_ITable the step data from the csv file.
233 * @return array attempt no as in csv file => the id of the quiz_attempt as stored in the db.
235 protected function walkthrough_attempts($steps) {
237 $attemptids = array();
238 for ($rowno = 0; $rowno < $steps->getRowCount(); $rowno++
) {
240 $step = $this->explode_dot_separated_keys_to_make_subindexs($steps->getRow($rowno));
241 // Find existing user or make a new user to do the quiz.
242 $username = array('firstname' => $step['firstname'],
243 'lastname' => $step['lastname']);
245 if (!$user = $DB->get_record('user', $username)) {
246 $user = $this->getDataGenerator()->create_user($username);
249 if (!isset($attemptids[$step['quizattempt']])) {
250 // Start the attempt.
251 $quizobj = quiz
::create($this->quiz
->id
, $user->id
);
252 $quba = question_engine
::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
253 $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour
);
255 $prevattempts = quiz_get_user_attempts($this->quiz
->id
, $user->id
, 'all', true);
256 $attemptnumber = count($prevattempts) +
1;
258 $attempt = quiz_create_attempt($quizobj, $attemptnumber, false, $timenow, false, $user->id
);
259 // Select variant and / or random sub question.
260 if (!isset($step['variants'])) {
261 $step['variants'] = array();
263 if (isset($step['randqs'])) {
264 // Replace 'names' with ids.
265 foreach ($step['randqs'] as $slotno => $randqname) {
266 $step['randqs'][$slotno] = $this->randqids
[$slotno][$randqname];
269 $step['randqs'] = array();
272 quiz_start_new_attempt($quizobj, $quba, $attempt, $attemptnumber, $timenow, $step['randqs'], $step['variants']);
273 quiz_attempt_save_started($quizobj, $quba, $attempt);
274 $attemptid = $attemptids[$step['quizattempt']] = $attempt->id
;
276 $attemptid = $attemptids[$step['quizattempt']];
279 // Process some responses from the student.
280 $attemptobj = quiz_attempt
::create($attemptid);
281 $attemptobj->process_submitted_actions($timenow, false, $step['responses']);
283 // Finish the attempt.
284 if (!isset($step['finished']) ||
($step['finished'] == 1)) {
285 $attemptobj = quiz_attempt
::create($attemptid);
286 $attemptobj->process_finish($timenow, false);
293 * @param $results PHPUnit_Extensions_Database_DataSet_ITable the results data from the csv file.
294 * @param $attemptids array attempt no as in csv file => the id of the quiz_attempt as stored in the db.
296 protected function check_attempts_results($results, $attemptids) {
297 for ($rowno = 0; $rowno < $results->getRowCount(); $rowno++
) {
298 $result = $this->explode_dot_separated_keys_to_make_subindexs($results->getRow($rowno));
299 // Re-load quiz attempt data.
300 $attemptobj = quiz_attempt
::create($attemptids[$result['quizattempt']]);
301 $this->check_attempt_results($result, $attemptobj);
306 * Check that attempt results are as specified in $result.
308 * @param array $result row of data read from csv file.
309 * @param quiz_attempt $attemptobj the attempt object loaded from db.
310 * @throws coding_exception
312 protected function check_attempt_results($result, $attemptobj) {
313 foreach ($result as $fieldname => $value) {
314 if ($value === '!NULL!') {
317 switch ($fieldname) {
320 case 'attemptnumber' :
321 $this->assertEquals($value, $attemptobj->get_attempt_number());
324 foreach ($value as $slotno => $slottests) {
325 foreach ($slottests as $slotfieldname => $slotvalue) {
326 switch ($slotfieldname) {
328 $this->assertEquals(round($slotvalue, 2), $attemptobj->get_question_mark($slotno),
329 "Mark for slot $slotno of attempt {$result['quizattempt']}.");
332 throw new coding_exception('Unknown slots sub field column in csv file '
339 $this->assertEquals((bool)$value, $attemptobj->is_finished());
342 $this->assertEquals($value, $attemptobj->get_sum_marks(), "Sum of marks of attempt {$result['quizattempt']}.");
345 // Check quiz grades.
346 $grades = quiz_get_user_grades($attemptobj->get_quiz(), $attemptobj->get_userid());
347 $grade = array_shift($grades);
348 $this->assertEquals($value, $grade->rawgrade
, "Quiz grade for attempt {$result['quizattempt']}.");
350 case 'gradebookgrade' :
352 $gradebookgrades = grade_get_grades($attemptobj->get_courseid(),
354 $attemptobj->get_quizid(),
355 $attemptobj->get_userid());
356 $gradebookitem = array_shift($gradebookgrades->items
);
357 $gradebookgrade = array_shift($gradebookitem->grades
);
358 $this->assertEquals($value, $gradebookgrade->grade
, "Gradebook grade for attempt {$result['quizattempt']}.");
361 throw new coding_exception('Unknown column in csv file '.s($fieldname));