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
/**
18
 * unilabel module.
19
 *
20
 * @package     mod_unilabel
21
 * @author      Andreas Grabs <info@grabs-edv.de>
22
 * @copyright   2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
23
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @param mixed $unilabel
25
 */
26
 
27
/**
28
 * Given an object containing all the necessary data,
29
 * (defined by the form in mod_form.php) this function
30
 * will create a new instance and return the id number
31
 * of the new instance.
32
 *
33
 * @param  \stdClass $unilabel
34
 * @return bool|int
35
 */
36
function unilabel_add_instance($unilabel) {
37
    global $DB;
38
 
39
    $unilabel->timemodified = time();
40
 
41
    $unilabel->id = $DB->insert_record('unilabel', $unilabel);
42
    $DB->update_record('unilabel', $unilabel);
43
 
44
    $completiontimeexpected = !empty($unilabel->completionexpected) ? $unilabel->completionexpected : null;
45
    \core_completion\api::update_completion_date_event($unilabel->coursemodule, 'unilabel', $unilabel->id, $completiontimeexpected);
46
 
47
    return $unilabel->id;
48
}
49
 
50
/**
51
 * Given an object containing all the necessary data,
52
 * (defined by the form in mod_form.php) this function
53
 * will update an existing instance with new data.
54
 *
55
 * @param  \stdClass $unilabel
56
 * @return bool
57
 */
58
function unilabel_update_instance($unilabel) {
59
    global $DB;
60
 
61
    $unilabel->timemodified = time();
62
    $unilabel->id           = $unilabel->instance;
63
 
64
    $completiontimeexpected = !empty($unilabel->completionexpected) ? $unilabel->completionexpected : null;
65
    \core_completion\api::update_completion_date_event($unilabel->coursemodule, 'unilabel', $unilabel->id, $completiontimeexpected);
66
 
67
    return $DB->update_record('unilabel', $unilabel);
68
}
69
 
70
/**
71
 * Given an ID of an instance of this module,
72
 * this function will permanently delete the instance
73
 * and any data that depends on it.
74
 *
75
 * @param  int  $id
76
 * @return bool
77
 */
78
function unilabel_delete_instance($id) {
79
    global $DB;
80
 
81
    if (!$unilabel = $DB->get_record('unilabel', ['id' => $id])) {
82
        return false;
83
    }
84
 
85
    $result = true;
86
 
87
    $cm = get_coursemodule_from_instance('unilabel', $id);
88
    \core_completion\api::update_completion_date_event($cm->id, 'unilabel', $unilabel->id, null);
89
 
90
    // Delete content form plugins.
91
    \mod_unilabel\factory::delete_plugin_content($unilabel->id);
92
 
93
    if (!$DB->delete_records('unilabel', ['id' => $unilabel->id])) {
94
        $result = false;
95
    }
96
 
97
    return $result;
98
}
99
 
100
/**
101
 * Given a course_module object, this function returns any
102
 * "extra" information that may be needed when printing
103
 * this activity in a course listing.
104
 * See get_array_of_activities() in course/lib.php.
105
 *
106
 * @param  \stdClass           $coursemodule
107
 * @return cached_cm_info|null
108
 */
109
function unilabel_get_coursemodule_info($coursemodule) {
110
    global $DB;
111
 
112
    if ($unilabel = $DB->get_record('unilabel', ['id' => $coursemodule->instance], 'id, name, intro, introformat, unilabeltype')) {
113
        $content = format_module_intro('unilabel', $unilabel, $coursemodule->id, false);
114
 
115
        $info = new cached_cm_info();
116
        // No filtering here because this info is cached and filtered later.
117
        $info->content = $content;
118
        $info->name    = $unilabel->name;
119
 
120
        return $info;
121
    }
122
 
123
    return null;
124
}
125
 
126
/**
127
 * Set the content for the course page to show there.
128
 *
129
 * @param  \cm_info $cm
130
 * @return void
131
 */
132
function unilabel_cm_info_view(cm_info $cm) {
133
    global $DB, $PAGE;
134
 
135
    $renderer     = $PAGE->get_renderer('mod_unilabel');
136
    $unilabel     = $DB->get_record('unilabel', ['id' => $cm->instance], 'id, course, name, intro, introformat, unilabeltype');
137
    $unilabeltype = \mod_unilabel\factory::get_plugin($unilabel->unilabeltype);
138
    if (!$unilabeltype->is_active()) {
139
        $unilabeltype = \mod_unilabel\factory::get_plugin('simpletext');
140
    }
141
 
142
    $content = ['content' => $unilabeltype->get_content($unilabel, $cm, $renderer)];
143
 
144
    // Add the edit link if needed.
145
    if ($PAGE->user_is_editing()) {
146
        if (has_capability('mod/unilabel:edit', $cm->context)) {
147
            $editlink            = new \stdClass();
148
            $editlink->title     = get_string('editcontent', 'mod_unilabel');
149
            $editlink->url       = new \moodle_url('/mod/unilabel/edit_content.php', ['cmid' => $cm->id]);
150
            $content['editlink'] = $editlink;
151
        }
152
    }
153
 
154
    $cm->set_content($renderer->render_from_template('mod_unilabel/content', $content));
155
 
156
    $cm->set_custom_cmlist_item(true);
157
}
158
 
159
/**
160
 * This function is used by the reset_course_userdata function in moodlelib.
161
 *
162
 * @param  \stdClass $data the data submitted from the reset course
163
 * @return array     status array
164
 */
165
function unilabel_reset_userdata($data) {
166
    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
167
    // See MDL-9367.
168
 
169
    return [];
170
}
171
 
172
/**
173
 * Returns all other caps used in module.
174
 *
175
 * @return array
176
 */
177
function unilabel_get_extra_capabilities() {
178
    return ['moodle/site:accessallgroups'];
179
}
180
 
181
/**
182
 * What features are supported in this activity?
183
 * @uses FEATURE_IDNUMBER
184
 * @uses FEATURE_GROUPS
185
 * @uses FEATURE_GROUPINGS
186
 * @uses FEATURE_MOD_INTRO
187
 * @uses FEATURE_COMPLETION_TRACKS_VIEWS
188
 * @uses FEATURE_GRADE_HAS_GRADE
189
 * @uses FEATURE_GRADE_OUTCOMES
190
 * @param  string    $feature FEATURE_xx constant for requested feature
191
 * @return bool|null True if module supports feature, false if not, null if doesn't know
192
 */
193
function unilabel_supports($feature) {
194
    switch ($feature) {
195
        case FEATURE_IDNUMBER:
196
            return false;
197
        case FEATURE_GROUPS:
198
            return false;
199
        case FEATURE_GROUPINGS:
200
            return false;
201
        case FEATURE_MOD_INTRO:
202
            return true;
203
        case FEATURE_COMPLETION_TRACKS_VIEWS:
204
            return false;
205
        case FEATURE_GRADE_HAS_GRADE:
206
            return false;
207
        case FEATURE_GRADE_OUTCOMES:
208
            return false;
209
        case FEATURE_MOD_ARCHETYPE:
210
            return MOD_ARCHETYPE_RESOURCE;
211
        case FEATURE_BACKUP_MOODLE2:
212
            return true;
213
        case FEATURE_NO_VIEW_LINK:
214
            return true;
215
        case FEATURE_MOD_PURPOSE:
216
            return MOD_PURPOSE_CONTENT;
217
        default:
218
            return null;
219
    }
220
}
221
 
222
/**
223
 * Check if the module has any update that affects the current user since a given time.
224
 *
225
 * @param  cm_info  $cm     course module data
226
 * @param  int      $from   the time to check updates from
227
 * @param  array    $filter if we need to check only specific updates
228
 * @return stdClass an object with the different type of areas indicating if they were updated or not
229
 * @since Moodle 3.2
230
 */
231
function unilabel_check_updates_since(cm_info $cm, $from, $filter = []) {
232
    $updates = course_check_module_updates_since($cm, $from, [], $filter);
233
 
234
    return $updates;
235
}
236
 
237
/**
238
 * Get a html fragment.
239
 *
240
 * @param  mixed  $args an array or object with context and parameters needed to get the data
241
 * @return string The html fragment we want to use by ajax
242
 */
243
function mod_unilabel_output_fragment_get_edit_element($args) {
244
    global $OUTPUT, $DB;
245
 
246
    $contextid = $args['contextid'] ?? 0;
247
    $formid = $args['formid'] ?? '';
248
    $repeatindex = $args['repeatindex'] ?? 0;
249
 
250
    if (empty($contextid)) {
251
        throw new \moodle_exception('Missing param "contextid"');
252
    }
253
    if (empty($formid)) {
254
        throw new \moodle_exception('Missing param "formid"');
255
    }
256
    if (empty($formid)) {
257
        $repeatindex = 0;
258
    }
259
 
260
    $context = \context::instance_by_id($contextid);
261
    if ($context->contextlevel != CONTEXT_MODULE) {
262
        throw new \moodle_exception('Wrong contextlevel');
263
    }
264
 
265
    if (!$cm = get_coursemodule_from_id('unilabel', $context->instanceid)) {
266
        throw new \moodle_exception('Wrong contextid "' . $context->id . '"');
267
    }
268
 
269
    if (!$unilabel = $DB->get_record('unilabel', ['id' => $cm->instance])) {
270
        throw new \moodle_exception('Wrong instance "' . $cm->instance . '"');
271
    }
272
    $course = get_course($cm->course);
273
 
274
    $type = $unilabel->unilabeltype;
275
    if (empty($type)) {
276
        throw new \moodle_exception('No unilabeltype defined');
277
    }
278
 
279
    $classname = 'unilabeltype_' . $type . '\\output\\edit_element';
280
    if (!class_exists($classname)) {
281
        throw new \moodle_exception('Could not find class "' . $classname . '"');
282
    }
283
 
284
    $formid = $args['formid'];
285
    $context = \context::instance_by_id($args['contextid']);
286
    $repeatindex = intval($args['repeatindex']);
287
 
288
    $editelement = new $classname(
289
        $formid,
290
        $context,
291
        $course,
292
        $type,
293
        $repeatindex
294
    );
295
 
296
    return $OUTPUT->render($editelement);
297
}
298
 
299
/**
300
 * Get a html fragment.
301
 *
302
 * @param  mixed  $args an array or object with context and parameters needed to get the data
303
 * @return string The html fragment we want to use by ajax
304
 */
305
function mod_unilabel_output_fragment_get_tinyconfig($args) {
306
    global $DB;
307
 
308
    if (!\mod_unilabel\tinymce_helper::tiny_active()) {
309
        throw new \moodle_exception('Use only if tinymce is the current editor');
310
    }
311
 
312
    $contextid = $args['contextid'] ?? 0;
313
    $targetid = $args['targetid'] ?? '';
314
    $targetname = $args['targetname'] ?? '';
315
    $draftitemid = $args['draftitemid'] ?? 0;
316
    $repeatindex = $args['repeatindex'] ?? 0;
317
 
318
    if (empty($contextid)) {
319
        throw new \moodle_exception('Missing param "contextid"');
320
    }
321
    if (empty($targetid)) {
322
        throw new \moodle_exception('Missing param "targetid"');
323
    }
324
    if (empty($targetname)) {
325
        throw new \moodle_exception('Missing param "targetname"');
326
    }
327
    if (empty($draftitemid)) {
328
        throw new \moodle_exception('Missing param "draftitemid"');
329
    }
330
 
331
    $context = \context::instance_by_id($contextid);
332
    if ($context->contextlevel != CONTEXT_MODULE) {
333
        throw new \moodle_exception('Wrong contextlevel');
334
    }
335
 
336
    list($course, $cm) = get_course_and_cm_from_cmid($context->instanceid, 'unilabel');
337
    if (!$unilabel = $DB->get_record('unilabel', ['id' => $cm->instance])) {
338
        throw new \moodle_exception('Wrong instance "' . $cm->instance . '"');
339
    }
340
 
341
    $type = $unilabel->unilabeltype;
342
    $classname = '\\unilabeltype_' . $type . '\\output\edit_element';
343
    if (!class_exists($classname)) {
344
        throw new \moodle_exception('Wrong element type "' . $classname . '"');
345
    }
346
    $editelementobj = new $classname('dummy', $context, $course, $type, $repeatindex, false);
347
    $elements = $editelementobj->get_elements();
348
 
349
    $tinyhelper = new \mod_unilabel\tinymce_helper();
350
 
351
    foreach ($elements as $element) {
352
        if ($element->getType() != 'editor') {
353
            continue;
354
        }
355
        $attributes = $element->getAttributes();
356
        if ($attributes['id'] == $targetid) {
357
            return $tinyhelper->get_options($editelementobj->editor_options(), $draftitemid);
358
        }
359
 
360
    }
361
 
362
    throw new \moodle_exception('Element not found');
363
}