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;
43 case FEATURE_SHOW_DESCRIPTION: return true;
50 * Returns all other caps used in module
53 function resource_get_extra_capabilities() {
54 return array('moodle/site:accessallgroups');
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
62 function resource_reset_userdata($data) {
67 * List of view style log actions
70 function resource_get_view_actions() {
71 return array('view','view all');
75 * List of update style log actions
78 function resource_get_post_actions() {
79 return array('update', 'add');
83 * Add resource instance.
85 * @param object $mform
86 * @return int new resource instance id
88 function resource_add_instance($data, $mform) {
90 require_once("$CFG->libdir/resourcelib.php");
91 $cmid = $data->coursemodule;
92 $data->timemodified = time();
93 $displayoptions = array();
94 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
95 $displayoptions['popupwidth'] = $data->popupwidth;
96 $displayoptions['popupheight'] = $data->popupheight;
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);
102 $data->displayoptions = serialize($displayoptions);
104 $data->id = $DB->insert_record('resource', $data);
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));
108 resource_set_mainfile($data);
113 * Update resource instance.
114 * @param object $data
115 * @param object $mform
118 function resource_update_instance($data, $mform) {
120 require_once("$CFG->libdir/resourcelib.php");
121 $data->timemodified = time();
122 $data->id = $data->instance;
125 $displayoptions = array();
126 if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
127 $displayoptions['popupwidth'] = $data->popupwidth;
128 $displayoptions['popupheight'] = $data->popupheight;
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);
134 $data->displayoptions = serialize($displayoptions);
136 $DB->update_record('resource', $data);
137 resource_set_mainfile($data);
142 * Delete resource instance.
146 function resource_delete_instance($id) {
149 if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
153 // note: all context files are deleted automatically
155 $DB->delete_records('resource', array('id'=>$resource->id));
162 * @param object $course
163 * @param object $user
165 * @param object $resource
166 * @return object|null
168 function resource_user_outline($course, $user, $mod, $resource) {
171 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
172 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
174 $numviews = count($logs);
175 $lastlog = array_pop($logs);
177 $result = new stdClass();
178 $result->info = get_string('numviews', '', $numviews);
179 $result->time = $lastlog->time;
187 * Return use complete
188 * @param object $course
189 * @param object $user
191 * @param object $resource
193 function resource_user_complete($course, $user, $mod, $resource) {
196 if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
197 'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
198 $numviews = count($logs);
199 $lastlog = array_pop($logs);
201 $strmostrecently = get_string('mostrecently');
202 $strnumviews = get_string('numviews', '', $numviews);
204 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
207 print_string('neverseen', 'resource');
212 * Returns the users with data in one resource
214 * @todo: deprecated - to be deleted in 2.2
216 * @param int $resourceid
219 function resource_get_participants($resourceid) {
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.
228 * See {@link get_array_of_activities()} in course/lib.php
230 * @param cm_info $coursemodule
231 * @return cached_cm_info info
233 function resource_get_coursemodule_info($coursemodule) {
235 require_once("$CFG->libdir/filelib.php");
236 require_once("$CFG->dirroot/mod/resource/locallib.php");
237 require_once($CFG->libdir.'/completionlib.php');
239 $context = get_context_instance(CONTEXT_MODULE, $coursemodule->id);
241 if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance),
242 'id, name, display, displayoptions, tobemigrated, revision, intro, introformat')) {
246 $info = new cached_cm_info();
247 $info->name = $resource->name;
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);
253 if ($resource->tobemigrated) {
254 $info->icon ='i/cross_red_big';
257 $fs = get_file_storage();
258 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
259 if (count($files) >= 1) {
260 $mainfile = reset($files);
261 $info->icon = file_extension_icon($mainfile->get_filename());
262 $resource->mainfile = $mainfile->get_filename();
265 $display = resource_get_final_display_type($resource);
267 if ($display == RESOURCELIB_DISPLAY_POPUP) {
268 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&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";
273 $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
275 } else if ($display == RESOURCELIB_DISPLAY_NEW) {
276 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&redirect=1";
277 $info->onclick = "window.open('$fullurl'); return false;";
279 } else if ($display == RESOURCELIB_DISPLAY_OPEN) {
280 $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&redirect=1";
281 $info->onclick = "window.location.href ='$fullurl';return false;";
283 } else if ($display == RESOURCELIB_DISPLAY_DOWNLOAD) {
284 if (empty($mainfile)) {
287 // do not open any window because it would be left there after download
288 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$mainfile->get_filepath().$mainfile->get_filename();
289 $fullurl = addslashes_js(file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true));
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
295 if (!$course = $DB->get_record('course', array('id'=>$coursemodule->course), 'id, enablecompletion')) {
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&redirect=1";
302 $info->onclick = "window.open('$fullurl'); return false;";
310 * Lists all browsable file areas
311 * @param object $course
313 * @param object $context
316 function resource_get_file_areas($course, $cm, $context) {
318 $areas['content'] = get_string('resourcecontent', 'resource');
323 * File browsing support for resource module content area.
324 * @param object $browser
325 * @param object $areas
326 * @param object $course
328 * @param object $context
329 * @param string $filearea
331 * @param string $filepath
332 * @param string $filename
333 * @return object file_info instance or null if not found
335 function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
338 if (!has_capability('moodle/course:managefiles', $context)) {
339 // students can not peak here!
343 $fs = get_file_storage();
345 if ($filearea === 'content') {
346 $filepath = is_null($filepath) ? '/' : $filepath;
347 $filename = is_null($filename) ? '.' : $filename;
349 $urlbase = $CFG->wwwroot.'/pluginfile.php';
350 if (!$storedfile = $fs->get_file($context->id, 'mod_resource', 'content', 0, $filepath, $filename)) {
351 if ($filepath === '/' and $filename === '.') {
352 $storedfile = new virtual_root_file($context->id, 'mod_resource', 'content', 0);
358 require_once("$CFG->dirroot/mod/resource/locallib.php");
359 return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
362 // note: resource_intro handled in file_browser automatically
368 * Serves the resource files.
369 * @param object $course
371 * @param object $context
372 * @param string $filearea
374 * @param bool $forcedownload
375 * @return bool false if file not found, does not return if found - just send the file
377 function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
379 require_once("$CFG->libdir/resourcelib.php");
381 if ($context->contextlevel != CONTEXT_MODULE) {
385 require_course_login($course, true, $cm);
386 if (!has_capability('mod/resource:view', $context)) {
390 if ($filearea !== 'content') {
391 // intro is handled automatically in pluginfile.php
395 array_shift($args); // ignore revision - designed to prevent caching problems only
397 $fs = get_file_storage();
398 $relativepath = implode('/', $args);
399 $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/');
401 if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
402 if ($fs->get_file_by_hash(sha1("$fullpath/."))) {
403 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
406 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
409 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
413 $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
414 if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
417 if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) {
420 // file migrate - update flag
421 $resource->legacyfileslast = time();
422 $DB->update_record('resource', $resource);
426 // should we apply filters?
427 $mimetype = $file->get_mimetype();
428 if ($mimetype === 'text/html' or $mimetype === 'text/plain') {
429 $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance));
430 $CFG->embeddedsoforcelinktarget = true;
435 // finally send the file
436 send_stored_file($file, 86400, $filter, $forcedownload);
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
445 function resource_page_type_list($pagetype, $parentcontext, $currentcontext) {
446 $module_pagetype = array('mod-resource-*'=>get_string('page-mod-resource-x', 'resource'));
447 return $module_pagetype;