-
Notifications
You must be signed in to change notification settings - Fork 8
/
mod_quiz_renderer.php
193 lines (165 loc) · 7.21 KB
/
mod_quiz_renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A renderer to override the default renderer for quiz view table.
*
* @package quizaccess
* @subpackage gradebycategory
* @copyright 2013 Portsmouth University
* @author Jamie Pratt ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/mod/quiz/renderer.php');
require_once($CFG->dirroot . '/mod/quiz/accessrule/gradebycategory/gradebycatcalculator.php');
class quizaccess_gradebycategory_mod_quiz_renderer extends mod_quiz_renderer {
/**
* Generates the table of data
*
* @param array $quiz Array contining quiz data
* @param int $context The page context ID
* @param mod_quiz_view_object $viewobj
*/
public function view_table($quiz, $context, $viewobj) {
// Most of the following code is unfortunately a duplicate of the existing renderer. There was no way to reuse the existing
// code and add the extra columns we wanted so I had to duplicate it.
// New parts of the code are marked with a comment // Grade by category code start. and // Grade by category code end.
global $DB;
// Grade by category code start.
// If gradebycategory setting is not on then just use the existing renderer.
if (!$quiz->gradebycategory) {
return parent::view_table($quiz, $context, $viewobj);
}
// Grade by category code end.
if (!$viewobj->attempts) {
return '';
}
// Prepare table header.
$table = new html_table();
$table->attributes['class'] = 'generaltable quizattemptsummary';
$table->head = array();
$table->align = array();
$table->size = array();
if ($viewobj->attemptcolumn) {
$table->head[] = get_string('attemptnumber', 'quiz');
$table->align[] = 'center';
$table->size[] = '';
}
$table->head[] = get_string('attemptstate', 'quiz');
$table->align[] = 'left';
$table->size[] = '';
if ($viewobj->markcolumn) {
$table->head[] = get_string('marks', 'quiz') . ' / ' .
quiz_format_grade($quiz, $quiz->sumgrades);
$table->align[] = 'center';
$table->size[] = '';
}
if ($viewobj->gradecolumn) {
$table->head[] = get_string('grade') . ' / ' .
quiz_format_grade($quiz, $quiz->grade);
$table->align[] = 'center';
$table->size[] = '';
}
// Grade by category code start.
$catgrades = new quizaccess_gradebycategory_calculator($quiz);
// $this->lateststeps may or may not already have been loaded depending if the reoprt
// is set to show question grades.
$catgrades->load_latest_steps($viewobj->attempts);
$categorynames = $catgrades->load_cat_data();
// Output column headings for category averages
foreach ($categorynames as $catname) {
$table->head[] = $catname. ' / ' .
quiz_format_grade($quiz, 100);
$table->align[] = 'center';
$table->size[] = '';
}
// Grade by category code end.
if ($viewobj->canreviewmine) {
$table->head[] = get_string('review', 'quiz');
$table->align[] = 'center';
$table->size[] = '';
}
if ($viewobj->feedbackcolumn) {
$table->head[] = get_string('feedback', 'quiz');
$table->align[] = 'left';
$table->size[] = '';
}
// One row for each attempt.
foreach ($viewobj->attemptobjs as $attemptobj) {
$attemptoptions = $attemptobj->get_display_options(true);
$row = array();
// Add the attempt number.
if ($viewobj->attemptcolumn) {
if ($attemptobj->is_preview()) {
$row[] = get_string('preview', 'quiz');
} else {
$row[] = $attemptobj->get_attempt_number();
}
}
$row[] = $this->attempt_state($attemptobj);
if ($viewobj->markcolumn) {
if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX &&
$attemptobj->is_finished()) {
$row[] = quiz_format_grade($quiz, $attemptobj->get_sum_marks());
} else {
$row[] = '';
}
}
// Outside the if because we may be showing feedback but not grades.
$attemptgrade = quiz_rescale_grade($attemptobj->get_sum_marks(), $quiz, false);
if ($viewobj->gradecolumn) {
if ($attemptoptions->marks >= question_display_options::MARK_AND_MAX &&
$attemptobj->is_finished()) {
// Highlight the highest grade if appropriate.
if ($viewobj->overallstats && !$attemptobj->is_preview()
&& $viewobj->numattempts > 1 && !is_null($viewobj->mygrade)
&& $attemptgrade == $viewobj->mygrade
&& $quiz->grademethod == QUIZ_GRADEHIGHEST) {
$table->rowclasses[$attemptobj->get_attempt_number()] = 'bestrow';
}
$row[] = quiz_format_grade($quiz, $attemptgrade);
} else {
$row[] = '';
}
}
// Grade by category code start.
// Output cell contents for category average.
foreach (array_keys($categorynames) as $catid) {
$row[] = $catgrades->grade_by_category($attemptobj->get_uniqueid(), $catid);
}
// Grade by category code end.
if ($viewobj->canreviewmine) {
$row[] = $viewobj->accessmanager->make_review_link($attemptobj->get_attempt(),
$attemptoptions, $this);
}
if ($viewobj->feedbackcolumn && $attemptobj->is_finished()) {
if ($attemptoptions->overallfeedback) {
$row[] = quiz_feedback_for_grade($attemptgrade, $quiz, $context);
} else {
$row[] = '';
}
}
if ($attemptobj->is_preview()) {
$table->data['preview'] = $row;
} else {
$table->data[$attemptobj->get_attempt_number()] = $row;
}
} // End of loop over attempts.
$output = '';
$output .= $this->view_table_heading();
$output .= html_writer::table($table);
return $output;
}
}