3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
25 defined('MOODLE_INTERNAL') || die;
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
32 function resource_supports($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;
42 case FEATURE_BACKUP_MOODLE2: return true;
49 * Returns all other caps used in module
52 function resource_get_extra_capabilities() {
53 return array('moodle/site:accessallgroups');
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
61 function resource_reset_userdata($data) {
66 * List of view style log actions
69 function resource_get_view_actions() {
70 return array('view','view all');
74 * List of update style log actions
77 function resource_get_post_actions() {
78 return array('update', 'add');
82 * Add resource instance.
84 * @param object $mform
85 * @return int new resoruce instance id
87 function resource_add_instance($data, $mform) {
89 require_once("$CFG->libdir/resourcelib.php");
90 $cmid = $data->coursemodule;
91 $data->timemodified = time();
92 $displayoptions = array();
93 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
94 $displayoptions['popupwidth'] = $data->popupwidth;
95 $displayoptions['popupheight'] = $data->popupheight;
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);
101 $data->displayoptions = serialize($displayoptions);
103 $data->id = $DB->insert_record('resource', $data);
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));
107 resource_set_mainfile($data);
112 * Update resource instance.
113 * @param object $data
114 * @param object $mform
117 function resource_update_instance($data, $mform) {
119 require_once("$CFG->libdir/resourcelib.php");
120 $data->timemodified = time();
121 $data->id = $data->instance;
124 $displayoptions = array();
125 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
126 $displayoptions['popupwidth'] = $data->popupwidth;
127 $displayoptions['popupheight'] = $data->popupheight;
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);
133 $data->displayoptions = serialize($displayoptions);
135 $DB->update_record('resource', $data);
136 resource_set_mainfile($data);
141 * Delete resource instance.
145 function resource_delete_instance($id) {
148 if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
152 // note: all context files are deleted automatically
154 $DB->delete_records('resource', array('id'=>$resource->id));
161 * @param object $course
162 * @param object $user
164 * @param object $resource
165 * @return object|null
167 function resource_user_outline($course, $user, $mod, $resource) {
170 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
171 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
173 $numviews = count($logs);
174 $lastlog = array_pop($logs);
176 $result = new stdClass();
177 $result->info = get_string('numviews', '', $numviews);
178 $result->time = $lastlog->time;
186 * Return use complete
187 * @param object $course
188 * @param object $user
190 * @param object $resource
192 function resource_user_complete($course, $user, $mod, $resource) {
195 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
196 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
197 $numviews = count($logs);
198 $lastlog = array_pop($logs);
200 $strmostrecently = get_string('mostrecently');
201 $strnumviews = get_string('numviews', '', $numviews);
203 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
206 print_string('neverseen', 'resource');
211 * Returns the users with data in one resource
213 * @param int $resourceid
216 function resource_get_participants($resourceid) {
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.
225 * See {@link get_array_of_activities()} in course/lib.php
227 * @param object $coursemodule
228 * @return object info
230 function resource_get_coursemodule_info($coursemodule) {
232 require_once("$CFG->libdir/filelib.php");
233 require_once("$CFG->dirroot/mod/resource/locallib.php");
234 $context = get_context_instance(CONTEXT_MODULE, $coursemodule->id);
236 if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance), 'id, name, display, displayoptions, tobemigrated, revision')) {
240 $info = new stdClass();
241 $info->name = $resource->name;
243 if ($resource->tobemigrated) {
244 $info->icon ='i/cross_red_big';
247 $fs = get_file_storage();
248 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder');
249 if (count($files) >= 1) {
250 $mainfile = array_pop($files);
251 $info->icon = file_extension_icon($mainfile->get_filename());
252 $resource->mainfile = $mainfile->get_filename();
255 $display = resource_get_final_display_type($resource);
257 if ($display == RESOURCELIB_DISPLAY_POPUP) {
258 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&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";
263 $info->extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
265 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
266 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&redirect=1";
267 $info->extra = "onclick=\"window.open('$fullurl'); return false;\"";
269 } else if ($display == RESOURCELIB_DISPLAY_OPEN) {
270 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&redirect=1";
271 $info->extra = "onclick=\"window.location.href ='$fullurl';return false;\"";
273 } else if ($display == RESOURCELIB_DISPLAY_DOWNLOAD) {
274 if (empty($mainfile)) {
277 // do not open any window because it would be left there after download
278 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$mainfile->get_filepath().$mainfile->get_filename();
279 $fullurl = addslashes_js(file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true));
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
285 if (!$course = $DB->get_record('course', array('id'=>$coursemodule->course), 'id, enablecompletion')) {
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&redirect=1";
292 $info->extra = "onclick=\"window.open('$fullurl'); return false;\"";
300 * Lists all browsable file areas
301 * @param object $course
303 * @param object $context
306 function resource_get_file_areas($course, $cm, $context) {
308 $areas['content'] = get_string('resourcecontent', 'resource');
313 * File browsing support for resource module content area.
314 * @param object $browser
315 * @param object $areas
316 * @param object $course
318 * @param object $context
319 * @param string $filearea
321 * @param string $filepath
322 * @param string $filename
323 * @return object file_info instance or null if not found
325 function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
328 if (!has_capability('moodle/course:managefiles', $context)) {
329 // students can not peak here!
333 $fs = get_file_storage();
335 if ($filearea === 'content') {
336 $filepath = is_null($filepath) ? '/' : $filepath;
337 $filename = is_null($filename) ? '.' : $filename;
339 $urlbase = $CFG->wwwroot.'/pluginfile.php';
340 if (!$storedfile = $fs->get_file($context->id, 'mod_resource', 'content', 0, $filepath, $filename)) {
341 if ($filepath === '/' and $filename === '.') {
342 $storedfile = new virtual_root_file($context->id, 'mod_resource', 'content', 0);
348 require_once("$CFG->dirroot/mod/resource/locallib.php");
349 return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
352 // note: resource_intro handled in file_browser automatically
358 * Serves the resource files.
359 * @param object $course
361 * @param object $context
362 * @param string $filearea
364 * @param bool $forcedownload
365 * @return bool false if file not found, does not return if found - just send the file
367 function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
369 require_once("$CFG->libdir/resourcelib.php");
371 if ($context->contextlevel != CONTEXT_MODULE) {
375 require_course_login($course, true, $cm);
376 if (!has_capability('mod/resource:view', $context)) {
380 if ($filearea !== 'content') {
381 // intro is handled automatically in pluginfile.php
385 array_shift($args); // ignore revision - designed to prevent caching problems only
387 $fs = get_file_storage();
388 $relativepath = implode('/', $args);
389 $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/');
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"))) {
396 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
399 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
403 $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
404 if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
407 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) {
410 // file migrate - update flag
411 $resource->legacyfileslast = time();
412 $DB->update_record('resource', $resource);
416 // should we apply filters?
417 $mimetype = $file->get_mimetype();
418 if ($mimetype === 'text/html' or $mimetype === 'text/plain') {
419 $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance));
420 $CFG->embeddedsoforcelinktarget = true;
425 // finally send the file
426 send_stored_file($file, 86400, $filter, $forcedownload);