Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 45... Línea 45...
45
class attempt_summary_information implements renderable, named_templatable {
45
class attempt_summary_information implements renderable, named_templatable {
Línea 46... Línea 46...
46
 
46
 
47
    /** @var array[] The rows of summary data. {@see add_item()} should make the structure clear. */
47
    /** @var array[] The rows of summary data. {@see add_item()} should make the structure clear. */
Línea -... Línea 48...
-
 
48
    protected array $summarydata = [];
-
 
49
 
-
 
50
    /** @var string The caption for attempt summary table. */
48
    protected array $summarydata = [];
51
    protected string $caption = '';
49
 
52
 
50
    /**
53
    /**
51
     * Add an item to the summary.
54
     * Add an item to the summary.
52
     *
55
     *
Línea 60... Línea 63...
60
            'content' => $content,
63
            'content' => $content,
61
        ];
64
        ];
62
    }
65
    }
Línea 63... Línea 66...
63
 
66
 
-
 
67
    /**
-
 
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
 
64
    /**
154
    /**
65
     * Filter the data held, to keep only the information with the given shortnames.
155
     * Filter the data held, to keep only the information with the given shortnames.
66
     *
156
     *
67
     * @param array $shortnames items to keep.
157
     * @param array $shortnames items to keep.
68
     */
158
     */
Línea 129... Línea 219...
129
            if ($attemptlist) {
219
            if ($attemptlist) {
130
                $summary->add_item('attemptlist', get_string('attempts', 'quiz'), $attemptlist);
220
                $summary->add_item('attemptlist', get_string('attempts', 'quiz'), $attemptlist);
131
            }
221
            }
132
        }
222
        }
Línea -... Línea 223...
-
 
223
 
-
 
224
        // Caption.
-
 
225
        $summary->set_caption(get_string('summaryofattemptscaption', 'quiz', $attemptobj->get_attempt_number()));
133
 
226
 
134
        // Attempt state.
227
        // Attempt state.
135
        $summary->add_item('state', get_string('attemptstate', 'quiz'),
228
        $summary->add_item('state', get_string('attemptstate', 'quiz'),
Línea 136... Línea 229...
136
            quiz_attempt::state_name($attemptobj->get_attempt()->state));
229
            quiz_attempt::state_name($attemptobj->get_attempt()->state));
Línea 215... Línea 308...
215
            return $grade;
308
            return $grade;
216
        }
309
        }
Línea 217... Línea 310...
217
 
310
 
218
        // Grades for extra grade items, if any.
311
        // Grades for extra grade items, if any.
219
        foreach ($attemptobj->get_grade_item_totals() as $gradeitemid => $gradeoutof) {
312
        foreach ($attemptobj->get_grade_item_totals() as $gradeitemid => $gradeoutof) {
-
 
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 ?
220
            $this->add_item('marks' . $gradeitemid, format_string($gradeoutof->name), $gradeoutof);
316
                        grade_out_of::NORMAL : grade_out_of::WITH_PERCENT));
Línea 221... Línea 317...
221
        }
317
        }
222
 
318
 
223
        // Show raw marks only if they are different from the grade.
319
        // Show raw marks only if they are different from the grade.
Línea 238... Línea 334...
238
    public function export_for_template(renderer_base $output): array {
334
    public function export_for_template(renderer_base $output): array {
Línea 239... Línea 335...
239
 
335
 
240
        $templatecontext = [
336
        $templatecontext = [
241
            'hasitems' => !empty($this->summarydata),
337
            'hasitems' => !empty($this->summarydata),
-
 
338
            'items' => [],
242
            'items' => [],
339
            'caption' => $this->caption,
243
        ];
340
        ];
244
        foreach ($this->summarydata as $item) {
341
        foreach ($this->summarydata as $item) {
245
            if ($item['title'] instanceof renderable) {
342
            if ($item['title'] instanceof renderable) {
246
                $title = $output->render($item['title']);
343
                $title = $output->render($item['title']);