adding support for directory links in file resource
[moodle.git] / mod / resource / lib.php
CommitLineData
28f672b2 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/>.
2a439ba7 17
28f672b2 18/**
702ab58c
PS
19 * @package mod
20 * @subpackage resource
21 * @copyright 2009 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28f672b2 23 */
24
702ab58c
PS
25defined('MOODLE_INTERNAL') || die;
26
aa54ed7b 27/**
28 * List of features supported in Resource module
29 * @param string $feature FEATURE_xx constant for requested feature
30 * @return mixed True if module supports feature, false if not, null if doesn't know
31 */
32function resource_supports($feature) {
33 switch($feature) {
34 case FEATURE_MOD_ARCHETYPE: return MOD_ARCHETYPE_RESOURCE;
35 case FEATURE_GROUPS: return false;
36 case FEATURE_GROUPINGS: return false;
37 case FEATURE_GROUPMEMBERSONLY: return true;
38 case FEATURE_MOD_INTRO: return true;
39 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
40 case FEATURE_GRADE_HAS_GRADE: return false;
41 case FEATURE_GRADE_OUTCOMES: return false;
a345de6e 42 case FEATURE_BACKUP_MOODLE2: return true;
214b1cf7 43
aa54ed7b 44 default: return null;
45 }
46}
713d78ea 47
28f672b2 48/**
aa54ed7b 49 * Returns all other caps used in module
50 * @return array
28f672b2 51 */
aa54ed7b 52function resource_get_extra_capabilities() {
53 return array('moodle/site:accessallgroups');
54}
3d30a455 55
d18830fe 56/**
aa54ed7b 57 * This function is used by the reset_course_userdata function in moodlelib.
58 * @param $data the data submitted from the reset course.
59 * @return array status array
28f672b2 60 */
aa54ed7b 61function resource_reset_userdata($data) {
62 return array();
63}
65634a81 64
aa54ed7b 65/**
66 * List of view style log actions
67 * @return array
68 */
69function resource_get_view_actions() {
70 return array('view','view all');
71}
65634a81 72
aa54ed7b 73/**
74 * List of update style log actions
75 * @return array
76 */
77function resource_get_post_actions() {
78 return array('update', 'add');
79}
65634a81 80
aa54ed7b 81/**
82 * Add resource instance.
83 * @param object $data
84 * @param object $mform
85 * @return int new resoruce instance id
86 */
87function resource_add_instance($data, $mform) {
88 global $CFG, $DB;
89 require_once("$CFG->libdir/resourcelib.php");
f79321f1 90 $cmid = $data->coursemodule;
aa54ed7b 91 $data->timemodified = time();
92 $displayoptions = array();
93 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
94 $displayoptions['popupwidth'] = $data->popupwidth;
95 $displayoptions['popupheight'] = $data->popupheight;
65634a81 96 }
aa54ed7b 97 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
98 $displayoptions['printheading'] = (int)!empty($data->printheading);
99 $displayoptions['printintro'] = (int)!empty($data->printintro);
ec81373f 100 }
aa54ed7b 101 $data->displayoptions = serialize($displayoptions);
d18830fe 102
aa54ed7b 103 $data->id = $DB->insert_record('resource', $data);
d18830fe 104
aa54ed7b 105 // we need to use context now, so we need to make sure all needed info is already in db
106 $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
f79321f1 107 resource_set_mainfile($data);
aa54ed7b 108 return $data->id;
109}
cccb016a 110
aa54ed7b 111/**
112 * Update resource instance.
113 * @param object $data
114 * @param object $mform
115 * @return bool true
116 */
117function resource_update_instance($data, $mform) {
118 global $CFG, $DB;
119 require_once("$CFG->libdir/resourcelib.php");
aa54ed7b 120 $data->timemodified = time();
121 $data->id = $data->instance;
122 $data->revision++;
2a439ba7 123
aa54ed7b 124 $displayoptions = array();
125 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
126 $displayoptions['popupwidth'] = $data->popupwidth;
127 $displayoptions['popupheight'] = $data->popupheight;
65634a81 128 }
aa54ed7b 129 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
130 $displayoptions['printheading'] = (int)!empty($data->printheading);
131 $displayoptions['printintro'] = (int)!empty($data->printintro);
3efe78df 132 }
aa54ed7b 133 $data->displayoptions = serialize($displayoptions);
3efe78df 134
aa54ed7b 135 $DB->update_record('resource', $data);
f79321f1 136 resource_set_mainfile($data);
aa54ed7b 137 return true;
d18830fe 138}
139
28f672b2 140/**
aa54ed7b 141 * Delete resource instance.
28f672b2 142 * @param int $id
aa54ed7b 143 * @return bool true
28f672b2 144 */
d18830fe 145function resource_delete_instance($id) {
aa54ed7b 146 global $DB;
ec81373f 147
aa54ed7b 148 if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
d18830fe 149 return false;
150 }
79035d46 151
aa54ed7b 152 // note: all context files are deleted automatically
ec81373f 153
aa54ed7b 154 $DB->delete_records('resource', array('id'=>$resource->id));
d18830fe 155
aa54ed7b 156 return true;
d18830fe 157}
158
28f672b2 159/**
aa54ed7b 160 * Return use outline
28f672b2 161 * @param object $course
162 * @param object $user
163 * @param object $mod
164 * @param object $resource
165 * @return object|null
166 */
2a439ba7 167function resource_user_outline($course, $user, $mod, $resource) {
5f5cd33c 168 global $DB;
169
aa54ed7b 170 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
171 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
2a439ba7 172
173 $numviews = count($logs);
174 $lastlog = array_pop($logs);
175
39790bd8 176 $result = new stdClass();
aa54ed7b 177 $result->info = get_string('numviews', '', $numviews);
2a439ba7 178 $result->time = $lastlog->time;
179
180 return $result;
181 }
182 return NULL;
183}
184
28f672b2 185/**
aa54ed7b 186 * Return use complete
28f672b2 187 * @param object $course
188 * @param object $user
189 * @param object $mod
190 * @param object $resource
191 */
2a439ba7 192function resource_user_complete($course, $user, $mod, $resource) {
5f5cd33c 193 global $CFG, $DB;
2a439ba7 194
aa54ed7b 195 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
196 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
2a439ba7 197 $numviews = count($logs);
198 $lastlog = array_pop($logs);
199
aa54ed7b 200 $strmostrecently = get_string('mostrecently');
201 $strnumviews = get_string('numviews', '', $numviews);
2a439ba7 202
203 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
204
205 } else {
aa54ed7b 206 print_string('neverseen', 'resource');
2a439ba7 207 }
208}
209
28f672b2 210/**
211 * Returns the users with data in one resource
28f672b2 212 *
213 * @param int $resourceid
214 * @return bool false
215 */
84caf038 216function resource_get_participants($resourceid) {
84caf038 217 return false;
218}
2a439ba7 219
28f672b2 220/**
221 * Given a course_module object, this function returns any
222 * "extra" information that may be needed when printing
223 * this activity in a course listing.
224 *
225 * See {@link get_array_of_activities()} in course/lib.php
226 *
28f672b2 227 * @param object $coursemodule
228 * @return object info
229 */
8dddba42 230function resource_get_coursemodule_info($coursemodule) {
aa54ed7b 231 global $CFG, $DB;
232 require_once("$CFG->libdir/filelib.php");
233 require_once("$CFG->dirroot/mod/resource/locallib.php");
f79321f1 234 $context = get_context_instance(CONTEXT_MODULE, $coursemodule->id);
ec81373f 235
f79321f1 236 if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance), 'id, name, display, displayoptions, tobemigrated, revision')) {
aa54ed7b 237 return NULL;
af65e103 238 }
ec81373f 239
39790bd8 240 $info = new stdClass();
aa54ed7b 241 $info->name = $resource->name;
8dddba42 242
aa54ed7b 243 if ($resource->tobemigrated) {
244 $info->icon ='i/cross_red_big';
245 return $info;
af65e103 246 }
f79321f1 247 $fs = get_file_storage();
64f93798 248 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder');
f79321f1
DC
249 if (count($files) >= 1) {
250 $mainfile = array_pop($files);
ede72522 251 $info->icon = file_extension_icon($mainfile->get_filename());
f79321f1
DC
252 $resource->mainfile = $mainfile->get_filename();
253 }
d18830fe 254
aa54ed7b 255 $display = resource_get_final_display_type($resource);
9f741612 256
aa54ed7b 257 if ($display == RESOURCELIB_DISPLAY_POPUP) {
258 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
259 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
260 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
261 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
262 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
9a9012dc 263 $info->extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
6da4b261 264
aa54ed7b 265 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
266 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
9a9012dc 267 $info->extra = "onclick=\"window.open('$fullurl'); return false;\"";
6da4b261 268
aa54ed7b 269 } else if ($display == RESOURCELIB_DISPLAY_OPEN) {
270 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
9a9012dc 271 $info->extra = "onclick=\"window.location.href ='$fullurl';return false;\"";
aa54ed7b 272
273 } else if ($display == RESOURCELIB_DISPLAY_DOWNLOAD) {
f79321f1
DC
274 if (empty($mainfile)) {
275 return NULL;
276 }
aa54ed7b 277 // do not open any window because it would be left there after download
64f93798 278 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$mainfile->get_filepath().$mainfile->get_filename();
aa54ed7b 279 $fullurl = addslashes_js(file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true));
c15a60e6
SM
280
281 // When completion information is enabled for download files, make
282 // the JavaScript version go to the view page with redirect set,
283 // instead of directly to the file, otherwise we can't make it tick
284 // the box for them
285 if (!$course = $DB->get_record('course', array('id'=>$coursemodule->course), 'id, enablecompletion')) {
286 return NULL;
287 }
288 $completion = new completion_info($course);
289 if ($completion->is_enabled($coursemodule) == COMPLETION_TRACKING_AUTOMATIC) {
290 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
291 }
9a9012dc 292 $info->extra = "onclick=\"window.open('$fullurl'); return false;\"";
6da4b261 293 }
89bfeee0 294
aa54ed7b 295 return $info;
6da4b261 296}
f3221af9 297
f3221af9 298
28f672b2 299/**
aa54ed7b 300 * Lists all browsable file areas
301 * @param object $course
302 * @param object $cm
303 * @param object $context
28f672b2 304 * @return array
305 */
aa54ed7b 306function resource_get_file_areas($course, $cm, $context) {
307 $areas = array();
64f93798 308 $areas['content'] = get_string('resourcecontent', 'resource');
aa54ed7b 309 return $areas;
f3221af9 310}
311
28f672b2 312/**
f1b8bcf7 313 * File browsing support for resource module content area.
aa54ed7b 314 * @param object $browser
315 * @param object $areas
28f672b2 316 * @param object $course
aa54ed7b 317 * @param object $cm
318 * @param object $context
319 * @param string $filearea
320 * @param int $itemid
321 * @param string $filepath
322 * @param string $filename
323 * @return object file_info instance or null if not found
28f672b2 324 */
aa54ed7b 325function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
326 global $CFG;
a69be0d8 327
64f93798
PS
328 if (!has_capability('moodle/course:managefiles', $context)) {
329 // students can not peak here!
330 return null;
331 }
a69be0d8 332
aa54ed7b 333 $fs = get_file_storage();
a69be0d8 334
64f93798 335 if ($filearea === 'content') {
aa54ed7b 336 $filepath = is_null($filepath) ? '/' : $filepath;
337 $filename = is_null($filename) ? '.' : $filename;
a69be0d8 338
aa54ed7b 339 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 340 if (!$storedfile = $fs->get_file($context->id, 'mod_resource', 'content', 0, $filepath, $filename)) {
aa54ed7b 341 if ($filepath === '/' and $filename === '.') {
64f93798 342 $storedfile = new virtual_root_file($context->id, 'mod_resource', 'content', 0);
a69be0d8 343 } else {
aa54ed7b 344 // not found
345 return null;
a69be0d8 346 }
347 }
aa54ed7b 348 require_once("$CFG->dirroot/mod/resource/locallib.php");
64f93798 349 return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
a69be0d8 350 }
351
aa54ed7b 352 // note: resource_intro handled in file_browser automatically
a69be0d8 353
aa54ed7b 354 return null;
a69be0d8 355}
356
0b5a80a1 357/**
aa54ed7b 358 * Serves the resource files.
359 * @param object $course
64f93798 360 * @param object $cm
aa54ed7b 361 * @param object $context
362 * @param string $filearea
363 * @param array $args
364 * @param bool $forcedownload
b4ff85aa 365 * @return bool false if file not found, does not return if found - just send the file
28f672b2 366 */
64f93798 367function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
aa54ed7b 368 global $CFG, $DB;
369 require_once("$CFG->libdir/resourcelib.php");
0d06b6fd 370
64f93798 371 if ($context->contextlevel != CONTEXT_MODULE) {
aa54ed7b 372 return false;
3efe78df 373 }
374
64f93798 375 require_course_login($course, true, $cm);
ce459060
PS
376 if (!has_capability('mod/resource:view', $context)) {
377 return false;
378 }
3efe78df 379
64f93798
PS
380 if ($filearea !== 'content') {
381 // intro is handled automatically in pluginfile.php
aa54ed7b 382 return false;
3efe78df 383 }
384
aa54ed7b 385 array_shift($args); // ignore revision - designed to prevent caching problems only
3efe78df 386
aa54ed7b 387 $fs = get_file_storage();
64f93798 388 $relativepath = implode('/', $args);
fe3c5dae
PS
389 $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/');
390 do {
391 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
392 if ($file = $fs->get_file_by_hash(sha1("$fullpath/.")) and $file->is_directory()) {
393 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
394 break;
395 }
396 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
397 break;
398 }
399 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
400 break;
401 }
402 }
403 $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
404 if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
405 return false;
406 }
407 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) {
408 return false;
409 }
410 // file migrate - update flag
411 $resource->legacyfileslast = time();
412 $DB->update_record('resource', $resource);
0d06b6fd 413 }
fe3c5dae 414 } while (false);
ffcfd8a7 415
aa54ed7b 416 // should we apply filters?
417 $mimetype = $file->get_mimetype();
b4ff85aa 418 if ($mimetype === 'text/html' or $mimetype === 'text/plain') {
64f93798 419 $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance));
4eaa964f 420 $CFG->embeddedsoforcelinktarget = true;
aa54ed7b 421 } else {
422 $filter = 0;
18a2a0cb 423 }
18a2a0cb 424
aa54ed7b 425 // finally send the file
426 send_stored_file($file, 86400, $filter, $forcedownload);
13ca1e06 427}