MDL-41757 quiz statistics : limit subq and variant stat rows
[moodle.git] / mod / quiz / report / statistics / statistics_table.php
CommitLineData
aeb15530 1<?php
04853f27
TH
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 statistics report, table for showing statistics of each question in the quiz.
19 *
8d76124c
TH
20 * @package quiz_statistics
21 * @copyright 2008 Jamie Pratt
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
04853f27
TH
23 */
24
a17b297d
TH
25defined('MOODLE_INTERNAL') || die();
26
6f51ed72 27require_once($CFG->libdir.'/tablelib.php');
b3db3620 28
04853f27
TH
29/**
30 * This table has one row for each question in the quiz, with sub-rows when
31 * random questions appear. There are columns for the various statistics.
32 *
8d76124c
TH
33 * @copyright 2008 Jamie Pratt
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
04853f27 35 */
59ea8176 36class quiz_statistics_table extends flexible_table {
04853f27
TH
37 /** @var object the quiz settings. */
38 protected $quiz;
aeb15530 39
04853f27
TH
40 /** @var integer the quiz course_module id. */
41 protected $cmid;
aeb15530 42
04853f27
TH
43 /**
44 * Constructor.
45 */
c7df5006 46 public function __construct() {
04853f27 47 parent::__construct('mod-quiz-report-statistics-report');
b3db3620 48 }
07e68609 49
c0250840 50 /**
90fbad64 51 * Set up the columns and headers and other properties of the table and then
c0250840 52 * call flexible_table::setup() method.
04853f27
TH
53 *
54 * @param object $quiz the quiz settings
f7970e3c 55 * @param int $cmid the quiz course_module id
04853f27 56 * @param moodle_url $reporturl the URL to redisplay this report.
f7970e3c 57 * @param int $s number of attempts included in the statistics.
c0250840 58 */
90fbad64 59 public function statistics_setup($quiz, $cmid, $reporturl, $s) {
c0250840 60 $this->quiz = $quiz;
61 $this->cmid = $cmid;
04853f27 62
768a7588 63 // Define the table columns.
c0250840 64 $columns = array();
65 $headers = array();
aeb15530 66
04853f27
TH
67 $columns[] = 'number';
68 $headers[] = get_string('questionnumber', 'quiz_statistics');
aeb15530 69
04853f27
TH
70 if (!$this->is_downloading()) {
71 $columns[] = 'icon';
72 $headers[] = '';
73 $columns[] = 'actions';
74 $headers[] = '';
b3db3620 75 } else {
04853f27
TH
76 $columns[] = 'qtype';
77 $headers[] = get_string('questiontype', 'quiz_statistics');
b3db3620 78 }
aeb15530 79
04853f27
TH
80 $columns[] = 'name';
81 $headers[] = get_string('questionname', 'quiz');
82
83 $columns[] = 's';
84 $headers[] = get_string('attempts', 'quiz_statistics');
f8d4e7b1 85
04853f27
TH
86 if ($s > 1) {
87 $columns[] = 'facility';
88 $headers[] = get_string('facility', 'quiz_statistics');
aeb15530 89
04853f27
TH
90 $columns[] = 'sd';
91 $headers[] = get_string('standarddeviationq', 'quiz_statistics');
f8d4e7b1 92 }
aeb15530 93
04853f27
TH
94 $columns[] = 'random_guess_score';
95 $headers[] = get_string('random_guess_score', 'quiz_statistics');
aeb15530 96
04853f27
TH
97 $columns[] = 'intended_weight';
98 $headers[] = get_string('intended_weight', 'quiz_statistics');
aeb15530 99
04853f27
TH
100 $columns[] = 'effective_weight';
101 $headers[] = get_string('effective_weight', 'quiz_statistics');
aeb15530 102
04853f27
TH
103 $columns[] = 'discrimination_index';
104 $headers[] = get_string('discrimination_index', 'quiz_statistics');
105
106 $columns[] = 'discriminative_efficiency';
107 $headers[] = get_string('discriminative_efficiency', 'quiz_statistics');
43ec99aa 108
c0250840 109 $this->define_columns($columns);
110 $this->define_headers($headers);
111 $this->sortable(false);
112
74af927e 113 $this->column_class('s', 'numcol');
04853f27
TH
114 $this->column_class('facility', 'numcol');
115 $this->column_class('sd', 'numcol');
74af927e 116 $this->column_class('random_guess_score', 'numcol');
117 $this->column_class('intended_weight', 'numcol');
ea751786 118 $this->column_class('effective_weight', 'numcol');
ea751786 119 $this->column_class('discrimination_index', 'numcol');
120 $this->column_class('discriminative_efficiency', 'numcol');
74af927e 121
768a7588 122 // Set up the table.
c0250840 123 $this->define_baseurl($reporturl->out());
124
125 $this->collapsible(true);
126
c0250840 127 $this->set_attribute('id', 'questionstatistics');
128 $this->set_attribute('class', 'generaltable generalbox boxaligncenter');
129
130 parent::setup();
b3db3620 131 }
132
04853f27
TH
133 /**
134 * The question number.
515b3ae6 135 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
136 * @return string contents of this table cell.
137 */
515b3ae6 138 protected function col_number($questionstat) {
1239d287
JP
139 $number = $questionstat->question->number;
140
df9ddae6 141 if (isset($questionstat->subqdisplayorder)) {
1239d287
JP
142 $number = $number . '.'.$questionstat->subqdisplayorder;
143 }
144
145 if ($questionstat->question->qtype != 'random' && !is_null($questionstat->variant)) {
146 $number = $number . '.'.$questionstat->variant;
43ec99aa 147 }
c0250840 148
1239d287 149 return $number;
b3db3620 150 }
d3e99e85 151
152 /**
04853f27 153 * The question type icon.
515b3ae6 154 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27 155 * @return string contents of this table cell.
d3e99e85 156 */
515b3ae6
JP
157 protected function col_icon($questionstat) {
158 return print_question_icon($questionstat->question, true);
d3e99e85 159 }
aeb15530 160
04853f27
TH
161 /**
162 * Actions that can be performed on the question by this user (e.g. edit or preview).
515b3ae6 163 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
164 * @return string contents of this table cell.
165 */
515b3ae6
JP
166 protected function col_actions($questionstat) {
167 return quiz_question_action_icons($this->quiz, $this->cmid, $questionstat->question, $this->baseurl);
b3db3620 168 }
aeb15530 169
04853f27
TH
170 /**
171 * The question type name.
515b3ae6
JP
172 *
173 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
174 * @return string contents of this table cell.
175 */
515b3ae6
JP
176 protected function col_qtype($questionstat) {
177 return question_bank::get_qtype_name($questionstat->question->qtype);
04853f27
TH
178 }
179
180 /**
181 * The question name.
515b3ae6
JP
182 *
183 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
184 * @return string contents of this table cell.
185 */
515b3ae6
JP
186 protected function col_name($questionstat) {
187 $name = $questionstat->question->name;
04853f27 188
1239d287
JP
189 if (!is_null($questionstat->variant)) {
190 $a = new stdClass();
191 $a->name = $name;
192 $a->variant = $questionstat->variant;
193 $name = get_string('nameforvariant', 'quiz_statistics', $a);
194 }
195
04853f27
TH
196 if ($this->is_downloading()) {
197 return $name;
4f5ffac0 198 }
04853f27 199
39153237 200 $url = null;
515b3ae6
JP
201 if ($questionstat->subquestion) {
202 $url = new moodle_url($this->baseurl, array('qid' => $questionstat->questionid));
203 } else if ($questionstat->slot && $questionstat->question->qtype != 'random') {
204 $url = new moodle_url($this->baseurl, array('slot' => $questionstat->slot));
04853f27
TH
205 }
206
39153237
TH
207 if ($url) {
208 $name = html_writer::link($url, $name,
209 array('title' => get_string('detailedanalysis', 'quiz_statistics')));
04853f27
TH
210 }
211
515b3ae6 212 if ($this->is_dubious_question($questionstat)) {
39153237 213 $name = html_writer::tag('div', $name, array('class' => 'dubious'));
04853f27
TH
214 }
215
df9ddae6
JP
216 if (!empty($questionstat->minmedianmaxnotice)) {
217 $name = $questionstat->minmedianmaxnotice . '<br />' . $name;
218 }
219
04853f27 220 return $name;
b3db3620 221 }
04853f27
TH
222
223 /**
224 * The number of attempts at this question.
515b3ae6
JP
225 *
226 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
227 * @return string contents of this table cell.
228 */
515b3ae6
JP
229 protected function col_s($questionstat) {
230 if (!isset($questionstat->s)) {
04853f27 231 return 0;
4f5ffac0 232 }
04853f27 233
515b3ae6 234 return $questionstat->s;
ea751786 235 }
04853f27
TH
236
237 /**
238 * The facility index (average fraction).
515b3ae6 239 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
240 * @return string contents of this table cell.
241 */
515b3ae6
JP
242 protected function col_facility($questionstat) {
243 if (is_null($questionstat->facility)) {
04853f27 244 return '';
ea751786 245 }
04853f27 246
515b3ae6 247 return number_format($questionstat->facility*100, 2) . '%';
ea751786 248 }
04853f27
TH
249
250 /**
251 * The standard deviation of the fractions.
515b3ae6 252 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
253 * @return string contents of this table cell.
254 */
515b3ae6
JP
255 protected function col_sd($questionstat) {
256 if (is_null($questionstat->sd) || $questionstat->maxmark == 0) {
4f5ffac0 257 return '';
258 }
04853f27 259
515b3ae6 260 return number_format($questionstat->sd*100 / $questionstat->maxmark, 2) . '%';
ea751786 261 }
04853f27
TH
262
263 /**
264 * An estimate of the fraction a student would get by guessing randomly.
515b3ae6 265 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
266 * @return string contents of this table cell.
267 */
515b3ae6
JP
268 protected function col_random_guess_score($questionstat) {
269 if (is_null($questionstat->randomguessscore)) {
04853f27 270 return '';
455c3efa 271 }
04853f27 272
515b3ae6 273 return number_format($questionstat->randomguessscore * 100, 2).'%';
6f51ed72 274 }
aeb15530 275
04853f27
TH
276 /**
277 * The intended question weight. Maximum mark for the question as a percentage
278 * of maximum mark for the quiz. That is, the indended influence this question
279 * on the student's overall mark.
515b3ae6 280 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
281 * @return string contents of this table cell.
282 */
515b3ae6
JP
283 protected function col_intended_weight($questionstat) {
284 return quiz_report_scale_summarks_as_percentage($questionstat->maxmark, $this->quiz);
04853f27
TH
285 }
286
287 /**
288 * The effective question weight. That is, an estimate of the actual
289 * influence this question has on the student's overall mark.
515b3ae6 290 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
291 * @return string contents of this table cell.
292 */
515b3ae6 293 protected function col_effective_weight($questionstat) {
442a3c0b
TH
294 global $OUTPUT;
295
515b3ae6 296 if ($questionstat->subquestion) {
aa3133bc 297 return '';
298 }
04853f27 299
515b3ae6 300 if ($questionstat->negcovar) {
04853f27
TH
301 $negcovar = get_string('negcovar', 'quiz_statistics');
302
303 if (!$this->is_downloading()) {
442a3c0b
TH
304 $negcovar = html_writer::tag('div',
305 $negcovar . $OUTPUT->help_icon('negcovar', 'quiz_statistics'),
306 array('class' => 'negcovar'));
04853f27
TH
307 }
308
309 return $negcovar;
310 }
311
515b3ae6 312 return number_format($questionstat->effectiveweight, 2) . '%';
f8d4e7b1 313 }
04853f27
TH
314
315 /**
316 * Discrimination index. This is the product moment correlation coefficient
515b3ae6 317 * between the fraction for this question, and the average fraction for the
04853f27 318 * other questions in this quiz.
515b3ae6 319 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
320 * @return string contents of this table cell.
321 */
515b3ae6
JP
322 protected function col_discrimination_index($questionstat) {
323 if (!is_numeric($questionstat->discriminationindex)) {
324 return $questionstat->discriminationindex;
e72efdd4 325 }
04853f27 326
515b3ae6 327 return number_format($questionstat->discriminationindex, 2) . '%';
f8d4e7b1 328 }
04853f27
TH
329
330 /**
331 * Discrimination efficiency, similar to, but different from, the Discrimination index.
515b3ae6
JP
332 *
333 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
04853f27
TH
334 * @return string contents of this table cell.
335 */
515b3ae6
JP
336 protected function col_discriminative_efficiency($questionstat) {
337 if (!is_numeric($questionstat->discriminativeefficiency)) {
1f23bda7 338 return '';
339 }
04853f27 340
515b3ae6 341 return number_format($questionstat->discriminativeefficiency, 2) . '%';
04853f27
TH
342 }
343
344 /**
345 * This method encapsulates the test for wheter a question should be considered dubious.
515b3ae6 346 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
f7970e3c 347 * @return bool is this question possibly not pulling it's weight?
04853f27 348 */
515b3ae6
JP
349 protected function is_dubious_question($questionstat) {
350 if (!is_numeric($questionstat->discriminativeefficiency)) {
04853f27
TH
351 return false;
352 }
353
515b3ae6 354 return $questionstat->discriminativeefficiency < 15;
f8d4e7b1 355 }
04853f27 356
9db17898 357 public function wrap_html_start() {
04853f27 358 // Horrible Moodle 2.0 wide-content work-around.
9db17898 359 if (!$this->is_downloading()) {
04853f27
TH
360 echo html_writer::start_tag('div', array('id' => 'tablecontainer',
361 'class' => 'statistics-tablecontainer'));
9db17898
SH
362 }
363 }
04853f27 364
9db17898
SH
365 public function wrap_html_finish() {
366 if (!$this->is_downloading()) {
04853f27 367 echo html_writer::end_tag('div');
9db17898
SH
368 }
369 }
b3db3620 370}