4bfc4115ab741472b153851461c1861b000643d4
[moodle.git] / mod / quiz / report / statistics / statistics_question_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 about a particular question.
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 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  *
37  * @copyright 2008 Jamie Pratt
38  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class quiz_statistics_question_table extends flexible_table {
41     /** @var object this question. */
42     protected $questiondata;
44     /**
45      * Constructor.
46      * @param int $qid the id of the particular question whose statistics are being
47      * displayed.
48      */
49     public function __construct($qid) {
50         parent::__construct('mod-quiz-report-statistics-question-table' . $qid);
51     }
53     /**
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
58      */
59     public function question_setup($reporturl, $questiondata, $s, \core_question\statistics\responses\analyser $responesstats) {
60         $this->questiondata = $questiondata;
61         $this->s = $s;
63         $this->define_baseurl($reporturl->out());
64         $this->collapsible(false);
65         $this->set_attribute('class', 'generaltable generalbox boxaligncenter quizresponseanalysis');
67         // Define the table columns.
68         $columns = array();
69         $headers = array();
71         if ($responesstats->has_subparts()) {
72             $columns[] = 'part';
73             $headers[] = get_string('partofquestion', 'quiz_statistics');
74         }
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');
83             }
85         } else {
86             $columns[] = 'response';
87             $headers[] = get_string('response', 'quiz_statistics');
88         }
90         $columns[] = 'fraction';
91         $headers[] = get_string('optiongrade', 'quiz_statistics');
93         $columns[] = 'count';
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');
110         parent::setup();
111     }
113     protected function format_percentage($fraction) {
114         return format_float($fraction * 100, 2) . '%';
115     }
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)) {
124             return '';
125         }
127         return $this->format_percentage($response->fraction);
128     }
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) {
136         if (!$this->s) {
137             return '';
138         }
140         return $this->format_percentage($response->count / $this->s);
141     }