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/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');
35 * Test helper subclass of quiz_statistics_report
37 * @copyright 2013 The Open University
38 * @author Jamie Pratt <me@jamiep.org>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class testable_quiz_statistics_report extends quiz_statistics_report {
43 public function get_stats($quiz, $useallattempts = true,
44 $currentgroup = 0, $groupstudents = array(), $nostudentsingroup = false) {
45 $this->clear_cached_data($quiz->id, $currentgroup, $useallattempts);
46 $questions = $this->load_and_initialise_questions_for_calculations($quiz);
47 return $this->get_quiz_and_questions_stats($quiz, $currentgroup, $nostudentsingroup,
48 $useallattempts, $groupstudents, $questions);
53 * Quiz attempt walk through using data from csv file.
57 * @copyright 2013 The Open University
58 * @author Jamie Pratt <me@jamiep.org>
59 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61 class quiz_report_statistics_from_steps extends mod_quiz_attempt_walkthrough_from_csv_testcase {
64 * @var quiz_statistics_report object to do stats calculations.
68 protected function get_full_path_of_csv_file($setname, $test) {
69 // Overridden here so that __DIR__ points to the path of this file.
70 return __DIR__."/fixtures/{$setname}{$test}.csv";
73 protected $files = array('questions', 'steps', 'results', 'qstats');
76 * Create a quiz add questions to it, walk through quiz attempts and then check results.
78 * @param PHPUnit_Extensions_Database_DataSet_ITable[] of data read from csv file "questionsXX.csv",
79 * "stepsXX.csv" and "resultsXX.csv".
80 * @dataProvider get_data_for_walkthrough
82 public function test_walkthrough_from_csv($quizsettings, $csvdata) {
84 // CSV data files for these tests were generated using :
85 // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
87 $this->resetAfterTest(true);
88 question_bank::get_qtype('random')->clear_caches_before_testing();
90 $this->create_quiz($quizsettings, $csvdata['questions']);
92 $attemptids = $this->walkthrough_attempts($csvdata['steps']);
94 $this->check_attempts_results($csvdata['results'], $attemptids);
96 $this->report = new testable_quiz_statistics_report();
97 list($quizstats, $questions, $subquestions, $s) = $this->report->get_stats($this->quiz);
99 // These quiz stats and the question stats found in qstats00.csv were calculated independently in spreadsheet which is
100 // available in open document or excel format here :
101 // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet
102 $quizstatsexpected = array(
104 'firstattemptsavg' => 4.617333332,
105 'allattemptsavg' => 4.617333332,
106 'firstattemptscount' => 25,
107 'allattemptscount' => 25,
108 'standarddeviation' => 0.8117265554,
109 'skewness' => -0.092502502,
110 'kurtosis' => -0.7073968557,
111 'cic' => -87.2230935542,
112 'errorratio' => 136.8294900795,
113 'standarderror' => 1.1106813066
116 foreach ($quizstatsexpected as $statname => $statvalue) {
117 $this->assertEquals($statvalue, $quizstats->$statname, $quizstats->$statname, abs($statvalue) * 1e-5);
120 for ($rowno = 0; $rowno < $csvdata['qstats']->getRowCount(); $rowno++) {
121 $slotqstats = $csvdata['qstats']->getRow($rowno);
122 foreach ($slotqstats as $statname => $slotqstat) {
123 if ($statname !== 'slot') {
126 case 'discriminationindex' :
127 case 'discriminativeefficiency' :
128 case 'effectiveweight' :
134 $slot = $slotqstats['slot'];
135 $delta = abs($slotqstat) * $precision;
136 $actual = $questions[$slot]->_stats->{$statname};
137 $this->assertEquals(floatval($slotqstat), $actual, "$statname for slot $slot", $delta);