| 1 |
efrain |
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/>.
|
|
|
16 |
|
|
|
17 |
namespace mod_quiz\output;
|
|
|
18 |
|
|
|
19 |
use action_link;
|
|
|
20 |
use core\output\named_templatable;
|
|
|
21 |
use html_writer;
|
|
|
22 |
use mod_quiz\grade_calculator;
|
|
|
23 |
use mod_quiz\output\grades\grade_out_of;
|
|
|
24 |
use mod_quiz\quiz_attempt;
|
|
|
25 |
use moodle_url;
|
|
|
26 |
use mod_quiz\question\display_options;
|
|
|
27 |
use question_display_options;
|
|
|
28 |
use renderable;
|
|
|
29 |
use renderer_base;
|
|
|
30 |
use stdClass;
|
|
|
31 |
use user_picture;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* A summary of a single quiz attempt for rendering.
|
|
|
35 |
*
|
|
|
36 |
* This is used in places like
|
|
|
37 |
* - at the top of the review attempt page (review.php)
|
|
|
38 |
* - at the top of the review single question page (reviewquestion.php)
|
|
|
39 |
* - on the quiz entry page (view.php).
|
|
|
40 |
*
|
|
|
41 |
* @package mod_quiz
|
|
|
42 |
* @copyright 2024 The Open University
|
|
|
43 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
44 |
*/
|
|
|
45 |
class attempt_summary_information implements renderable, named_templatable {
|
|
|
46 |
|
|
|
47 |
/** @var array[] The rows of summary data. {@see add_item()} should make the structure clear. */
|
|
|
48 |
protected array $summarydata = [];
|
|
|
49 |
|
| 1441 |
ariadna |
50 |
/** @var string The caption for attempt summary table. */
|
|
|
51 |
protected string $caption = '';
|
|
|
52 |
|
| 1 |
efrain |
53 |
/**
|
|
|
54 |
* Add an item to the summary.
|
|
|
55 |
*
|
|
|
56 |
* @param string $shortname unique identifier of this item (not displayed).
|
|
|
57 |
* @param string|renderable $title the title of this item.
|
|
|
58 |
* @param string|renderable $content the content of this item.
|
|
|
59 |
*/
|
|
|
60 |
public function add_item(string $shortname, string|renderable $title, string|renderable $content): void {
|
|
|
61 |
$this->summarydata[$shortname] = [
|
|
|
62 |
'title' => $title,
|
|
|
63 |
'content' => $content,
|
|
|
64 |
];
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
| 1441 |
ariadna |
68 |
* Set the caption for the summary table.
|
|
|
69 |
*
|
|
|
70 |
* @param string $caption
|
|
|
71 |
*/
|
|
|
72 |
public function set_caption(string $caption): void {
|
|
|
73 |
$this->caption = $caption;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Add an item to the summary just before the given item.
|
|
|
78 |
*
|
|
|
79 |
* If that item is not present, then add as the first item.
|
|
|
80 |
*
|
|
|
81 |
* @param string $shortname unique identifier of this item (not displayed).
|
|
|
82 |
* @param string|renderable $title the title of this item.
|
|
|
83 |
* @param string|renderable $content the content of this item.
|
|
|
84 |
* @param string $addbefore identifier of the other item to add this before.
|
|
|
85 |
*/
|
|
|
86 |
public function add_item_before(
|
|
|
87 |
string $shortname,
|
|
|
88 |
string|renderable $title,
|
|
|
89 |
string|renderable $content,
|
|
|
90 |
string $addbefore,
|
|
|
91 |
): void {
|
|
|
92 |
$position = array_search($addbefore, array_keys($this->summarydata));
|
|
|
93 |
if ($position !== false) {
|
|
|
94 |
$this->insert_new_item_at_position($shortname, $title, $content, $position);
|
|
|
95 |
} else {
|
|
|
96 |
$this->insert_new_item_at_position($shortname, $title, $content, 0);
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Add an item to the summary just after the given item.
|
|
|
102 |
*
|
|
|
103 |
* If that item is not present, then just add at the end.
|
|
|
104 |
*
|
|
|
105 |
* @param string $shortname unique identifier of this item (not displayed).
|
|
|
106 |
* @param string|renderable $title the title of this item.
|
|
|
107 |
* @param string|renderable $content the content of this item.
|
|
|
108 |
* @param string $addafter identifier of the other item to add this before.
|
|
|
109 |
*/
|
|
|
110 |
public function add_item_after(
|
|
|
111 |
string $shortname,
|
|
|
112 |
string|renderable $title,
|
|
|
113 |
string|renderable $content,
|
|
|
114 |
string $addafter,
|
|
|
115 |
): void {
|
|
|
116 |
$position = array_search($addafter, array_keys($this->summarydata));
|
|
|
117 |
if ($position !== false) {
|
|
|
118 |
$this->insert_new_item_at_position($shortname, $title, $content, $position + 1);
|
|
|
119 |
} else {
|
|
|
120 |
$this->add_item($shortname, $title, $content);
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Add an item to the summary just before the given position.
|
|
|
126 |
*
|
|
|
127 |
* @param string $shortname unique identifier of this item (not displayed).
|
|
|
128 |
* @param string|renderable $title the title of this item.
|
|
|
129 |
* @param string|renderable $content the content of this item.
|
|
|
130 |
* @param int $position Numerical position to insert the item at. 0 means first.
|
|
|
131 |
*/
|
|
|
132 |
protected function insert_new_item_at_position(
|
|
|
133 |
string $shortname,
|
|
|
134 |
string|renderable $title,
|
|
|
135 |
string|renderable $content,
|
|
|
136 |
int $position,
|
|
|
137 |
) {
|
|
|
138 |
$this->summarydata = array_merge(
|
|
|
139 |
array_slice($this->summarydata, 0, $position),
|
|
|
140 |
[$shortname => ['title' => $title, 'content' => $content]],
|
|
|
141 |
array_slice($this->summarydata, $position, count($this->summarydata)),
|
|
|
142 |
);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Remove an item, if present.
|
|
|
147 |
*
|
|
|
148 |
* @param string $shortname
|
|
|
149 |
*/
|
|
|
150 |
public function remove_item(string $shortname): void {
|
|
|
151 |
unset($this->summarydata[$shortname]);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
| 1 |
efrain |
155 |
* Filter the data held, to keep only the information with the given shortnames.
|
|
|
156 |
*
|
|
|
157 |
* @param array $shortnames items to keep.
|
|
|
158 |
*/
|
|
|
159 |
public function filter_keeping_only(array $shortnames): void {
|
|
|
160 |
foreach ($this->summarydata as $shortname => $rowdata) {
|
|
|
161 |
if (!in_array($shortname, $shortnames)) {
|
|
|
162 |
unset($this->summarydata[$shortname]);
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* To aid conversion of old code. This converts the old array format into an instance of this class.
|
|
|
169 |
*
|
|
|
170 |
* @param array $items array of $shortname => [$title, $content].
|
|
|
171 |
* @return static
|
|
|
172 |
*/
|
|
|
173 |
public static function create_from_legacy_array(array $items): static {
|
|
|
174 |
$summary = new static();
|
|
|
175 |
foreach ($items as $shortname => $item) {
|
|
|
176 |
$summary->add_item($shortname, $item['title'], $item['content']);
|
|
|
177 |
}
|
|
|
178 |
return $summary;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Initialise an instance of this class for a particular quiz attempt.
|
|
|
183 |
*
|
|
|
184 |
* @param quiz_attempt $attemptobj the attempt to summarise.
|
|
|
185 |
* @param display_options $options options for what can be seen.
|
|
|
186 |
* @param int|null $pageforlinkingtootherattempts if null, no links to other attempsts will be created.
|
|
|
187 |
* If specified, the URL of this particular page of the attempt, otherwise
|
|
|
188 |
* the URL will go to the first page. If -1, deduce $page from $slot.
|
|
|
189 |
* @param bool|null $showall if true, the URL will be to review the entire attempt on one page,
|
|
|
190 |
* and $page will be ignored. If null, a sensible default will be chosen.
|
|
|
191 |
* @return self summary information.
|
|
|
192 |
*/
|
|
|
193 |
public static function create_for_attempt(
|
|
|
194 |
quiz_attempt $attemptobj,
|
|
|
195 |
display_options $options,
|
|
|
196 |
?int $pageforlinkingtootherattempts = null,
|
|
|
197 |
?bool $showall = null,
|
|
|
198 |
): static {
|
|
|
199 |
global $DB, $USER;
|
|
|
200 |
$summary = new static();
|
|
|
201 |
|
|
|
202 |
// Prepare summary information about the whole attempt.
|
|
|
203 |
if (!$attemptobj->get_quiz()->showuserpicture && $attemptobj->get_userid() != $USER->id) {
|
|
|
204 |
// If showuserpicture is true, the picture is shown elsewhere, so don't repeat it.
|
|
|
205 |
$student = $DB->get_record('user', ['id' => $attemptobj->get_userid()]);
|
|
|
206 |
$userpicture = new user_picture($student);
|
|
|
207 |
$userpicture->courseid = $attemptobj->get_courseid();
|
|
|
208 |
$summary->add_item('user', $userpicture,
|
|
|
209 |
new action_link(
|
|
|
210 |
new moodle_url('/user/view.php', ['id' => $student->id, 'course' => $attemptobj->get_courseid()]),
|
|
|
211 |
fullname($student, true),
|
|
|
212 |
)
|
|
|
213 |
);
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
if ($pageforlinkingtootherattempts !== null && $attemptobj->has_capability('mod/quiz:viewreports')) {
|
|
|
217 |
$attemptlist = $attemptobj->links_to_other_attempts(
|
|
|
218 |
$attemptobj->review_url(null, $pageforlinkingtootherattempts, $showall));
|
|
|
219 |
if ($attemptlist) {
|
|
|
220 |
$summary->add_item('attemptlist', get_string('attempts', 'quiz'), $attemptlist);
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
|
| 1441 |
ariadna |
224 |
// Caption.
|
|
|
225 |
$summary->set_caption(get_string('summaryofattemptscaption', 'quiz', $attemptobj->get_attempt_number()));
|
|
|
226 |
|
| 1 |
efrain |
227 |
// Attempt state.
|
|
|
228 |
$summary->add_item('state', get_string('attemptstate', 'quiz'),
|
|
|
229 |
quiz_attempt::state_name($attemptobj->get_attempt()->state));
|
|
|
230 |
|
|
|
231 |
// Timing information.
|
|
|
232 |
$attempt = $attemptobj->get_attempt();
|
|
|
233 |
$quiz = $attemptobj->get_quiz();
|
|
|
234 |
$overtime = 0;
|
|
|
235 |
|
|
|
236 |
if ($attempt->state == quiz_attempt::FINISHED) {
|
|
|
237 |
if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
|
|
|
238 |
if ($quiz->timelimit && $timetaken > ($quiz->timelimit + 60)) {
|
|
|
239 |
$overtime = $timetaken - $quiz->timelimit;
|
|
|
240 |
$overtime = format_time($overtime);
|
|
|
241 |
}
|
|
|
242 |
$timetaken = format_time($timetaken);
|
|
|
243 |
} else {
|
|
|
244 |
$timetaken = "-";
|
|
|
245 |
}
|
|
|
246 |
} else {
|
|
|
247 |
$timetaken = get_string('unfinished', 'quiz');
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
$summary->add_item('startedon', get_string('startedon', 'quiz'), userdate($attempt->timestart));
|
|
|
251 |
|
|
|
252 |
if ($attempt->state == quiz_attempt::FINISHED) {
|
|
|
253 |
$summary->add_item('completedon', get_string('completedon', 'quiz'),
|
|
|
254 |
userdate($attempt->timefinish));
|
|
|
255 |
$summary->add_item('timetaken', get_string('attemptduration', 'quiz'), $timetaken);
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
if (!empty($overtime)) {
|
|
|
259 |
$summary->add_item('overdue', get_string('overdue', 'quiz'), $overtime);
|
|
|
260 |
}
|
|
|
261 |
|
|
|
262 |
// Show marks (if the user is allowed to see marks at the moment).
|
|
|
263 |
$grade = $summary->add_attempt_grades_if_appropriate($attemptobj, $options);
|
|
|
264 |
|
|
|
265 |
// Any additional summary data from the behaviour.
|
|
|
266 |
foreach ($attemptobj->get_additional_summary_data($options) as $shortname => $data) {
|
|
|
267 |
$summary->add_item($shortname, $data['title'], $data['content']);
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
// Feedback if there is any, and the user is allowed to see it now.
|
|
|
271 |
$feedback = $attemptobj->get_overall_feedback($grade);
|
|
|
272 |
if ($options->overallfeedback && $feedback) {
|
|
|
273 |
$summary->add_item('feedback', get_string('feedback', 'quiz'), $feedback);
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
return $summary;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* Add the grade information to this summary information.
|
|
|
281 |
*
|
|
|
282 |
* This is a helper used by {@see create_for_attempt()}.
|
|
|
283 |
*
|
|
|
284 |
* @param quiz_attempt $attemptobj the attempt to summarise.
|
|
|
285 |
* @param display_options $options options for what can be seen.
|
|
|
286 |
* @return float|null the overall attempt grade, if it exists, else null. Raw value, not formatted.
|
|
|
287 |
*/
|
|
|
288 |
public function add_attempt_grades_if_appropriate(
|
|
|
289 |
quiz_attempt $attemptobj,
|
|
|
290 |
display_options $options,
|
|
|
291 |
): ?float {
|
|
|
292 |
$quiz = $attemptobj->get_quiz();
|
|
|
293 |
$grade = quiz_rescale_grade($attemptobj->get_sum_marks(), $quiz, false);
|
|
|
294 |
|
|
|
295 |
if ($options->marks < question_display_options::MARK_AND_MAX) {
|
|
|
296 |
// User can't see grades.
|
|
|
297 |
return $grade;
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
if (!quiz_has_grades($quiz) || $attemptobj->get_state() != quiz_attempt::FINISHED) {
|
|
|
301 |
// No grades to show.
|
|
|
302 |
return $grade;
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
if (is_null($grade)) {
|
|
|
306 |
// Attempt needs ot be graded.
|
|
|
307 |
$this->add_item('grade', get_string('gradenoun'), quiz_format_grade($quiz, $grade));
|
|
|
308 |
return $grade;
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
// Grades for extra grade items, if any.
|
|
|
312 |
foreach ($attemptobj->get_grade_item_totals() as $gradeitemid => $gradeoutof) {
|
| 1441 |
ariadna |
313 |
$this->add_item('marks' . $gradeitemid, format_string($gradeoutof->name),
|
|
|
314 |
new grade_out_of($quiz, $gradeoutof->grade, $gradeoutof->maxgrade,
|
|
|
315 |
style: abs($gradeoutof->maxgrade - 100) < grade_calculator::ALMOST_ZERO ?
|
|
|
316 |
grade_out_of::NORMAL : grade_out_of::WITH_PERCENT));
|
| 1 |
efrain |
317 |
}
|
|
|
318 |
|
|
|
319 |
// Show raw marks only if they are different from the grade.
|
|
|
320 |
if ($quiz->grade != $quiz->sumgrades) {
|
|
|
321 |
$this->add_item('marks', get_string('marks', 'quiz'),
|
|
|
322 |
new grade_out_of($quiz, $attemptobj->get_sum_marks(), $quiz->sumgrades, style: grade_out_of::SHORT));
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
// Now the scaled grade.
|
|
|
326 |
$this->add_item('grade', get_string('gradenoun'),
|
|
|
327 |
new grade_out_of($quiz, $grade, $quiz->grade,
|
|
|
328 |
style: abs($quiz->grade - 100) < grade_calculator::ALMOST_ZERO ?
|
|
|
329 |
grade_out_of::NORMAL : grade_out_of::WITH_PERCENT));
|
|
|
330 |
|
|
|
331 |
return $grade;
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
public function export_for_template(renderer_base $output): array {
|
|
|
335 |
|
|
|
336 |
$templatecontext = [
|
|
|
337 |
'hasitems' => !empty($this->summarydata),
|
|
|
338 |
'items' => [],
|
| 1441 |
ariadna |
339 |
'caption' => $this->caption,
|
| 1 |
efrain |
340 |
];
|
|
|
341 |
foreach ($this->summarydata as $item) {
|
|
|
342 |
if ($item['title'] instanceof renderable) {
|
|
|
343 |
$title = $output->render($item['title']);
|
|
|
344 |
} else {
|
|
|
345 |
$title = $item['title'];
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
if ($item['content'] instanceof renderable) {
|
|
|
349 |
$content = $output->render($item['content']);
|
|
|
350 |
} else {
|
|
|
351 |
$content = $item['content'];
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
$templatecontext['items'][] = (object) ['title' => $title, 'content' => $content];
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
return $templatecontext;
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
public function get_template_name(renderer_base $renderer): string {
|
|
|
361 |
// Only reason we are forced to implement this is that we want the quiz renderer
|
|
|
362 |
// passed to export_for_template, not a core_renderer.
|
|
|
363 |
return 'mod_quiz/attempt_summary_information';
|
|
|
364 |
}
|
|
|
365 |
}
|