MDL-41754 (2) quiz statistics : using Sam's new progress classes
[moodle.git] / mod / quiz / report / statistics / tests / stats_from_steps_walkthrough_test.php
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/>.
17 /**
18  * Quiz attempt walk through using data from csv file.
19  *
20  * @package    quiz_statistics
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  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
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 /**
36  * Quiz attempt walk through using data from csv file.
37  *
38  * @package    quiz_statistics
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  */
44 class quiz_report_statistics_from_steps_testcase extends mod_quiz_attempt_walkthrough_from_csv_testcase {
46     /**
47      * @var quiz_statistics_report object to do stats calculations.
48      */
49     protected $report;
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     }
56     protected $files = array('questions', 'steps', 'results', 'qstats');
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      */
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->resetAfterTest(true);
71         question_bank::get_qtype('random')->clear_caches_before_testing();
73         $this->create_quiz($quizsettings, $csvdata['questions']);
75         $attemptids = $this->walkthrough_attempts($csvdata['steps']);
77         $this->check_attempts_results($csvdata['results'], $attemptids);
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);
83         list($quizstats, $questionstats, $subquestionstats) =
84                         $this->report->get_all_stats_and_analysis($this->quiz, $whichattempts, $groupstudents, $questions);
86         $qubaids = quiz_statistics_qubaids_condition($this->quiz->id, $groupstudents, $whichattempts);
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
90         $quizcalc = new \quiz_statistics\calculator();
91         // Should not be a delay of more than one second between the calculation of stats above and here.
92         $this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids));
94         $qcalc = new \core_question\statistics\questions\calculator($questions);
95         $this->assertTimeCurrent($qcalc->get_last_calculated_time($qubaids));
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);
102             $this->assertTimeCurrent($responesstats->get_last_analysed_time($qubaids));
103         }
105         // These quiz stats and the question stats found in qstats00.csv were calculated independently in spreadsheets which are
106         // available in open document or excel format here :
107         // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet
109         // These quiz stats and the position stats here are calculated in stats.xls and stats.ods available, see above github URL.
110         $quizstatsexpected = array(
111             'median' => 4.5,
112             'firstattemptsavg' => 4.617333332,
113             'allattemptsavg' => 4.617333332,
114             'firstattemptscount' => 25,
115             'allattemptscount' => 25,
116             'standarddeviation' => 0.8117265554,
117             'skewness' => -0.092502502,
118             'kurtosis' => -0.7073968557,
119             'cic' => -87.2230935542,
120             'errorratio' => 136.8294900795,
121             'standarderror' => 1.1106813066
122         );
124         foreach ($quizstatsexpected as $statname => $statvalue) {
125             $this->assertEquals($statvalue, $quizstats->$statname, $quizstats->$statname, abs($statvalue) * 1.5e-5);
126         }
128         for ($rowno = 0; $rowno < $csvdata['qstats']->getRowCount(); $rowno++) {
129             $slotqstats = $csvdata['qstats']->getRow($rowno);
130             foreach ($slotqstats as $statname => $slotqstat) {
131                 if ($statname !== 'slot') {
132                     $this->assert_stat_equals($questionstats, $subquestionstats, $slotqstats['slot'],
133                                               null, null, $statname, (float)$slotqstat);
134                 }
135             }
136         }
138         $itemstats = array('s' => 12,
139                           'effectiveweight' => null,
140                           'discriminationindex' => 35.803933,
141                           'discriminativeefficiency' => 39.39393939,
142                           'sd' => 0.514928651,
143                           'facility' => 0.583333333,
144                           'maxmark' => 1,
145                           'positions' => '1',
146                           'slot' => null,
147                           'subquestion' => true);
148         foreach ($itemstats as $statname => $expected) {
149             $this->assert_stat_equals($questionstats, $subquestionstats, 1, null, 'numerical', $statname, $expected);
150         }
151     }
153     /**
154      * Check that the stat is as expected within a reasonable tolerance.
155      *
156      * @param \core_question\statistics\questions\calculated[] $questionstats
157      * @param \core_question\statistics\questions\calculated_for_subquestion[] $subquestionstats
158      * @param int                                              $slot
159      * @param int|null                                         $variant if null then not a variant stat.
160      * @param string|null                                      $subqname if null then not an item stat.
161      * @param string                                           $statname
162      * @param float                                            $expected
163      */
164     protected function assert_stat_equals($questionstats, $subquestionstats, $slot, $variant, $subqname, $statname, $expected) {
166         if ($variant === null && $subqname === null) {
167             $actual = $questionstats[$slot]->{$statname};
168         } else if ($subqname !== null) {
169             $actual = $subquestionstats[$this->randqids[$slot][$subqname]]->{$statname};
170         } else {
171             $actual = $questionstats[$slot]->variantstats[$variant]->{$statname};
172         }
173         if (is_bool($expected) || is_string($expected)) {
174             $this->assertEquals($expected, $actual, "$statname for slot $slot");
175         } else {
176             switch ($statname) {
177                 case 'covariance' :
178                 case 'discriminationindex' :
179                 case 'discriminativeefficiency' :
180                 case 'effectiveweight' :
181                     $precision = 1e-5;
182                     break;
183                 default :
184                     $precision = 1e-6;
185             }
186             $delta = abs($expected) * $precision;
187             $this->assertEquals(floatval($expected), $actual, "$statname for slot $slot", $delta);
188         }
189     }