Merge branch 'MDL-42957-26' into MDL-42957-master
[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');
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 */
41class testable_quiz_statistics_report extends quiz_statistics_report {
42
6dd9362e
JP
43 public function get_stats($quiz, $whichattempts = QUIZ_GRADEAVERAGE, $groupstudents = array()) {
44 $qubaids = quiz_statistics_qubaids_condition($quiz->id, $groupstudents, $whichattempts);
e68e4ccf 45 $this->clear_cached_data($qubaids);
3652dddd 46 $questions = $this->load_and_initialise_questions_for_calculations($quiz);
6dd9362e 47 return $this->get_quiz_and_questions_stats($quiz, $whichattempts, $groupstudents, $questions);
3652dddd
JP
48 }
49}
50
51/**
52 * Quiz attempt walk through using data from csv file.
53 *
e68e4ccf 54 * @package quiz_statistics
3652dddd
JP
55 * @category phpunit
56 * @copyright 2013 The Open University
57 * @author Jamie Pratt <me@jamiep.org>
58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
59 */
60class quiz_report_statistics_from_steps extends mod_quiz_attempt_walkthrough_from_csv_testcase {
61
62 /**
63 * @var quiz_statistics_report object to do stats calculations.
64 */
65 protected $report;
66
67 protected function get_full_path_of_csv_file($setname, $test) {
68 // Overridden here so that __DIR__ points to the path of this file.
69 return __DIR__."/fixtures/{$setname}{$test}.csv";
70 }
71
72 protected $files = array('questions', 'steps', 'results', 'qstats');
73
74 /**
75 * Create a quiz add questions to it, walk through quiz attempts and then check results.
76 *
77 * @param PHPUnit_Extensions_Database_DataSet_ITable[] of data read from csv file "questionsXX.csv",
78 * "stepsXX.csv" and "resultsXX.csv".
79 * @dataProvider get_data_for_walkthrough
80 */
764f6153 81 public function test_walkthrough_from_csv($quizsettings, $csvdata) {
3652dddd
JP
82
83 // CSV data files for these tests were generated using :
84 // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/responsegenerator
85
86 $this->resetAfterTest(true);
87 question_bank::get_qtype('random')->clear_caches_before_testing();
88
764f6153 89 $this->create_quiz($quizsettings, $csvdata['questions']);
3652dddd
JP
90
91 $attemptids = $this->walkthrough_attempts($csvdata['steps']);
92
93 $this->check_attempts_results($csvdata['results'], $attemptids);
94
95 $this->report = new testable_quiz_statistics_report();
515b3ae6 96 list($quizstats, $questionstats, $subquestionstats) = $this->report->get_stats($this->quiz);
3652dddd
JP
97
98 // These quiz stats and the question stats found in qstats00.csv were calculated independently in spreadsheet which is
99 // available in open document or excel format here :
100 // https://github.com/jamiepratt/moodle-quiz-tools/tree/master/statsspreadsheet
101 $quizstatsexpected = array(
102 'median' => 4.5,
103 'firstattemptsavg' => 4.617333332,
104 'allattemptsavg' => 4.617333332,
105 'firstattemptscount' => 25,
106 'allattemptscount' => 25,
107 'standarddeviation' => 0.8117265554,
108 'skewness' => -0.092502502,
109 'kurtosis' => -0.7073968557,
110 'cic' => -87.2230935542,
111 'errorratio' => 136.8294900795,
112 'standarderror' => 1.1106813066
113 );
114
115 foreach ($quizstatsexpected as $statname => $statvalue) {
f9dc4bd7 116 $this->assertEquals($statvalue, $quizstats->$statname, $quizstats->$statname, abs($statvalue) * 1.5e-5);
3652dddd
JP
117 }
118
119 for ($rowno = 0; $rowno < $csvdata['qstats']->getRowCount(); $rowno++) {
120 $slotqstats = $csvdata['qstats']->getRow($rowno);
121 foreach ($slotqstats as $statname => $slotqstat) {
122 if ($statname !== 'slot') {
123 switch ($statname) {
124 case 'covariance' :
125 case 'discriminationindex' :
126 case 'discriminativeefficiency' :
127 case 'effectiveweight' :
128 $precision = 1e-5;
129 break;
130 default :
131 $precision = 1e-6;
132 }
133 $slot = $slotqstats['slot'];
134 $delta = abs($slotqstat) * $precision;
515b3ae6 135 $actual = $questionstats[$slot]->{$statname};
3652dddd
JP
136 $this->assertEquals(floatval($slotqstat), $actual, "$statname for slot $slot", $delta);
137 }
138 }
139 }
140 }
141}