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.
20 * @package quiz_statistics
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/tests/attempt_walkthrough_from_csv_test.php');
31 require_once($CFG->dirroot . '/mod/quiz/report/default.php');
32 require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
33 require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
36 * Quiz attempt walk through using data from csv file.
38 * @package quiz_statistics
40 * @copyright 2013 The Open University
41 * @author Jamie Pratt <me@jamiep.org>
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class quiz_report_responses_from_steps_testcase extends mod_quiz_attempt_walkthrough_from_csv_testcase {
45 protected function get_full_path_of_csv_file($setname, $test) {
46 // Overridden here so that __DIR__ points to the path of this file.
47 return __DIR__."/fixtures/{$setname}{$test}.csv";
50 protected $files = array('questions', 'steps', 'responses');
53 * Create a quiz add questions to it, walk through quiz attempts and then check results.
55 * @param array $quizsettings settings to override default settings for quiz created by generator. Taken from quizzes.csv.
56 * @param PHPUnit\DbUnit\DataSet\ITable[] $csvdata of data read from csv file "questionsXX.csv",
57 * "stepsXX.csv" and "responsesXX.csv".
58 * @dataProvider get_data_for_walkthrough
60 public function test_walkthrough_from_csv($quizsettings, $csvdata) {
62 $this->resetAfterTest(true);
63 question_bank::get_qtype('random')->clear_caches_before_testing();
65 $this->create_quiz($quizsettings, $csvdata['questions']);
67 $quizattemptids = $this->walkthrough_attempts($csvdata['steps']);
69 for ($rowno = 0; $rowno < $csvdata['responses']->getRowCount(); $rowno++) {
70 $responsesfromcsv = $csvdata['responses']->getRow($rowno);
71 $responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv);
73 if (!isset($quizattemptids[$responses['quizattempt']])) {
74 throw new coding_exception("There is no quizattempt {$responses['quizattempt']}!");
76 $this->assert_response_test($quizattemptids[$responses['quizattempt']], $responses);
80 protected function assert_response_test($quizattemptid, $responses) {
81 $quizattempt = quiz_attempt::create($quizattemptid);
83 foreach ($responses['slot'] as $slot => $tests) {
84 $slothastests = false;
85 foreach ($tests as $test) {
93 $qa = $quizattempt->get_question_attempt($slot);
94 $stepswithsubmit = $qa->get_steps_with_submitted_response_iterator();
95 $step = $stepswithsubmit[$responses['submittedstepno']];
97 throw new coding_exception("There is no step no {$responses['submittedstepno']} ".
98 "for slot $slot in quizattempt {$responses['quizattempt']}!");
100 foreach (array('responsesummary', 'fraction', 'state') as $column) {
101 if (isset($tests[$column]) && $tests[$column] != '') {
103 case 'responsesummary' :
104 $actual = $qa->get_question()->summarise_response($step->get_qt_data());
107 if (count($stepswithsubmit) == $responses['submittedstepno']) {
108 // If this is the last step then we need to look at the fraction after the question has been
110 $actual = $qa->get_fraction();
112 $actual = $step->get_fraction();
116 if (count($stepswithsubmit) == $responses['submittedstepno']) {
117 // If this is the last step then we need to look at the state after the question has been
119 $state = $qa->get_state();
121 $state = $step->get_state();
123 $actual = substr(get_class($state), strlen('question_state_'));
125 $expected = $tests[$column];
126 $failuremessage = "Error in quizattempt {$responses['quizattempt']} in $column, slot $slot, ".
127 "submittedstepno {$responses['submittedstepno']}";
128 $this->assertEquals($expected, $actual, $failuremessage);