MDL-20066 new folder module, includes migrate from old mod/resource; remaining issues...
[moodle.git] / mod / folder / lib.php
CommitLineData
0e16b939 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/>.
17
18/**
19 * Mandatory public API of folder module
20 *
21 * @package mod-folder
22 * @copyright 2009 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26/**
27 * List of features supported in Folder module
28 * @param string $feature FEATURE_xx constant for requested feature
29 * @return mixed True if module supports feature, false if not, null if doesn't know
30 */
31function folder_supports($feature) {
32 switch($feature) {
33 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
34 case FEATURE_GROUPS: return false;
35 case FEATURE_GROUPINGS: return false;
36 case FEATURE_GROUPMEMBERSONLY: return true;
37 case FEATURE_MOD_INTRO: return true;
38 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
39 case FEATURE_GRADE_HAS_GRADE: return false;
40 case FEATURE_GRADE_OUTCOMES: return false;
41
42 default: return null;
43 }
44}
45
46/**
47 * Returns all other caps used in module
48 * @return array
49 */
50function folder_get_extra_capabilities() {
51 return array('moodle/site:accessallgroups');
52}
53
54/**
55 * This function is used by the reset_course_userdata function in moodlelib.
56 * @param $data the data submitted from the reset course.
57 * @return array status array
58 */
59function folder_reset_userdata($data) {
60 return array();
61}
62
63/**
64 * List of view style log actions
65 * @return array
66 */
67function folder_get_view_actions() {
68 return array('view', 'view all');
69}
70
71/**
72 * List of update style log actions
73 * @return array
74 */
75function folder_get_post_actions() {
76 return array('update', 'add');
77}
78
79/**
80 * Add folder instance.
81 * @param object $data
82 * @param object $mform
83 * @return int new folder instance id
84 */
85function folder_add_instance($data, $mform) {
86 global $DB;
87
88 $cmid = $data->coursemodule;
89 $draftitemid = $data->files;
90
91 $data->timemodified = time();
92 $data->id = $DB->insert_record('folder', $data);
93
94 // we need to use context now, so we need to make sure all needed info is already in db
95 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
96 $context = get_context_instance(CONTEXT_MODULE, $cmid);
97
98 if ($draftitemid) {
99 file_save_draft_area_files($draftitemid, $context->id, 'folder_content', 0, array('subdirs'=>true));
100 }
101
102 return $data->id;
103}
104
105/**
106 * Update folder instance.
107 * @param object $data
108 * @param object $mform
109 * @return bool true
110 */
111function folder_update_instance($data, $mform) {
112 global $CFG, $DB;
113
114 $cmid = $data->coursemodule;
115 $draftitemid = $data->files;
116
117 $data->timemodified = time();
118 $data->id = $data->instance;
119 $data->revision++;
120
121 $DB->update_record('folder', $data);
122
123 $context = get_context_instance(CONTEXT_MODULE, $cmid);
124 if ($draftitemid = file_get_submitted_draft_itemid('files')) {
125 file_save_draft_area_files($draftitemid, $context->id, 'folder_content', 0, array('subdirs'=>true));
126 }
127
128 return true;
129}
130
131/**
132 * Delete folder instance.
133 * @param int $id
134 * @return bool true
135 */
136function folder_delete_instance($id) {
137 global $DB;
138
139 if (!$folder = $DB->get_record('folder', array('id'=>$id))) {
140 return false;
141 }
142
143 // note: all context files are deleted automatically
144
145 $DB->delete_records('folder', array('id'=>$folder->id));
146
147 return true;
148}
149
150/**
151 * Return use outline
152 * @param object $course
153 * @param object $user
154 * @param object $mod
155 * @param object $folder
156 * @return object|null
157 */
158function folder_user_outline($course, $user, $mod, $folder) {
159 global $DB;
160
161 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'folder',
162 'action'=>'view', 'info'=>$folder->id), 'time ASC')) {
163
164 $numviews = count($logs);
165 $lastlog = array_pop($logs);
166
167 $result = new object();
168 $result->info = get_string('numviews', '', $numviews);
169 $result->time = $lastlog->time;
170
171 return $result;
172 }
173 return NULL;
174}
175
176/**
177 * Return use complete
178 * @param object $course
179 * @param object $user
180 * @param object $mod
181 * @param object $folder
182 */
183function folder_user_complete($course, $user, $mod, $folder) {
184 global $CFG, $DB;
185
186 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'folder',
187 'action'=>'view', 'info'=>$folder->id), 'time ASC')) {
188 $numviews = count($logs);
189 $lastlog = array_pop($logs);
190
191 $strmostrecently = get_string('mostrecently');
192 $strnumviews = get_string('numviews', '', $numviews);
193
194 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
195
196 } else {
197 print_string('neverseen', 'folder');
198 }
199}
200
201/**
202 * Returns the users with data in one folder
203 *
204 * @param int $folderid
205 * @return bool false
206 */
207function folder_get_participants($folderid) {
208 return false;
209}
210
211/**
212 * Lists all browsable file areas
213 * @param object $course
214 * @param object $cm
215 * @param object $context
216 * @return array
217 */
218function folder_get_file_areas($course, $cm, $context) {
219 $areas = array();
220 if (has_capability('moodle/course:managefiles', $context)) {
221 $areas['folder_content'] = get_string('foldercontent', 'folder');
222 }
223 return $areas;
224}
225
226/**
227 * File browsing support for folder module ontent area.
228 * @param object $browser
229 * @param object $areas
230 * @param object $course
231 * @param object $cm
232 * @param object $context
233 * @param string $filearea
234 * @param int $itemid
235 * @param string $filepath
236 * @param string $filename
237 * @return object file_info instance or null if not found
238 */
239function folder_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
240 global $CFG;
241
242 $canwrite = has_capability('moodle/course:managefiles', $context);
243
244 $fs = get_file_storage();
245
246 if ($filearea === 'folder_content') {
247 $filepath = is_null($filepath) ? '/' : $filepath;
248 $filename = is_null($filename) ? '.' : $filename;
249
250 $urlbase = $CFG->wwwroot.'/pluginfile.php';
251 if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
252 if ($filepath === '/' and $filename === '.') {
253 $storedfile = new virtual_root_file($context->id, $filearea, 0);
254 } else {
255 // not found
256 return null;
257 }
258 }
259 require_once("$CFG->dirroot/mod/folder/locallib.php");
260 return new folder_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, $canwrite, false);
261 }
262
263 // note: folder_intro handled in file_browser automatically
264
265 return null;
266}
267
268/**
269 * Serves the folder files.
270 *
271 * @param object $course
272 * @param object $cminfo
273 * @param object $context
274 * @param string $filearea
275 * @param array $args
276 * @param bool $forcedownload
277 * @return bool false if file not found, does not return if found - justsend the file
278 */
279function folder_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) {
280 global $CFG, $DB;
281
282 if (!$cminfo->uservisible) {
283 return false;
284 }
285
286 if ($filearea !== 'folder_content') {
287 // intro is handled automatically in pluginfile.php
288 return false;
289 }
290
291 if (!$cm = get_coursemodule_from_instance('folder', $cminfo->instance, $course->id)) {
292 return false;
293 }
294
295 require_course_login($course, true, $cm);
296
297 array_shift($args); // ignore revision - designed to prevent caching problems only
298
299 $fs = get_file_storage();
300 $relativepath = '/'.implode('/', $args);
301 $fullpath = $context->id.$filearea.'0'.$relativepath;
302 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
303 return false;
304 }
305
306 // finally send the file
307 send_stored_file($file, 86400, 0, $forcedownload);
308}