c295e513cff0b8462e32d724d42b92d7f4db247f
[moodle.git] / mod / quiz / report / statistics / statistics_table.php
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.
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/>.
17 /**
18  * Quiz statistics report, table for showing statistics of each question in the quiz.
19  *
20  * @package   quiz_statistics
21  * @copyright 2008 Jamie Pratt
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir.'/tablelib.php');
29 /**
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.
32  *
33  * @copyright 2008 Jamie Pratt
34  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class quiz_statistics_table extends flexible_table {
37     /** @var object the quiz settings. */
38     protected $quiz;
40     /** @var integer the quiz course_module id. */
41     protected $cmid;
43     /**
44      * Constructor.
45      */
46     public function __construct() {
47         parent::__construct('mod-quiz-report-statistics-report');
48     }
50     /**
51      * Set up the columns and headers and other properties of the table and then
52      * call flexible_table::setup() method.
53      *
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.
58      */
59     public function statistics_setup($quiz, $cmid, $reporturl, $s) {
60         $this->quiz = $quiz;
61         $this->cmid = $cmid;
63         // Define the table columns.
64         $columns = array();
65         $headers = array();
67         $columns[] = 'number';
68         $headers[] = get_string('questionnumber', 'quiz_statistics');
70         if (!$this->is_downloading()) {
71             $columns[] = 'icon';
72             $headers[] = '';
73             $columns[] = 'actions';
74             $headers[] = '';
75         } else {
76             $columns[] = 'qtype';
77             $headers[] = get_string('questiontype', 'quiz_statistics');
78         }
80         $columns[] = 'name';
81         $headers[] = get_string('questionname', 'quiz');
83         $columns[] = 's';
84         $headers[] = get_string('attempts', 'quiz_statistics');
86         if ($s > 1) {
87             $columns[] = 'facility';
88             $headers[] = get_string('facility', 'quiz_statistics');
90             $columns[] = 'sd';
91             $headers[] = get_string('standarddeviationq', 'quiz_statistics');
92         }
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');
122         // Set up the table.
123         $this->define_baseurl($reporturl->out());
125         $this->collapsible(true);
127         $this->set_attribute('id', 'questionstatistics');
128         $this->set_attribute('class', 'generaltable generalbox boxaligncenter');
130         parent::setup();
131     }
133     /**
134      * The question number.
135      * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
136      * @return string contents of this table cell.
137      */
138     protected function col_number($questionstat) {
139         $number = $questionstat->question->number;
141         if ($questionstat->subquestion) {
142             $number = $number . '.'.$questionstat->subqdisplayorder;
143         }
145         if ($questionstat->question->qtype != 'random' && !is_null($questionstat->variant)) {
146             $number = $number . '.'.$questionstat->variant;
147         }
149         return $number;
150     }
152     /**
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.
156      */
157     protected function col_icon($questionstat) {
158         return print_question_icon($questionstat->question, true);
159     }
161     /**
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.
165      */
166     protected function col_actions($questionstat) {
167         return quiz_question_action_icons($this->quiz, $this->cmid, $questionstat->question, $this->baseurl);
168     }
170     /**
171      * The question type name.
172      *
173      * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
174      * @return string contents of this table cell.
175      */
176     protected function col_qtype($questionstat) {
177         return question_bank::get_qtype_name($questionstat->question->qtype);
178     }
180     /**
181      * The question name.
182      *
183      * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
184      * @return string contents of this table cell.
185      */
186     protected function col_name($questionstat) {
187         $name = $questionstat->question->name;
189         if (!is_null($questionstat->variant)) {
190             $a = new stdClass();
191             $a->name = $name;
192             $a->variant = $questionstat->variant;
193             $name = get_string('nameforvariant', 'quiz_statistics', $a);
194         }
196         if ($this->is_downloading()) {
197             return $name;
198         }
200         $url = null;
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));
205         }
207         if ($url) {
208             $name = html_writer::link($url, $name,
209                     array('title' => get_string('detailedanalysis', 'quiz_statistics')));
210         }
212         if ($this->is_dubious_question($questionstat)) {
213             $name = html_writer::tag('div', $name, array('class' => 'dubious'));
214         }
216         return $name;
217     }
219     /**
220      * The number of attempts at this question.
221      *
222      * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
223      * @return string contents of this table cell.
224      */
225     protected function col_s($questionstat) {
226         if (!isset($questionstat->s)) {
227             return 0;
228         }
230         return $questionstat->s;
231     }
233     /**
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.
237      */
238     protected function col_facility($questionstat) {
239         if (is_null($questionstat->facility)) {
240             return '';
241         }
243         return number_format($questionstat->facility*100, 2) . '%';
244     }
246     /**
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.
250      */
251     protected function col_sd($questionstat) {
252         if (is_null($questionstat->sd) || $questionstat->maxmark == 0) {
253             return '';
254         }
256         return number_format($questionstat->sd*100 / $questionstat->maxmark, 2) . '%';
257     }
259     /**
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.
263      */
264     protected function col_random_guess_score($questionstat) {
265         if (is_null($questionstat->randomguessscore)) {
266             return '';
267         }
269         return number_format($questionstat->randomguessscore * 100, 2).'%';
270     }
272     /**
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.
278      */
279     protected function col_intended_weight($questionstat) {
280         return quiz_report_scale_summarks_as_percentage($questionstat->maxmark, $this->quiz);
281     }
283     /**
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.
288      */
289     protected function col_effective_weight($questionstat) {
290         global $OUTPUT;
292         if ($questionstat->subquestion) {
293             return '';
294         }
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'));
303             }
305             return $negcovar;
306         }
308         return number_format($questionstat->effectiveweight, 2) . '%';
309     }
311     /**
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.
317      */
318     protected function col_discrimination_index($questionstat) {
319         if (!is_numeric($questionstat->discriminationindex)) {
320             return $questionstat->discriminationindex;
321         }
323         return number_format($questionstat->discriminationindex, 2) . '%';
324     }
326     /**
327      * Discrimination efficiency, similar to, but different from, the Discrimination index.
328      *
329      * @param \core_question\statistics\questions\calculated $questionstat stats for the question.
330      * @return string contents of this table cell.
331      */
332     protected function col_discriminative_efficiency($questionstat) {
333         if (!is_numeric($questionstat->discriminativeefficiency)) {
334             return '';
335         }
337         return number_format($questionstat->discriminativeefficiency, 2) . '%';
338     }
340     /**
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?
344      */
345     protected function is_dubious_question($questionstat) {
346         if (!is_numeric($questionstat->discriminativeefficiency)) {
347             return false;
348         }
350         return $questionstat->discriminativeefficiency < 15;
351     }
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'));
358         }
359     }
361     public function wrap_html_finish() {
362         if (!$this->is_downloading()) {
363             echo html_writer::end_tag('div');
364         }
365     }