MDL-32322 more changes to make the code more similar before refactoring.
[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
e97d60ad
TH
39 public function __construct($quiz, $context, $qmsubselect, quiz_responses_options $options,
40 $groupstudents, $students, $questions, $reporturl) {
9b40c540 41 parent::__construct('mod-quiz-report-responses-report', $quiz, $context,
e97d60ad 42 $qmsubselect, $options, $groupstudents, $students, $questions, $reporturl);
7f29a7db 43 }
9b40c540
TH
44
45 public function build_table() {
4747c788
TH
46 if (!$this->rawdata) {
47 return;
7f29a7db 48 }
4747c788
TH
49
50 $this->strtimeformat = str_replace(',', ' ', get_string('strftimedatetime'));
51 parent::build_table();
7f29a7db 52 }
53
9b40c540 54 public function col_sumgrades($attempt) {
65778323 55 if (!$attempt->timefinish) {
7f29a7db 56 return '-';
57 }
65778323
TH
58
59 $grade = quiz_rescale_grade($attempt->sumgrades, $this->quiz);
60 if ($this->is_downloading()) {
61 return $grade;
62 }
63
55ca80ed
TH
64 $gradehtml = '<a href="review.php?q=' . $this->quiz->id . '&amp;attempt=' .
65 $attempt->attempt . '">' . $grade . '</a>';
65778323 66 return $gradehtml;
7f29a7db 67 }
65778323 68
9b40c540
TH
69 public function data_col($slot, $field, $attempt) {
70 global $CFG;
aeb15530 71
9b40c540
TH
72 if ($attempt->usageid == 0) {
73 return '-';
74 }
ca4b4f8f 75
9b40c540
TH
76 $question = $this->questions[$slot];
77 if (!isset($this->lateststeps[$attempt->usageid][$slot])) {
78 return '-';
79 }
ca4b4f8f 80
9b40c540 81 $stepdata = $this->lateststeps[$attempt->usageid][$slot];
aeb15530 82
9b40c540
TH
83 if (is_null($stepdata->$field)) {
84 $summary = '-';
7f29a7db 85 } else {
9b40c540 86 $summary = trim($stepdata->$field);
7f29a7db 87 }
9b40c540
TH
88
89 if ($this->is_downloading() || $field != 'responsesummary') {
90 return $summary;
91 }
92
93 return $this->make_review_link($summary, $attempt, $slot);
7f29a7db 94 }
95
9b40c540
TH
96 public function other_cols($colname, $attempt) {
97 if (preg_match('/^question(\d+)$/', $colname, $matches)) {
98 return $this->data_col($matches[1], 'questionsummary', $attempt);
99
100 } else if (preg_match('/^response(\d+)$/', $colname, $matches)) {
101 return $this->data_col($matches[1], 'responsesummary', $attempt);
102
103 } else if (preg_match('/^right(\d+)$/', $colname, $matches)) {
104 return $this->data_col($matches[1], 'rightanswer', $attempt);
105
7f29a7db 106 } else {
55ca80ed 107 return null;
7f29a7db 108 }
9b40c540 109 }
7f29a7db 110
9b40c540
TH
111 protected function requires_latest_steps_loaded() {
112 return true;
7f29a7db 113 }
114
9b40c540
TH
115 protected function is_latest_step_column($column) {
116 if (preg_match('/^(?:question|response|right)([0-9]+)/', $column, $matches)) {
117 return $matches[1];
2ea2c1f5 118 }
9b40c540 119 return false;
7f29a7db 120 }
aeb15530 121
9b40c540
TH
122 /**
123 * Get any fields that might be needed when sorting on date for a particular slot.
f7970e3c 124 * @param int $slot the slot for the column we want.
9b40c540
TH
125 * @param string $alias the table alias for latest state information relating to that slot.
126 */
127 protected function get_required_latest_state_fields($slot, $alias) {
128 return "$alias.questionsummary AS question$slot,
129 $alias.rightanswer AS right$slot,
130 $alias.responsesummary AS response$slot";
131 }
132}