Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
50
    /**
51
     * Add an item to the summary.
52
     *
53
     * @param string $shortname unique identifier of this item (not displayed).
54
     * @param string|renderable $title the title of this item.
55
     * @param string|renderable $content the content of this item.
56
     */
57
    public function add_item(string $shortname, string|renderable $title, string|renderable $content): void {
58
        $this->summarydata[$shortname] = [
59
            'title'   => $title,
60
            'content' => $content,
61
        ];
62
    }
63
 
64
    /**
65
     * Filter the data held, to keep only the information with the given shortnames.
66
     *
67
     * @param array $shortnames items to keep.
68
     */
69
    public function filter_keeping_only(array $shortnames): void {
70
        foreach ($this->summarydata as $shortname => $rowdata) {
71
            if (!in_array($shortname, $shortnames)) {
72
                unset($this->summarydata[$shortname]);
73
            }
74
        }
75
    }
76
 
77
    /**
78
     * To aid conversion of old code. This converts the old array format into an instance of this class.
79
     *
80
     * @param array $items array of $shortname => [$title, $content].
81
     * @return static
82
     */
83
    public static function create_from_legacy_array(array $items): static {
84
        $summary = new static();
85
        foreach ($items as $shortname => $item) {
86
            $summary->add_item($shortname, $item['title'], $item['content']);
87
        }
88
        return $summary;
89
    }
90
 
91
    /**
92
     * Initialise an instance of this class for a particular quiz attempt.
93
     *
94
     * @param quiz_attempt $attemptobj the attempt to summarise.
95
     * @param display_options $options options for what can be seen.
96
     * @param int|null $pageforlinkingtootherattempts if null, no links to other attempsts will be created.
97
     *      If specified, the URL of this particular page of the attempt, otherwise
98
     *      the URL will go to the first page.  If -1, deduce $page from $slot.
99
     * @param bool|null $showall if true, the URL will be to review the entire attempt on one page,
100
     *      and $page will be ignored. If null, a sensible default will be chosen.
101
     * @return self summary information.
102
     */
103
    public static function create_for_attempt(
104
        quiz_attempt $attemptobj,
105
        display_options $options,
106
        ?int $pageforlinkingtootherattempts = null,
107
        ?bool $showall = null,
108
    ): static {
109
        global $DB, $USER;
110
        $summary = new static();
111
 
112
        // Prepare summary information about the whole attempt.
113
        if (!$attemptobj->get_quiz()->showuserpicture && $attemptobj->get_userid() != $USER->id) {
114
            // If showuserpicture is true, the picture is shown elsewhere, so don't repeat it.
115
            $student = $DB->get_record('user', ['id' => $attemptobj->get_userid()]);
116
            $userpicture = new user_picture($student);
117
            $userpicture->courseid = $attemptobj->get_courseid();
118
            $summary->add_item('user', $userpicture,
119
                new action_link(
120
                    new moodle_url('/user/view.php', ['id' => $student->id, 'course' => $attemptobj->get_courseid()]),
121
                    fullname($student, true),
122
                )
123
            );
124
        }
125
 
126
        if ($pageforlinkingtootherattempts !== null && $attemptobj->has_capability('mod/quiz:viewreports')) {
127
            $attemptlist = $attemptobj->links_to_other_attempts(
128
                $attemptobj->review_url(null, $pageforlinkingtootherattempts, $showall));
129
            if ($attemptlist) {
130
                $summary->add_item('attemptlist', get_string('attempts', 'quiz'), $attemptlist);
131
            }
132
        }
133
 
134
        // Attempt state.
135
        $summary->add_item('state', get_string('attemptstate', 'quiz'),
136
            quiz_attempt::state_name($attemptobj->get_attempt()->state));
137
 
138
        // Timing information.
139
        $attempt = $attemptobj->get_attempt();
140
        $quiz = $attemptobj->get_quiz();
141
        $overtime = 0;
142
 
143
        if ($attempt->state == quiz_attempt::FINISHED) {
144
            if ($timetaken = ($attempt->timefinish - $attempt->timestart)) {
145
                if ($quiz->timelimit && $timetaken > ($quiz->timelimit + 60)) {
146
                    $overtime = $timetaken - $quiz->timelimit;
147
                    $overtime = format_time($overtime);
148
                }
149
                $timetaken = format_time($timetaken);
150
            } else {
151
                $timetaken = "-";
152
            }
153
        } else {
154
            $timetaken = get_string('unfinished', 'quiz');
155
        }
156
 
157
        $summary->add_item('startedon', get_string('startedon', 'quiz'), userdate($attempt->timestart));
158
 
159
        if ($attempt->state == quiz_attempt::FINISHED) {
160
            $summary->add_item('completedon', get_string('completedon', 'quiz'),
161
                userdate($attempt->timefinish));
162
            $summary->add_item('timetaken', get_string('attemptduration', 'quiz'), $timetaken);
163
        }
164
 
165
        if (!empty($overtime)) {
166
            $summary->add_item('overdue', get_string('overdue', 'quiz'), $overtime);
167
        }
168
 
169
        // Show marks (if the user is allowed to see marks at the moment).
170
        $grade = $summary->add_attempt_grades_if_appropriate($attemptobj, $options);
171
 
172
        // Any additional summary data from the behaviour.
173
        foreach ($attemptobj->get_additional_summary_data($options) as $shortname => $data) {
174
            $summary->add_item($shortname, $data['title'], $data['content']);
175
        }
176
 
177
        // Feedback if there is any, and the user is allowed to see it now.
178
        $feedback = $attemptobj->get_overall_feedback($grade);
179
        if ($options->overallfeedback && $feedback) {
180
            $summary->add_item('feedback', get_string('feedback', 'quiz'), $feedback);
181
        }
182
 
183
        return $summary;
184
    }
185
 
186
    /**
187
     * Add the grade information to this summary information.
188
     *
189
     * This is a helper used by {@see create_for_attempt()}.
190
     *
191
     * @param quiz_attempt $attemptobj the attempt to summarise.
192
     * @param display_options $options options for what can be seen.
193
     * @return float|null the overall attempt grade, if it exists, else null. Raw value, not formatted.
194
     */
195
    public function add_attempt_grades_if_appropriate(
196
        quiz_attempt $attemptobj,
197
        display_options $options,
198
    ): ?float {
199
        $quiz = $attemptobj->get_quiz();
200
        $grade = quiz_rescale_grade($attemptobj->get_sum_marks(), $quiz, false);
201
 
202
        if ($options->marks < question_display_options::MARK_AND_MAX) {
203
            // User can't see grades.
204
            return $grade;
205
        }
206
 
207
        if (!quiz_has_grades($quiz) || $attemptobj->get_state() != quiz_attempt::FINISHED) {
208
            // No grades to show.
209
            return $grade;
210
        }
211
 
212
        if (is_null($grade)) {
213
            // Attempt needs ot be graded.
214
            $this->add_item('grade', get_string('gradenoun'), quiz_format_grade($quiz, $grade));
215
            return $grade;
216
        }
217
 
218
        // Grades for extra grade items, if any.
219
        foreach ($attemptobj->get_grade_item_totals() as $gradeitemid => $gradeoutof) {
220
            $this->add_item('marks' . $gradeitemid, format_string($gradeoutof->name), $gradeoutof);
221
        }
222
 
223
        // Show raw marks only if they are different from the grade.
224
        if ($quiz->grade != $quiz->sumgrades) {
225
            $this->add_item('marks', get_string('marks', 'quiz'),
226
                new grade_out_of($quiz, $attemptobj->get_sum_marks(), $quiz->sumgrades, style: grade_out_of::SHORT));
227
        }
228
 
229
        // Now the scaled grade.
230
        $this->add_item('grade', get_string('gradenoun'),
231
            new grade_out_of($quiz, $grade, $quiz->grade,
232
                style: abs($quiz->grade - 100) < grade_calculator::ALMOST_ZERO ?
233
                    grade_out_of::NORMAL : grade_out_of::WITH_PERCENT));
234
 
235
        return $grade;
236
    }
237
 
238
    public function export_for_template(renderer_base $output): array {
239
 
240
        $templatecontext = [
241
            'hasitems' => !empty($this->summarydata),
242
            'items' => [],
243
        ];
244
        foreach ($this->summarydata as $item) {
245
            if ($item['title'] instanceof renderable) {
246
                $title = $output->render($item['title']);
247
            } else {
248
                $title = $item['title'];
249
            }
250
 
251
            if ($item['content'] instanceof renderable) {
252
                $content = $output->render($item['content']);
253
            } else {
254
                $content = $item['content'];
255
            }
256
 
257
            $templatecontext['items'][] = (object) ['title' => $title, 'content' => $content];
258
        }
259
 
260
        return $templatecontext;
261
    }
262
 
263
    public function get_template_name(renderer_base $renderer): string {
264
        // Only reason we are forced to implement this is that we want the quiz renderer
265
        // passed to export_for_template, not a core_renderer.
266
        return 'mod_quiz/attempt_summary_information';
267
    }
268
}