MDL-57878 core_calendar: additional unit test for bailout callback
[moodle.git] / mod / resource / lib.php
CommitLineData
28f672b2 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/>.
2a439ba7 17
28f672b2 18/**
11e41f1a 19 * @package mod_resource
702ab58c
PS
20 * @copyright 2009 Petr Skoda {@link http://skodak.org}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28f672b2 22 */
23
702ab58c
PS
24defined('MOODLE_INTERNAL') || die;
25
aa54ed7b 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
30 */
31function 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;
aa54ed7b 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;
a345de6e 40 case FEATURE_BACKUP_MOODLE2: return true;
3e4c2435 41 case FEATURE_SHOW_DESCRIPTION: return true;
214b1cf7 42
aa54ed7b 43 default: return null;
44 }
45}
713d78ea 46
28f672b2 47/**
aa54ed7b 48 * Returns all other caps used in module
49 * @return array
28f672b2 50 */
aa54ed7b 51function resource_get_extra_capabilities() {
52 return array('moodle/site:accessallgroups');
53}
3d30a455 54
d18830fe 55/**
aa54ed7b 56 * This function is used by the reset_course_userdata function in moodlelib.
57 * @param $data the data submitted from the reset course.
58 * @return array status array
28f672b2 59 */
aa54ed7b 60function resource_reset_userdata($data) {
61 return array();
62}
65634a81 63
aa54ed7b 64/**
b2b4ec30
RT
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 *
aa54ed7b 72 * @return array
73 */
74function resource_get_view_actions() {
cf051cc4 75 return array('view','view all');
aa54ed7b 76}
65634a81 77
aa54ed7b 78/**
b2b4ec30
RT
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 *
aa54ed7b 86 * @return array
87 */
88function resource_get_post_actions() {
cf051cc4 89 return array('update', 'add');
aa54ed7b 90}
65634a81 91
aa54ed7b 92/**
93 * Add resource instance.
94 * @param object $data
95 * @param object $mform
c6c9a3bc 96 * @return int new resource instance id
aa54ed7b 97 */
98function resource_add_instance($data, $mform) {
99 global $CFG, $DB;
100 require_once("$CFG->libdir/resourcelib.php");
abed5d65 101 require_once("$CFG->dirroot/mod/resource/locallib.php");
f79321f1 102 $cmid = $data->coursemodule;
aa54ed7b 103 $data->timemodified = time();
a2c57666 104
105 resource_set_display_options($data);
d18830fe 106
aa54ed7b 107 $data->id = $DB->insert_record('resource', $data);
d18830fe 108
aa54ed7b 109 // we need to use context now, so we need to make sure all needed info is already in db
110 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
f79321f1 111 resource_set_mainfile($data);
aa54ed7b 112 return $data->id;
113}
cccb016a 114
aa54ed7b 115/**
116 * Update resource instance.
117 * @param object $data
118 * @param object $mform
119 * @return bool true
120 */
121function resource_update_instance($data, $mform) {
122 global $CFG, $DB;
123 require_once("$CFG->libdir/resourcelib.php");
aa54ed7b 124 $data->timemodified = time();
125 $data->id = $data->instance;
126 $data->revision++;
2a439ba7 127
a2c57666 128 resource_set_display_options($data);
129
130 $DB->update_record('resource', $data);
131 resource_set_mainfile($data);
132 return true;
133}
134
135/**
136 * Updates display options based on form input.
137 *
138 * Shared code used by resource_add_instance and resource_update_instance.
139 *
140 * @param object $data Data object
141 */
142function resource_set_display_options($data) {
aa54ed7b 143 $displayoptions = array();
144 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
145 $displayoptions['popupwidth'] = $data->popupwidth;
146 $displayoptions['popupheight'] = $data->popupheight;
65634a81 147 }
aa54ed7b 148 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
aa54ed7b 149 $displayoptions['printintro'] = (int)!empty($data->printintro);
3efe78df 150 }
a2c57666 151 if (!empty($data->showsize)) {
152 $displayoptions['showsize'] = 1;
153 }
154 if (!empty($data->showtype)) {
155 $displayoptions['showtype'] = 1;
156 }
ba8b641d
AH
157 if (!empty($data->showdate)) {
158 $displayoptions['showdate'] = 1;
159 }
aa54ed7b 160 $data->displayoptions = serialize($displayoptions);
d18830fe 161}
162
28f672b2 163/**
aa54ed7b 164 * Delete resource instance.
28f672b2 165 * @param int $id
aa54ed7b 166 * @return bool true
28f672b2 167 */
d18830fe 168function resource_delete_instance($id) {
aa54ed7b 169 global $DB;
ec81373f 170
aa54ed7b 171 if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
d18830fe 172 return false;
173 }
79035d46 174
aa54ed7b 175 // note: all context files are deleted automatically
ec81373f 176
aa54ed7b 177 $DB->delete_records('resource', array('id'=>$resource->id));
d18830fe 178
aa54ed7b 179 return true;
d18830fe 180}
181
28f672b2 182/**
183 * Given a course_module object, this function returns any
184 * "extra" information that may be needed when printing
185 * this activity in a course listing.
186 *
187 * See {@link get_array_of_activities()} in course/lib.php
188 *
3c620d7f 189 * @param stdClass $coursemodule
3e4c2435 190 * @return cached_cm_info info
28f672b2 191 */
8dddba42 192function resource_get_coursemodule_info($coursemodule) {
aa54ed7b 193 global $CFG, $DB;
194 require_once("$CFG->libdir/filelib.php");
195 require_once("$CFG->dirroot/mod/resource/locallib.php");
516c5eca
PS
196 require_once($CFG->libdir.'/completionlib.php');
197
bf0f06b1 198 $context = context_module::instance($coursemodule->id);
ec81373f 199
3e4c2435 200 if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance),
201 'id, name, display, displayoptions, tobemigrated, revision, intro, introformat')) {
aa54ed7b 202 return NULL;
af65e103 203 }
ec81373f 204
3e4c2435 205 $info = new cached_cm_info();
aa54ed7b 206 $info->name = $resource->name;
3e4c2435 207 if ($coursemodule->showdescription) {
208 // Convert intro to html. Do not filter cached version, filters run at display time.
209 $info->content = format_module_intro('resource', $resource, $coursemodule->id, false);
210 }
8dddba42 211
aa54ed7b 212 if ($resource->tobemigrated) {
c3847443 213 $info->icon ='i/invalid';
aa54ed7b 214 return $info;
af65e103 215 }
66234de2 216
217 // See if there is at least one file.
f79321f1 218 $fs = get_file_storage();
66234de2 219 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false, 0, 0, 1);
f79321f1 220 if (count($files) >= 1) {
020088d6 221 $mainfile = reset($files);
72225e18 222 $info->icon = file_file_icon($mainfile, 24);
f79321f1
DC
223 $resource->mainfile = $mainfile->get_filename();
224 }
d18830fe 225
aa54ed7b 226 $display = resource_get_final_display_type($resource);
9f741612 227
aa54ed7b 228 if ($display == RESOURCELIB_DISPLAY_POPUP) {
229 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
230 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
231 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
232 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
233 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
3e4c2435 234 $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
6da4b261 235
aa54ed7b 236 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
237 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
3e4c2435 238 $info->onclick = "window.open('$fullurl'); return false;";
6da4b261 239
6da4b261 240 }
89bfeee0 241
9dfe8704
MG
242 // If any optional extra details are turned on, store in custom data,
243 // add some file details as well to be used later by resource_get_optional_details() without retriving.
244 // Do not store filedetails if this is a reference - they will still need to be retrieved every time.
245 if (($filedetails = resource_get_file_details($resource, $coursemodule)) && empty($filedetails['isref'])) {
246 $displayoptions = @unserialize($resource->displayoptions);
247 $displayoptions['filedetails'] = $filedetails;
248 $info->customdata = serialize($displayoptions);
249 } else {
250 $info->customdata = $resource->displayoptions;
251 }
a2c57666 252
aa54ed7b 253 return $info;
6da4b261 254}
f3221af9 255
a2c57666 256/**
257 * Called when viewing course page. Shows extra details after the link if
258 * enabled.
259 *
260 * @param cm_info $cm Course module information
261 */
262function resource_cm_info_view(cm_info $cm) {
9a584346
MG
263 global $CFG;
264 require_once($CFG->dirroot . '/mod/resource/locallib.php');
265
266 $resource = (object)array('displayoptions' => $cm->customdata);
267 $details = resource_get_optional_details($resource, $cm);
a2c57666 268 if ($details) {
269 $cm->set_after_link(' ' . html_writer::tag('span', $details,
270 array('class' => 'resourcelinkdetails')));
271 }
272}
f3221af9 273
28f672b2 274/**
aa54ed7b 275 * Lists all browsable file areas
d2b7803e
DC
276 *
277 * @package mod_resource
278 * @category files
279 * @param stdClass $course course object
280 * @param stdClass $cm course module object
281 * @param stdClass $context context object
28f672b2 282 * @return array
283 */
aa54ed7b 284function resource_get_file_areas($course, $cm, $context) {
285 $areas = array();
64f93798 286 $areas['content'] = get_string('resourcecontent', 'resource');
aa54ed7b 287 return $areas;
f3221af9 288}
289
28f672b2 290/**
f1b8bcf7 291 * File browsing support for resource module content area.
d2b7803e
DC
292 *
293 * @package mod_resource
294 * @category files
295 * @param stdClass $browser file browser instance
296 * @param stdClass $areas file areas
297 * @param stdClass $course course object
298 * @param stdClass $cm course module object
299 * @param stdClass $context context object
300 * @param string $filearea file area
301 * @param int $itemid item ID
302 * @param string $filepath file path
303 * @param string $filename file name
304 * @return file_info instance or null if not found
28f672b2 305 */
aa54ed7b 306function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
307 global $CFG;
a69be0d8 308
64f93798
PS
309 if (!has_capability('moodle/course:managefiles', $context)) {
310 // students can not peak here!
311 return null;
312 }
a69be0d8 313
aa54ed7b 314 $fs = get_file_storage();
a69be0d8 315
64f93798 316 if ($filearea === 'content') {
aa54ed7b 317 $filepath = is_null($filepath) ? '/' : $filepath;
318 $filename = is_null($filename) ? '.' : $filename;
a69be0d8 319
aa54ed7b 320 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 321 if (!$storedfile = $fs->get_file($context->id, 'mod_resource', 'content', 0, $filepath, $filename)) {
aa54ed7b 322 if ($filepath === '/' and $filename === '.') {
64f93798 323 $storedfile = new virtual_root_file($context->id, 'mod_resource', 'content', 0);
a69be0d8 324 } else {
aa54ed7b 325 // not found
326 return null;
a69be0d8 327 }
328 }
aa54ed7b 329 require_once("$CFG->dirroot/mod/resource/locallib.php");
64f93798 330 return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
a69be0d8 331 }
332
aa54ed7b 333 // note: resource_intro handled in file_browser automatically
a69be0d8 334
aa54ed7b 335 return null;
a69be0d8 336}
337
0b5a80a1 338/**
aa54ed7b 339 * Serves the resource files.
d2b7803e
DC
340 *
341 * @package mod_resource
342 * @category files
343 * @param stdClass $course course object
344 * @param stdClass $cm course module object
345 * @param stdClass $context context object
346 * @param string $filearea file area
347 * @param array $args extra arguments
348 * @param bool $forcedownload whether or not force download
261cbbac 349 * @param array $options additional options affecting the file serving
b4ff85aa 350 * @return bool false if file not found, does not return if found - just send the file
28f672b2 351 */
261cbbac 352function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
aa54ed7b 353 global $CFG, $DB;
354 require_once("$CFG->libdir/resourcelib.php");
0d06b6fd 355
64f93798 356 if ($context->contextlevel != CONTEXT_MODULE) {
aa54ed7b 357 return false;
3efe78df 358 }
359
64f93798 360 require_course_login($course, true, $cm);
ce459060
PS
361 if (!has_capability('mod/resource:view', $context)) {
362 return false;
363 }
3efe78df 364
64f93798
PS
365 if ($filearea !== 'content') {
366 // intro is handled automatically in pluginfile.php
aa54ed7b 367 return false;
3efe78df 368 }
369
aa54ed7b 370 array_shift($args); // ignore revision - designed to prevent caching problems only
3efe78df 371
aa54ed7b 372 $fs = get_file_storage();
64f93798 373 $relativepath = implode('/', $args);
fe3c5dae
PS
374 $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/');
375 do {
376 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
9815ccee 377 if ($fs->get_file_by_hash(sha1("$fullpath/."))) {
fe3c5dae
PS
378 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
379 break;
380 }
381 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
382 break;
383 }
384 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
385 break;
386 }
387 }
388 $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
389 if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
390 return false;
391 }
392 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) {
393 return false;
394 }
395 // file migrate - update flag
396 $resource->legacyfileslast = time();
397 $DB->update_record('resource', $resource);
0d06b6fd 398 }
fe3c5dae 399 } while (false);
ffcfd8a7 400
aa54ed7b 401 // should we apply filters?
402 $mimetype = $file->get_mimetype();
112aed60 403 if ($mimetype === 'text/html' or $mimetype === 'text/plain' or $mimetype === 'application/xhtml+xml') {
64f93798 404 $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance));
4eaa964f 405 $CFG->embeddedsoforcelinktarget = true;
aa54ed7b 406 } else {
407 $filter = 0;
18a2a0cb 408 }
18a2a0cb 409
aa54ed7b 410 // finally send the file
0c431257 411 send_stored_file($file, null, $filter, $forcedownload, $options);
13ca1e06 412}
b1627a92
DC
413
414/**
415 * Return a list of page types
416 * @param string $pagetype current page type
417 * @param stdClass $parentcontext Block's parent context
418 * @param stdClass $currentcontext Current context of block
419 */
b38e2e28 420function resource_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
421 $module_pagetype = array('mod-resource-*'=>get_string('page-mod-resource-x', 'resource'));
422 return $module_pagetype;
423}
ec0d6ea2
DC
424
425/**
426 * Export file resource contents
427 *
428 * @return array of file content
429 */
430function resource_export_contents($cm, $baseurl) {
431 global $CFG, $DB;
432 $contents = array();
bf0f06b1 433 $context = context_module::instance($cm->id);
ec0d6ea2
DC
434 $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
435
436 $fs = get_file_storage();
437 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
438
439 foreach ($files as $fileinfo) {
440 $file = array();
441 $file['type'] = 'file';
442 $file['filename'] = $fileinfo->get_filename();
443 $file['filepath'] = $fileinfo->get_filepath();
444 $file['filesize'] = $fileinfo->get_filesize();
445 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_resource/content/'.$resource->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
446 $file['timecreated'] = $fileinfo->get_timecreated();
447 $file['timemodified'] = $fileinfo->get_timemodified();
448 $file['sortorder'] = $fileinfo->get_sortorder();
449 $file['userid'] = $fileinfo->get_userid();
450 $file['author'] = $fileinfo->get_author();
451 $file['license'] = $fileinfo->get_license();
452 $contents[] = $file;
453 }
454
455 return $contents;
456}
32528f94
DS
457
458/**
459 * Register the ability to handle drag and drop file uploads
460 * @return array containing details of the files / types the mod can handle
461 */
6b8b0b2e 462function resource_dndupload_register() {
32528f94
DS
463 return array('files' => array(
464 array('extension' => '*', 'message' => get_string('dnduploadresource', 'mod_resource'))
465 ));
466}
467
468/**
469 * Handle a file that has been uploaded
470 * @param object $uploadinfo details of the file / content that has been uploaded
471 * @return int instance id of the newly created mod
472 */
6b8b0b2e 473function resource_dndupload_handle($uploadinfo) {
abed5d65
DS
474 // Gather the required info.
475 $data = new stdClass();
476 $data->course = $uploadinfo->course->id;
477 $data->name = $uploadinfo->displayname;
5fece0f2 478 $data->intro = '';
abed5d65
DS
479 $data->introformat = FORMAT_HTML;
480 $data->coursemodule = $uploadinfo->coursemodule;
481 $data->files = $uploadinfo->draftitemid;
482
483 // Set the display options to the site defaults.
32528f94 484 $config = get_config('resource');
abed5d65
DS
485 $data->display = $config->display;
486 $data->popupheight = $config->popupheight;
487 $data->popupwidth = $config->popupwidth;
abed5d65 488 $data->printintro = $config->printintro;
10654e32
SH
489 $data->showsize = (isset($config->showsize)) ? $config->showsize : 0;
490 $data->showtype = (isset($config->showtype)) ? $config->showtype : 0;
ba8b641d 491 $data->showdate = (isset($config->showdate)) ? $config->showdate : 0;
a274df85 492 $data->filterfiles = $config->filterfiles;
32528f94 493
abed5d65 494 return resource_add_instance($data, null);
32528f94 495}
537f7b38
JL
496
497/**
498 * Mark the activity completed (if required) and trigger the course_module_viewed event.
499 *
500 * @param stdClass $resource resource object
501 * @param stdClass $course course object
502 * @param stdClass $cm course module object
503 * @param stdClass $context context object
504 * @since Moodle 3.0
505 */
506function resource_view($resource, $course, $cm, $context) {
507
508 // Trigger course_module_viewed event.
509 $params = array(
510 'context' => $context,
511 'objectid' => $resource->id
512 );
513
514 $event = \mod_resource\event\course_module_viewed::create($params);
515 $event->add_record_snapshot('course_modules', $cm);
516 $event->add_record_snapshot('course', $course);
517 $event->add_record_snapshot('resource', $resource);
518 $event->trigger();
519
520 // Completion.
521 $completion = new completion_info($course);
522 $completion->set_module_viewed($cm);
523}
02a73d76
JL
524
525/**
526 * Check if the module has any update that affects the current user since a given time.
527 *
528 * @param cm_info $cm course module data
529 * @param int $from the time to check updates from
530 * @param array $filter if we need to check only specific updates
531 * @return stdClass an object with the different type of areas indicating if they were updated or not
532 * @since Moodle 3.2
533 */
534function resource_check_updates_since(cm_info $cm, $from, $filter = array()) {
535 $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
536 return $updates;
537}