MDL-80880 quiz: change display of previous attempts summary
[moodle.git] / mod / quiz / classes / output / list_of_attempts.php
blobc1ae66e14e5c046e0bdd9b697216af44ec0e5795
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.
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/>.
17 namespace mod_quiz\output;
19 use core\output\named_templatable;
20 use mod_quiz\quiz_attempt;
21 use renderable;
22 use renderer_base;
24 /**
25 * Display summary information about a list of attempts.
27 * This is used on the front page of the quiz (view.php).
29 * @package mod_quiz
30 * @copyright 2024 The Open University
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class list_of_attempts implements renderable, named_templatable {
35 /** @var int time to consider as now. */
36 protected int $timenow;
38 /** @var quiz_attempt[] The list of attempts to summarise. */
39 protected array $attempts = [];
41 /**
42 * Constructor.
44 * @param int $timenow time that is now.
46 public function __construct(int $timenow) {
47 $this->timenow = $timenow;
50 /**
51 * Add an event to the list.
53 * @param quiz_attempt $attemptobj
55 public function add_attempt(quiz_attempt $attemptobj): void {
56 $this->attempts[] = $attemptobj;
59 public function export_for_template(renderer_base $output): array {
61 $templatecontext = [
62 'hasattempts' => !empty($this->attempts),
63 'attempts' => [],
66 foreach ($this->attempts as $attemptobj) {
67 $displayoptions = $attemptobj->get_display_options(true);
68 $templatecontext['attempts'][] = (object) [
69 'name' => get_string('attempt', 'mod_quiz', $attemptobj->get_attempt_number()),
70 'summarydata' => attempt_summary_information::create_for_attempt(
71 $attemptobj, $displayoptions)->export_for_template($output),
72 'reviewlink' => $attemptobj->get_access_manager($this->timenow)->make_review_link(
73 $attemptobj->get_attempt(), $displayoptions, $output),
77 return $templatecontext;
80 public function get_template_name(\renderer_base $renderer): string {
81 // Only reason we are forced to implement this is that we want the quiz renderer
82 // passed to export_for_template, not a core_renderer.
83 return 'mod_quiz/list_of_attempts';