weekly release 2.3dev
[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();
a2c57666 93
94 resource_set_display_options($data);
d18830fe 95
aa54ed7b 96 $data->id = $DB->insert_record('resource', $data);
d18830fe 97
aa54ed7b 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));
f79321f1 100 resource_set_mainfile($data);
aa54ed7b 101 return $data->id;
102}
cccb016a 103
aa54ed7b 104/**
105 * Update resource instance.
106 * @param object $data
107 * @param object $mform
108 * @return bool true
109 */
110function resource_update_instance($data, $mform) {
111 global $CFG, $DB;
112 require_once("$CFG->libdir/resourcelib.php");
aa54ed7b 113 $data->timemodified = time();
114 $data->id = $data->instance;
115 $data->revision++;
2a439ba7 116
a2c57666 117 resource_set_display_options($data);
118
119 $DB->update_record('resource', $data);
120 resource_set_mainfile($data);
121 return true;
122}
123
124/**
125 * Updates display options based on form input.
126 *
127 * Shared code used by resource_add_instance and resource_update_instance.
128 *
129 * @param object $data Data object
130 */
131function resource_set_display_options($data) {
aa54ed7b 132 $displayoptions = array();
133 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
134 $displayoptions['popupwidth'] = $data->popupwidth;
135 $displayoptions['popupheight'] = $data->popupheight;
65634a81 136 }
aa54ed7b 137 if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
138 $displayoptions['printheading'] = (int)!empty($data->printheading);
139 $displayoptions['printintro'] = (int)!empty($data->printintro);
3efe78df 140 }
a2c57666 141 if (!empty($data->showsize)) {
142 $displayoptions['showsize'] = 1;
143 }
144 if (!empty($data->showtype)) {
145 $displayoptions['showtype'] = 1;
146 }
aa54ed7b 147 $data->displayoptions = serialize($displayoptions);
d18830fe 148}
149
28f672b2 150/**
aa54ed7b 151 * Delete resource instance.
28f672b2 152 * @param int $id
aa54ed7b 153 * @return bool true
28f672b2 154 */
d18830fe 155function resource_delete_instance($id) {
aa54ed7b 156 global $DB;
ec81373f 157
aa54ed7b 158 if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
d18830fe 159 return false;
160 }
79035d46 161
aa54ed7b 162 // note: all context files are deleted automatically
ec81373f 163
aa54ed7b 164 $DB->delete_records('resource', array('id'=>$resource->id));
d18830fe 165
aa54ed7b 166 return true;
d18830fe 167}
168
28f672b2 169/**
aa54ed7b 170 * Return use outline
28f672b2 171 * @param object $course
172 * @param object $user
173 * @param object $mod
174 * @param object $resource
175 * @return object|null
176 */
2a439ba7 177function resource_user_outline($course, $user, $mod, $resource) {
5f5cd33c 178 global $DB;
179
aa54ed7b 180 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
181 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
2a439ba7 182
183 $numviews = count($logs);
184 $lastlog = array_pop($logs);
185
39790bd8 186 $result = new stdClass();
aa54ed7b 187 $result->info = get_string('numviews', '', $numviews);
2a439ba7 188 $result->time = $lastlog->time;
189
190 return $result;
191 }
192 return NULL;
193}
194
28f672b2 195/**
aa54ed7b 196 * Return use complete
28f672b2 197 * @param object $course
198 * @param object $user
199 * @param object $mod
200 * @param object $resource
201 */
2a439ba7 202function resource_user_complete($course, $user, $mod, $resource) {
5f5cd33c 203 global $CFG, $DB;
2a439ba7 204
aa54ed7b 205 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
206 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
2a439ba7 207 $numviews = count($logs);
208 $lastlog = array_pop($logs);
209
aa54ed7b 210 $strmostrecently = get_string('mostrecently');
211 $strnumviews = get_string('numviews', '', $numviews);
2a439ba7 212
213 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
214
215 } else {
aa54ed7b 216 print_string('neverseen', 'resource');
2a439ba7 217 }
218}
219
28f672b2 220/**
221 * Returns the users with data in one resource
28f672b2 222 *
2b04c41c
SH
223 * @todo: deprecated - to be deleted in 2.2
224 *
28f672b2 225 * @param int $resourceid
226 * @return bool false
227 */
84caf038 228function resource_get_participants($resourceid) {
84caf038 229 return false;
230}
2a439ba7 231
28f672b2 232/**
233 * Given a course_module object, this function returns any
234 * "extra" information that may be needed when printing
235 * this activity in a course listing.
236 *
237 * See {@link get_array_of_activities()} in course/lib.php
238 *
3e4c2435 239 * @param cm_info $coursemodule
240 * @return cached_cm_info info
28f672b2 241 */
8dddba42 242function resource_get_coursemodule_info($coursemodule) {
aa54ed7b 243 global $CFG, $DB;
244 require_once("$CFG->libdir/filelib.php");
245 require_once("$CFG->dirroot/mod/resource/locallib.php");
516c5eca
PS
246 require_once($CFG->libdir.'/completionlib.php');
247
f79321f1 248 $context = get_context_instance(CONTEXT_MODULE, $coursemodule->id);
ec81373f 249
3e4c2435 250 if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance),
251 'id, name, display, displayoptions, tobemigrated, revision, intro, introformat')) {
aa54ed7b 252 return NULL;
af65e103 253 }
ec81373f 254
3e4c2435 255 $info = new cached_cm_info();
aa54ed7b 256 $info->name = $resource->name;
3e4c2435 257 if ($coursemodule->showdescription) {
258 // Convert intro to html. Do not filter cached version, filters run at display time.
259 $info->content = format_module_intro('resource', $resource, $coursemodule->id, false);
260 }
8dddba42 261
aa54ed7b 262 if ($resource->tobemigrated) {
263 $info->icon ='i/cross_red_big';
264 return $info;
af65e103 265 }
f79321f1 266 $fs = get_file_storage();
3d8f1d3a 267 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
f79321f1 268 if (count($files) >= 1) {
020088d6 269 $mainfile = reset($files);
ede72522 270 $info->icon = file_extension_icon($mainfile->get_filename());
f79321f1
DC
271 $resource->mainfile = $mainfile->get_filename();
272 }
d18830fe 273
aa54ed7b 274 $display = resource_get_final_display_type($resource);
9f741612 275
aa54ed7b 276 if ($display == RESOURCELIB_DISPLAY_POPUP) {
277 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
278 $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
279 $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
280 $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
281 $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
3e4c2435 282 $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
6da4b261 283
aa54ed7b 284 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
285 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
3e4c2435 286 $info->onclick = "window.open('$fullurl'); return false;";
6da4b261 287
6da4b261 288 }
89bfeee0 289
a2c57666 290 // If any optional extra details are turned on, store in custom data
291 $info->customdata = resource_get_optional_details($resource, $coursemodule);
292
aa54ed7b 293 return $info;
6da4b261 294}
f3221af9 295
a2c57666 296/**
297 * Called when viewing course page. Shows extra details after the link if
298 * enabled.
299 *
300 * @param cm_info $cm Course module information
301 */
302function resource_cm_info_view(cm_info $cm) {
303 $details = $cm->get_custom_data();
304 if ($details) {
305 $cm->set_after_link(' ' . html_writer::tag('span', $details,
306 array('class' => 'resourcelinkdetails')));
307 }
308}
f3221af9 309
28f672b2 310/**
aa54ed7b 311 * Lists all browsable file areas
312 * @param object $course
313 * @param object $cm
314 * @param object $context
28f672b2 315 * @return array
316 */
aa54ed7b 317function resource_get_file_areas($course, $cm, $context) {
318 $areas = array();
64f93798 319 $areas['content'] = get_string('resourcecontent', 'resource');
aa54ed7b 320 return $areas;
f3221af9 321}
322
28f672b2 323/**
f1b8bcf7 324 * File browsing support for resource module content area.
aa54ed7b 325 * @param object $browser
326 * @param object $areas
28f672b2 327 * @param object $course
aa54ed7b 328 * @param object $cm
329 * @param object $context
330 * @param string $filearea
331 * @param int $itemid
332 * @param string $filepath
333 * @param string $filename
334 * @return object file_info instance or null if not found
28f672b2 335 */
aa54ed7b 336function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
337 global $CFG;
a69be0d8 338
64f93798
PS
339 if (!has_capability('moodle/course:managefiles', $context)) {
340 // students can not peak here!
341 return null;
342 }
a69be0d8 343
aa54ed7b 344 $fs = get_file_storage();
a69be0d8 345
64f93798 346 if ($filearea === 'content') {
aa54ed7b 347 $filepath = is_null($filepath) ? '/' : $filepath;
348 $filename = is_null($filename) ? '.' : $filename;
a69be0d8 349
aa54ed7b 350 $urlbase = $CFG->wwwroot.'/pluginfile.php';
64f93798 351 if (!$storedfile = $fs->get_file($context->id, 'mod_resource', 'content', 0, $filepath, $filename)) {
aa54ed7b 352 if ($filepath === '/' and $filename === '.') {
64f93798 353 $storedfile = new virtual_root_file($context->id, 'mod_resource', 'content', 0);
a69be0d8 354 } else {
aa54ed7b 355 // not found
356 return null;
a69be0d8 357 }
358 }
aa54ed7b 359 require_once("$CFG->dirroot/mod/resource/locallib.php");
64f93798 360 return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
a69be0d8 361 }
362
aa54ed7b 363 // note: resource_intro handled in file_browser automatically
a69be0d8 364
aa54ed7b 365 return null;
a69be0d8 366}
367
0b5a80a1 368/**
aa54ed7b 369 * Serves the resource files.
370 * @param object $course
64f93798 371 * @param object $cm
aa54ed7b 372 * @param object $context
373 * @param string $filearea
374 * @param array $args
375 * @param bool $forcedownload
b4ff85aa 376 * @return bool false if file not found, does not return if found - just send the file
28f672b2 377 */
64f93798 378function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
aa54ed7b 379 global $CFG, $DB;
380 require_once("$CFG->libdir/resourcelib.php");
0d06b6fd 381
64f93798 382 if ($context->contextlevel != CONTEXT_MODULE) {
aa54ed7b 383 return false;
3efe78df 384 }
385
64f93798 386 require_course_login($course, true, $cm);
ce459060
PS
387 if (!has_capability('mod/resource:view', $context)) {
388 return false;
389 }
3efe78df 390
64f93798
PS
391 if ($filearea !== 'content') {
392 // intro is handled automatically in pluginfile.php
aa54ed7b 393 return false;
3efe78df 394 }
395
aa54ed7b 396 array_shift($args); // ignore revision - designed to prevent caching problems only
3efe78df 397
aa54ed7b 398 $fs = get_file_storage();
64f93798 399 $relativepath = implode('/', $args);
fe3c5dae
PS
400 $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/');
401 do {
402 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
9815ccee 403 if ($fs->get_file_by_hash(sha1("$fullpath/."))) {
fe3c5dae
PS
404 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
405 break;
406 }
407 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
408 break;
409 }
410 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
411 break;
412 }
413 }
414 $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
415 if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
416 return false;
417 }
418 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) {
419 return false;
420 }
421 // file migrate - update flag
422 $resource->legacyfileslast = time();
423 $DB->update_record('resource', $resource);
0d06b6fd 424 }
fe3c5dae 425 } while (false);
ffcfd8a7 426
aa54ed7b 427 // should we apply filters?
428 $mimetype = $file->get_mimetype();
b4ff85aa 429 if ($mimetype === 'text/html' or $mimetype === 'text/plain') {
64f93798 430 $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance));
4eaa964f 431 $CFG->embeddedsoforcelinktarget = true;
aa54ed7b 432 } else {
433 $filter = 0;
18a2a0cb 434 }
18a2a0cb 435
aa54ed7b 436 // finally send the file
437 send_stored_file($file, 86400, $filter, $forcedownload);
13ca1e06 438}
b1627a92
DC
439
440/**
441 * Return a list of page types
442 * @param string $pagetype current page type
443 * @param stdClass $parentcontext Block's parent context
444 * @param stdClass $currentcontext Current context of block
445 */
b38e2e28 446function resource_page_type_list($pagetype, $parentcontext, $currentcontext) {
b1627a92
DC
447 $module_pagetype = array('mod-resource-*'=>get_string('page-mod-resource-x', 'resource'));
448 return $module_pagetype;
449}
ec0d6ea2
DC
450
451/**
452 * Export file resource contents
453 *
454 * @return array of file content
455 */
456function resource_export_contents($cm, $baseurl) {
457 global $CFG, $DB;
458 $contents = array();
459 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
460 $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
461
462 $fs = get_file_storage();
463 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
464
465 foreach ($files as $fileinfo) {
466 $file = array();
467 $file['type'] = 'file';
468 $file['filename'] = $fileinfo->get_filename();
469 $file['filepath'] = $fileinfo->get_filepath();
470 $file['filesize'] = $fileinfo->get_filesize();
471 $file['fileurl'] = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_resource/content/'.$resource->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
472 $file['timecreated'] = $fileinfo->get_timecreated();
473 $file['timemodified'] = $fileinfo->get_timemodified();
474 $file['sortorder'] = $fileinfo->get_sortorder();
475 $file['userid'] = $fileinfo->get_userid();
476 $file['author'] = $fileinfo->get_author();
477 $file['license'] = $fileinfo->get_license();
478 $contents[] = $file;
479 }
480
481 return $contents;
482}