MDL-42957 (3) quiz statistics : removing assertEquals(time(), ..) tests
[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');
36 /**
37  * Quiz attempt walk through using data from csv file.
38  *
39  * @package    quiz_statistics
40  * @category   phpunit
41  * @copyright  2013 The Open University
42  * @author     Jamie Pratt <me@jamiep.org>
43  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44  */
45 class quiz_report_statistics_from_steps extends mod_quiz_attempt_walkthrough_from_csv_testcase {
47     /**
48      * @var quiz_statistics_report object to do stats calculations.
49      */
50     protected $report;
52     protected function get_full_path_of_csv_file($setname, $test) {
53         // Overridden here so that __DIR__ points to the path of this file.
54         return  __DIR__."/fixtures/{$setname}{$test}.csv";
55     }
57     protected $files = array('questions', 'steps', 'results', 'qstats');
59     /**
60      * Create a quiz add questions to it, walk through quiz attempts and then check results.
61      *
62      * @param PHPUnit_Extensions_Database_DataSet_ITable[] of data read from csv file "questionsXX.csv",
63      *                                                                                  "stepsXX.csv" and "resultsXX.csv".
64      * @dataProvider get_data_for_walkthrough
65      */
66     public function test_walkthrough_from_csv($quizsettings, $csvdata) {
68         // CSV data files for these tests were generated using :
69         // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
71         $this->resetAfterTest(true);
72         question_bank::get_qtype('random')->clear_caches_before_testing();
74         $this->create_quiz($quizsettings, $csvdata['questions']);
76         $attemptids = $this->walkthrough_attempts($csvdata['steps']);
78         $this->check_attempts_results($csvdata['results'], $attemptids);
80         $this->report = new quiz_statistics_report();
81         $whichattempts = QUIZ_GRADEAVERAGE;
82         $groupstudents = array();
83         $questions = $this->report->load_and_initialise_questions_for_calculations($this->quiz);
84         list($quizstats, $questionstats, $subquestionstats) =
85                         $this->report->get_quiz_and_questions_stats($this->quiz, $whichattempts, $groupstudents, $questions);
87         $qubaids = quiz_statistics_qubaids_condition($this->quiz->id, $groupstudents, $whichattempts);
89         // We will create some quiz and question stat calculator instances and some response analyser instances, just in order
90         // to check the time of the
91         $quizcalc = new quiz_statistics_calculator();
92         // Should not be a delay of more than one second between the calculation of stats above and here.
93         $this->assertTimeCurrent($quizcalc->get_last_calculated_time($qubaids));
95         $qcalc = new \core_question\statistics\questions\calculator($questions);
96         $this->assertTimeCurrent($qcalc->get_last_calculated_time($qubaids));
98         foreach ($questions as $question) {
99             if (!question_bank::get_qtype($question->qtype, false)->can_analyse_responses()) {
100                 continue;
101             }
102             $responesstats = new \core_question\statistics\responses\analyser($question);
103             $this->assertTimeCurrent($responesstats->get_last_analysed_time($qubaids));
104         }
106         // These quiz stats and the question stats found in qstats00.csv were calculated independently in spreadsheet which is
107         // available in open document or excel format here :
108         // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet
109         $quizstatsexpected = array(
110             'median' => 4.5,
111             'firstattemptsavg' => 4.617333332,
112             'allattemptsavg' => 4.617333332,
113             'firstattemptscount' => 25,
114             'allattemptscount' => 25,
115             'standarddeviation' => 0.8117265554,
116             'skewness' => -0.092502502,
117             'kurtosis' => -0.7073968557,
118             'cic' => -87.2230935542,
119             'errorratio' => 136.8294900795,
120             'standarderror' => 1.1106813066
121         );
123         foreach ($quizstatsexpected as $statname => $statvalue) {
124             $this->assertEquals($statvalue, $quizstats->$statname, $quizstats->$statname, abs($statvalue) * 1.5e-5);
125         }
127         for ($rowno = 0; $rowno < $csvdata['qstats']->getRowCount(); $rowno++) {
128             $slotqstats = $csvdata['qstats']->getRow($rowno);
129             foreach ($slotqstats as $statname => $slotqstat) {
130                 if ($statname !== 'slot') {
131                     switch ($statname) {
132                         case 'covariance' :
133                         case 'discriminationindex' :
134                         case 'discriminativeefficiency' :
135                         case 'effectiveweight' :
136                             $precision = 1e-5;
137                             break;
138                         default :
139                             $precision = 1e-6;
140                     }
141                     $slot = $slotqstats['slot'];
142                     $delta = abs($slotqstat) * $precision;
143                     $actual = $questionstats[$slot]->{$statname};
144                     $this->assertEquals(floatval($slotqstat), $actual, "$statname for slot $slot", $delta);
145                 }
146             }
147         }
148     }