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/>.
19 * Resource module version information
22 * @subpackage resource
23 * @copyright 2009 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../../config.php');
28 require_once($CFG->dirroot.'/mod/resource/locallib.php');
29 require_once($CFG->libdir.'/completionlib.php');
31 $id = optional_param('id', 0, PARAM_INT); // Course Module ID
32 $r = optional_param('r', 0, PARAM_INT); // Resource instance ID
33 $redirect = optional_param('redirect', 0, PARAM_BOOL);
36 if (!$resource = $DB->get_record('resource', array('id'=>$r))) {
37 resource_redirect_if_migrated($r, 0);
38 print_error('invalidaccessparameter');
40 $cm = get_coursemodule_from_instance('resource', $resource->id, $resource->course, false, MUST_EXIST);
43 if (!$cm = get_coursemodule_from_id('resource', $id)) {
44 resource_redirect_if_migrated(0, $id);
45 print_error('invalidcoursemodule');
47 $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
50 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
52 require_course_login($course, true, $cm);
53 $context = context_module::instance($cm->id);
54 require_capability('mod/resource:view', $context);
57 'context' => $context,
58 'objectid' => $resource->id
60 $event = \mod_resource\event\course_module_viewed::create($params);
61 $event->add_record_snapshot('resource', $resource);
64 // Update 'viewed' state if required by completion system
65 $completion = new completion_info($course);
66 $completion->set_module_viewed($cm);
68 $PAGE->set_url('/mod/resource/view.php', array('id' => $cm->id));
70 if ($resource->tobemigrated) {
71 resource_print_tobemigrated($resource, $cm, $course);
75 $fs = get_file_storage();
76 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
77 if (count($files) < 1) {
78 resource_print_filenotfound($resource, $cm, $course);
81 $file = reset($files);
85 $resource->mainfile = $file->get_filename();
86 $displaytype = resource_get_final_display_type($resource);
87 if ($displaytype == RESOURCELIB_DISPLAY_OPEN || $displaytype == RESOURCELIB_DISPLAY_DOWNLOAD) {
88 // For 'open' and 'download' links, we always redirect to the content - except
89 // if the user just chose 'save and display' from the form then that would be
91 if (!isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], 'modedit.php') === false) {
97 // coming from course page or url index page
98 // this redirect trick solves caching problems when tracking views ;-)
99 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
100 $fullurl = moodle_url::make_file_url('/pluginfile.php', $path, $displaytype == RESOURCELIB_DISPLAY_DOWNLOAD);
104 switch ($displaytype) {
105 case RESOURCELIB_DISPLAY_EMBED:
106 resource_display_embed($resource, $cm, $course, $file);
108 case RESOURCELIB_DISPLAY_FRAME:
109 resource_display_frame($resource, $cm, $course, $file);
112 resource_print_workaround($resource, $cm, $course, $file);