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 - https://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 <https://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Library of interface functions and constants.
19
 *
20
 * @package     mod_subsection
21
 * @copyright   2023 Amaia Anabitarte <amaia@moodle.com>
22
 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
use core_courseformat\formatactions;
26
use mod_subsection\manager;
27
 
28
/**
29
 * Return if the plugin supports $feature.
30
 *
31
 * @param string $feature Constant representing the feature.
32
 * @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
33
 */
34
function subsection_supports($feature) {
35
    return match ($feature) {
36
        FEATURE_MOD_ARCHETYPE => MOD_ARCHETYPE_RESOURCE,
37
        FEATURE_GROUPS => false,
38
        FEATURE_GROUPINGS => false,
39
        FEATURE_MOD_INTRO => false,
40
        FEATURE_COMPLETION => false,
41
        FEATURE_COMPLETION_TRACKS_VIEWS => false,
42
        FEATURE_GRADE_HAS_GRADE => false,
43
        FEATURE_GRADE_OUTCOMES => false,
44
        FEATURE_BACKUP_MOODLE2 => true,
45
        FEATURE_SHOW_DESCRIPTION => false,
46
        FEATURE_MOD_PURPOSE => MOD_PURPOSE_CONTENT,
47
        FEATURE_MODEDIT_DEFAULT_COMPLETION => false,
48
        FEATURE_QUICKCREATE => true,
49
        FEATURE_CAN_UNINSTALL => false,
50
        default => null,
51
    };
52
}
53
 
54
/**
55
 * Saves a new instance of the mod_subsection into the database.
56
 *
57
 * Given an object containing all the necessary data, (defined by the form
58
 * in mod_form.php) this function will create a new instance and return the id
59
 * number of the instance.
60
 *
61
 * @param object $moduleinstance An object from the form.
62
 * @param mod_subsection_mod_form $mform The form.
63
 * @return int The id of the newly inserted record.
64
 */
65
function subsection_add_instance($moduleinstance, $mform = null) {
66
    global $DB;
67
 
68
    $moduleinstance->timecreated = time();
69
 
70
    $id = $DB->insert_record('subsection', $moduleinstance);
71
 
72
    // Due to name collision, when the object came from the form, the availability conditions are called
73
    // availabilityconditionsjson instead of availability.
74
    $cmavailability = $moduleinstance->availabilityconditionsjson ?? $moduleinstance->availability ?? null;
75
    // Availability could be an empty string but we need to force null.
76
    if (empty($cmavailability)) {
77
        $cmavailability = null;
78
    }
79
 
80
    formatactions::section($moduleinstance->course)->create_delegated(
81
        manager::PLUGINNAME,
82
        $id,
83
        (object)[
84
            'name' => $moduleinstance->name,
85
            'visible' => $moduleinstance->visible,
86
            'availability' => $cmavailability,
87
        ]
88
    );
89
 
90
    return $id;
91
}
92
 
93
/**
94
 * Updates an instance of the mod_subsection in the database.
95
 *
96
 * Given an object containing all the necessary data (defined in mod_form.php),
97
 * this function will update an existing instance with new data.
98
 *
99
 * @param object $moduleinstance An object from the form in mod_form.php.
100
 * @param mod_subsection_mod_form $mform The form.
101
 * @return bool True if successful, false otherwise.
102
 */
103
function subsection_update_instance($moduleinstance, $mform = null) {
104
    global $DB;
105
 
106
    $moduleinstance->timemodified = time();
107
    $moduleinstance->id = $moduleinstance->instance;
108
 
109
    // Due to name collision, when the object came from the form, the availability conditions are called
110
    // availabilityconditionsjson instead of availability.
111
    $cmavailability = $moduleinstance->availabilityconditionsjson ?? $moduleinstance->availability ?? null;
112
    if (!empty($cmavailability)) {
113
        $DB->set_field(
114
            'course_sections',
115
            'availability',
116
            $cmavailability,
117
            ['component' => manager::PLUGINNAME, 'itemid' => $moduleinstance->id]
118
        );
119
    }
120
 
121
    return $DB->update_record('subsection', $moduleinstance);
122
}
123
 
124
/**
125
 * Removes an instance of the mod_subsection from the database.
126
 *
127
 * @param int $id Id of the module instance.
128
 * @return bool True if successful, false on failure.
129
 */
130
function subsection_delete_instance($id) {
131
    global $DB;
132
 
133
    $exists = $DB->get_record('subsection', ['id' => $id]);
134
    if (!$exists) {
135
        return false;
136
    }
137
 
138
    $cm = get_coursemodule_from_instance(manager::MODULE, $id);
139
    $delegatesection = get_fast_modinfo($cm->course)->get_section_info_by_component(manager::PLUGINNAME, $id);
140
    if ($delegatesection) {
141
        formatactions::section($cm->course)->delete($delegatesection);
142
    }
143
 
144
    $DB->delete_records('subsection', ['id' => $id]);
145
 
146
    return true;
147
}
148
 
149
/**
150
 * Returns the lists of all browsable file areas within the given module context.
151
 *
152
 * The file area 'intro' for the activity introduction field is added automatically
153
 * by {@see file_browser::get_file_info_context_module()}.
154
 *
155
 * @package     mod_subsection
156
 * @category    files
157
 *
158
 * @param stdClass $course
159
 * @param stdClass $cm
160
 * @param stdClass $context
161
 * @return string[].
162
 */
163
function subsection_get_file_areas($course, $cm, $context) {
164
    return [];
165
}
166
 
167
/**
168
 * File browsing support for mod_subsection file areas.
169
 *
170
 * @package     mod_subsection
171
 * @category    files
172
 *
173
 * @param file_browser $browser
174
 * @param array $areas
175
 * @param stdClass $course
176
 * @param stdClass $cm
177
 * @param stdClass $context
178
 * @param string $filearea
179
 * @param int $itemid
180
 * @param string $filepath
181
 * @param string $filename
182
 * @return file_info|null file_info instance or null if not found.
183
 */
184
function subsection_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
185
    return null;
186
}
187
 
188
/**
189
 * Serves the files from the mod_subsection file areas.
190
 *
191
 * @package     mod_subsection
192
 * @category    files
193
 *
194
 * @param stdClass $course The course object.
195
 * @param stdClass $cm The course module object.
196
 * @param stdClass $context The mod_subsection's context.
197
 * @param string $filearea The name of the file area.
198
 * @param array $args Extra arguments (itemid, path).
199
 * @param bool $forcedownload Whether or not force download.
200
 * @param array $options Additional options affecting the file serving.
201
 */
202
function subsection_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, $options = []) {
203
    global $DB, $CFG;
204
 
205
    if ($context->contextlevel != CONTEXT_MODULE) {
206
        send_file_not_found();
207
    }
208
 
209
    require_login($course, true, $cm);
210
    send_file_not_found();
211
}
212
 
213
/**
214
 * Extends the global navigation tree by adding mod_subsection nodes if there is a relevant content.
215
 *
216
 * This can be called by an AJAX request so do not rely on $PAGE as it might not be set up properly.
217
 *
218
 * @param navigation_node $subsectionnode An object representing the navigation tree node.
219
 * @param stdClass $course
220
 * @param stdClass $module
221
 * @param cm_info $cm
222
 */
223
function subsection_extend_navigation($subsectionnode, $course, $module, $cm) {
224
}
225
 
226
/**
227
 * Extends the settings navigation with the mod_subsection settings.
228
 *
229
 * This function is called when the context for the page is a mod_subsection module.
230
 * This is not called by AJAX so it is safe to rely on the $PAGE.
231
 *
232
 * @param settings_navigation $settingsnav {@see settings_navigation}
233
 * @param navigation_node $subsectionnode {@see navigation_node}
234
 */
235
function subsection_extend_settings_navigation($settingsnav, $subsectionnode = null) {
236
}
237
 
238
/**
239
 * Sets dynamic information about a course module
240
 *
241
 * This function is called from cm_info when displaying the module
242
 * mod_subsection can be displayed inline on course page and therefore have no course link
243
 *
244
 * @param cm_info $cm
245
 */
246
function subsection_cm_info_dynamic(cm_info $cm) {
247
    $cm->set_no_view_link();
248
}
249
 
250
/**
251
 * Sets the special subsection display on course page.
252
 *
253
 * @param cm_info $cm Course-module object
254
 */
255
function subsection_cm_info_view(cm_info $cm) {
256
    global $DB, $PAGE;
257
 
258
    $cm->set_custom_cmlist_item(true);
259
    $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
260
 
261
    // Get the section info.
262
    $delegatedsection = manager::create_from_coursemodule($cm)->get_delegated_section_info();
263
 
264
    // Render the delegated section.
265
    $format = course_get_format($course);
266
    $renderer = $PAGE->get_renderer('format_' . $course->format);
267
    $outputclass = $format->get_output_classname('content\\delegatedsection');
268
    /** @var \core_courseformat\output\local\content\delegatedsection $delegatedsectionoutput */
269
    $delegatedsectionoutput = new $outputclass($format, $delegatedsection);
270
 
271
    $cm->set_content($renderer->render($delegatedsectionoutput), true);
272
}
273
 
274
/**
275
 * Add a get_coursemodule_info function to add 'extra' information for the course (see resource).
276
 *
277
 * Given a course_module object, this function returns any "extra" information that may be needed
278
 * when printing this activity in a course listing.  See get_array_of_activities() in course/lib.php.
279
 *
280
 * @param stdClass $coursemodule The coursemodule object (record).
281
 * @return cached_cm_info|bool An object on information that the courses will know about. False if not found.
282
 */
283
function subsection_get_coursemodule_info(stdClass $coursemodule): cached_cm_info|bool {
284
    global $DB;
285
 
286
    $dbparams = ['component' => 'mod_subsection', 'itemid' => $coursemodule->instance];
287
    if (! $delegatedsection = $DB->get_record('course_sections', $dbparams, 'id, name')) {
288
        return false;
289
    }
290
 
291
    $result = new cached_cm_info();
292
    $result->name = $delegatedsection->name;
293
 
294
    $result->customdata['sectionid'] = $delegatedsection->id;
295
 
296
    return $result;
297
}
298
 
299
/**
300
 * Get icon mapping for font-awesome.
301
 */
302
function mod_subsection_get_fontawesome_icon_map() {
303
    return [
304
        'mod_subsection:subsection' => 'fa-rectangle-list',
305
    ];
306
}
307
 
308
/**
309
 * Get the course content items for the subsection module.
310
 *
311
 * This function is called when the course content is being generated for the activity chooser.
312
 * However, here this module is never shown in the activity chooser so we return an empty array.
313
 *
314
 * @param \core_course\local\entity\content_item $contentitem
315
 * @param stdClass $user
316
 * @param stdClass $course
317
 * @return array
318
 */
319
function mod_subsection_get_course_content_items(
320
    core_course\local\entity\content_item $contentitem,
321
    stdClass $user,
322
    stdClass $course
323
): array {
324
    return [];
325
}