MDL-41758 quiz statistics : link to full break down of stats for slots
[moodle.git] / mod / quiz / report / statistics / tests / stats_from_steps_walkthrough_test.php
CommitLineData
3652dddd
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 *
e68e4ccf 20 * @package quiz_statistics
3652dddd
JP
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/tests/attempt_walkthrough_from_csv_test.php');
31require_once($CFG->dirroot . '/mod/quiz/report/default.php');
32require_once($CFG->dirroot . '/mod/quiz/report/statistics/report.php');
33require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
3652dddd 34
3652dddd
JP
35/**
36 * Quiz attempt walk through using data from csv file.
37 *
e68e4ccf 38 * @package quiz_statistics
3652dddd
JP
39 * @category phpunit
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
43 */
8e328617 44class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkthrough_from_csv_testcase {
3652dddd
JP
45
46 /**
47 * @var quiz_statistics_report object to do stats calculations.
48 */
49 protected $report;
50
51 protected function get_full_path_of_csv_file($setname, $test) {
52 // Overridden here so that __DIR__ points to the path of this file.
53 return __DIR__."/fixtures/{$setname}{$test}.csv";
54 }
55
56 protected $files = array('questions', 'steps', 'results', 'qstats');
57
58 /**
59 * Create a quiz add questions to it, walk through quiz attempts and then check results.
60 *
61 * @param PHPUnit_Extensions_Database_DataSet_ITable[] of data read from csv file "questionsXX.csv",
62 * "stepsXX.csv" and "resultsXX.csv".
63 * @dataProvider get_data_for_walkthrough
64 */
764f6153 65 public function test_walkthrough_from_csv($quizsettings, $csvdata) {
3652dddd
JP
66
67 // CSV data files for these tests were generated using :
68 // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
69
70 $this->resetAfterTest(true);
71 question_bank::get_qtype('random')->clear_caches_before_testing();
72
764f6153 73 $this->create_quiz($quizsettings, $csvdata['questions']);
3652dddd
JP
74
75 $attemptids = $this->walkthrough_attempts($csvdata['steps']);
76
77 $this->check_attempts_results($csvdata['results'], $attemptids);
78
487e7e66
JP
79 $this->report = new quiz_statistics_report();
80 $whichattempts = QUIZ_GRADEAVERAGE;
81 $groupstudents = array();
82 $questions = $this->report->load_and_initialise_questions_for_calculations($this->quiz);
c3e2e754 83 list($quizstats, $questionstats) =
8da6fc9d 84 $this->report->get_all_stats_and_analysis($this->quiz, $whichattempts, $groupstudents, $questions);
487e7e66
JP
85
86 $qubaids = quiz_statistics_qubaids_condition($this->quiz->id, $groupstudents, $whichattempts);
87
88 // We will create some quiz and question stat calculator instances and some response analyser instances, just in order
89 // to check the time of the
8da6fc9d 90 $quizcalc = new \quiz_statistics\calculator();
487e7e66 91 // Should not be a delay of more than one second between the calculation of stats above and here.
7d6b28d8 92 $this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids));
487e7e66
JP
93
94 $qcalc = new \core_question\statistics\questions\calculator($questions);
7d6b28d8 95 $this->assertTimeCurrent($qcalc->get_last_calculated_time($qubaids));
487e7e66
JP
96
97 foreach ($questions as $question) {
98 if (!question_bank::get_qtype($question->qtype, false)->can_analyse_responses()) {
99 continue;
100 }
101 $responesstats = new \core_question\statistics\responses\analyser($question);
7d6b28d8 102 $this->assertTimeCurrent($responesstats->get_last_analysed_time($qubaids));
487e7e66 103 }
3652dddd 104
c3e2e754 105 // These quiz stats and the question stats found in qstats00.csv were calculated independently in spreadsheet which is
3652dddd
JP
106 // available in open document or excel format here :
107 // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet
108 $quizstatsexpected = array(
109 'median' => 4.5,
110 'firstattemptsavg' => 4.617333332,
111 'allattemptsavg' => 4.617333332,
112 'firstattemptscount' => 25,
113 'allattemptscount' => 25,
114 'standarddeviation' => 0.8117265554,
115 'skewness' => -0.092502502,
116 'kurtosis' => -0.7073968557,
117 'cic' => -87.2230935542,
118 'errorratio' => 136.8294900795,
119 'standarderror' => 1.1106813066
120 );
121
122 foreach ($quizstatsexpected as $statname => $statvalue) {
f9dc4bd7 123 $this->assertEquals($statvalue, $quizstats->$statname, $quizstats->$statname, abs($statvalue) * 1.5e-5);
3652dddd
JP
124 }
125
126 for ($rowno = 0; $rowno < $csvdata['qstats']->getRowCount(); $rowno++) {
127 $slotqstats = $csvdata['qstats']->getRow($rowno);
128 foreach ($slotqstats as $statname => $slotqstat) {
129 if ($statname !== 'slot') {
c3e2e754 130 $this->assert_stat_equals($questionstats, $slotqstats['slot'], null, null, $statname, (float)$slotqstat);
3652dddd
JP
131 }
132 }
133 }
8e328617
JP
134
135 $itemstats = array('s' => 12,
136 'effectiveweight' => null,
137 'discriminationindex' => 35.803933,
138 'discriminativeefficiency' => 39.39393939,
139 'sd' => 0.514928651,
140 'facility' => 0.583333333,
141 'maxmark' => 1,
142 'positions' => '1',
143 'slot' => null,
144 'subquestion' => true);
145 foreach ($itemstats as $statname => $expected) {
c3e2e754 146 $this->assert_stat_equals($questionstats, 1, null, 'numerical', $statname, $expected);
8e328617 147 }
4f7516f6
JP
148
149
150
151 // These variant's stats are calculated in stats_for_variant_1.xls and stats_for_variant_8.xls
152 // The calculations in the spreadsheets are the same but applied just to the attempts where the variants appeared.
153
154 $statsforslot2variants = array(1 => array('s' => 6,
155 'effectiveweight' => null,
156 'discriminationindex' => -10.5999788,
157 'discriminativeefficiency' => -14.28571429,
158 'sd' => 0.5477225575,
159 'facility' => 0.50,
160 'maxmark' => 1,
161 'variant' => 1,
162 'slot' => 2,
163 'subquestion' => false),
164 8 => array('s' => 5,
165 'effectiveweight' => null,
166 'discriminationindex' => -57.77466679,
167 'discriminativeefficiency' => -71.05263241,
168 'sd' => 0.547722558,
169 'facility' => 0.40,
170 'maxmark' => 1,
171 'variant' => 8,
172 'slot' => 2,
173 'subquestion' => false));
174 foreach ($statsforslot2variants as $variant => $stats) {
175 foreach ($stats as $statname => $expected) {
c3e2e754 176 $this->assert_stat_equals($questionstats, 2, $variant, null, $statname, $expected);
4f7516f6
JP
177 }
178 }
8e328617
JP
179 }
180
181 /**
182 * Check that the stat is as expected within a reasonable tolerance.
183 *
c3e2e754 184 * @param \core_question\statistics\questions\all_calculated_for_qubaid_condition $questionstats
8e328617
JP
185 * @param int $slot
186 * @param int|null $variant if null then not a variant stat.
187 * @param string|null $subqname if null then not an item stat.
188 * @param string $statname
189 * @param float $expected
190 */
c3e2e754 191 protected function assert_stat_equals($questionstats, $slot, $variant, $subqname, $statname, $expected) {
8e328617
JP
192
193 if ($variant === null && $subqname === null) {
c3e2e754 194 $actual = $questionstats->for_slot($slot)->{$statname};
8e328617 195 } else if ($subqname !== null) {
c3e2e754 196 $actual = $questionstats->for_subq($this->randqids[$slot][$subqname])->{$statname};
8e328617 197 } else {
c3e2e754 198 $actual = $questionstats->for_slot($slot, $variant)->{$statname};
8e328617
JP
199 }
200 if (is_bool($expected) || is_string($expected)) {
201 $this->assertEquals($expected, $actual, "$statname for slot $slot");
202 } else {
203 switch ($statname) {
204 case 'covariance' :
205 case 'discriminationindex' :
206 case 'discriminativeefficiency' :
207 case 'effectiveweight' :
208 $precision = 1e-5;
209 break;
210 default :
211 $precision = 1e-6;
212 }
213 $delta = abs($expected) * $precision;
214 $this->assertEquals(floatval($expected), $actual, "$statname for slot $slot", $delta);
215 }
3652dddd
JP
216 }
217}