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
 * Mandatory public API of imscp module
19
 *
20
 * @package mod_imscp
21
 * @copyright  2009 Petr Skoda  {@link http://skodak.org}
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
/**
28
 * List of features supported in IMS CP module
29
 * @param string $feature FEATURE_xx constant for requested feature
30
 * @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
31
 */
32
function imscp_supports($feature) {
33
    switch($feature) {
34
        case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
35
        case FEATURE_GROUPS:                  return false;
36
        case FEATURE_GROUPINGS:               return false;
37
        case FEATURE_MOD_INTRO:               return true;
38
        case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
39
        case FEATURE_GRADE_HAS_GRADE:         return false;
40
        case FEATURE_GRADE_OUTCOMES:          return false;
41
        case FEATURE_BACKUP_MOODLE2:          return true;
42
        case FEATURE_SHOW_DESCRIPTION:        return true;
43
        case FEATURE_MOD_PURPOSE:
44
            return MOD_PURPOSE_INTERACTIVECONTENT;
45
 
46
        default: return null;
47
    }
48
}
49
 
50
/**
51
 * This function is used by the reset_course_userdata function in moodlelib.
52
 *
53
 * @param stdClass $data the data submitted from the reset course.
54
 * @return array status array
55
 */
56
function imscp_reset_userdata($data) {
57
 
58
    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
59
    // See MDL-9367.
60
 
61
    return array();
62
}
63
 
64
/**
65
 * List the actions that correspond to a view of this module.
66
 * This is used by the participation report.
67
 *
68
 * Note: This is not used by new logging system. Event with
69
 *       crud = 'r' and edulevel = LEVEL_PARTICIPATING will
70
 *       be considered as view action.
71
 *
72
 * @return array
73
 */
74
function imscp_get_view_actions() {
75
    return array('view', 'view all');
76
}
77
 
78
/**
79
 * List the actions that correspond to a post of this module.
80
 * This is used by the participation report.
81
 *
82
 * Note: This is not used by new logging system. Event with
83
 *       crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
84
 *       will be considered as post action.
85
 *
86
 * @return array
87
 */
88
function imscp_get_post_actions() {
89
    return array('update', 'add');
90
}
91
 
92
/**
93
 * Add imscp instance.
94
 * @param object $data
95
 * @param object $mform
96
 * @return int new imscp instance id
97
 */
98
function imscp_add_instance($data, $mform) {
99
    global $CFG, $DB;
100
    require_once("$CFG->dirroot/mod/imscp/locallib.php");
101
 
102
    $cmid = $data->coursemodule;
103
 
104
    $data->timemodified = time();
105
    $data->revision     = 1;
106
    $data->structure    = null;
107
 
108
    $data->id = $DB->insert_record('imscp', $data);
109
 
110
    // We need to use context now, so we need to make sure all needed info is already in db.
111
    $DB->set_field('course_modules', 'instance', $data->id, array('id' => $cmid));
112
    $context = context_module::instance($cmid);
113
    $imscp = $DB->get_record('imscp', array('id' => $data->id), '*', MUST_EXIST);
114
 
115
    if (!empty($data->package)) {
116
        // Save uploaded files to 'backup' filearea.
117
        $fs = get_file_storage();
118
        $fs->delete_area_files($context->id, 'mod_imscp', 'backup', 1);
119
        file_save_draft_area_files($data->package, $context->id, 'mod_imscp', 'backup',
120
            1, array('subdirs' => 0, 'maxfiles' => 1));
121
        // Get filename of zip that was uploaded.
122
        $files = $fs->get_area_files($context->id, 'mod_imscp', 'backup', 1, '', false);
123
        if ($files) {
124
            // Extract package content to 'content' filearea.
125
            $package = reset($files);
126
            $packer = get_file_packer('application/zip');
127
            $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', 1, '/');
128
            $structure = imscp_parse_structure($imscp, $context);
129
            $imscp->structure = is_array($structure) ? serialize($structure) : null;
130
            $DB->update_record('imscp', $imscp);
131
        }
132
    }
133
 
134
    $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
135
    \core_completion\api::update_completion_date_event($cmid, 'imscp', $data->id, $completiontimeexpected);
136
 
137
    return $data->id;
138
}
139
 
140
/**
141
 * Update imscp instance.
142
 * @param object $data
143
 * @param object $mform
144
 * @return bool true
145
 */
146
function imscp_update_instance($data, $mform) {
147
    global $CFG, $DB;
148
    require_once("$CFG->dirroot/mod/imscp/locallib.php");
149
 
150
    $cmid = $data->coursemodule;
151
 
152
    $data->timemodified = time();
153
    $data->id           = $data->instance;
154
    $data->structure   = null; // Better reparse structure after each update.
155
 
156
    $DB->update_record('imscp', $data);
157
 
158
    $context = context_module::instance($cmid);
159
    $imscp = $DB->get_record('imscp', array('id' => $data->id), '*', MUST_EXIST);
160
 
161
    if (!empty($data->package) && ($draftareainfo = file_get_draft_area_info($data->package)) &&
162
            $draftareainfo['filecount']) {
163
        $fs = get_file_storage();
164
 
165
        $imscp->revision++;
166
        $DB->update_record('imscp', $imscp);
167
 
168
        // Get a list of existing packages before adding new package.
169
        if ($imscp->keepold > -1) {
170
            $packages = $fs->get_area_files($context->id, 'mod_imscp', 'backup', false, "itemid ASC", false);
171
        } else {
172
            $packages = array();
173
        }
174
 
175
        file_save_draft_area_files($data->package, $context->id, 'mod_imscp', 'backup',
176
            $imscp->revision, array('subdirs' => 0, 'maxfiles' => 1));
177
        $files = $fs->get_area_files($context->id, 'mod_imscp', 'backup', $imscp->revision, '', false);
178
        $package = reset($files);
179
 
180
        // Purge all extracted content.
181
        $fs->delete_area_files($context->id, 'mod_imscp', 'content');
182
 
183
        // Extract package content.
184
        if ($package) {
185
            $packer = get_file_packer('application/zip');
186
            $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', $imscp->revision, '/');
187
        }
188
 
189
        // Cleanup old package files, keep current + keep old.
190
        while ($packages and (count($packages) > $imscp->keepold)) {
191
            $package = array_shift($packages);
192
            $fs->delete_area_files($context->id, 'mod_imscp', 'backup', $package->get_itemid());
193
        }
194
    }
195
 
196
    $structure = imscp_parse_structure($imscp, $context);
197
    $imscp->structure = is_array($structure) ? serialize($structure) : null;
198
    $DB->update_record('imscp', $imscp);
199
 
200
    $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
201
    \core_completion\api::update_completion_date_event($cmid, 'imscp', $imscp->id, $completiontimeexpected);
202
 
203
    return true;
204
}
205
 
206
/**
207
 * Delete imscp instance.
208
 * @param int $id
209
 * @return bool true
210
 */
211
function imscp_delete_instance($id) {
212
    global $DB;
213
 
214
    if (!$imscp = $DB->get_record('imscp', array('id' => $id))) {
215
        return false;
216
    }
217
 
218
    $cm = get_coursemodule_from_instance('imscp', $id);
219
    \core_completion\api::update_completion_date_event($cm->id, 'imscp', $id, null);
220
 
221
    // Note: all context files are deleted automatically.
222
 
223
    $DB->delete_records('imscp', array('id' => $imscp->id));
224
 
225
    return true;
226
}
227
 
228
/**
229
 * Lists all browsable file areas
230
 *
231
 * @package  mod_imscp
232
 * @category files
233
 * @param stdClass $course course object
234
 * @param stdClass $cm course module object
235
 * @param stdClass $context context object
236
 * @return array
237
 */
238
function imscp_get_file_areas($course, $cm, $context) {
239
    $areas = array();
240
 
241
    $areas['content'] = get_string('areacontent', 'imscp');
242
    $areas['backup']  = get_string('areabackup', 'imscp');
243
 
244
    return $areas;
245
}
246
 
247
/**
248
 * File browsing support for imscp module ontent area.
249
 *
250
 * @package  mod_imscp
251
 * @category files
252
 * @param file_browser $browser file browser
253
 * @param stdClass $areas file areas
254
 * @param stdClass $course course object
255
 * @param stdClass $cm course module object
256
 * @param stdClass $context context object
257
 * @param string $filearea file area
258
 * @param int $itemid item ID
259
 * @param string $filepath file path
260
 * @param string $filename file name
261
 * @return file_info instance or null if not found
262
 */
263
function imscp_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
264
    global $CFG, $DB;
265
 
266
    // Note: imscp_intro handled in file_browser automatically.
267
 
268
    if (!has_capability('moodle/course:managefiles', $context)) {
269
        // No peeking here for students!
270
        return null;
271
    }
272
 
273
    if ($filearea !== 'content' and $filearea !== 'backup') {
274
        return null;
275
    }
276
 
277
    require_once("$CFG->dirroot/mod/imscp/locallib.php");
278
 
279
    if (is_null($itemid)) {
280
        return new imscp_file_info($browser, $course, $cm, $context, $areas, $filearea, $itemid);
281
    }
282
 
283
    $fs = get_file_storage();
284
    $filepath = is_null($filepath) ? '/' : $filepath;
285
    $filename = is_null($filename) ? '.' : $filename;
286
    if (!$storedfile = $fs->get_file($context->id, 'mod_imscp', $filearea, $itemid, $filepath, $filename)) {
287
        return null;
288
    }
289
 
290
    // Do not allow manual modification of any files!
291
    $urlbase = $CFG->wwwroot.'/pluginfile.php';
292
    return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemid, true, true, false, false); // No writing here!
293
}
294
 
295
/**
296
 * Serves the imscp files.
297
 *
298
 * @package  mod_imscp
299
 * @category files
300
 * @param stdClass $course course object
301
 * @param stdClass $cm course module object
302
 * @param stdClass $context context object
303
 * @param string $filearea file area
304
 * @param array $args extra arguments
305
 * @param bool $forcedownload whether or not force download
306
 * @param array $options additional options affecting the file serving
307
 * @return bool false if file not found, does not return if found - justsend the file
308
 */
309
function imscp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
310
    global $CFG, $DB;
311
 
312
    if ($context->contextlevel != CONTEXT_MODULE) {
313
        return false;
314
    }
315
 
316
    require_login($course, true, $cm);
317
 
318
    if ($filearea === 'content') {
319
        if (!has_capability('mod/imscp:view', $context)) {
320
            return false;
321
        }
322
        $revision = array_shift($args);
323
        $fs = get_file_storage();
324
        $relativepath = implode('/', $args);
325
        if ($relativepath === 'imsmanifest.xml') {
326
            if (!has_capability('moodle/course:managefiles', $context)) {
327
                // No stealing of detailed package info.
328
                return false;
329
            }
330
        }
331
        $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
332
        if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
333
            return false;
334
        }
335
 
336
        // Finally send the file.
337
        send_stored_file($file, null, 0, $forcedownload, $options);
338
 
339
    } else if ($filearea === 'backup') {
340
        if (!has_capability('moodle/course:managefiles', $context)) {
341
            // No stealing of package backups.
342
            return false;
343
        }
344
        $revision = array_shift($args);
345
        $fs = get_file_storage();
346
        $relativepath = implode('/', $args);
347
        $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
348
        if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
349
            return false;
350
        }
351
 
352
        // Finally send the file.
353
        send_stored_file($file, null, 0, $forcedownload, $options);
354
 
355
    } else {
356
        return false;
357
    }
358
}
359
 
360
/**
361
 * Return a list of page types
362
 * @param string $pagetype current page type
363
 * @param stdClass $parentcontext Block's parent context
364
 * @param stdClass $currentcontext Current context of block
365
 * @return array $modulepagetype list
366
 */
367
function imscp_page_type_list($pagetype, $parentcontext, $currentcontext) {
368
    $modulepagetype = array('mod-imscp-*' => get_string('page-mod-imscp-x', 'imscp'));
369
    return $modulepagetype;
370
}
371
 
372
/**
373
 * Export imscp resource contents
374
 *
375
 * @param  stdClass $cm     Course module object
376
 * @param  string $baseurl  Base URL for file downloads
377
 * @return array of file content
378
 */
379
function imscp_export_contents($cm, $baseurl) {
380
    global $DB;
381
 
382
    $contents = array();
383
    $context = context_module::instance($cm->id);
384
 
385
    $imscp = $DB->get_record('imscp', array('id' => $cm->instance), '*', MUST_EXIST);
386
 
387
    // We export the IMSCP structure as json encoded string.
388
    $structure = array();
389
    $structure['type']         = 'content';
390
    $structure['filename']     = 'structure';
391
    $structure['filepath']     = '/';
392
    $structure['filesize']     = 0;
393
    $structure['fileurl']      = null;
394
    $structure['timecreated']  = $imscp->timemodified;
395
    $structure['timemodified'] = $imscp->timemodified;
396
    $structure['content']      = json_encode(unserialize_array($imscp->structure));
397
    $structure['sortorder']    = 0;
398
    $structure['userid']       = null;
399
    $structure['author']       = null;
400
    $structure['license']      = null;
401
    $contents[] = $structure;
402
 
403
    // Area files.
404
    $fs = get_file_storage();
405
    $files = $fs->get_area_files($context->id, 'mod_imscp', 'content', $imscp->revision, 'id ASC', false);
406
    foreach ($files as $fileinfo) {
407
        $file = array();
408
        $file['type']         = 'file';
409
        $file['filename']     = $fileinfo->get_filename();
410
        $file['filepath']     = $fileinfo->get_filepath();
411
        $file['filesize']     = $fileinfo->get_filesize();
412
        $file['fileurl']      = moodle_url::make_webservice_pluginfile_url(
413
                                    $context->id, 'mod_imscp', 'content', $imscp->revision,
414
                                    $fileinfo->get_filepath(), $fileinfo->get_filename())->out(false);
415
        $file['timecreated']  = $fileinfo->get_timecreated();
416
        $file['timemodified'] = $fileinfo->get_timemodified();
417
        $file['sortorder']    = $fileinfo->get_sortorder();
418
        $file['userid']       = $fileinfo->get_userid();
419
        $file['author']       = $fileinfo->get_author();
420
        $file['license']      = $fileinfo->get_license();
421
        $file['mimetype']     = $fileinfo->get_mimetype();
422
        $file['isexternalfile'] = $fileinfo->is_external_file();
423
        if ($file['isexternalfile']) {
424
            $file['repositorytype'] = $fileinfo->get_repository_type();
425
        }
426
        $contents[] = $file;
427
    }
428
 
429
    return $contents;
430
}
431
 
432
/**
433
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
434
 *
435
 * @param  stdClass $imscp   imscp object
436
 * @param  stdClass $course     course object
437
 * @param  stdClass $cm         course module object
438
 * @param  stdClass $context    context object
439
 * @since Moodle 3.0
440
 */
441
function imscp_view($imscp, $course, $cm, $context) {
442
 
443
    // Trigger course_module_viewed event.
444
    $params = array(
445
        'context' => $context,
446
        'objectid' => $imscp->id
447
    );
448
 
449
    $event = \mod_imscp\event\course_module_viewed::create($params);
450
    $event->add_record_snapshot('course_modules', $cm);
451
    $event->add_record_snapshot('course', $course);
452
    $event->add_record_snapshot('imscp', $imscp);
453
    $event->trigger();
454
 
455
    // Completion.
456
    $completion = new completion_info($course);
457
    $completion->set_module_viewed($cm);
458
}
459
 
460
/**
461
 * Check if the module has any update that affects the current user since a given time.
462
 *
463
 * @param  cm_info $cm course module data
464
 * @param  int $from the time to check updates from
465
 * @param  array $filter  if we need to check only specific updates
466
 * @return stdClass an object with the different type of areas indicating if they were updated or not
467
 * @since Moodle 3.2
468
 */
469
function imscp_check_updates_since(cm_info $cm, $from, $filter = array()) {
470
    $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
471
    return $updates;
472
}
473
 
474
/**
475
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
476
 *
477
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
478
 * is not displayed on the block.
479
 *
480
 * @param calendar_event $event
481
 * @param \core_calendar\action_factory $factory
482
 * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
483
 * @return \core_calendar\local\event\entities\action_interface|null
484
 */
485
function mod_imscp_core_calendar_provide_event_action(calendar_event $event,
486
                                                      \core_calendar\action_factory $factory,
487
                                                      int $userid = 0) {
488
    $cm = get_fast_modinfo($event->courseid, $userid)->instances['imscp'][$event->instance];
489
 
490
    if (!$cm->uservisible) {
491
        // The module is not visible to the user for any reason.
492
        return null;
493
    }
494
 
495
    $completion = new \completion_info($cm->get_course());
496
 
497
    $completiondata = $completion->get_data($cm, false, $userid);
498
 
499
    if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
500
        return null;
501
    }
502
 
503
    return $factory->create_instance(
504
        get_string('view'),
505
        new \moodle_url('/mod/imscp/view.php', ['id' => $cm->id]),
506
        1,
507
        true
508
    );
509
}