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
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();
30 * List of features supported in Folder module
31 * @param string $feature FEATURE_xx constant for requested feature
32 * @return mixed True if module supports feature, false if not, null if doesn't know
34 function folder_supports($feature) {
36 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
37 case FEATURE_GROUPS: return false;
38 case FEATURE_GROUPINGS: return false;
39 case FEATURE_GROUPMEMBERSONLY: return true;
40 case FEATURE_MOD_INTRO: return true;
41 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
42 case FEATURE_GRADE_HAS_GRADE: return false;
43 case FEATURE_GRADE_OUTCOMES: return false;
44 case FEATURE_BACKUP_MOODLE2: return true;
51 * Returns all other caps used in module
54 function folder_get_extra_capabilities() {
55 return array('moodle/site:accessallgroups');
59 * This function is used by the reset_course_userdata function in moodlelib.
60 * @param $data the data submitted from the reset course.
61 * @return array status array
63 function folder_reset_userdata($data) {
68 * List of view style log actions
71 function folder_get_view_actions() {
72 return array('view', 'view all');
76 * List of update style log actions
79 function folder_get_post_actions() {
80 return array('update', 'add');
84 * Add folder instance.
86 * @param object $mform
87 * @return int new folder instance id
89 function folder_add_instance($data, $mform) {
92 $cmid = $data->coursemodule;
93 $draftitemid = $data->files;
95 $data->timemodified = time();
96 $data->id = $DB->insert_record('folder', $data);
98 // we need to use context now, so we need to make sure all needed info is already in db
99 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
100 $context = get_context_instance(CONTEXT_MODULE, $cmid);
103 file_save_draft_area_files($draftitemid, $context->id, 'mod_folder', 'content', 0, array('subdirs'=>true));
110 * Update folder instance.
111 * @param object $data
112 * @param object $mform
115 function folder_update_instance($data, $mform) {
118 $cmid = $data->coursemodule;
119 $draftitemid = $data->files;
121 $data->timemodified = time();
122 $data->id = $data->instance;
125 $DB->update_record('folder', $data);
127 $context = get_context_instance(CONTEXT_MODULE, $cmid);
128 if ($draftitemid = file_get_submitted_draft_itemid('files')) {
129 file_save_draft_area_files($draftitemid, $context->id, 'mod_folder', 'content', 0, array('subdirs'=>true));
136 * Delete folder instance.
140 function folder_delete_instance($id) {
143 if (!$folder = $DB->get_record('folder', array('id'=>$id))) {
147 // note: all context files are deleted automatically
149 $DB->delete_records('folder', array('id'=>$folder->id));
156 * @param object $course
157 * @param object $user
159 * @param object $folder
160 * @return object|null
162 function folder_user_outline($course, $user, $mod, $folder) {
165 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'folder',
166 'action'=>'view', 'info'=>$folder->id), 'time ASC')) {
168 $numviews = count($logs);
169 $lastlog = array_pop($logs);
171 $result = new stdClass();
172 $result->info = get_string('numviews', '', $numviews);
173 $result->time = $lastlog->time;
181 * Return use complete
182 * @param object $course
183 * @param object $user
185 * @param object $folder
187 function folder_user_complete($course, $user, $mod, $folder) {
190 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'folder',
191 'action'=>'view', 'info'=>$folder->id), 'time ASC')) {
192 $numviews = count($logs);
193 $lastlog = array_pop($logs);
195 $strmostrecently = get_string('mostrecently');
196 $strnumviews = get_string('numviews', '', $numviews);
198 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
201 print_string('neverseen', 'folder');
206 * Returns the users with data in one folder
208 * @param int $folderid
211 function folder_get_participants($folderid) {
216 * Lists all browsable file areas
217 * @param object $course
219 * @param object $context
222 function folder_get_file_areas($course, $cm, $context) {
224 $areas['content'] = get_string('foldercontent', 'folder');
230 * File browsing support for folder module content area.
231 * @param object $browser
232 * @param object $areas
233 * @param object $course
235 * @param object $context
236 * @param string $filearea
238 * @param string $filepath
239 * @param string $filename
240 * @return object file_info instance or null if not found
242 function folder_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
246 if ($filearea === 'content') {
247 $fs = get_file_storage();
249 $filepath = is_null($filepath) ? '/' : $filepath;
250 $filename = is_null($filename) ? '.' : $filename;
251 if (!$storedfile = $fs->get_file($context->id, 'mod_folder', 'content', 0, $filepath, $filename)) {
252 if ($filepath === '/' and $filename === '.') {
253 $storedfile = new virtual_root_file($context->id, 'mod_folder', 'content', 0);
260 require_once("$CFG->dirroot/mod/folder/locallib.php");
261 $urlbase = $CFG->wwwroot.'/pluginfile.php';
263 // students may read files here
264 $canwrite = has_capability('mod/folder:managefiles', $context);
265 return new folder_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, $canwrite, false);
268 // note: folder_intro handled in file_browser automatically
274 * Serves the folder files.
276 * @param object $course
278 * @param object $context
279 * @param string $filearea
281 * @param bool $forcedownload
282 * @return bool false if file not found, does not return if found - just send the file
284 function folder_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
287 if ($context->contextlevel != CONTEXT_MODULE) {
291 require_course_login($course, true, $cm);
292 if (!has_capability('mod/folder:view', $context)) {
296 if ($filearea !== 'content') {
297 // intro is handled automatically in pluginfile.php
301 array_shift($args); // ignore revision - designed to prevent caching problems only
303 $fs = get_file_storage();
304 $relativepath = implode('/', $args);
305 $fullpath = "/$context->id/mod_folder/content/0/$relativepath";
306 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
310 // finally send the file
311 // for folder module, we force download file all the time
312 send_stored_file($file, 86400, 0, true);
316 * This function extends the global navigation for the site.
317 * It is important to note that you should not rely on PAGE objects within this
318 * body of code as there is no guarantee that during an AJAX request they are
321 * @param navigation_node $navigation The folder node within the global navigation
322 * @param stdClass $course The course object returned from the DB
323 * @param stdClass $module The module object returned from the DB
324 * @param stdClass $cm The course module instance returned from the DB
326 function folder_extend_navigation($navigation, $course, $module, $cm) {
328 * This is currently just a stub so that it can be easily expanded upon.
329 * When expanding just remove this comment and the line below and then add
332 $navigation->nodetype = navigation_node::NODETYPE_LEAF;