MDL-18011 - removing reference to loancalc block from core
[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;
78692f23 41 case FEATURE_BACKUP_MOODLE2: return true;
0e16b939 42
43 default: return null;
44 }
45}
46
47/**
48 * Returns all other caps used in module
49 * @return array
50 */
51function folder_get_extra_capabilities() {
52 return array('moodle/site:accessallgroups');
53}
54
55/**
56 * This function is used by the reset_course_userdata function in moodlelib.
57 * @param $data the data submitted from the reset course.
58 * @return array status array
59 */
60function folder_reset_userdata($data) {
61 return array();
62}
63
64/**
65 * List of view style log actions
66 * @return array
67 */
68function folder_get_view_actions() {
69 return array('view', 'view all');
70}
71
72/**
73 * List of update style log actions
74 * @return array
75 */
76function folder_get_post_actions() {
77 return array('update', 'add');
78}
79
80/**
81 * Add folder instance.
82 * @param object $data
83 * @param object $mform
84 * @return int new folder instance id
85 */
86function folder_add_instance($data, $mform) {
87 global $DB;
88
89 $cmid = $data->coursemodule;
90 $draftitemid = $data->files;
91
92 $data->timemodified = time();
93 $data->id = $DB->insert_record('folder', $data);
94
95 // we need to use context now, so we need to make sure all needed info is already in db
96 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
97 $context = get_context_instance(CONTEXT_MODULE, $cmid);
98
99 if ($draftitemid) {
100 file_save_draft_area_files($draftitemid, $context->id, 'folder_content', 0, array('subdirs'=>true));
101 }
102
103 return $data->id;
104}
105
106/**
107 * Update folder instance.
108 * @param object $data
109 * @param object $mform
110 * @return bool true
111 */
112function folder_update_instance($data, $mform) {
113 global $CFG, $DB;
114
115 $cmid = $data->coursemodule;
116 $draftitemid = $data->files;
117
118 $data->timemodified = time();
119 $data->id = $data->instance;
120 $data->revision++;
121
122 $DB->update_record('folder', $data);
123
124 $context = get_context_instance(CONTEXT_MODULE, $cmid);
125 if ($draftitemid = file_get_submitted_draft_itemid('files')) {
126 file_save_draft_area_files($draftitemid, $context->id, 'folder_content', 0, array('subdirs'=>true));
127 }
128
129 return true;
130}
131
132/**
133 * Delete folder instance.
134 * @param int $id
135 * @return bool true
136 */
137function folder_delete_instance($id) {
138 global $DB;
139
140 if (!$folder = $DB->get_record('folder', array('id'=>$id))) {
141 return false;
142 }
143
144 // note: all context files are deleted automatically
145
146 $DB->delete_records('folder', array('id'=>$folder->id));
147
148 return true;
149}
150
151/**
152 * Return use outline
153 * @param object $course
154 * @param object $user
155 * @param object $mod
156 * @param object $folder
157 * @return object|null
158 */
159function folder_user_outline($course, $user, $mod, $folder) {
160 global $DB;
161
162 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'folder',
163 'action'=>'view', 'info'=>$folder->id), 'time ASC')) {
164
165 $numviews = count($logs);
166 $lastlog = array_pop($logs);
167
168 $result = new object();
169 $result->info = get_string('numviews', '', $numviews);
170 $result->time = $lastlog->time;
171
172 return $result;
173 }
174 return NULL;
175}
176
177/**
178 * Return use complete
179 * @param object $course
180 * @param object $user
181 * @param object $mod
182 * @param object $folder
183 */
184function folder_user_complete($course, $user, $mod, $folder) {
185 global $CFG, $DB;
186
187 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'folder',
188 'action'=>'view', 'info'=>$folder->id), 'time ASC')) {
189 $numviews = count($logs);
190 $lastlog = array_pop($logs);
191
192 $strmostrecently = get_string('mostrecently');
193 $strnumviews = get_string('numviews', '', $numviews);
194
195 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
196
197 } else {
198 print_string('neverseen', 'folder');
199 }
200}
201
202/**
203 * Returns the users with data in one folder
204 *
205 * @param int $folderid
206 * @return bool false
207 */
208function folder_get_participants($folderid) {
209 return false;
210}
211
212/**
213 * Lists all browsable file areas
214 * @param object $course
215 * @param object $cm
216 * @param object $context
217 * @return array
218 */
219function folder_get_file_areas($course, $cm, $context) {
220 $areas = array();
221 if (has_capability('moodle/course:managefiles', $context)) {
222 $areas['folder_content'] = get_string('foldercontent', 'folder');
223 }
224 return $areas;
225}
226
227/**
228 * File browsing support for folder module ontent area.
229 * @param object $browser
230 * @param object $areas
231 * @param object $course
232 * @param object $cm
233 * @param object $context
234 * @param string $filearea
235 * @param int $itemid
236 * @param string $filepath
237 * @param string $filename
238 * @return object file_info instance or null if not found
239 */
240function folder_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
241 global $CFG;
242
243 $canwrite = has_capability('moodle/course:managefiles', $context);
244
245 $fs = get_file_storage();
246
247 if ($filearea === 'folder_content') {
248 $filepath = is_null($filepath) ? '/' : $filepath;
249 $filename = is_null($filename) ? '.' : $filename;
250
251 $urlbase = $CFG->wwwroot.'/pluginfile.php';
252 if (!$storedfile = $fs->get_file($context->id, $filearea, 0, $filepath, $filename)) {
253 if ($filepath === '/' and $filename === '.') {
254 $storedfile = new virtual_root_file($context->id, $filearea, 0);
255 } else {
256 // not found
257 return null;
258 }
259 }
260 require_once("$CFG->dirroot/mod/folder/locallib.php");
261 return new folder_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, $canwrite, false);
262 }
263
264 // note: folder_intro handled in file_browser automatically
265
266 return null;
267}
268
269/**
270 * Serves the folder files.
271 *
272 * @param object $course
273 * @param object $cminfo
274 * @param object $context
275 * @param string $filearea
276 * @param array $args
277 * @param bool $forcedownload
278 * @return bool false if file not found, does not return if found - justsend the file
279 */
280function folder_pluginfile($course, $cminfo, $context, $filearea, $args, $forcedownload) {
281 global $CFG, $DB;
282
283 if (!$cminfo->uservisible) {
284 return false;
285 }
286
287 if ($filearea !== 'folder_content') {
288 // intro is handled automatically in pluginfile.php
289 return false;
290 }
291
292 if (!$cm = get_coursemodule_from_instance('folder', $cminfo->instance, $course->id)) {
293 return false;
294 }
295
296 require_course_login($course, true, $cm);
297
298 array_shift($args); // ignore revision - designed to prevent caching problems only
299
300 $fs = get_file_storage();
301 $relativepath = '/'.implode('/', $args);
302 $fullpath = $context->id.$filearea.'0'.$relativepath;
303 if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
304 return false;
305 }
306
307 // finally send the file
308 send_stored_file($file, 86400, 0, $forcedownload);
309}
92059c7e
SH
310
311/**
312 * This function extends the global navigaiton for the site.
313 * It is important to note that you should not rely on PAGE objects within this
314 * body of code as there is no guarantee that during an AJAX request they are
315 * available
316 *
317 * @param navigation_node $navigation The folder node within the global navigation
318 * @param stdClass $course The course object returned from the DB
319 * @param stdClass $module The module object returned from the DB
320 * @param stdClass $cm The course module isntance returned from the DB
321 */
322function folder_extend_navigation($navigation, $course, $module, $cm) {
323 /**
324 * This is currently just a stub so that it can be easily expanded upon.
325 * When expanding just remove this comment and the line below and then add
326 * you content.
327 */
328 $navigation->nodetype = navigation_node::NODETYPE_LEAF;
78692f23 329}