weekly release 2.7dev
[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 {
d50b05e6 41 /** @var object full question object for this question. */
04853f27
TH
42 protected $questiondata;
43
d50b05e6
JP
44 /** @var int no of attempts. */
45 protected $s;
46
869309b8 47 /**
04853f27 48 * Constructor.
515b3ae6 49 * @param int $qid the id of the particular question whose statistics are being
04853f27 50 * displayed.
869309b8 51 */
04853f27
TH
52 public function __construct($qid) {
53 parent::__construct('mod-quiz-report-statistics-question-table' . $qid);
869309b8 54 }
55
56 /**
d50b05e6
JP
57 * @param moodle_url $reporturl
58 * @param object $questiondata
59 * @param integer $s number of attempts on this question.
60 * @param \core_question\statistics\responses\analysis_for_question $responseanalysis
869309b8 61 */
d50b05e6 62 public function question_setup($reporturl, $questiondata, $s, $responseanalysis) {
04853f27 63 $this->questiondata = $questiondata;
515b3ae6 64 $this->s = $s;
04853f27
TH
65
66 $this->define_baseurl($reporturl->out());
67 $this->collapsible(false);
8dbcbe21 68 $this->set_attribute('class', 'generaltable generalbox boxaligncenter quizresponseanalysis');
04853f27 69
768a7588 70 // Define the table columns.
869309b8 71 $columns = array();
72 $headers = array();
aeb15530 73
d50b05e6 74 if ($responseanalysis->has_subparts()) {
04853f27 75 $columns[] = 'part';
620233b8 76 $headers[] = get_string('partofquestion', 'quiz_statistics');
869309b8 77 }
78
d50b05e6 79 if ($responseanalysis->has_multiple_response_classes()) {
04853f27
TH
80 $columns[] = 'responseclass';
81 $headers[] = get_string('modelresponse', 'quiz_statistics');
82
d50b05e6 83 if ($responseanalysis->has_actual_responses()) {
04853f27
TH
84 $columns[] = 'response';
85 $headers[] = get_string('actualresponse', 'quiz_statistics');
86 }
aeb15530 87
04853f27
TH
88 } else {
89 $columns[] = 'response';
90 $headers[] = get_string('response', 'quiz_statistics');
91 }
869309b8 92
04853f27
TH
93 $columns[] = 'fraction';
94 $headers[] = get_string('optiongrade', 'quiz_statistics');
aeb15530 95
04853f27
TH
96 $columns[] = 'count';
97 $headers[] = get_string('count', 'quiz_statistics');
869309b8 98
04853f27
TH
99 $columns[] = 'frequency';
100 $headers[] = get_string('frequency', 'quiz_statistics');
869309b8 101
102 $this->define_columns($columns);
103 $this->define_headers($headers);
104 $this->sortable(false);
105
04853f27
TH
106 $this->column_class('fraction', 'numcol');
107 $this->column_class('count', 'numcol');
869309b8 108 $this->column_class('frequency', 'numcol');
109
04853f27
TH
110 $this->column_suppress('part');
111 $this->column_suppress('responseclass');
869309b8 112
113 parent::setup();
114 }
aeb15530 115
04853f27
TH
116 protected function format_percentage($fraction) {
117 return format_float($fraction * 100, 2) . '%';
869309b8 118 }
aeb15530 119
04853f27
TH
120 /**
121 * The mark fraction that this response earns.
122 * @param object $response containst the data to display.
123 * @return string contents of this table cell.
124 */
125 protected function col_fraction($response) {
126 if (is_null($response->fraction)) {
869309b8 127 return '';
128 }
04853f27
TH
129
130 return $this->format_percentage($response->fraction);
869309b8 131 }
aeb15530 132
04853f27
TH
133 /**
134 * The frequency with which this response was given.
d50b05e6 135 * @param object $response contains the data to display.
04853f27
TH
136 * @return string contents of this table cell.
137 */
138 protected function col_frequency($response) {
515b3ae6 139 if (!$this->s) {
869309b8 140 return '';
141 }
869309b8 142
515b3ae6 143 return $this->format_percentage($response->count / $this->s);
04853f27 144 }
869309b8 145}