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 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');
30 * This table has one row for each question in the quiz, with sub-rows when
31 * random questions appear. There are columns for the various statistics.
33 * @copyright 2008 Jamie Pratt
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class quiz_statistics_table extends flexible_table {
37 /** @var object the quiz settings. */
40 /** @var integer the quiz course_module id. */
46 public function __construct() {
47 parent::__construct('mod-quiz-report-statistics-report');
51 * Set up the columns and headers and other properties of the table and then
52 * call flexible_table::setup() method.
54 * @param object $quiz the quiz settings
55 * @param int $cmid the quiz course_module id
56 * @param moodle_url $reporturl the URL to redisplay this report.
57 * @param int $s number of attempts included in the statistics.
59 public function statistics_setup($quiz, $cmid, $reporturl, $s) {
63 // Define the table columns.
67 $columns[] = 'number';
68 $headers[] = get_string('questionnumber', 'quiz_statistics');
70 if (!$this->is_downloading()) {
73 $columns[] = 'actions';
77 $headers[] = get_string('questiontype', 'quiz_statistics');
81 $headers[] = get_string('questionname', 'quiz');
84 $headers[] = get_string('attempts', 'quiz_statistics');
87 $columns[] = 'facility';
88 $headers[] = get_string('facility', 'quiz_statistics');
91 $headers[] = get_string('standarddeviationq', 'quiz_statistics');
94 $columns[] = 'random_guess_score';
95 $headers[] = get_string('random_guess_score', 'quiz_statistics');
97 $columns[] = 'intended_weight';
98 $headers[] = get_string('intended_weight', 'quiz_statistics');
100 $columns[] = 'effective_weight';
101 $headers[] = get_string('effective_weight', 'quiz_statistics');
103 $columns[] = 'discrimination_index';
104 $headers[] = get_string('discrimination_index', 'quiz_statistics');
106 $columns[] = 'discriminative_efficiency';
107 $headers[] = get_string('discriminative_efficiency', 'quiz_statistics');
109 $this->define_columns($columns);
110 $this->define_headers($headers);
111 $this->sortable(false);
113 $this->column_class('s', 'numcol');
114 $this->column_class('facility', 'numcol');
115 $this->column_class('sd', 'numcol');
116 $this->column_class('random_guess_score', 'numcol');
117 $this->column_class('intended_weight', 'numcol');
118 $this->column_class('effective_weight', 'numcol');
119 $this->column_class('discrimination_index', 'numcol');
120 $this->column_class('discriminative_efficiency', 'numcol');
123 $this->define_baseurl($reporturl->out());
125 $this->collapsible(true);
127 $this->set_attribute('id', 'questionstatistics');
128 $this->set_attribute('class', 'generaltable generalbox boxaligncenter');
134 * The question number.
135 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
136 * @return string contents of this table cell.
138 protected function col_number($questionstat) {
139 $number = $questionstat->question->number;
141 if ($questionstat->subquestion) {
142 $number = $number . '.'.$questionstat->subqdisplayorder;
145 if ($questionstat->question->qtype != 'random' && !is_null($questionstat->variant)) {
146 $number = $number . '.'.$questionstat->variant;
153 * The question type icon.
154 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
155 * @return string contents of this table cell.
157 protected function col_icon($questionstat) {
158 return print_question_icon($questionstat->question, true);
162 * Actions that can be performed on the question by this user (e.g. edit or preview).
163 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
164 * @return string contents of this table cell.
166 protected function col_actions($questionstat) {
167 return quiz_question_action_icons($this->quiz, $this->cmid, $questionstat->question, $this->baseurl);
171 * The question type name.
173 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
174 * @return string contents of this table cell.
176 protected function col_qtype($questionstat) {
177 return question_bank::get_qtype_name($questionstat->question->qtype);
183 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
184 * @return string contents of this table cell.
186 protected function col_name($questionstat) {
187 $name = $questionstat->question->name;
189 if (!is_null($questionstat->variant)) {
192 $a->variant = $questionstat->variant;
193 $name = get_string('nameforvariant', 'quiz_statistics', $a);
196 if ($this->is_downloading()) {
201 if ($questionstat->subquestion) {
202 $url = new moodle_url($this->baseurl, array('qid' => $questionstat->questionid));
203 } else if ($questionstat->slot && $questionstat->question->qtype != 'random') {
204 $url = new moodle_url($this->baseurl, array('slot' => $questionstat->slot));
208 $name = html_writer::link($url, $name,
209 array('title' => get_string('detailedanalysis', 'quiz_statistics')));
212 if ($this->is_dubious_question($questionstat)) {
213 $name = html_writer::tag('div', $name, array('class' => 'dubious'));
220 * The number of attempts at this question.
222 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
223 * @return string contents of this table cell.
225 protected function col_s($questionstat) {
226 if (!isset($questionstat->s)) {
230 return $questionstat->s;
234 * The facility index (average fraction).
235 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
236 * @return string contents of this table cell.
238 protected function col_facility($questionstat) {
239 if (is_null($questionstat->facility)) {
243 return number_format($questionstat->facility*100, 2) . '%';
247 * The standard deviation of the fractions.
248 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
249 * @return string contents of this table cell.
251 protected function col_sd($questionstat) {
252 if (is_null($questionstat->sd) || $questionstat->maxmark == 0) {
256 return number_format($questionstat->sd*100 / $questionstat->maxmark, 2) . '%';
260 * An estimate of the fraction a student would get by guessing randomly.
261 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
262 * @return string contents of this table cell.
264 protected function col_random_guess_score($questionstat) {
265 if (is_null($questionstat->randomguessscore)) {
269 return number_format($questionstat->randomguessscore * 100, 2).'%';
273 * The intended question weight. Maximum mark for the question as a percentage
274 * of maximum mark for the quiz. That is, the indended influence this question
275 * on the student's overall mark.
276 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
277 * @return string contents of this table cell.
279 protected function col_intended_weight($questionstat) {
280 return quiz_report_scale_summarks_as_percentage($questionstat->maxmark, $this->quiz);
284 * The effective question weight. That is, an estimate of the actual
285 * influence this question has on the student's overall mark.
286 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
287 * @return string contents of this table cell.
289 protected function col_effective_weight($questionstat) {
292 if ($questionstat->subquestion) {
296 if ($questionstat->negcovar) {
297 $negcovar = get_string('negcovar', 'quiz_statistics');
299 if (!$this->is_downloading()) {
300 $negcovar = html_writer::tag('div',
301 $negcovar . $OUTPUT->help_icon('negcovar', 'quiz_statistics'),
302 array('class' => 'negcovar'));
308 return number_format($questionstat->effectiveweight, 2) . '%';
312 * Discrimination index. This is the product moment correlation coefficient
313 * between the fraction for this question, and the average fraction for the
314 * other questions in this quiz.
315 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
316 * @return string contents of this table cell.
318 protected function col_discrimination_index($questionstat) {
319 if (!is_numeric($questionstat->discriminationindex)) {
320 return $questionstat->discriminationindex;
323 return number_format($questionstat->discriminationindex, 2) . '%';
327 * Discrimination efficiency, similar to, but different from, the Discrimination index.
329 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
330 * @return string contents of this table cell.
332 protected function col_discriminative_efficiency($questionstat) {
333 if (!is_numeric($questionstat->discriminativeefficiency)) {
337 return number_format($questionstat->discriminativeefficiency, 2) . '%';
341 * This method encapsulates the test for wheter a question should be considered dubious.
342 * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
343 * @return bool is this question possibly not pulling it's weight?
345 protected function is_dubious_question($questionstat) {
346 if (!is_numeric($questionstat->discriminativeefficiency)) {
350 return $questionstat->discriminativeefficiency < 15;
353 public function wrap_html_start() {
354 // Horrible Moodle 2.0 wide-content work-around.
355 if (!$this->is_downloading()) {
356 echo html_writer::start_tag('div', array('id' => 'tablecontainer',
357 'class' => 'statistics-tablecontainer'));
361 public function wrap_html_finish() {
362 if (!$this->is_downloading()) {
363 echo html_writer::end_tag('div');