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 array $csvdata of data read from csv file "questionsXX.csv", "stepsXX.csv" and "responsesXX.csv".
57 * @dataProvider get_data_for_walkthrough
59 public function test_walkthrough_from_csv($quizsettings, $csvdata) {
61 $this->resetAfterTest(true);
62 question_bank::get_qtype('random')->clear_caches_before_testing();
64 $this->create_quiz($quizsettings, $csvdata['questions']);
66 $quizattemptids = $this->walkthrough_attempts($csvdata['steps']);
68 foreach ($csvdata['responses'] as $responsesfromcsv) {
69 $responses = $this->explode_dot_separated_keys_to_make_subindexs($responsesfromcsv);
71 if (!isset($quizattemptids[$responses['quizattempt']])) {
72 throw new coding_exception("There is no quizattempt {$responses['quizattempt']}!");
74 $this->assert_response_test($quizattemptids[$responses['quizattempt']], $responses);
78 protected function assert_response_test($quizattemptid, $responses) {
79 $quizattempt = quiz_attempt::create($quizattemptid);
81 foreach ($responses['slot'] as $slot => $tests) {
82 $slothastests = false;
83 foreach ($tests as $test) {
91 $qa = $quizattempt->get_question_attempt($slot);
92 $stepswithsubmit = $qa->get_steps_with_submitted_response_iterator();
93 $step = $stepswithsubmit[$responses['submittedstepno']];
95 throw new coding_exception("There is no step no {$responses['submittedstepno']} ".
96 "for slot $slot in quizattempt {$responses['quizattempt']}!");
98 foreach (array('responsesummary', 'fraction', 'state') as $column) {
99 if (isset($tests[$column]) && $tests[$column] != '') {
101 case 'responsesummary' :
102 $actual = $qa->get_question()->summarise_response($step->get_qt_data());
105 if (count($stepswithsubmit) == $responses['submittedstepno']) {
106 // If this is the last step then we need to look at the fraction after the question has been
108 $actual = $qa->get_fraction();
110 $actual = $step->get_fraction();
114 if (count($stepswithsubmit) == $responses['submittedstepno']) {
115 // If this is the last step then we need to look at the state after the question has been
117 $state = $qa->get_state();
119 $state = $step->get_state();
121 $actual = substr(get_class($state), strlen('question_state_'));
123 $expected = $tests[$column];
124 $failuremessage = "Error in quizattempt {$responses['quizattempt']} in $column, slot $slot, ".
125 "submittedstepno {$responses['submittedstepno']}";
126 $this->assertEquals($expected, $actual, $failuremessage);