Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 core_courseformat;
18
 
19
use cm_info;
20
use core\context\module as module_context;
21
use core_completion\cm_completion_details;
22
use core_courseformat\local\overview\overviewitem;
23
use core_courseformat\output\local\overview\activityname;
24
use core_courseformat\output\local\overview\overviewpage;
25
use core_courseformat\base as courseformat;
26
use grade_item;
27
use grade_grade;
28
 
29
/**
30
 * Base class for activity overview.
31
 *
32
 * Plugins must extend this class on their \mod_PLUGINNAME\courseformat\overview
33
 * integration to provide overview items about a course module instance.
34
 *
35
 * @package    core_courseformat
36
 * @copyright  2025 Ferran Recio <ferran@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
abstract class activityoverviewbase {
40
    /** @var cm_info The course module. */
41
    protected module_context $context;
42
 
43
    /** @var \stdClass $course */
44
    protected \stdClass $course;
45
 
46
    /** @var courseformat $format the course format */
47
    protected courseformat $format;
48
 
49
    /**
50
     * Activity Overview Base class constructor.
51
     *
52
     * Overview integrations are meant to use dependency injection.
53
     * Don't create instances of this class directly, use the factory instead.
54
     *
55
     * \core_courseformat\local\overview\overviewfactory::create($cm);
56
     *
57
     * Plugins can override the constructor adding more dependencies such as:
58
     *
59
     *  - protected readonly \moodle_database $db -> To access the database.
60
     *  - protected readonly \core\clock $clock -> The clock interface to handle time.
61
     *
62
     * However, it is important to note all original dependencies must be kept.
63
     *
64
     * @param cm_info $cm The course module information (loaded by the factory).
65
     */
66
    public function __construct(
67
        /** @var cm_info The course module. */
68
        protected readonly cm_info $cm,
69
    ) {
70
        $this->context = $cm->context;
71
        $this->course = $cm->get_course();
72
        $this->format = courseformat::instance($this->course);
73
    }
74
 
75
    /**
76
     * Redirects to the overview page for the activity.
77
     *
78
     * @param int $courseid The course id.
79
     * @param string $modname The module name.
80
     */
81
    public static function redirect_to_overview_page(int $courseid, string $modname): void {
82
        redirect(overviewpage::get_modname_url($courseid, $modname));
83
    }
84
 
85
    /**
86
     * Get the plugin specific overview items for the activity.
87
     *
88
     * Plugins can override this method to provide their own overview items.
89
     *
90
     * The resulting array must be indexed by item shortname.
91
     *
92
     * @return overviewitem[] Array of overview items indexed by item shortname.
93
     */
94
    public function get_extra_overview_items(): array {
95
        return [];
96
    }
97
 
98
    /**
99
     * Get the name of the activity.
100
     */
101
    final public function get_name_overview(): overviewitem {
102
        return new overviewitem(
103
            name: get_string('name'),
104
            value: $this->cm->name,
105
            content: new activityname($this->cm),
106
        );
107
    }
108
 
109
    /**
110
     * Retrieves the due date overview for the activity.
111
     *
112
     * @return overviewitem|null null if module does not have a due date.
113
     */
114
    public function get_due_date_overview(): ?overviewitem {
115
        return null;
116
    }
117
 
118
    /**
119
     * Retrieves the actions overview for the activity.
120
     *
121
     * @return overviewitem|null null if module does not have a main action item.
122
     */
123
    public function get_actions_overview(): ?overviewitem {
124
        return null;
125
    }
126
 
127
    /**
128
     * Retrieves the completion overview for the activity.
129
     *
130
     * @return overviewitem|null null if completion is not enabled.
131
     */
132
    public function get_completion_overview(): ?overviewitem {
133
        global $USER;
134
 
135
        $showcompletionconditions = $this->course->showcompletionconditions == COMPLETION_SHOW_CONDITIONS;
136
 
137
        $completiondetails = cm_completion_details::get_instance(
138
            $this->cm,
139
            $USER->id,
140
            $showcompletionconditions
141
        );
142
 
143
        if (!$completiondetails->is_tracked_user()) {
144
            return null;
145
        }
146
 
147
        $showcompletioninfo = $completiondetails->has_completion() &&
148
            ($showcompletionconditions || $completiondetails->show_manual_completion());
149
 
150
        if (!$showcompletioninfo) {
151
            return new overviewitem(
152
                name: get_string('completion_status', 'completion'),
153
                value: null,
154
                content: '-',
155
            );
156
        }
157
 
158
        $status = $completiondetails->get_overall_completion();
159
 
160
        $completionclass = $this->format->get_output_classname('content\\cm\\completion');
161
        /** @var \core_courseformat\output\local\content\cm\completion $completion */
162
        $completion = new $completionclass(
163
            $this->format,
164
            $this->cm->get_section_info(),
165
            $this->cm
166
        );
167
        $completion->set_smallbutton(false);
168
 
169
        return new overviewitem(
170
            name: get_string('completion_status', 'completion'),
171
            value: $status,
172
            content: $completion,
173
        );
174
    }
175
 
176
    /**
177
     * Retrieves the grades overview items for the activity.
178
     *
179
     * Most activities will have none or one grade. However, some activities
180
     * may have multiple grades, such as workshop or quiz.
181
     *
182
     * It is not recommended to override this method unless the plugin
183
     * has specific requirements. Instead, plugins should override
184
     * get_grade_item_names to provide the grade item names.
185
     *
186
     * @return overviewitem[] Array of overview items representing the grades.
187
     */
188
    public function get_grades_overviews(): array {
189
        global $CFG, $USER;
190
        // This overview is to see the own grades, users with full gradebook
191
        // access will see all grades in the gradebook.
192
        if (has_capability('moodle/grade:viewall', $this->context)) {
193
            return [];
194
        }
195
        if (!plugin_supports('mod', $this->cm->modname, FEATURE_GRADE_HAS_GRADE, false)) {
196
            return [];
197
        }
198
        require_once($CFG->libdir . '/gradelib.php');
199
 
200
        $items = grade_item::fetch_all([
201
                'itemtype' => 'mod',
202
                'itemmodule' => $this->cm->modname,
203
                'iteminstance' => $this->cm->instance,
204
                'courseid' => $this->course->id,
205
        ]);
206
        if (empty($items)) {
207
            return [];
208
        }
209
 
210
        $itemnames = $this->get_grade_item_names($items);
211
        $result = [];
212
        foreach ($items as $item) {
213
            // Plugins may decide to hide a specific grade item by not setting a name.
214
            if (empty($itemnames[$item->id])) {
215
                continue;
216
            }
217
 
218
            $gradegrade = grade_grade::fetch(['itemid' => $item->id, 'userid' => $USER->id]);
219
            if ((!$gradegrade || ($gradegrade->is_hidden() && !has_capability('moodle/grade:viewhidden', $this->context)))) {
220
                if ($item->is_gradable()) {
221
                    $result[] = new overviewitem(
222
                        name: $itemnames[$item->id],
223
                        value: '-',
224
                        content: '-',
225
                    );
226
                }
227
                continue;
228
            }
229
 
230
            $result[] = new overviewitem(
231
                name: $itemnames[$item->id],
232
                value: $gradegrade->finalgrade,
233
                content: grade_format_gradevalue($gradegrade->finalgrade, $item),
234
            );
235
        }
236
        return $result;
237
    }
238
 
239
    /**
240
     * Retrieves the grade item names for the activity.
241
     *
242
     * By default, the overview will display the grade if the activities
243
     * has only one grade item. The name of the grade item will be 'Grade'.
244
     * For plugins with multiple grade items, the plugin must override this method
245
     * and provide names for each grade item that want to be displayed.
246
     *
247
     * @param grade_item[] $items
248
     * @return array<integer, string> the grade item names indexed by item id.
249
     */
250
    protected function get_grade_item_names(array $items): array {
251
        if (count($items) == 1) {
252
            return [reset($items)->id => get_string('gradenoun')];
253
        }
254
        return [];
255
    }
256
}