Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * @package    mod_resource
20
 * @copyright  2009 Petr Skoda  {@link http://skodak.org}
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
defined('MOODLE_INTERNAL') || die;
25
 
26
/**
27
 * List of features supported in Resource module
28
 * @param string $feature FEATURE_xx constant for requested feature
29
 * @return mixed True if module supports feature, false if not, null if doesn't know or string for the module purpose.
30
 */
31
function resource_supports($feature) {
32
    switch($feature) {
33
        case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
34
        case FEATURE_GROUPS:                  return false;
35
        case FEATURE_GROUPINGS:               return false;
36
        case FEATURE_MOD_INTRO:               return true;
37
        case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
38
        case FEATURE_GRADE_HAS_GRADE:         return false;
39
        case FEATURE_GRADE_OUTCOMES:          return false;
40
        case FEATURE_BACKUP_MOODLE2:          return true;
41
        case FEATURE_SHOW_DESCRIPTION:        return true;
42
        case FEATURE_MOD_PURPOSE:             return MOD_PURPOSE_CONTENT;
43
 
44
        default: return null;
45
    }
46
}
47
 
48
/**
49
 * This function is used by the reset_course_userdata function in moodlelib.
50
 * @param $data the data submitted from the reset course.
51
 * @return array status array
52
 */
53
function resource_reset_userdata($data) {
54
 
55
    // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
56
    // See MDL-9367.
57
 
58
    return array();
59
}
60
 
61
/**
62
 * List the actions that correspond to a view of this module.
63
 * This is used by the participation report.
64
 *
65
 * Note: This is not used by new logging system. Event with
66
 *       crud = 'r' and edulevel = LEVEL_PARTICIPATING will
67
 *       be considered as view action.
68
 *
69
 * @return array
70
 */
71
function resource_get_view_actions() {
72
    return array('view','view all');
73
}
74
 
75
/**
76
 * List the actions that correspond to a post of this module.
77
 * This is used by the participation report.
78
 *
79
 * Note: This is not used by new logging system. Event with
80
 *       crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
81
 *       will be considered as post action.
82
 *
83
 * @return array
84
 */
85
function resource_get_post_actions() {
86
    return array('update', 'add');
87
}
88
 
89
/**
90
 * Add resource instance.
91
 * @param object $data
92
 * @param object $mform
93
 * @return int new resource instance id
94
 */
95
function resource_add_instance($data, $mform) {
96
    global $CFG, $DB;
97
    require_once("$CFG->libdir/resourcelib.php");
98
    require_once("$CFG->dirroot/mod/resource/locallib.php");
99
    $cmid = $data->coursemodule;
100
    $data->timemodified = time();
101
 
102
    resource_set_display_options($data);
103
 
104
    $data->id = $DB->insert_record('resource', $data);
105
 
106
    // we need to use context now, so we need to make sure all needed info is already in db
107
    $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
108
    resource_set_mainfile($data);
109
 
110
    $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
111
    \core_completion\api::update_completion_date_event($cmid, 'resource', $data->id, $completiontimeexpected);
112
 
113
    return $data->id;
114
}
115
 
116
/**
117
 * Update resource instance.
118
 * @param object $data
119
 * @param object $mform
120
 * @return bool true
121
 */
122
function resource_update_instance($data, $mform) {
123
    global $CFG, $DB;
124
    require_once("$CFG->libdir/resourcelib.php");
125
    $data->timemodified = time();
126
    $data->id           = $data->instance;
127
    $data->revision++;
128
 
129
    resource_set_display_options($data);
130
 
131
    $DB->update_record('resource', $data);
132
    resource_set_mainfile($data);
133
 
134
    $completiontimeexpected = !empty($data->completionexpected) ? $data->completionexpected : null;
135
    \core_completion\api::update_completion_date_event($data->coursemodule, 'resource', $data->id, $completiontimeexpected);
136
 
137
    return true;
138
}
139
 
140
/**
141
 * Updates display options based on form input.
142
 *
143
 * Shared code used by resource_add_instance and resource_update_instance.
144
 *
145
 * @param object $data Data object
146
 */
147
function resource_set_display_options($data) {
148
    $displayoptions = array();
149
    if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
150
        $displayoptions['popupwidth']  = $data->popupwidth;
151
        $displayoptions['popupheight'] = $data->popupheight;
152
    }
153
    if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
154
        $displayoptions['printintro']   = (int)!empty($data->printintro);
155
    }
156
    if (!empty($data->showsize)) {
157
        $displayoptions['showsize'] = 1;
158
    }
159
    if (!empty($data->showtype)) {
160
        $displayoptions['showtype'] = 1;
161
    }
162
    if (!empty($data->showdate)) {
163
        $displayoptions['showdate'] = 1;
164
    }
165
    $data->displayoptions = serialize($displayoptions);
166
}
167
 
168
/**
169
 * Delete resource instance.
170
 * @param int $id
171
 * @return bool true
172
 */
173
function resource_delete_instance($id) {
174
    global $DB;
175
 
176
    if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
177
        return false;
178
    }
179
 
180
    $cm = get_coursemodule_from_instance('resource', $id);
181
    \core_completion\api::update_completion_date_event($cm->id, 'resource', $id, null);
182
 
183
    // note: all context files are deleted automatically
184
 
185
    $DB->delete_records('resource', array('id'=>$resource->id));
186
 
187
    return true;
188
}
189
 
190
/**
191
 * Given a course_module object, this function returns any
192
 * "extra" information that may be needed when printing
193
 * this activity in a course listing.
194
 *
195
 * See {@link course_modinfo::get_array_of_activities()}
196
 *
197
 * @param stdClass $coursemodule
198
 * @return cached_cm_info info
199
 */
200
function resource_get_coursemodule_info($coursemodule) {
201
    global $CFG, $DB;
202
    require_once("$CFG->libdir/filelib.php");
203
    require_once("$CFG->dirroot/mod/resource/locallib.php");
204
    require_once($CFG->libdir.'/completionlib.php');
205
 
206
    $context = context_module::instance($coursemodule->id);
207
 
208
    if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance),
209
            'id, name, display, displayoptions, tobemigrated, revision, intro, introformat')) {
210
        return NULL;
211
    }
212
 
213
    $info = new cached_cm_info();
214
    $info->name = $resource->name;
215
    if ($coursemodule->showdescription) {
216
        // Convert intro to html. Do not filter cached version, filters run at display time.
217
        $info->content = format_module_intro('resource', $resource, $coursemodule->id, false);
218
    }
219
 
220
    if ($resource->tobemigrated) {
221
        return $info;
222
    }
223
 
224
    // See if there is at least one file.
225
    $fs = get_file_storage();
226
    $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false, 0, 0, 1);
227
    if (count($files) >= 1) {
228
        $mainfile = reset($files);
229
        $resource->mainfile = $mainfile->get_filename();
230
        $info->icon = file_file_icon($mainfile);
231
        $info->customdata['filtericon'] = true;
232
    }
233
 
234
    $display = resource_get_final_display_type($resource);
235
 
236
    if ($display == RESOURCELIB_DISPLAY_POPUP) {
237
        $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
238
        $options = empty($resource->displayoptions) ? [] : (array) unserialize_array($resource->displayoptions);
239
        $width  = empty($options['popupwidth'])  ? 620 : $options['popupwidth'];
240
        $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
241
        $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
242
        $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
243
 
244
    } else if ($display == RESOURCELIB_DISPLAY_NEW) {
245
        $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
246
        $info->onclick = "window.open('$fullurl'); return false;";
247
 
248
    }
249
 
250
    // If any optional extra details are turned on, store in custom data,
251
    // add some file details as well to be used later by resource_get_optional_details() without retriving.
252
    // Do not store filedetails if this is a reference - they will still need to be retrieved every time.
253
    if (($filedetails = resource_get_file_details($resource, $coursemodule)) && empty($filedetails['isref'])) {
254
        $displayoptions = (array) unserialize_array($resource->displayoptions);
255
        $displayoptions['filedetails'] = $filedetails;
256
        $info->customdata['displayoptions'] = serialize($displayoptions);
257
    } else {
258
        $info->customdata['displayoptions'] = $resource->displayoptions;
259
    }
260
    $info->customdata['display'] = $display;
261
 
262
    return $info;
263
}
264
 
265
/**
266
 * Called when viewing course page. Shows extra details after the link if
267
 * enabled.
268
 *
269
 * @param cm_info $cm Course module information
270
 */
271
function resource_cm_info_view(cm_info $cm) {
272
    global $CFG;
273
    require_once($CFG->dirroot . '/mod/resource/locallib.php');
274
    $customdata = $cm->customdata;
275
    if (is_array($customdata) && isset($customdata['displayoptions'])) {
276
        $resource = (object) ['displayoptions' => $customdata['displayoptions']];
277
        $details = resource_get_optional_details($resource, $cm, false);
278
        if ($details) {
279
            $cm->set_after_link(' ' . html_writer::tag('span', $details, ['class' => 'resourcelinkdetails']));
280
        }
281
    }
282
}
283
 
284
/**
285
 * Lists all browsable file areas
286
 *
287
 * @package  mod_resource
288
 * @category files
289
 * @param stdClass $course course object
290
 * @param stdClass $cm course module object
291
 * @param stdClass $context context object
292
 * @return array
293
 */
294
function resource_get_file_areas($course, $cm, $context) {
295
    $areas = array();
296
    $areas['content'] = get_string('resourcecontent', 'resource');
297
    return $areas;
298
}
299
 
300
/**
301
 * File browsing support for resource module content area.
302
 *
303
 * @package  mod_resource
304
 * @category files
305
 * @param file_browser $browser file browser instance
306
 * @param stdClass $areas file areas
307
 * @param stdClass $course course object
308
 * @param stdClass $cm course module object
309
 * @param stdClass $context context object
310
 * @param string $filearea file area
311
 * @param int $itemid item ID
312
 * @param string $filepath file path
313
 * @param string $filename file name
314
 * @return file_info instance or null if not found
315
 */
316
function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
317
    global $CFG;
318
 
319
    if (!has_capability('moodle/course:managefiles', $context)) {
320
        // students can not peak here!
321
        return null;
322
    }
323
 
324
    $fs = get_file_storage();
325
 
326
    if ($filearea === 'content') {
327
        $filepath = is_null($filepath) ? '/' : $filepath;
328
        $filename = is_null($filename) ? '.' : $filename;
329
 
330
        $urlbase = $CFG->wwwroot.'/pluginfile.php';
331
        if (!$storedfile = $fs->get_file($context->id, 'mod_resource', 'content', 0, $filepath, $filename)) {
332
            if ($filepath === '/' and $filename === '.') {
333
                $storedfile = new virtual_root_file($context->id, 'mod_resource', 'content', 0);
334
            } else {
335
                // not found
336
                return null;
337
            }
338
        }
339
        require_once("$CFG->dirroot/mod/resource/locallib.php");
340
        return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
341
    }
342
 
343
    // note: resource_intro handled in file_browser automatically
344
 
345
    return null;
346
}
347
 
348
/**
349
 * Serves the resource files.
350
 *
351
 * @package  mod_resource
352
 * @category files
353
 * @param stdClass $course course object
354
 * @param stdClass $cm course module object
355
 * @param stdClass $context context object
356
 * @param string $filearea file area
357
 * @param array $args extra arguments
358
 * @param bool $forcedownload whether or not force download
359
 * @param array $options additional options affecting the file serving
360
 * @return bool false if file not found, does not return if found - just send the file
361
 */
362
function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
363
    global $CFG, $DB;
364
    require_once("$CFG->libdir/resourcelib.php");
365
 
366
    if ($context->contextlevel != CONTEXT_MODULE) {
367
        return false;
368
    }
369
 
370
    require_course_login($course, true, $cm);
371
    if (!has_capability('mod/resource:view', $context)) {
372
        return false;
373
    }
374
 
375
    if ($filearea !== 'content') {
376
        // intro is handled automatically in pluginfile.php
377
        return false;
378
    }
379
 
380
    array_shift($args); // ignore revision - designed to prevent caching problems only
381
 
382
    $fs = get_file_storage();
383
    $relativepath = implode('/', $args);
384
    $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/');
385
    do {
386
        if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
387
            if ($fs->get_file_by_hash(sha1("$fullpath/."))) {
388
                if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
389
                    break;
390
                }
391
                if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
392
                    break;
393
                }
394
                if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
395
                    break;
396
                }
397
            }
398
            $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
399
            if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
400
                return false;
401
            }
402
            if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) {
403
                return false;
404
            }
405
            // file migrate - update flag
406
            $resource->legacyfileslast = time();
407
            $DB->update_record('resource', $resource);
408
        }
409
    } while (false);
410
 
411
    // should we apply filters?
412
    $mimetype = $file->get_mimetype();
413
    if ($mimetype === 'text/html' or $mimetype === 'text/plain' or $mimetype === 'application/xhtml+xml') {
414
        $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance));
415
        $CFG->embeddedsoforcelinktarget = true;
416
    } else {
417
        $filter = 0;
418
    }
419
 
420
    // finally send the file
421
    send_stored_file($file, null, $filter, $forcedownload, $options);
422
}
423
 
424
/**
425
 * Return a list of page types
426
 * @param string $pagetype current page type
427
 * @param stdClass $parentcontext Block's parent context
428
 * @param stdClass $currentcontext Current context of block
429
 */
430
function resource_page_type_list($pagetype, $parentcontext, $currentcontext) {
431
    $module_pagetype = array('mod-resource-*'=>get_string('page-mod-resource-x', 'resource'));
432
    return $module_pagetype;
433
}
434
 
435
/**
436
 * Export file resource contents
437
 *
438
 * @return array of file content
439
 */
440
function resource_export_contents($cm, $baseurl) {
441
    global $CFG, $DB;
442
    $contents = array();
443
    $context = context_module::instance($cm->id);
444
    $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
445
 
446
    $fs = get_file_storage();
447
    $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
448
 
449
    foreach ($files as $fileinfo) {
450
        $file = array();
451
        $file['type'] = 'file';
452
        $file['filename']     = $fileinfo->get_filename();
453
        $file['filepath']     = $fileinfo->get_filepath();
454
        $file['filesize']     = $fileinfo->get_filesize();
455
        $file['fileurl']      = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_resource/content/'.$resource->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
456
        $file['timecreated']  = $fileinfo->get_timecreated();
457
        $file['timemodified'] = $fileinfo->get_timemodified();
458
        $file['sortorder']    = $fileinfo->get_sortorder();
459
        $file['userid']       = $fileinfo->get_userid();
460
        $file['author']       = $fileinfo->get_author();
461
        $file['license']      = $fileinfo->get_license();
462
        $file['mimetype']     = $fileinfo->get_mimetype();
463
        $file['isexternalfile'] = $fileinfo->is_external_file();
464
        if ($file['isexternalfile']) {
465
            $file['repositorytype'] = $fileinfo->get_repository_type();
466
        }
467
        $contents[] = $file;
468
    }
469
 
470
    return $contents;
471
}
472
 
473
/**
474
 * Register the ability to handle drag and drop file uploads
475
 * @return array containing details of the files / types the mod can handle
476
 */
477
function resource_dndupload_register() {
478
    return array('files' => array(
479
                     array('extension' => '*', 'message' => get_string('dnduploadresource', 'mod_resource'))
480
                 ));
481
}
482
 
483
/**
484
 * Handle a file that has been uploaded
485
 * @param object $uploadinfo details of the file / content that has been uploaded
486
 * @return int instance id of the newly created mod
487
 */
488
function resource_dndupload_handle($uploadinfo) {
489
    // Gather the required info.
490
    $data = new stdClass();
491
    $data->course = $uploadinfo->course->id;
492
    $data->name = $uploadinfo->displayname;
493
    $data->intro = '';
494
    $data->introformat = FORMAT_HTML;
495
    $data->coursemodule = $uploadinfo->coursemodule;
496
    $data->files = $uploadinfo->draftitemid;
497
 
498
    // Set the display options to the site defaults.
499
    $config = get_config('resource');
500
    $data->display = $config->display;
501
    $data->popupheight = $config->popupheight;
502
    $data->popupwidth = $config->popupwidth;
503
    $data->printintro = $config->printintro;
504
    $data->showsize = (isset($config->showsize)) ? $config->showsize : 0;
505
    $data->showtype = (isset($config->showtype)) ? $config->showtype : 0;
506
    $data->showdate = (isset($config->showdate)) ? $config->showdate : 0;
507
    $data->filterfiles = $config->filterfiles;
508
 
509
    return resource_add_instance($data, null);
510
}
511
 
512
/**
513
 * Mark the activity completed (if required) and trigger the course_module_viewed event.
514
 *
515
 * @param  stdClass $resource   resource object
516
 * @param  stdClass $course     course object
517
 * @param  stdClass $cm         course module object
518
 * @param  stdClass $context    context object
519
 * @since Moodle 3.0
520
 */
521
function resource_view($resource, $course, $cm, $context) {
522
 
523
    // Trigger course_module_viewed event.
524
    $params = array(
525
        'context' => $context,
526
        'objectid' => $resource->id
527
    );
528
 
529
    $event = \mod_resource\event\course_module_viewed::create($params);
530
    $event->add_record_snapshot('course_modules', $cm);
531
    $event->add_record_snapshot('course', $course);
532
    $event->add_record_snapshot('resource', $resource);
533
    $event->trigger();
534
 
535
    // Completion.
536
    $completion = new completion_info($course);
537
    $completion->set_module_viewed($cm);
538
}
539
 
540
/**
541
 * Check if the module has any update that affects the current user since a given time.
542
 *
543
 * @param  cm_info $cm course module data
544
 * @param  int $from the time to check updates from
545
 * @param  array $filter  if we need to check only specific updates
546
 * @return stdClass an object with the different type of areas indicating if they were updated or not
547
 * @since Moodle 3.2
548
 */
549
function resource_check_updates_since(cm_info $cm, $from, $filter = array()) {
550
    $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
551
    return $updates;
552
}
553
 
554
/**
555
 * This function receives a calendar event and returns the action associated with it, or null if there is none.
556
 *
557
 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
558
 * is not displayed on the block.
559
 *
560
 * @param calendar_event $event
561
 * @param \core_calendar\action_factory $factory
562
 * @return \core_calendar\local\event\entities\action_interface|null
563
 */
564
function mod_resource_core_calendar_provide_event_action(calendar_event $event,
565
                                                      \core_calendar\action_factory $factory, $userid = 0) {
566
 
567
    global $USER;
568
 
569
    if (empty($userid)) {
570
        $userid = $USER->id;
571
    }
572
 
573
    $cm = get_fast_modinfo($event->courseid, $userid)->instances['resource'][$event->instance];
574
 
575
    $completion = new \completion_info($cm->get_course());
576
 
577
    $completiondata = $completion->get_data($cm, false, $userid);
578
 
579
    if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
580
        return null;
581
    }
582
 
583
    return $factory->create_instance(
584
        get_string('view'),
585
        new \moodle_url('/mod/resource/view.php', ['id' => $cm->id]),
586
        1,
587
        true
588
    );
589
}
590
 
591
 
592
/**
593
 * Given an array with a file path, it returns the itemid and the filepath for the defined filearea.
594
 *
595
 * @param  string $filearea The filearea.
596
 * @param  array  $args The path (the part after the filearea and before the filename).
597
 * @return array The itemid and the filepath inside the $args path, for the defined filearea.
598
 */
599
function mod_resource_get_path_from_pluginfile(string $filearea, array $args): array {
600
    // Resource never has an itemid (the number represents the revision but it's not stored in database).
601
    array_shift($args);
602
 
603
    // Get the filepath.
604
    if (empty($args)) {
605
        $filepath = '/';
606
    } else {
607
        $filepath = '/' . implode('/', $args) . '/';
608
    }
609
 
610
    return [
611
        'itemid' => 0,
612
        'filepath' => $filepath,
613
    ];
614
}