2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Quiz statistics report, table for showing statistics about a particular question.
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');
30 * This table shows statistics about a particular question.
32 * Lists the responses that students made to this question, with frequency counts.
34 * The responses may be grouped, either by subpart of the question, or by the
37 * @copyright 2008 Jamie Pratt
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class quiz_statistics_question_table extends flexible_table {
41 /** @var object this question. */
42 protected $questiondata;
46 * @param int $qid the id of the particular question whose statistics are being
49 public function __construct($qid) {
50 parent::__construct('mod-quiz-report-statistics-question-table' . $qid);
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
59 public function question_setup($reporturl, $questiondata, $s, \core_question\statistics\responses\analyser $responesstats) {
60 $this->questiondata = $questiondata;
63 $this->define_baseurl($reporturl->out());
64 $this->collapsible(false);
65 $this->set_attribute('class', 'generaltable generalbox boxaligncenter quizresponseanalysis');
67 // Define the table columns.
71 if ($responesstats->has_subparts()) {
73 $headers[] = get_string('partofquestion', 'quiz_statistics');
76 if ($responesstats->has_response_classes()) {
77 $columns[] = 'responseclass';
78 $headers[] = get_string('modelresponse', 'quiz_statistics');
80 if ($responesstats->has_actual_responses()) {
81 $columns[] = 'response';
82 $headers[] = get_string('actualresponse', 'quiz_statistics');
86 $columns[] = 'response';
87 $headers[] = get_string('response', 'quiz_statistics');
90 $columns[] = 'fraction';
91 $headers[] = get_string('optiongrade', 'quiz_statistics');
94 $headers[] = get_string('count', 'quiz_statistics');
96 $columns[] = 'frequency';
97 $headers[] = get_string('frequency', 'quiz_statistics');
99 $this->define_columns($columns);
100 $this->define_headers($headers);
101 $this->sortable(false);
103 $this->column_class('fraction', 'numcol');
104 $this->column_class('count', 'numcol');
105 $this->column_class('frequency', 'numcol');
107 $this->column_suppress('part');
108 $this->column_suppress('responseclass');
113 protected function format_percentage($fraction) {
114 return format_float($fraction * 100, 2) . '%';
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.
122 protected function col_fraction($response) {
123 if (is_null($response->fraction)) {
127 return $this->format_percentage($response->fraction);
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.
135 protected function col_frequency($response) {
140 return $this->format_percentage($response->count / $this->s);