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