Commit | Line | Data |
---|---|---|
7de1e35b 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 | * The statistics calculator returns an instance of this class which contains the calculated statistics. | |
19 | * | |
20 | * @package quiz_statistics | |
21 | * @copyright 2013 The Open University | |
22 | * @author James Pratt me@jamiep.org | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | class quiz_statistics_calculated { | |
26 | ||
27 | public function __construct($allattempts = null) { | |
28 | if ($allattempts !== null) { | |
29 | $this->allattempts = $allattempts; | |
30 | } | |
31 | } | |
32 | ||
33 | /** | |
34 | * @var bool whether we are calculating calculate stats from all attempts. | |
35 | */ | |
36 | public $allattempts; | |
37 | ||
38 | public $firstattemptscount = 0; | |
39 | ||
40 | public $allattemptscount = 0; | |
41 | ||
42 | public $firstattemptsavg; | |
43 | ||
44 | public $allattemptsavg; | |
45 | ||
46 | public $firstattemptstotal = 0; | |
47 | ||
48 | public $allattemptstotal = 0; | |
49 | ||
50 | public $median; | |
51 | ||
52 | public $standarddeviation; | |
53 | ||
54 | public $skewness; | |
55 | ||
56 | public $kurtosis; | |
57 | ||
58 | public $cic; | |
59 | ||
60 | public $errorratio; | |
61 | ||
62 | public $standarderror; | |
63 | ||
64 | public $timemodified; | |
65 | ||
66 | public function s() { | |
67 | if ($this->allattempts) { | |
68 | return $this->allattemptscount; | |
69 | } else { | |
70 | return $this->firstattemptscount; | |
71 | } | |
72 | } | |
73 | ||
74 | public function avg() { | |
75 | if ($this->allattempts) { | |
76 | return $this->allattemptsavg; | |
77 | } else { | |
78 | return $this->firstattemptsavg; | |
79 | } | |
80 | } | |
81 | ||
82 | public function total() { | |
83 | if ($this->allattempts) { | |
84 | return $this->allattemptstotal; | |
85 | } else { | |
86 | return $this->firstattemptstotal; | |
87 | } | |
88 | } | |
89 | ||
90 | /** | |
91 | * @param $course | |
92 | * @param $cm | |
93 | * @param $quiz | |
94 | * @return array to display in table or spreadsheet. | |
95 | */ | |
96 | public function get_formatted_quiz_info_data($course, $cm, $quiz) { | |
97 | ||
98 | // You can edit this array to control which statistics are displayed. | |
99 | $todisplay = array('firstattemptscount' => 'number', | |
100 | 'allattemptscount' => 'number', | |
101 | 'firstattemptsavg' => 'summarks_as_percentage', | |
102 | 'allattemptsavg' => 'summarks_as_percentage', | |
103 | 'median' => 'summarks_as_percentage', | |
104 | 'standarddeviation' => 'summarks_as_percentage', | |
105 | 'skewness' => 'number_format', | |
106 | 'kurtosis' => 'number_format', | |
107 | 'cic' => 'number_format_percent', | |
108 | 'errorratio' => 'number_format_percent', | |
109 | 'standarderror' => 'summarks_as_percentage'); | |
110 | ||
111 | // General information about the quiz. | |
112 | $quizinfo = array(); | |
113 | $quizinfo[get_string('quizname', 'quiz_statistics')] = format_string($quiz->name); | |
114 | $quizinfo[get_string('coursename', 'quiz_statistics')] = format_string($course->fullname); | |
115 | if ($cm->idnumber) { | |
116 | $quizinfo[get_string('idnumbermod')] = $cm->idnumber; | |
117 | } | |
118 | if ($quiz->timeopen) { | |
119 | $quizinfo[get_string('quizopen', 'quiz')] = userdate($quiz->timeopen); | |
120 | } | |
121 | if ($quiz->timeclose) { | |
122 | $quizinfo[get_string('quizclose', 'quiz')] = userdate($quiz->timeclose); | |
123 | } | |
124 | if ($quiz->timeopen && $quiz->timeclose) { | |
125 | $quizinfo[get_string('duration', 'quiz_statistics')] = | |
126 | format_time($quiz->timeclose - $quiz->timeopen); | |
127 | } | |
128 | ||
129 | // The statistics. | |
130 | foreach ($todisplay as $property => $format) { | |
131 | if (!isset($this->$property)) { | |
132 | continue; | |
133 | } | |
134 | $value = $this->$property; | |
135 | ||
136 | switch ($format) { | |
137 | case 'summarks_as_percentage': | |
138 | $formattedvalue = quiz_report_scale_summarks_as_percentage($value, $quiz); | |
139 | break; | |
140 | case 'number_format_percent': | |
141 | $formattedvalue = quiz_format_grade($quiz, $value) . '%'; | |
142 | break; | |
143 | case 'number_format': | |
144 | // 2 extra decimal places, since not a percentage, | |
145 | // and we want the same number of sig figs. | |
146 | $formattedvalue = format_float($value, $quiz->decimalpoints + 2); | |
147 | break; | |
148 | case 'number': | |
149 | $formattedvalue = $value + 0; | |
150 | break; | |
151 | default: | |
152 | $formattedvalue = $value; | |
153 | } | |
154 | ||
155 | $quizinfo[get_string($property, 'quiz_statistics', $this->using_attempts_string())] = $formattedvalue; | |
156 | } | |
157 | ||
158 | return $quizinfo; | |
159 | } | |
160 | ||
161 | /** | |
162 | * @return string the appropriate lang string to describe this option. | |
163 | */ | |
164 | protected function using_attempts_string() { | |
165 | if ($this->allattempts) { | |
166 | return get_string('allattempts', 'quiz_statistics'); | |
167 | } else { | |
168 | return get_string('firstattempts', 'quiz_statistics'); | |
169 | } | |
170 | } | |
171 | ||
172 | ||
173 | protected $fieldsindb = array('allattempts', 'firstattemptscount', 'allattemptscount', 'firstattemptsavg', 'allattemptsavg', | |
174 | 'median', 'standarddeviation', 'skewness', | |
175 | 'kurtosis', 'cic', 'errorratio', 'standarderror'); | |
176 | ||
177 | /** | |
178 | * @param $qubaids qubaid_condition | |
179 | */ | |
180 | public function cache($qubaids) { | |
181 | global $DB; | |
182 | ||
183 | $toinsert = new stdClass(); | |
184 | ||
185 | foreach ($this->fieldsindb as $field) { | |
186 | $toinsert->{$field} = $this->{$field}; | |
187 | } | |
188 | ||
189 | $toinsert->hashcode = $qubaids->get_hash_code(); | |
190 | $toinsert->timemodified = time(); | |
191 | ||
192 | // Fix up some dodgy data. | |
193 | if (isset($toinsert->errorratio) && is_nan($toinsert->errorratio)) { | |
194 | $toinsert->errorratio = null; | |
195 | } | |
196 | if (isset($toinsert->standarderror) && is_nan($toinsert->standarderror)) { | |
197 | $toinsert->standarderror = null; | |
198 | } | |
199 | ||
200 | // Store the data. | |
201 | $DB->insert_record('quiz_statistics', $toinsert); | |
202 | ||
203 | } | |
204 | ||
205 | public function populate_from_record($record) { | |
206 | foreach ($this->fieldsindb as $field) { | |
207 | $this->$field = $record->$field; | |
208 | } | |
209 | $this->timemodified = $record->timemodified; | |
210 | } | |
211 | } |