3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * Mandatory public API of folder module
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);
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) {
40 case FEATURE_MOD_ARCHETYPE
: return MOD_ARCHETYPE_RESOURCE
;
41 case FEATURE_GROUPS
: return false;
42 case FEATURE_GROUPINGS
: return false;
43 case FEATURE_MOD_INTRO
: return true;
44 case FEATURE_COMPLETION_TRACKS_VIEWS
: return true;
45 case FEATURE_GRADE_HAS_GRADE
: return false;
46 case FEATURE_GRADE_OUTCOMES
: return false;
47 case FEATURE_BACKUP_MOODLE2
: return true;
48 case FEATURE_SHOW_DESCRIPTION
: return true;
55 * Returns all other caps used in module
58 function folder_get_extra_capabilities() {
59 return array('moodle/site:accessallgroups');
63 * This function is used by the reset_course_userdata function in moodlelib.
64 * @param $data the data submitted from the reset course.
65 * @return array status array
67 function folder_reset_userdata($data) {
69 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
76 * List the actions that correspond to a view of this module.
77 * This is used by the participation report.
79 * Note: This is not used by new logging system. Event with
80 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
81 * be considered as view action.
85 function folder_get_view_actions() {
86 return array('view', 'view all');
90 * List the actions that correspond to a post of this module.
91 * This is used by the participation report.
93 * Note: This is not used by new logging system. Event with
94 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
95 * will be considered as post action.
99 function folder_get_post_actions() {
100 return array('update', 'add');
104 * Add folder instance.
105 * @param object $data
106 * @param object $mform
107 * @return int new folder instance id
109 function folder_add_instance($data, $mform) {
112 $cmid = $data->coursemodule
;
113 $draftitemid = $data->files
;
115 $data->timemodified
= time();
116 $data->id
= $DB->insert_record('folder', $data);
118 // we need to use context now, so we need to make sure all needed info is already in db
119 $DB->set_field('course_modules', 'instance', $data->id
, array('id'=>$cmid));
120 $context = context_module
::instance($cmid);
123 file_save_draft_area_files($draftitemid, $context->id
, 'mod_folder', 'content', 0, array('subdirs'=>true));
126 $completiontimeexpected = !empty($data->completionexpected
) ?
$data->completionexpected
: null;
127 \core_completion\api
::update_completion_date_event($data->coursemodule
, 'folder', $data->id
, $completiontimeexpected);
133 * Update folder instance.
134 * @param object $data
135 * @param object $mform
138 function folder_update_instance($data, $mform) {
141 $cmid = $data->coursemodule
;
142 $draftitemid = $data->files
;
144 $data->timemodified
= time();
145 $data->id
= $data->instance
;
148 $DB->update_record('folder', $data);
150 $context = context_module
::instance($cmid);
151 if ($draftitemid = file_get_submitted_draft_itemid('files')) {
152 file_save_draft_area_files($draftitemid, $context->id
, 'mod_folder', 'content', 0, array('subdirs'=>true));
155 $completiontimeexpected = !empty($data->completionexpected
) ?
$data->completionexpected
: null;
156 \core_completion\api
::update_completion_date_event($data->coursemodule
, 'folder', $data->id
, $completiontimeexpected);
162 * Delete folder instance.
166 function folder_delete_instance($id) {
169 if (!$folder = $DB->get_record('folder', array('id'=>$id))) {
173 $cm = get_coursemodule_from_instance('folder', $id);
174 \core_completion\api
::update_completion_date_event($cm->id
, 'folder', $folder->id
, null);
176 // note: all context files are deleted automatically
178 $DB->delete_records('folder', array('id'=>$folder->id
));
184 * Lists all browsable file areas
186 * @package mod_folder
188 * @param stdClass $course course object
189 * @param stdClass $cm course module object
190 * @param stdClass $context context object
193 function folder_get_file_areas($course, $cm, $context) {
195 $areas['content'] = get_string('foldercontent', 'folder');
201 * File browsing support for folder module content area.
203 * @package mod_folder
205 * @param file_browser $browser file browser instance
206 * @param array $areas file areas
207 * @param stdClass $course course object
208 * @param stdClass $cm course module object
209 * @param stdClass $context context object
210 * @param string $filearea file area
211 * @param int $itemid item ID
212 * @param string $filepath file path
213 * @param string $filename file name
214 * @return file_info instance or null if not found
216 function folder_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
220 if ($filearea === 'content') {
221 if (!has_capability('mod/folder:view', $context)) {
224 $fs = get_file_storage();
226 $filepath = is_null($filepath) ?
'/' : $filepath;
227 $filename = is_null($filename) ?
'.' : $filename;
228 if (!$storedfile = $fs->get_file($context->id
, 'mod_folder', 'content', 0, $filepath, $filename)) {
229 if ($filepath === '/' and $filename === '.') {
230 $storedfile = new virtual_root_file($context->id
, 'mod_folder', 'content', 0);
237 require_once("$CFG->dirroot/mod/folder/locallib.php");
238 $urlbase = $CFG->wwwroot
.'/pluginfile.php';
240 // students may read files here
241 $canwrite = has_capability('mod/folder:managefiles', $context);
242 return new folder_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, $canwrite, false);
245 // note: folder_intro handled in file_browser automatically
251 * Serves the folder files.
253 * @package mod_folder
255 * @param stdClass $course course object
256 * @param stdClass $cm course module
257 * @param stdClass $context context object
258 * @param string $filearea file area
259 * @param array $args extra arguments
260 * @param bool $forcedownload whether or not force download
261 * @param array $options additional options affecting the file serving
262 * @return bool false if file not found, does not return if found - just send the file
264 function folder_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
267 if ($context->contextlevel
!= CONTEXT_MODULE
) {
271 require_course_login($course, true, $cm);
272 if (!has_capability('mod/folder:view', $context)) {
276 if ($filearea !== 'content') {
277 // intro is handled automatically in pluginfile.php
281 array_shift($args); // ignore revision - designed to prevent caching problems only
283 $fs = get_file_storage();
284 $relativepath = implode('/', $args);
285 $fullpath = "/$context->id/mod_folder/content/0/$relativepath";
286 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
290 // finally send the file
291 // for folder module, we force download file all the time
292 send_stored_file($file, 0, 0, true, $options);
296 * Return a list of page types
297 * @param string $pagetype current page type
298 * @param stdClass $parentcontext Block's parent context
299 * @param stdClass $currentcontext Current context of block
301 function folder_page_type_list($pagetype, $parentcontext, $currentcontext) {
302 $module_pagetype = array('mod-folder-*'=>get_string('page-mod-folder-x', 'folder'));
303 return $module_pagetype;
307 * Export folder resource contents
309 * @return array of file content
311 function folder_export_contents($cm, $baseurl) {
314 $context = context_module
::instance($cm->id
);
315 $folder = $DB->get_record('folder', array('id'=>$cm->instance
), '*', MUST_EXIST
);
317 $fs = get_file_storage();
318 $files = $fs->get_area_files($context->id
, 'mod_folder', 'content', 0, 'sortorder DESC, id ASC', false);
320 foreach ($files as $fileinfo) {
322 $file['type'] = 'file';
323 $file['filename'] = $fileinfo->get_filename();
324 $file['filepath'] = $fileinfo->get_filepath();
325 $file['filesize'] = $fileinfo->get_filesize();
326 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id
.'/mod_folder/content/'.$folder->revision
.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
327 $file['timecreated'] = $fileinfo->get_timecreated();
328 $file['timemodified'] = $fileinfo->get_timemodified();
329 $file['sortorder'] = $fileinfo->get_sortorder();
330 $file['userid'] = $fileinfo->get_userid();
331 $file['author'] = $fileinfo->get_author();
332 $file['license'] = $fileinfo->get_license();
333 $file['mimetype'] = $fileinfo->get_mimetype();
334 $file['isexternalfile'] = $fileinfo->is_external_file();
335 if ($file['isexternalfile']) {
336 $file['repositorytype'] = $fileinfo->get_repository_type();
345 * Register the ability to handle drag and drop file uploads
346 * @return array containing details of the files / types the mod can handle
348 function folder_dndupload_register() {
349 return array('files' => array(
350 array('extension' => 'zip', 'message' => get_string('dnduploadmakefolder', 'mod_folder'))
355 * Handle a file that has been uploaded
356 * @param object $uploadinfo details of the file / content that has been uploaded
357 * @return int instance id of the newly created mod
359 function folder_dndupload_handle($uploadinfo) {
362 // Gather the required info.
363 $data = new stdClass();
364 $data->course
= $uploadinfo->course
->id
;
365 $data->name
= $uploadinfo->displayname
;
366 $data->intro
= '<p>'.$uploadinfo->displayname
.'</p>';
367 $data->introformat
= FORMAT_HTML
;
368 $data->coursemodule
= $uploadinfo->coursemodule
;
369 $data->files
= null; // We will unzip the file and sort out the contents below.
371 $data->id
= folder_add_instance($data, null);
373 // Retrieve the file from the draft file area.
374 $context = context_module
::instance($uploadinfo->coursemodule
);
375 file_save_draft_area_files($uploadinfo->draftitemid
, $context->id
, 'mod_folder', 'temp', 0, array('subdirs'=>true));
376 $fs = get_file_storage();
377 $files = $fs->get_area_files($context->id
, 'mod_folder', 'temp', 0, 'sortorder', false);
378 // Only ever one file - extract the contents.
379 $file = reset($files);
381 $success = $file->extract_to_storage(new zip_packer(), $context->id
, 'mod_folder', 'content', 0, '/', $USER->id
);
382 $fs->delete_area_files($context->id
, 'mod_folder', 'temp', 0);
388 $DB->delete_records('folder', array('id' => $data->id
));
393 * Given a coursemodule object, this function returns the extra
394 * information needed to print this activity in various places.
396 * If folder needs to be displayed inline we store additional information
397 * in customdata, so functions {@link folder_cm_info_dynamic()} and
398 * {@link folder_cm_info_view()} do not need to do DB queries
401 * @return cached_cm_info info
403 function folder_get_coursemodule_info($cm) {
405 if (!($folder = $DB->get_record('folder', array('id' => $cm->instance
),
406 'id, name, display, showexpanded, showdownloadfolder, intro, introformat'))) {
409 $cminfo = new cached_cm_info();
410 $cminfo->name
= $folder->name
;
411 if ($folder->display
== FOLDER_DISPLAY_INLINE
) {
412 // prepare folder object to store in customdata
413 $fdata = new stdClass();
414 $fdata->showexpanded
= $folder->showexpanded
;
415 $fdata->showdownloadfolder
= $folder->showdownloadfolder
;
416 if ($cm->showdescription
&& strlen(trim($folder->intro
))) {
417 $fdata->intro
= $folder->intro
;
418 if ($folder->introformat
!= FORMAT_MOODLE
) {
419 $fdata->introformat
= $folder->introformat
;
422 $cminfo->customdata
= $fdata;
424 if ($cm->showdescription
) {
425 // Convert intro to html. Do not filter cached version, filters run at display time.
426 $cminfo->content
= format_module_intro('folder', $folder, $cm->id
, false);
433 * Sets dynamic information about a course module
435 * This function is called from cm_info when displaying the module
436 * mod_folder can be displayed inline on course page and therefore have no course link
440 function folder_cm_info_dynamic(cm_info
$cm) {
441 if ($cm->customdata
) {
442 // the field 'customdata' is not empty IF AND ONLY IF we display contens inline
443 $cm->set_no_view_link();
448 * Overwrites the content in the course-module object with the folder files list
449 * if folder.display == FOLDER_DISPLAY_INLINE
453 function folder_cm_info_view(cm_info
$cm) {
455 if ($cm->uservisible
&& $cm->customdata
&&
456 has_capability('mod/folder:view', $cm->context
)) {
457 // Restore folder object from customdata.
458 // Note the field 'customdata' is not empty IF AND ONLY IF we display contens inline.
459 // Otherwise the content is default.
460 $folder = $cm->customdata
;
461 $folder->id
= (int)$cm->instance
;
462 $folder->course
= (int)$cm->course
;
463 $folder->display
= FOLDER_DISPLAY_INLINE
;
464 $folder->name
= $cm->name
;
465 if (empty($folder->intro
)) {
468 if (empty($folder->introformat
)) {
469 $folder->introformat
= FORMAT_MOODLE
;
472 $renderer = $PAGE->get_renderer('mod_folder');
473 $cm->set_content($renderer->display_folder($folder));
478 * Mark the activity completed (if required) and trigger the course_module_viewed event.
480 * @param stdClass $folder folder object
481 * @param stdClass $course course object
482 * @param stdClass $cm course module object
483 * @param stdClass $context context object
486 function folder_view($folder, $course, $cm, $context) {
488 // Trigger course_module_viewed event.
490 'context' => $context,
491 'objectid' => $folder->id
494 $event = \mod_folder\event\course_module_viewed
::create($params);
495 $event->add_record_snapshot('course_modules', $cm);
496 $event->add_record_snapshot('course', $course);
497 $event->add_record_snapshot('folder', $folder);
501 $completion = new completion_info($course);
502 $completion->set_module_viewed($cm);
506 * Check if the folder can be zipped and downloaded.
507 * @param stdClass $folder
508 * @param context_module $cm
509 * @return bool True if the folder can be zipped and downloaded.
510 * @throws \dml_exception
512 function folder_archive_available($folder, $cm) {
513 if (!$folder->showdownloadfolder
) {
517 $context = context_module
::instance($cm->id
);
518 $fs = get_file_storage();
519 $dir = $fs->get_area_tree($context->id
, 'mod_folder', 'content', 0);
521 $size = folder_get_directory_size($dir);
522 $maxsize = get_config('folder', 'maxsizetodownload') * 1024 * 1024;
528 if (!empty($maxsize) && $size > $maxsize) {
536 * Recursively measure the size of the files in a directory.
537 * @param array $directory
538 * @return int size of directory contents in bytes
540 function folder_get_directory_size($directory) {
543 foreach ($directory['files'] as $file) {
544 $size +
= $file->get_filesize();
547 foreach ($directory['subdirs'] as $subdirectory) {
548 $size +
= folder_get_directory_size($subdirectory);
555 * Mark the activity completed (if required) and trigger the all_files_downloaded event.
557 * @param stdClass $folder folder object
558 * @param stdClass $course course object
559 * @param stdClass $cm course module object
560 * @param stdClass $context context object
563 function folder_downloaded($folder, $course, $cm, $context) {
565 'context' => $context,
566 'objectid' => $folder->id
568 $event = \mod_folder\event\all_files_downloaded
::create($params);
569 $event->add_record_snapshot('course_modules', $cm);
570 $event->add_record_snapshot('course', $course);
571 $event->add_record_snapshot('folder', $folder);
575 $completion = new completion_info($course);
576 $completion->set_module_viewed($cm);
580 * Returns all uploads since a given time in specified folder.
582 * @param array $activities
584 * @param int $timestart
585 * @param int $courseid
588 * @param int $groupid not used, but required for compatibilty with other modules
590 function folder_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
591 global $COURSE, $DB, $OUTPUT;
593 if ($COURSE->id
== $courseid) {
596 $course = $DB->get_record('course', array('id' => $courseid));
599 $modinfo = get_fast_modinfo($course);
600 $cm = $modinfo->cms
[$cmid];
602 $context = context_module
::instance($cm->id
);
603 if (!has_capability('mod/folder:view', $context)) {
606 $files = folder_get_recent_activity($context, $timestart, $userid);
608 foreach ($files as $file) {
609 $tmpactivity = new stdClass();
611 $tmpactivity->type
= 'folder';
612 $tmpactivity->cmid
= $cm->id
;
613 $tmpactivity->sectionnum
= $cm->sectionnum
;
614 $tmpactivity->timestamp
= $file->get_timemodified();
615 $tmpactivity->user
= core_user
::get_user($file->get_userid());
617 $tmpactivity->content
= new stdClass();
618 $tmpactivity->content
->url
= moodle_url
::make_pluginfile_url($file->get_contextid(), 'mod_folder', 'content',
619 $file->get_itemid(), $file->get_filepath(), $file->get_filename());
621 if (file_extension_in_typegroup($file->get_filename(), 'web_image')) {
622 $image = $tmpactivity->content
->url
->out(false, array('preview' => 'tinyicon', 'oid' => $file->get_timemodified()));
623 $image = html_writer
::empty_tag('img', array('src' => $image));
625 $image = $OUTPUT->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
628 $tmpactivity->content
->image
= $image;
629 $tmpactivity->content
->filename
= $file->get_filename();
631 $activities[$index++
] = $tmpactivity;
637 * Outputs the folder uploads indicated by $activity.
639 * @param object $activity the activity object the folder resides in
640 * @param int $courseid the id of the course the folder resides in
641 * @param bool $detail not used, but required for compatibilty with other modules
642 * @param int $modnames not used, but required for compatibilty with other modules
643 * @param bool $viewfullnames not used, but required for compatibilty with other modules
645 function folder_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
648 $content = $activity->content
;
651 'cellpadding' => '3',
654 $output = html_writer
::start_tag('table', $tableoptions);
655 $output .= html_writer
::start_tag('tr');
656 $output .= html_writer
::tag('td', $content->image
, ['class' => 'fp-icon', 'valign' => 'top']);
657 $output .= html_writer
::start_tag('td');
658 $output .= html_writer
::start_div('fp-filename');
659 $output .= html_writer
::link($content->url
, $content->filename
);
660 $output .= html_writer
::end_div();
662 // Show the uploader.
663 $fullname = fullname($activity->user
, $viewfullnames);
664 $userurl = new moodle_url('/user/view.php');
665 $userurl->params(['id' => $activity->user
->id
, 'course' => $courseid]);
666 $by = new stdClass();
667 $by->name
= html_writer
::link($userurl, $fullname);
668 $by->date
= userdate($activity->timestamp
);
669 $authornamedate = get_string('bynameondate', 'folder', $by);
670 $output .= html_writer
::div($authornamedate, 'user');
672 // Finish up the table.
673 $output .= html_writer
::end_tag('tr');
674 $output .= html_writer
::end_tag('table');
680 * Gets recent file uploads in a given folder. Does not perform security checks.
682 * @param object $context
683 * @param int $timestart
688 function folder_get_recent_activity($context, $timestart, $userid=0) {
690 $fs = get_file_storage();
691 $files = $fs->get_area_files($context->id
, 'mod_folder', 'content');
692 foreach ($files as $file) {
693 if ($file->get_timemodified() <= $timestart) {
696 if ($file->get_filename() === '.') {
699 if (!empty($userid) && $userid !== $file->get_userid()) {
708 * Given a course and a date, prints a summary of all the new
709 * files posted in folder resources since that date
711 * @uses CONTEXT_MODULE
712 * @param object $course
713 * @param bool $viewfullnames capability
714 * @param int $timestart
715 * @return bool success
717 function folder_print_recent_activity($course, $viewfullnames, $timestart) {
720 $folders = get_all_instances_in_course('folder', $course);
722 if (empty($folders)) {
728 $modinfo = get_fast_modinfo($course);
729 foreach ($folders as $folder) {
730 // Skip resources if the user can't view them.
731 $cm = $modinfo->cms
[$folder->coursemodule
];
732 $context = context_module
::instance($cm->id
);
733 if (!has_capability('mod/folder:view', $context)) {
737 // Get the files uploaded in the current time frame.
738 $newfiles = array_merge($newfiles, folder_get_recent_activity($context, $timestart));
741 if (empty($newfiles)) {
745 // Build list of files.
746 echo $OUTPUT->heading(get_string('newfoldercontent', 'folder').':', 3);
747 $list = html_writer
::start_tag('ul', ['class' => 'unlist']);
748 foreach ($newfiles as $file) {
749 $filename = $file->get_filename();
750 $url = moodle_url
::make_pluginfile_url($file->get_contextid(), 'mod_folder', 'content',
751 $file->get_itemid(), $file->get_filepath(), $filename);
753 $list .= html_writer
::start_tag('li');
754 $list .= html_writer
::start_div('head');
755 $list .= html_writer
::div(userdate($file->get_timemodified(), get_string('strftimerecent')), 'date');
756 $list .= html_writer
::div($file->get_author(), 'name');
757 $list .= html_writer
::end_div(); // Head.
759 $list .= html_writer
::start_div('info');
760 $list .= html_writer
::link($url, $filename);
761 $list .= html_writer
::end_div(); // Info.
762 $list .= html_writer
::end_tag('li');
764 $list .= html_writer
::end_tag('ul');
770 * Check if the module has any update that affects the current user since a given time.
772 * @param cm_info $cm course module data
773 * @param int $from the time to check updates from
774 * @param array $filter if we need to check only specific updates
775 * @return stdClass an object with the different type of areas indicating if they were updated or not
778 function folder_check_updates_since(cm_info
$cm, $from, $filter = array()) {
779 $updates = course_check_module_updates_since($cm, $from, array('content'), $filter);
784 * This function receives a calendar event and returns the action associated with it, or null if there is none.
786 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
787 * is not displayed on the block.
789 * @param calendar_event $event
790 * @param \core_calendar\action_factory $factory
791 * @return \core_calendar\local\event\entities\action_interface|null
793 function mod_folder_core_calendar_provide_event_action(calendar_event
$event,
794 \core_calendar\action_factory
$factory) {
795 $cm = get_fast_modinfo($event->courseid
)->instances
['folder'][$event->instance
];
797 $completion = new \
completion_info($cm->get_course());
799 $completiondata = $completion->get_data($cm, false);
801 if ($completiondata->completionstate
!= COMPLETION_INCOMPLETE
) {
805 return $factory->create_instance(
807 new \
moodle_url('/mod/folder/view.php', ['id' => $cm->id
]),