MDL-40481 quiz responses report needs cols need text_sorting
[moodle.git] / mod / quiz / report / responses / responses_table.php
CommitLineData
aeb15530 1<?php
9b40c540
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/**
4747c788 18 * This file defines the quiz responses table.
9b40c540 19 *
8d76124c
TH
20 * @package quiz_responses
21 * @copyright 2008 Jean-Michel Vedrine
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9b40c540
TH
23 */
24
25
a17b297d
TH
26defined('MOODLE_INTERNAL') || die();
27
f5e42695
TH
28require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport_table.php');
29
a17b297d 30
9b40c540
TH
31/**
32 * This is a table subclass for displaying the quiz responses report.
33 *
8d76124c
TH
34 * @copyright 2008 Jean-Michel Vedrine
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9b40c540 36 */
ac4d9157 37class quiz_responses_table extends quiz_attempts_report_table {
9b40c540 38
9d58dae3
TH
39 /**
40 * Constructor
41 * @param object $quiz
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
49 */
e97d60ad
TH
50 public function __construct($quiz, $context, $qmsubselect, quiz_responses_options $options,
51 $groupstudents, $students, $questions, $reporturl) {
9b40c540 52 parent::__construct('mod-quiz-report-responses-report', $quiz, $context,
e97d60ad 53 $qmsubselect, $options, $groupstudents, $students, $questions, $reporturl);
7f29a7db 54 }
9b40c540
TH
55
56 public function build_table() {
4747c788
TH
57 if (!$this->rawdata) {
58 return;
7f29a7db 59 }
4747c788
TH
60
61 $this->strtimeformat = str_replace(',', ' ', get_string('strftimedatetime'));
62 parent::build_table();
7f29a7db 63 }
64
9b40c540 65 public function col_sumgrades($attempt) {
698c42ee 66 if ($attempt->state != quiz_attempt::FINISHED) {
7f29a7db 67 return '-';
68 }
65778323
TH
69
70 $grade = quiz_rescale_grade($attempt->sumgrades, $this->quiz);
71 if ($this->is_downloading()) {
72 return $grade;
73 }
74
55ca80ed
TH
75 $gradehtml = '<a href="review.php?q=' . $this->quiz->id . '&amp;attempt=' .
76 $attempt->attempt . '">' . $grade . '</a>';
65778323 77 return $gradehtml;
7f29a7db 78 }
65778323 79
9b40c540
TH
80 public function data_col($slot, $field, $attempt) {
81 global $CFG;
aeb15530 82
9b40c540
TH
83 if ($attempt->usageid == 0) {
84 return '-';
85 }
ca4b4f8f 86
9b40c540
TH
87 $question = $this->questions[$slot];
88 if (!isset($this->lateststeps[$attempt->usageid][$slot])) {
89 return '-';
90 }
ca4b4f8f 91
9b40c540 92 $stepdata = $this->lateststeps[$attempt->usageid][$slot];
aeb15530 93
94ffbef1
TH
94 if (property_exists($stepdata, $field . 'full')) {
95 $value = $stepdata->{$field . 'full'};
96 } else {
97 $value = $stepdata->$field;
98 }
99
100 if (is_null($value)) {
9b40c540 101 $summary = '-';
7f29a7db 102 } else {
94ffbef1 103 $summary = trim($value);
7f29a7db 104 }
9b40c540
TH
105
106 if ($this->is_downloading() || $field != 'responsesummary') {
107 return $summary;
108 }
109
110 return $this->make_review_link($summary, $attempt, $slot);
7f29a7db 111 }
112
9b40c540
TH
113 public function other_cols($colname, $attempt) {
114 if (preg_match('/^question(\d+)$/', $colname, $matches)) {
115 return $this->data_col($matches[1], 'questionsummary', $attempt);
116
117 } else if (preg_match('/^response(\d+)$/', $colname, $matches)) {
118 return $this->data_col($matches[1], 'responsesummary', $attempt);
119
120 } else if (preg_match('/^right(\d+)$/', $colname, $matches)) {
121 return $this->data_col($matches[1], 'rightanswer', $attempt);
122
7f29a7db 123 } else {
55ca80ed 124 return null;
7f29a7db 125 }
9b40c540 126 }
7f29a7db 127
9b40c540
TH
128 protected function requires_latest_steps_loaded() {
129 return true;
7f29a7db 130 }
131
9b40c540
TH
132 protected function is_latest_step_column($column) {
133 if (preg_match('/^(?:question|response|right)([0-9]+)/', $column, $matches)) {
134 return $matches[1];
2ea2c1f5 135 }
9b40c540 136 return false;
7f29a7db 137 }
aeb15530 138
9b40c540
TH
139 /**
140 * Get any fields that might be needed when sorting on date for a particular slot.
f7970e3c 141 * @param int $slot the slot for the column we want.
9b40c540
TH
142 * @param string $alias the table alias for latest state information relating to that slot.
143 */
144 protected function get_required_latest_state_fields($slot, $alias) {
94ffbef1
TH
145 global $DB;
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}";
152 } else {
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";
160 }
9b40c540
TH
161 }
162}