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