MDL-30431 behat: Fixed wiki behat
[moodle.git] / mod / folder / lib.php
blobb2014f6b420ccd896a48449750c1464dbe9182c4
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_folder
22 * @copyright 2009 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /** Display folder contents on a separate page */
29 define('FOLDER_DISPLAY_PAGE', 0);
30 /** Display folder contents inline in a course */
31 define('FOLDER_DISPLAY_INLINE', 1);
33 /**
34 * List of features supported in Folder module
35 * @param string $feature FEATURE_xx constant for requested feature
36 * @return mixed True if module supports feature, false if not, null if doesn't know
38 function folder_supports($feature) {
39 switch($feature) {
40 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
41 case FEATURE_GROUPS: return false;
42 case FEATURE_GROUPINGS: return false;
43 case FEATURE_GROUPMEMBERSONLY: return true;
44 case FEATURE_MOD_INTRO: return true;
45 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
46 case FEATURE_GRADE_HAS_GRADE: return false;
47 case FEATURE_GRADE_OUTCOMES: return false;
48 case FEATURE_BACKUP_MOODLE2: return true;
49 case FEATURE_SHOW_DESCRIPTION: return true;
51 default: return null;
55 /**
56 * Returns all other caps used in module
57 * @return array
59 function folder_get_extra_capabilities() {
60 return array('moodle/site:accessallgroups');
63 /**
64 * This function is used by the reset_course_userdata function in moodlelib.
65 * @param $data the data submitted from the reset course.
66 * @return array status array
68 function folder_reset_userdata($data) {
69 return array();
72 /**
73 * List the actions that correspond to a view of this module.
74 * This is used by the participation report.
76 * Note: This is not used by new logging system. Event with
77 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
78 * be considered as view action.
80 * @return array
82 function folder_get_view_actions() {
83 return array('view', 'view all');
86 /**
87 * List the actions that correspond to a post of this module.
88 * This is used by the participation report.
90 * Note: This is not used by new logging system. Event with
91 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
92 * will be considered as post action.
94 * @return array
96 function folder_get_post_actions() {
97 return array('update', 'add');
101 * Add folder instance.
102 * @param object $data
103 * @param object $mform
104 * @return int new folder instance id
106 function folder_add_instance($data, $mform) {
107 global $DB;
109 $cmid = $data->coursemodule;
110 $draftitemid = $data->files;
112 $data->timemodified = time();
113 $data->id = $DB->insert_record('folder', $data);
115 // we need to use context now, so we need to make sure all needed info is already in db
116 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
117 $context = context_module::instance($cmid);
119 if ($draftitemid) {
120 file_save_draft_area_files($draftitemid, $context->id, 'mod_folder', 'content', 0, array('subdirs'=>true));
123 return $data->id;
127 * Update folder instance.
128 * @param object $data
129 * @param object $mform
130 * @return bool true
132 function folder_update_instance($data, $mform) {
133 global $CFG, $DB;
135 $cmid = $data->coursemodule;
136 $draftitemid = $data->files;
138 $data->timemodified = time();
139 $data->id = $data->instance;
140 $data->revision++;
142 $DB->update_record('folder', $data);
144 $context = context_module::instance($cmid);
145 if ($draftitemid = file_get_submitted_draft_itemid('files')) {
146 file_save_draft_area_files($draftitemid, $context->id, 'mod_folder', 'content', 0, array('subdirs'=>true));
149 return true;
153 * Delete folder instance.
154 * @param int $id
155 * @return bool true
157 function folder_delete_instance($id) {
158 global $DB;
160 if (!$folder = $DB->get_record('folder', array('id'=>$id))) {
161 return false;
164 // note: all context files are deleted automatically
166 $DB->delete_records('folder', array('id'=>$folder->id));
168 return true;
172 * Lists all browsable file areas
174 * @package mod_folder
175 * @category files
176 * @param stdClass $course course object
177 * @param stdClass $cm course module object
178 * @param stdClass $context context object
179 * @return array
181 function folder_get_file_areas($course, $cm, $context) {
182 $areas = array();
183 $areas['content'] = get_string('foldercontent', 'folder');
185 return $areas;
189 * File browsing support for folder module content area.
191 * @package mod_folder
192 * @category files
193 * @param file_browser $browser file browser instance
194 * @param array $areas file areas
195 * @param stdClass $course course object
196 * @param stdClass $cm course module object
197 * @param stdClass $context context object
198 * @param string $filearea file area
199 * @param int $itemid item ID
200 * @param string $filepath file path
201 * @param string $filename file name
202 * @return file_info instance or null if not found
204 function folder_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
205 global $CFG;
208 if ($filearea === 'content') {
209 if (!has_capability('mod/folder:view', $context)) {
210 return NULL;
212 $fs = get_file_storage();
214 $filepath = is_null($filepath) ? '/' : $filepath;
215 $filename = is_null($filename) ? '.' : $filename;
216 if (!$storedfile = $fs->get_file($context->id, 'mod_folder', 'content', 0, $filepath, $filename)) {
217 if ($filepath === '/' and $filename === '.') {
218 $storedfile = new virtual_root_file($context->id, 'mod_folder', 'content', 0);
219 } else {
220 // not found
221 return null;
225 require_once("$CFG->dirroot/mod/folder/locallib.php");
226 $urlbase = $CFG->wwwroot.'/pluginfile.php';
228 // students may read files here
229 $canwrite = has_capability('mod/folder:managefiles', $context);
230 return new folder_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, $canwrite, false);
233 // note: folder_intro handled in file_browser automatically
235 return null;
239 * Serves the folder files.
241 * @package mod_folder
242 * @category files
243 * @param stdClass $course course object
244 * @param stdClass $cm course module
245 * @param stdClass $context context object
246 * @param string $filearea file area
247 * @param array $args extra arguments
248 * @param bool $forcedownload whether or not force download
249 * @param array $options additional options affecting the file serving
250 * @return bool false if file not found, does not return if found - just send the file
252 function folder_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
253 global $CFG, $DB;
255 if ($context->contextlevel != CONTEXT_MODULE) {
256 return false;
259 require_course_login($course, true, $cm);
260 if (!has_capability('mod/folder:view', $context)) {
261 return false;
264 if ($filearea !== 'content') {
265 // intro is handled automatically in pluginfile.php
266 return false;
269 array_shift($args); // ignore revision - designed to prevent caching problems only
271 $fs = get_file_storage();
272 $relativepath = implode('/', $args);
273 $fullpath = "/$context->id/mod_folder/content/0/$relativepath";
274 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
275 return false;
278 // finally send the file
279 // for folder module, we force download file all the time
280 send_stored_file($file, 0, 0, true, $options);
284 * Return a list of page types
285 * @param string $pagetype current page type
286 * @param stdClass $parentcontext Block's parent context
287 * @param stdClass $currentcontext Current context of block
289 function folder_page_type_list($pagetype, $parentcontext, $currentcontext) {
290 $module_pagetype = array('mod-folder-*'=>get_string('page-mod-folder-x', 'folder'));
291 return $module_pagetype;
295 * Export folder resource contents
297 * @return array of file content
299 function folder_export_contents($cm, $baseurl) {
300 global $CFG, $DB;
301 $contents = array();
302 $context = context_module::instance($cm->id);
303 $folder = $DB->get_record('folder', array('id'=>$cm->instance), '*', MUST_EXIST);
305 $fs = get_file_storage();
306 $files = $fs->get_area_files($context->id, 'mod_folder', 'content', 0, 'sortorder DESC, id ASC', false);
308 foreach ($files as $fileinfo) {
309 $file = array();
310 $file['type'] = 'file';
311 $file['filename'] = $fileinfo->get_filename();
312 $file['filepath'] = $fileinfo->get_filepath();
313 $file['filesize'] = $fileinfo->get_filesize();
314 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_folder/content/'.$folder->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
315 $file['timecreated'] = $fileinfo->get_timecreated();
316 $file['timemodified'] = $fileinfo->get_timemodified();
317 $file['sortorder'] = $fileinfo->get_sortorder();
318 $file['userid'] = $fileinfo->get_userid();
319 $file['author'] = $fileinfo->get_author();
320 $file['license'] = $fileinfo->get_license();
321 $contents[] = $file;
324 return $contents;
328 * Register the ability to handle drag and drop file uploads
329 * @return array containing details of the files / types the mod can handle
331 function folder_dndupload_register() {
332 return array('files' => array(
333 array('extension' => 'zip', 'message' => get_string('dnduploadmakefolder', 'mod_folder'))
338 * Handle a file that has been uploaded
339 * @param object $uploadinfo details of the file / content that has been uploaded
340 * @return int instance id of the newly created mod
342 function folder_dndupload_handle($uploadinfo) {
343 global $DB, $USER;
345 // Gather the required info.
346 $data = new stdClass();
347 $data->course = $uploadinfo->course->id;
348 $data->name = $uploadinfo->displayname;
349 $data->intro = '<p>'.$uploadinfo->displayname.'</p>';
350 $data->introformat = FORMAT_HTML;
351 $data->coursemodule = $uploadinfo->coursemodule;
352 $data->files = null; // We will unzip the file and sort out the contents below.
354 $data->id = folder_add_instance($data, null);
356 // Retrieve the file from the draft file area.
357 $context = context_module::instance($uploadinfo->coursemodule);
358 file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_folder', 'temp', 0, array('subdirs'=>true));
359 $fs = get_file_storage();
360 $files = $fs->get_area_files($context->id, 'mod_folder', 'temp', 0, 'sortorder', false);
361 // Only ever one file - extract the contents.
362 $file = reset($files);
364 $success = $file->extract_to_storage(new zip_packer(), $context->id, 'mod_folder', 'content', 0, '/', $USER->id);
365 $fs->delete_area_files($context->id, 'mod_folder', 'temp', 0);
367 if ($success) {
368 return $data->id;
371 $DB->delete_records('folder', array('id' => $data->id));
372 return false;
376 * Given a coursemodule object, this function returns the extra
377 * information needed to print this activity in various places.
379 * If folder needs to be displayed inline we store additional information
380 * in customdata, so functions {@link folder_cm_info_dynamic()} and
381 * {@link folder_cm_info_view()} do not need to do DB queries
383 * @param cm_info $cm
384 * @return cached_cm_info info
386 function folder_get_coursemodule_info($cm) {
387 global $DB;
388 if (!($folder = $DB->get_record('folder', array('id' => $cm->instance),
389 'id, name, display, showexpanded, intro, introformat'))) {
390 return NULL;
392 $cminfo = new cached_cm_info();
393 $cminfo->name = $folder->name;
394 if ($folder->display == FOLDER_DISPLAY_INLINE) {
395 // prepare folder object to store in customdata
396 $fdata = new stdClass();
397 $fdata->showexpanded = $folder->showexpanded;
398 if ($cm->showdescription && strlen(trim($folder->intro))) {
399 $fdata->intro = $folder->intro;
400 if ($folder->introformat != FORMAT_MOODLE) {
401 $fdata->introformat = $folder->introformat;
404 $cminfo->customdata = $fdata;
405 } else {
406 if ($cm->showdescription) {
407 // Convert intro to html. Do not filter cached version, filters run at display time.
408 $cminfo->content = format_module_intro('folder', $folder, $cm->id, false);
411 return $cminfo;
415 * Sets dynamic information about a course module
417 * This function is called from cm_info when displaying the module
418 * mod_folder can be displayed inline on course page and therefore have no course link
420 * @param cm_info $cm
422 function folder_cm_info_dynamic(cm_info $cm) {
423 if ($cm->customdata) {
424 // the field 'customdata' is not empty IF AND ONLY IF we display contens inline
425 $cm->set_no_view_link();
430 * Overwrites the content in the course-module object with the folder files list
431 * if folder.display == FOLDER_DISPLAY_INLINE
433 * @param cm_info $cm
435 function folder_cm_info_view(cm_info $cm) {
436 global $PAGE;
437 if ($cm->uservisible && $cm->customdata &&
438 has_capability('mod/folder:view', $cm->context)) {
439 // Restore folder object from customdata.
440 // Note the field 'customdata' is not empty IF AND ONLY IF we display contens inline.
441 // Otherwise the content is default.
442 $folder = $cm->customdata;
443 $folder->id = (int)$cm->instance;
444 $folder->course = (int)$cm->course;
445 $folder->display = FOLDER_DISPLAY_INLINE;
446 $folder->name = $cm->name;
447 if (empty($folder->intro)) {
448 $folder->intro = '';
450 if (empty($folder->introformat)) {
451 $folder->introformat = FORMAT_MOODLE;
453 // display folder
454 $renderer = $PAGE->get_renderer('mod_folder');
455 $cm->set_content($renderer->display_folder($folder));