MDL-41752 question statistics class moved and improved
[moodle.git] / mod / quiz / report / statistics / statistics_question_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 about a particular question.
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
04853f27
TH
27require_once($CFG->libdir . '/tablelib.php');
28
04853f27
TH
29/**
30 * This table shows statistics about a particular question.
31 *
32 * Lists the responses that students made to this question, with frequency counts.
33 *
34 * The responses may be grouped, either by subpart of the question, or by the
35 * answer they match.
36 *
8d76124c
TH
37 * @copyright 2008 Jamie Pratt
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
04853f27 39 */
59ea8176 40class quiz_statistics_question_table extends flexible_table {
515b3ae6 41 /** @var object this question. */
04853f27
TH
42 protected $questiondata;
43
869309b8 44 /**
04853f27 45 * Constructor.
515b3ae6 46 * @param int $qid the id of the particular question whose statistics are being
04853f27 47 * displayed.
869309b8 48 */
04853f27
TH
49 public function __construct($qid) {
50 parent::__construct('mod-quiz-report-statistics-question-table' . $qid);
869309b8 51 }
52
53 /**
515b3ae6
JP
54 * @param moodle_url $reporturl
55 * @param object $questiondata
56 * @param integer $s number of attempts on this question.
57 * @param \core_question\statistics\responses\analyser $responesstats
869309b8 58 */
515b3ae6 59 public function question_setup($reporturl, $questiondata, $s, \core_question\statistics\responses\analyser $responesstats) {
04853f27 60 $this->questiondata = $questiondata;
515b3ae6 61 $this->s = $s;
04853f27
TH
62
63 $this->define_baseurl($reporturl->out());
64 $this->collapsible(false);
8dbcbe21 65 $this->set_attribute('class', 'generaltable generalbox boxaligncenter quizresponseanalysis');
04853f27 66
768a7588 67 // Define the table columns.
869309b8 68 $columns = array();
69 $headers = array();
aeb15530 70
04853f27
TH
71 if ($responesstats->has_subparts()) {
72 $columns[] = 'part';
620233b8 73 $headers[] = get_string('partofquestion', 'quiz_statistics');
869309b8 74 }
75
04853f27
TH
76 if ($responesstats->has_response_classes()) {
77 $columns[] = 'responseclass';
78 $headers[] = get_string('modelresponse', 'quiz_statistics');
79
80 if ($responesstats->has_actual_responses()) {
81 $columns[] = 'response';
82 $headers[] = get_string('actualresponse', 'quiz_statistics');
83 }
aeb15530 84
04853f27
TH
85 } else {
86 $columns[] = 'response';
87 $headers[] = get_string('response', 'quiz_statistics');
88 }
869309b8 89
04853f27
TH
90 $columns[] = 'fraction';
91 $headers[] = get_string('optiongrade', 'quiz_statistics');
aeb15530 92
04853f27
TH
93 $columns[] = 'count';
94 $headers[] = get_string('count', 'quiz_statistics');
869309b8 95
04853f27
TH
96 $columns[] = 'frequency';
97 $headers[] = get_string('frequency', 'quiz_statistics');
869309b8 98
99 $this->define_columns($columns);
100 $this->define_headers($headers);
101 $this->sortable(false);
102
04853f27
TH
103 $this->column_class('fraction', 'numcol');
104 $this->column_class('count', 'numcol');
869309b8 105 $this->column_class('frequency', 'numcol');
106
04853f27
TH
107 $this->column_suppress('part');
108 $this->column_suppress('responseclass');
869309b8 109
110 parent::setup();
111 }
aeb15530 112
04853f27
TH
113 protected function format_percentage($fraction) {
114 return format_float($fraction * 100, 2) . '%';
869309b8 115 }
aeb15530 116
04853f27
TH
117 /**
118 * The mark fraction that this response earns.
119 * @param object $response containst the data to display.
120 * @return string contents of this table cell.
121 */
122 protected function col_fraction($response) {
123 if (is_null($response->fraction)) {
869309b8 124 return '';
125 }
04853f27
TH
126
127 return $this->format_percentage($response->fraction);
869309b8 128 }
aeb15530 129
04853f27
TH
130 /**
131 * The frequency with which this response was given.
132 * @param object $response containst the data to display.
133 * @return string contents of this table cell.
134 */
135 protected function col_frequency($response) {
515b3ae6 136 if (!$this->s) {
869309b8 137 return '';
138 }
869309b8 139
515b3ae6 140 return $this->format_percentage($response->count / $this->s);
04853f27 141 }
869309b8 142}