MDL-42387 standardise file lifetime handling
[moodle.git] / mod / folder / lib.php
blob2a4465e3701167dad8e2721e9b65c8ecf9cc18a5
1 <?php
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.
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/>.
18 /**
19 * Mandatory public API of folder module
21 * @package mod
22 * @subpackage folder
23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /** Display folder contents on a separate page */
30 define('FOLDER_DISPLAY_PAGE', 0);
31 /** Display folder contents inline in a course */
32 define('FOLDER_DISPLAY_INLINE', 1);
34 /**
35 * List of features supported in Folder module
36 * @param string $feature FEATURE_xx constant for requested feature
37 * @return mixed True if module supports feature, false if not, null if doesn't know
39 function folder_supports($feature) {
40 switch($feature) {
41 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
42 case FEATURE_GROUPS: return false;
43 case FEATURE_GROUPINGS: return false;
44 case FEATURE_GROUPMEMBERSONLY: return true;
45 case FEATURE_MOD_INTRO: return true;
46 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
47 case FEATURE_GRADE_HAS_GRADE: return false;
48 case FEATURE_GRADE_OUTCOMES: return false;
49 case FEATURE_BACKUP_MOODLE2: return true;
50 case FEATURE_SHOW_DESCRIPTION: return true;
52 default: return null;
56 /**
57 * Returns all other caps used in module
58 * @return array
60 function folder_get_extra_capabilities() {
61 return array('moodle/site:accessallgroups');
64 /**
65 * This function is used by the reset_course_userdata function in moodlelib.
66 * @param $data the data submitted from the reset course.
67 * @return array status array
69 function folder_reset_userdata($data) {
70 return array();
73 /**
74 * List of view style log actions
75 * @return array
77 function folder_get_view_actions() {
78 return array('view', 'view all');
81 /**
82 * List of update style log actions
83 * @return array
85 function folder_get_post_actions() {
86 return array('update', 'add');
89 /**
90 * Add folder instance.
91 * @param object $data
92 * @param object $mform
93 * @return int new folder instance id
95 function folder_add_instance($data, $mform) {
96 global $DB;
98 $cmid = $data->coursemodule;
99 $draftitemid = $data->files;
101 $data->timemodified = time();
102 $data->id = $DB->insert_record('folder', $data);
104 // we need to use context now, so we need to make sure all needed info is already in db
105 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
106 $context = context_module::instance($cmid);
108 if ($draftitemid) {
109 file_save_draft_area_files($draftitemid, $context->id, 'mod_folder', 'content', 0, array('subdirs'=>true));
112 return $data->id;
116 * Update folder instance.
117 * @param object $data
118 * @param object $mform
119 * @return bool true
121 function folder_update_instance($data, $mform) {
122 global $CFG, $DB;
124 $cmid = $data->coursemodule;
125 $draftitemid = $data->files;
127 $data->timemodified = time();
128 $data->id = $data->instance;
129 $data->revision++;
131 $DB->update_record('folder', $data);
133 $context = context_module::instance($cmid);
134 if ($draftitemid = file_get_submitted_draft_itemid('files')) {
135 file_save_draft_area_files($draftitemid, $context->id, 'mod_folder', 'content', 0, array('subdirs'=>true));
138 return true;
142 * Delete folder instance.
143 * @param int $id
144 * @return bool true
146 function folder_delete_instance($id) {
147 global $DB;
149 if (!$folder = $DB->get_record('folder', array('id'=>$id))) {
150 return false;
153 // note: all context files are deleted automatically
155 $DB->delete_records('folder', array('id'=>$folder->id));
157 return true;
161 * Return use outline
162 * @param object $course
163 * @param object $user
164 * @param object $mod
165 * @param object $folder
166 * @return object|null
168 function folder_user_outline($course, $user, $mod, $folder) {
169 global $DB;
171 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'folder',
172 'action'=>'view', 'info'=>$folder->id), 'time ASC')) {
174 $numviews = count($logs);
175 $lastlog = array_pop($logs);
177 $result = new stdClass();
178 $result->info = get_string('numviews', '', $numviews);
179 $result->time = $lastlog->time;
181 return $result;
183 return NULL;
187 * Return use complete
188 * @param object $course
189 * @param object $user
190 * @param object $mod
191 * @param object $folder
193 function folder_user_complete($course, $user, $mod, $folder) {
194 global $CFG, $DB;
196 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'folder',
197 'action'=>'view', 'info'=>$folder->id), 'time ASC')) {
198 $numviews = count($logs);
199 $lastlog = array_pop($logs);
201 $strmostrecently = get_string('mostrecently');
202 $strnumviews = get_string('numviews', '', $numviews);
204 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
206 } else {
207 print_string('neverseen', 'folder');
212 * Lists all browsable file areas
214 * @package mod_folder
215 * @category files
216 * @param stdClass $course course object
217 * @param stdClass $cm course module object
218 * @param stdClass $context context object
219 * @return array
221 function folder_get_file_areas($course, $cm, $context) {
222 $areas = array();
223 $areas['content'] = get_string('foldercontent', 'folder');
225 return $areas;
229 * File browsing support for folder module content area.
231 * @package mod_folder
232 * @category files
233 * @param file_browser $browser file browser instance
234 * @param array $areas file areas
235 * @param stdClass $course course object
236 * @param stdClass $cm course module object
237 * @param stdClass $context context object
238 * @param string $filearea file area
239 * @param int $itemid item ID
240 * @param string $filepath file path
241 * @param string $filename file name
242 * @return file_info instance or null if not found
244 function folder_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
245 global $CFG;
248 if ($filearea === 'content') {
249 if (!has_capability('mod/folder:view', $context)) {
250 return NULL;
252 $fs = get_file_storage();
254 $filepath = is_null($filepath) ? '/' : $filepath;
255 $filename = is_null($filename) ? '.' : $filename;
256 if (!$storedfile = $fs->get_file($context->id, 'mod_folder', 'content', 0, $filepath, $filename)) {
257 if ($filepath === '/' and $filename === '.') {
258 $storedfile = new virtual_root_file($context->id, 'mod_folder', 'content', 0);
259 } else {
260 // not found
261 return null;
265 require_once("$CFG->dirroot/mod/folder/locallib.php");
266 $urlbase = $CFG->wwwroot.'/pluginfile.php';
268 // students may read files here
269 $canwrite = has_capability('mod/folder:managefiles', $context);
270 return new folder_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, $canwrite, false);
273 // note: folder_intro handled in file_browser automatically
275 return null;
279 * Serves the folder files.
281 * @package mod_folder
282 * @category files
283 * @param stdClass $course course object
284 * @param stdClass $cm course module
285 * @param stdClass $context context object
286 * @param string $filearea file area
287 * @param array $args extra arguments
288 * @param bool $forcedownload whether or not force download
289 * @param array $options additional options affecting the file serving
290 * @return bool false if file not found, does not return if found - just send the file
292 function folder_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
293 global $CFG, $DB;
295 if ($context->contextlevel != CONTEXT_MODULE) {
296 return false;
299 require_course_login($course, true, $cm);
300 if (!has_capability('mod/folder:view', $context)) {
301 return false;
304 if ($filearea !== 'content') {
305 // intro is handled automatically in pluginfile.php
306 return false;
309 array_shift($args); // ignore revision - designed to prevent caching problems only
311 $fs = get_file_storage();
312 $relativepath = implode('/', $args);
313 $fullpath = "/$context->id/mod_folder/content/0/$relativepath";
314 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
315 return false;
318 // finally send the file
319 // for folder module, we force download file all the time
320 send_stored_file($file, 0, 0, true, $options);
324 * Return a list of page types
325 * @param string $pagetype current page type
326 * @param stdClass $parentcontext Block's parent context
327 * @param stdClass $currentcontext Current context of block
329 function folder_page_type_list($pagetype, $parentcontext, $currentcontext) {
330 $module_pagetype = array('mod-folder-*'=>get_string('page-mod-folder-x', 'folder'));
331 return $module_pagetype;
335 * Export folder resource contents
337 * @return array of file content
339 function folder_export_contents($cm, $baseurl) {
340 global $CFG, $DB;
341 $contents = array();
342 $context = context_module::instance($cm->id);
343 $folder = $DB->get_record('folder', array('id'=>$cm->instance), '*', MUST_EXIST);
345 $fs = get_file_storage();
346 $files = $fs->get_area_files($context->id, 'mod_folder', 'content', 0, 'sortorder DESC, id ASC', false);
348 foreach ($files as $fileinfo) {
349 $file = array();
350 $file['type'] = 'file';
351 $file['filename'] = $fileinfo->get_filename();
352 $file['filepath'] = $fileinfo->get_filepath();
353 $file['filesize'] = $fileinfo->get_filesize();
354 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_folder/content/'.$folder->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
355 $file['timecreated'] = $fileinfo->get_timecreated();
356 $file['timemodified'] = $fileinfo->get_timemodified();
357 $file['sortorder'] = $fileinfo->get_sortorder();
358 $file['userid'] = $fileinfo->get_userid();
359 $file['author'] = $fileinfo->get_author();
360 $file['license'] = $fileinfo->get_license();
361 $contents[] = $file;
364 return $contents;
368 * Register the ability to handle drag and drop file uploads
369 * @return array containing details of the files / types the mod can handle
371 function folder_dndupload_register() {
372 return array('files' => array(
373 array('extension' => 'zip', 'message' => get_string('dnduploadmakefolder', 'mod_folder'))
378 * Handle a file that has been uploaded
379 * @param object $uploadinfo details of the file / content that has been uploaded
380 * @return int instance id of the newly created mod
382 function folder_dndupload_handle($uploadinfo) {
383 global $DB, $USER;
385 // Gather the required info.
386 $data = new stdClass();
387 $data->course = $uploadinfo->course->id;
388 $data->name = $uploadinfo->displayname;
389 $data->intro = '<p>'.$uploadinfo->displayname.'</p>';
390 $data->introformat = FORMAT_HTML;
391 $data->coursemodule = $uploadinfo->coursemodule;
392 $data->files = null; // We will unzip the file and sort out the contents below.
394 $data->id = folder_add_instance($data, null);
396 // Retrieve the file from the draft file area.
397 $context = context_module::instance($uploadinfo->coursemodule);
398 file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_folder', 'temp', 0, array('subdirs'=>true));
399 $fs = get_file_storage();
400 $files = $fs->get_area_files($context->id, 'mod_folder', 'temp', 0, 'sortorder', false);
401 // Only ever one file - extract the contents.
402 $file = reset($files);
404 $success = $file->extract_to_storage(new zip_packer(), $context->id, 'mod_folder', 'content', 0, '/', $USER->id);
405 $fs->delete_area_files($context->id, 'mod_folder', 'temp', 0);
407 if ($success) {
408 return $data->id;
411 $DB->delete_records('folder', array('id' => $data->id));
412 return false;
416 * Given a coursemodule object, this function returns the extra
417 * information needed to print this activity in various places.
419 * If folder needs to be displayed inline we store additional information
420 * in customdata, so functions {@link folder_cm_info_dynamic()} and
421 * {@link folder_cm_info_view()} do not need to do DB queries
423 * @param cm_info $cm
424 * @return cached_cm_info info
426 function folder_get_coursemodule_info($cm) {
427 global $DB;
428 if (!($folder = $DB->get_record('folder', array('id' => $cm->instance),
429 'id, name, display, showexpanded, intro, introformat'))) {
430 return NULL;
432 $cminfo = new cached_cm_info();
433 $cminfo->name = $folder->name;
434 if ($folder->display == FOLDER_DISPLAY_INLINE) {
435 // prepare folder object to store in customdata
436 $fdata = new stdClass();
437 $fdata->showexpanded = $folder->showexpanded;
438 if ($cm->showdescription && strlen(trim($folder->intro))) {
439 $fdata->intro = $folder->intro;
440 if ($folder->introformat != FORMAT_MOODLE) {
441 $fdata->introformat = $folder->introformat;
444 $cminfo->customdata = $fdata;
445 } else {
446 if ($cm->showdescription) {
447 // Convert intro to html. Do not filter cached version, filters run at display time.
448 $cminfo->content = format_module_intro('folder', $folder, $cm->id, false);
451 return $cminfo;
455 * Sets dynamic information about a course module
457 * This function is called from cm_info when displaying the module
458 * mod_folder can be displayed inline on course page and therefore have no course link
460 * @param cm_info $cm
462 function folder_cm_info_dynamic(cm_info $cm) {
463 if ($cm->get_custom_data()) {
464 // the field 'customdata' is not empty IF AND ONLY IF we display contens inline
465 $cm->set_no_view_link();
470 * Overwrites the content in the course-module object with the folder files list
471 * if folder.display == FOLDER_DISPLAY_INLINE
473 * @param cm_info $cm
475 function folder_cm_info_view(cm_info $cm) {
476 global $PAGE;
477 if ($cm->uservisible && $cm->get_custom_data() &&
478 has_capability('mod/folder:view', $cm->context)) {
479 // Restore folder object from customdata.
480 // Note the field 'customdata' is not empty IF AND ONLY IF we display contens inline.
481 // Otherwise the content is default.
482 $folder = $cm->get_custom_data();
483 $folder->id = (int)$cm->instance;
484 $folder->course = (int)$cm->course;
485 $folder->display = FOLDER_DISPLAY_INLINE;
486 $folder->name = $cm->name;
487 if (empty($folder->intro)) {
488 $folder->intro = '';
490 if (empty($folder->introformat)) {
491 $folder->introformat = FORMAT_MOODLE;
493 // display folder
494 $renderer = $PAGE->get_renderer('mod_folder');
495 $cm->set_content($renderer->display_folder($folder));