MDL-41572 pass through question behaviour vars with correct prefix
[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    mod_quiz
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');
34 /**
35  * Test helper subclass of quiz_statistics_report
36  *
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
40  */
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);
49     }
50 }
52 /**
53  * Quiz attempt walk through using data from csv file.
54  *
55  * @package    mod_quiz
56  * @category   phpunit
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
60  */
61 class quiz_report_statistics_from_steps extends mod_quiz_attempt_walkthrough_from_csv_testcase {
63     /**
64      * @var quiz_statistics_report object to do stats calculations.
65      */
66     protected $report;
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";
71     }
73     protected $files = array('questions', 'steps', 'results', 'qstats');
75     /**
76      * Create a quiz add questions to it, walk through quiz attempts and then check results.
77      *
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
81      */
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(
103             'median' => 4.5,
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
114         );
116         foreach ($quizstatsexpected as $statname => $statvalue) {
117             $this->assertEquals($statvalue, $quizstats->$statname, $quizstats->$statname, abs($statvalue) * 1e-5);
118         }
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') {
124                     switch ($statname) {
125                         case 'covariance' :
126                         case 'discriminationindex' :
127                         case 'discriminativeefficiency' :
128                         case 'effectiveweight' :
129                             $precision = 1e-5;
130                             break;
131                         default :
132                             $precision = 1e-6;
133                     }
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);
138                 }
139             }
140         }
141     }