MDL-63186 quiz_statistics: Highlight the summary row if is dubious
[moodle.git] / mod / quiz / report / statistics / statistics_table.php
blob8b492132548403203e1e581e93f7275238eaa8b8
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.
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 statistics report, table for showing statistics of each question in the quiz.
20 * @package quiz_statistics
21 * @copyright 2008 Jamie Pratt
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir.'/tablelib.php');
29 use \core_question\statistics\questions\calculated_random_question_summary;
30 /**
31 * This table has one row for each question in the quiz, with sub-rows when
32 * random questions and variants appear.
34 * There are columns for the various item and position statistics.
36 * @copyright 2008 Jamie Pratt
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class quiz_statistics_table extends flexible_table {
40 /** @var object the quiz settings. */
41 protected $quiz;
43 /** @var integer the quiz course_module id. */
44 protected $cmid;
46 /**
47 * Constructor.
49 public function __construct() {
50 parent::__construct('mod-quiz-report-statistics-report');
53 /**
54 * Set up the columns and headers and other properties of the table and then
55 * call flexible_table::setup() method.
57 * @param object $quiz the quiz settings
58 * @param int $cmid the quiz course_module id
59 * @param moodle_url $reporturl the URL to redisplay this report.
60 * @param int $s number of attempts included in the statistics.
62 public function statistics_setup($quiz, $cmid, $reporturl, $s) {
63 $this->quiz = $quiz;
64 $this->cmid = $cmid;
66 // Define the table columns.
67 $columns = array();
68 $headers = array();
70 $columns[] = 'number';
71 $headers[] = get_string('questionnumber', 'quiz_statistics');
73 if (!$this->is_downloading()) {
74 $columns[] = 'icon';
75 $headers[] = '';
76 $columns[] = 'actions';
77 $headers[] = '';
78 } else {
79 $columns[] = 'qtype';
80 $headers[] = get_string('questiontype', 'quiz_statistics');
83 $columns[] = 'name';
84 $headers[] = get_string('questionname', 'quiz');
86 $columns[] = 's';
87 $headers[] = get_string('attempts', 'quiz_statistics');
89 if ($s > 1) {
90 $columns[] = 'facility';
91 $headers[] = get_string('facility', 'quiz_statistics');
93 $columns[] = 'sd';
94 $headers[] = get_string('standarddeviationq', 'quiz_statistics');
97 $columns[] = 'random_guess_score';
98 $headers[] = get_string('random_guess_score', 'quiz_statistics');
100 $columns[] = 'intended_weight';
101 $headers[] = get_string('intended_weight', 'quiz_statistics');
103 $columns[] = 'effective_weight';
104 $headers[] = get_string('effective_weight', 'quiz_statistics');
106 $columns[] = 'discrimination_index';
107 $headers[] = get_string('discrimination_index', 'quiz_statistics');
109 $columns[] = 'discriminative_efficiency';
110 $headers[] = get_string('discriminative_efficiency', 'quiz_statistics');
112 $this->define_columns($columns);
113 $this->define_headers($headers);
114 $this->sortable(false);
116 $this->column_class('s', 'numcol');
117 $this->column_class('facility', 'numcol');
118 $this->column_class('sd', 'numcol');
119 $this->column_class('random_guess_score', 'numcol');
120 $this->column_class('intended_weight', 'numcol');
121 $this->column_class('effective_weight', 'numcol');
122 $this->column_class('discrimination_index', 'numcol');
123 $this->column_class('discriminative_efficiency', 'numcol');
125 // Set up the table.
126 $this->define_baseurl($reporturl->out());
128 $this->collapsible(true);
130 $this->set_attribute('id', 'questionstatistics');
131 $this->set_attribute('class', 'generaltable generalbox boxaligncenter');
133 parent::setup();
137 * The question number.
138 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
139 * @return string contents of this table cell.
141 protected function col_number($questionstat) {
142 if ($this->is_random_question_summary($questionstat)) {
143 return '';
145 if (!isset($questionstat->question->number)) {
146 return '';
148 $number = $questionstat->question->number;
150 if (isset($questionstat->subqdisplayorder)) {
151 $number = $number . '.'.$questionstat->subqdisplayorder;
154 if ($questionstat->question->qtype != 'random' && !is_null($questionstat->variant)) {
155 $number = $number . '.'.$questionstat->variant;
158 return $number;
162 * The question type icon.
163 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
164 * @return string contents of this table cell.
166 protected function col_icon($questionstat) {
167 if ($this->is_random_question_summary($questionstat)) {
168 return '';
169 } else {
170 return print_question_icon($questionstat->question, true);
175 * Actions that can be performed on the question by this user (e.g. edit or preview).
176 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
177 * @return string contents of this table cell.
179 protected function col_actions($questionstat) {
180 if ($this->is_random_question_summary($questionstat)) {
181 return '';
182 } else {
183 return quiz_question_action_icons($this->quiz, $this->cmid,
184 $questionstat->question, $this->baseurl, $questionstat->variant);
189 * The question type name.
191 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
192 * @return string contents of this table cell.
194 protected function col_qtype($questionstat) {
195 return question_bank::get_qtype_name($questionstat->question->qtype);
199 * The question name.
201 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
202 * @return string contents of this table cell.
204 protected function col_name($questionstat) {
205 $name = $questionstat->question->name;
207 if (!is_null($questionstat->variant)) {
208 $a = new stdClass();
209 $a->name = $name;
210 $a->variant = $questionstat->variant;
211 $name = get_string('nameforvariant', 'quiz_statistics', $a);
214 if ($this->is_downloading()) {
215 return $name;
218 $baseurl = new moodle_url($this->baseurl);
219 if (!is_null($questionstat->variant)) {
220 if ($questionstat->subquestion) {
221 // Variant of a sub-question.
222 $url = new moodle_url($baseurl, array('qid' => $questionstat->questionid, 'variant' => $questionstat->variant));
223 $name = html_writer::link($url, $name, array('title' => get_string('detailedanalysisforvariant',
224 'quiz_statistics',
225 $questionstat->variant)));
226 } else if ($questionstat->slot) {
227 // Variant of a question in a slot.
228 $url = new moodle_url($baseurl, array('slot' => $questionstat->slot, 'variant' => $questionstat->variant));
229 $name = html_writer::link($url, $name, array('title' => get_string('detailedanalysisforvariant',
230 'quiz_statistics',
231 $questionstat->variant)));
233 } else {
234 if ($questionstat->subquestion && !$questionstat->get_variants()) {
235 // Sub question without variants.
236 $url = new moodle_url($baseurl, array('qid' => $questionstat->questionid));
237 $name = html_writer::link($url, $name, array('title' => get_string('detailedanalysis', 'quiz_statistics')));
238 } else if ($baseurl->param('slot') === null && $questionstat->slot) {
239 // Question in a slot, we are not on a page showing structural analysis of one slot,
240 // we don't want linking on those pages.
241 $number = $questionstat->question->number;
242 $israndomquestion = $questionstat->question->qtype == 'random';
243 $url = new moodle_url($baseurl, array('slot' => $questionstat->slot));
244 if ($israndomquestion) {
245 if ($this->is_random_question_summary($questionstat)) {
246 // Only make the random question summary row name link to the slot structure
247 // analysis page with specific text to clearly indicate the link to the user.
248 // Random question rows will render the name without a link to improve clarity
249 // in the UI.
250 $name = html_writer::link($url,
251 get_string('viewanalysis', 'quiz_statistics'),
252 array('title' => get_string('viewanalysishint', 'quiz_statistics', $number)));
254 } else if ($questionstat->get_variants() || $questionstat->get_sub_question_ids()) {
255 // Question can be broken down into sub-questions or variants. Link will show structural analysis page.
256 $name = html_writer::link($url,
257 $name,
258 array('title' => get_string('slotstructureanalysis', 'quiz_statistics', $number)));
259 } else {
260 // Question cannot be broken down into sub-questions or variants. Link will show response analysis page.
261 $name = html_writer::link($url,
262 $name,
263 array('title' => get_string('detailedanalysis', 'quiz_statistics')));
269 if ($this->is_dubious_question($questionstat)) {
270 $name = html_writer::tag('div', $name, array('class' => 'dubious'));
273 if (!empty($questionstat->minmedianmaxnotice)) {
274 $name = get_string($questionstat->minmedianmaxnotice, 'quiz_statistics') . '<br />' . $name;
277 return $name;
281 * The number of attempts at this question.
283 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
284 * @return string contents of this table cell.
286 protected function col_s($questionstat) {
287 if ($this->is_random_question_summary($questionstat)) {
288 list($min, $max) = $questionstat->get_min_max_of('s');
289 $a = new stdClass();
290 $a->min = $min ?: 0;
291 $a->max = $max ?: 0;
292 return get_string('rangebetween', 'quiz_statistics', $a);
293 } else if (!isset($questionstat->s)) {
294 return 0;
295 } else {
296 return $questionstat->s;
301 * The facility index (average fraction).
302 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
303 * @return string contents of this table cell.
305 protected function col_facility($questionstat) {
306 if ($this->is_random_question_summary($questionstat)) {
307 list($min, $max) = $questionstat->get_min_max_of('facility');
309 if (is_null($min) && is_null($max)) {
310 return '';
311 } else {
312 $a = new stdClass();
313 $a->min = get_string('percents', 'moodle', number_format($min * 100, 2));
314 $a->max = get_string('percents', 'moodle', number_format($max * 100, 2));
315 return get_string('rangebetween', 'quiz_statistics', $a);
317 } else if (is_null($questionstat->facility)) {
318 return '';
319 } else {
320 return get_string('percents', 'moodle', number_format($questionstat->facility * 100, 2));
325 * The standard deviation of the fractions.
326 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
327 * @return string contents of this table cell.
329 protected function col_sd($questionstat) {
330 if ($this->is_random_question_summary($questionstat)) {
331 list($min, $max) = $questionstat->get_min_max_of('sd');
333 if (is_null($min) && is_null($max)) {
334 return '';
335 } else {
336 $a = new stdClass();
337 $a->min = get_string('percents', 'moodle', number_format($min * 100, 2));
338 $a->max = get_string('percents', 'moodle', number_format($max * 100, 2));
339 return get_string('rangebetween', 'quiz_statistics', $a);
341 } else if (is_null($questionstat->sd) || $questionstat->maxmark == 0) {
342 return '';
343 } else {
344 return get_string('percents', 'moodle', number_format($questionstat->sd * 100 / $questionstat->maxmark, 2));
349 * An estimate of the fraction a student would get by guessing randomly.
350 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
351 * @return string contents of this table cell.
353 protected function col_random_guess_score($questionstat) {
354 if ($this->is_random_question_summary($questionstat)) {
355 list($min, $max) = $questionstat->get_min_max_of('randomguessscore');
357 if (is_null($min) && is_null($max)) {
358 return '';
359 } else {
360 $a = new stdClass();
361 $a->min = get_string('percents', 'moodle', number_format($min * 100, 2));
362 $a->max = get_string('percents', 'moodle', number_format($max * 100, 2));
364 return get_string('rangebetween', 'quiz_statistics', $a);
366 } else if (is_null($questionstat->randomguessscore)) {
367 return '';
368 } else {
369 return get_string('percents', 'moodle', number_format($questionstat->randomguessscore * 100, 2));
374 * The intended question weight. Maximum mark for the question as a percentage
375 * of maximum mark for the quiz. That is, the indended influence this question
376 * on the student's overall mark.
377 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
378 * @return string contents of this table cell.
380 protected function col_intended_weight($questionstat) {
381 if ($this->is_random_question_summary($questionstat)) {
382 list($min, $max) = $questionstat->get_min_max_of('maxmark');
384 if (is_null($min) && is_null($max)) {
385 return '';
386 } else {
387 $a = new stdClass();
388 $a->min = quiz_report_scale_summarks_as_percentage($min, $this->quiz);
389 $a->max = quiz_report_scale_summarks_as_percentage($max, $this->quiz);
390 return get_string('rangebetween', 'quiz_statistics', $a);
392 } else {
393 return quiz_report_scale_summarks_as_percentage($questionstat->maxmark, $this->quiz);
398 * The effective question weight. That is, an estimate of the actual
399 * influence this question has on the student's overall mark.
400 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
401 * @return string contents of this table cell.
403 protected function col_effective_weight($questionstat) {
404 global $OUTPUT;
406 if ($this->is_random_question_summary($questionstat)) {
407 list($min, $max) = $questionstat->get_min_max_of('effectiveweight');
409 if (is_null($min) && is_null($max)) {
410 return '';
411 } else {
412 list( , $negcovar) = $questionstat->get_min_max_of('negcovar');
413 if ($negcovar) {
414 $min = get_string('negcovar', 'quiz_statistics');
417 $a = new stdClass();
418 $a->min = $min;
419 $a->max = $max;
420 return get_string('rangebetween', 'quiz_statistics', $a);
422 } else if (is_null($questionstat->effectiveweight)) {
423 return '';
424 } else if ($questionstat->negcovar) {
425 $negcovar = get_string('negcovar', 'quiz_statistics');
427 if (!$this->is_downloading()) {
428 $negcovar = html_writer::tag('div',
429 $negcovar . $OUTPUT->help_icon('negcovar', 'quiz_statistics'),
430 array('class' => 'negcovar'));
433 return $negcovar;
434 } else {
435 return get_string('percents', 'moodle', number_format($questionstat->effectiveweight, 2));
440 * Discrimination index. This is the product moment correlation coefficient
441 * between the fraction for this question, and the average fraction for the
442 * other questions in this quiz.
443 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
444 * @return string contents of this table cell.
446 protected function col_discrimination_index($questionstat) {
447 if ($this->is_random_question_summary($questionstat)) {
448 list($min, $max) = $questionstat->get_min_max_of('discriminationindex');
450 if (is_numeric($min)) {
451 $min = get_string('percents', 'moodle', number_format($min, 2));
453 if (is_numeric($max)) {
454 $max = get_string('percents', 'moodle', number_format($max, 2));
457 $a = new stdClass();
458 $a->min = $min;
459 $a->max = $max;
460 return get_string('rangebetween', 'quiz_statistics', $a);
461 } else if (!is_numeric($questionstat->discriminationindex)) {
462 return $questionstat->discriminationindex;
463 } else {
464 return get_string('percents', 'moodle', number_format($questionstat->discriminationindex, 2));
469 * Discrimination efficiency, similar to, but different from, the Discrimination index.
471 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
472 * @return string contents of this table cell.
474 protected function col_discriminative_efficiency($questionstat) {
475 if ($this->is_random_question_summary($questionstat)) {
476 list($min, $max) = $questionstat->get_min_max_of('discriminativeefficiency');
478 if (!is_numeric($min) && !is_numeric($max)) {
479 return '';
480 } else {
481 $a = new stdClass();
482 $a->min = get_string('percents', 'moodle', number_format($min, 2));
483 $a->max = get_string('percents', 'moodle', number_format($max, 2));
484 return get_string('rangebetween', 'quiz_statistics', $a);
486 } else if (!is_numeric($questionstat->discriminativeefficiency)) {
487 return '';
488 } else {
489 return get_string('percents', 'moodle', number_format($questionstat->discriminativeefficiency, 2));
494 * This method encapsulates the test for wheter a question should be considered dubious.
495 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
496 * @return bool is this question possibly not pulling it's weight?
498 protected function is_dubious_question($questionstat) {
499 if ($this->is_random_question_summary($questionstat)) {
500 // We only care about the minimum value here.
501 // If the minimum value is less than the threshold, then we know that there is at least one value below the threshold.
502 list($discriminativeefficiency) = $questionstat->get_min_max_of('discriminativeefficiency');
503 } else {
504 $discriminativeefficiency = $questionstat->discriminativeefficiency;
507 if (!is_numeric($discriminativeefficiency)) {
508 return false;
511 return $discriminativeefficiency < 15;
515 * Check if the given stats object is an instance of calculated_random_question_summary.
517 * @param \core_question\statistics\questions\calculated $questionstat Stats object
518 * @return bool
520 protected function is_random_question_summary($questionstat) {
521 return $questionstat instanceof calculated_random_question_summary;
524 public function wrap_html_start() {
525 // Horrible Moodle 2.0 wide-content work-around.
526 if (!$this->is_downloading()) {
527 echo html_writer::start_tag('div', array('id' => 'tablecontainer',
528 'class' => 'statistics-tablecontainer'));
532 public function wrap_html_finish() {
533 if (!$this->is_downloading()) {
534 echo html_writer::end_tag('div');