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 * This file defines the quiz responses table.
20 * @package quiz_responses
21 * @copyright 2008 Jean-Michel Vedrine
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport_table.php');
32 * This is a table subclass for displaying the quiz responses report.
34 * @copyright 2008 Jean-Michel Vedrine
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class quiz_responses_table extends quiz_attempts_report_table {
42 * @param context $context
43 * @param string $qmsubselect
44 * @param quiz_responses_options $options
45 * @param array $groupstudents
46 * @param array $students
47 * @param array $questions
48 * @param moodle_url $reporturl
50 public function __construct($quiz, $context, $qmsubselect, quiz_responses_options $options,
51 $groupstudents, $students, $questions, $reporturl) {
52 parent::__construct('mod-quiz-report-responses-report', $quiz, $context,
53 $qmsubselect, $options, $groupstudents, $students, $questions, $reporturl);
56 public function build_table() {
57 if (!$this->rawdata) {
61 $this->strtimeformat = str_replace(',', ' ', get_string('strftimedatetime'));
62 parent::build_table();
65 public function col_sumgrades($attempt) {
66 if ($attempt->state != quiz_attempt::FINISHED) {
70 $grade = quiz_rescale_grade($attempt->sumgrades, $this->quiz);
71 if ($this->is_downloading()) {
75 $gradehtml = '<a href="review.php?q=' . $this->quiz->id . '&attempt=' .
76 $attempt->attempt . '">' . $grade . '</a>';
80 public function data_col($slot, $field, $attempt) {
83 if ($attempt->usageid == 0) {
87 $question = $this->questions[$slot];
88 if (!isset($this->lateststeps[$attempt->usageid][$slot])) {
92 $stepdata = $this->lateststeps[$attempt->usageid][$slot];
94 if (property_exists($stepdata, $field . 'full')) {
95 $value = $stepdata->{$field . 'full'};
97 $value = $stepdata->$field;
100 if (is_null($value)) {
103 $summary = trim($value);
106 if ($this->is_downloading() || $field != 'responsesummary') {
110 return $this->make_review_link($summary, $attempt, $slot);
113 public function other_cols($colname, $attempt) {
114 if (preg_match('/^question(\d+)$/', $colname, $matches)) {
115 return $this->data_col($matches[1], 'questionsummary', $attempt);
117 } else if (preg_match('/^response(\d+)$/', $colname, $matches)) {
118 return $this->data_col($matches[1], 'responsesummary', $attempt);
120 } else if (preg_match('/^right(\d+)$/', $colname, $matches)) {
121 return $this->data_col($matches[1], 'rightanswer', $attempt);
128 protected function requires_latest_steps_loaded() {
132 protected function is_latest_step_column($column) {
133 if (preg_match('/^(?:question|response|right)([0-9]+)/', $column, $matches)) {
140 * Get any fields that might be needed when sorting on date for a particular slot.
141 * @param int $slot the slot for the column we want.
142 * @param string $alias the table alias for latest state information relating to that slot.
144 protected function get_required_latest_state_fields($slot, $alias) {
146 $sortableresponse = $DB->sql_order_by_text("{$alias}.questionsummary");
147 if ($sortableresponse === "{$alias}.questionsummary") {
148 // Can just order by text columns. No complexity needed.
149 return "{$alias}.questionsummary AS question{$slot},
150 {$alias}.rightanswer AS right{$slot},
151 {$alias}.responsesummary AS response{$slot}";
153 // Work-around required.
154 return $DB->sql_order_by_text("{$alias}.questionsummary") . " AS question{$slot},
155 {$alias}.questionsummary AS question{$slot}full,
156 " . $DB->sql_order_by_text("{$alias}.rightanswer") . " AS right{$slot},
157 {$alias}.rightanswer AS right{$slot}full,
158 " . $DB->sql_order_by_text("{$alias}.responsesummary") . " AS response{$slot},
159 {$alias}.responsesummary AS response{$slot}full";