MDL-63186 quiz_statistics: Remove q number from the summary row
[moodle.git] / mod / quiz / report / statistics / statistics_table.php
blob4c221df26abbee836e77dbfe0063ffca3349c190
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 return '';
291 if (!isset($questionstat->s)) {
292 return 0;
295 return $questionstat->s;
299 * The facility index (average fraction).
300 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
301 * @return string contents of this table cell.
303 protected function col_facility($questionstat) {
304 if (is_null($questionstat->facility)) {
305 return '';
308 return number_format($questionstat->facility*100, 2) . '%';
312 * The standard deviation of the fractions.
313 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
314 * @return string contents of this table cell.
316 protected function col_sd($questionstat) {
317 if (is_null($questionstat->sd) || $questionstat->maxmark == 0) {
318 return '';
321 return number_format($questionstat->sd*100 / $questionstat->maxmark, 2) . '%';
325 * An estimate of the fraction a student would get by guessing randomly.
326 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
327 * @return string contents of this table cell.
329 protected function col_random_guess_score($questionstat) {
330 if (is_null($questionstat->randomguessscore)) {
331 return '';
334 return number_format($questionstat->randomguessscore * 100, 2).'%';
338 * The intended question weight. Maximum mark for the question as a percentage
339 * of maximum mark for the quiz. That is, the indended influence this question
340 * on the student's overall mark.
341 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
342 * @return string contents of this table cell.
344 protected function col_intended_weight($questionstat) {
345 if ($this->is_random_question_summary($questionstat)) {
346 return '';
349 return quiz_report_scale_summarks_as_percentage($questionstat->maxmark, $this->quiz);
353 * The effective question weight. That is, an estimate of the actual
354 * influence this question has on the student's overall mark.
355 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
356 * @return string contents of this table cell.
358 protected function col_effective_weight($questionstat) {
359 global $OUTPUT;
361 if (is_null($questionstat->effectiveweight)) {
362 return '';
365 if ($questionstat->negcovar) {
366 $negcovar = get_string('negcovar', 'quiz_statistics');
368 if (!$this->is_downloading()) {
369 $negcovar = html_writer::tag('div',
370 $negcovar . $OUTPUT->help_icon('negcovar', 'quiz_statistics'),
371 array('class' => 'negcovar'));
374 return $negcovar;
377 return number_format($questionstat->effectiveweight, 2) . '%';
381 * Discrimination index. This is the product moment correlation coefficient
382 * between the fraction for this question, and the average fraction for the
383 * other questions in this quiz.
384 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
385 * @return string contents of this table cell.
387 protected function col_discrimination_index($questionstat) {
388 if (!is_numeric($questionstat->discriminationindex)) {
389 return $questionstat->discriminationindex;
392 return number_format($questionstat->discriminationindex, 2) . '%';
396 * Discrimination efficiency, similar to, but different from, the Discrimination index.
398 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
399 * @return string contents of this table cell.
401 protected function col_discriminative_efficiency($questionstat) {
402 if (!is_numeric($questionstat->discriminativeefficiency)) {
403 return '';
406 return number_format($questionstat->discriminativeefficiency, 2) . '%';
410 * This method encapsulates the test for wheter a question should be considered dubious.
411 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
412 * @return bool is this question possibly not pulling it's weight?
414 protected function is_dubious_question($questionstat) {
415 if (!is_numeric($questionstat->discriminativeefficiency)) {
416 return false;
419 return $questionstat->discriminativeefficiency < 15;
423 * Check if the given stats object is an instance of calculated_random_question_summary.
425 * @param \core_question\statistics\questions\calculated $questionstat Stats object
426 * @return bool
428 protected function is_random_question_summary($questionstat) {
429 return $questionstat instanceof calculated_random_question_summary;
432 public function wrap_html_start() {
433 // Horrible Moodle 2.0 wide-content work-around.
434 if (!$this->is_downloading()) {
435 echo html_writer::start_tag('div', array('id' => 'tablecontainer',
436 'class' => 'statistics-tablecontainer'));
440 public function wrap_html_finish() {
441 if (!$this->is_downloading()) {
442 echo html_writer::end_tag('div');