Merge branch 'MDL-25708' of git://github.com/stronk7/moodle
[moodle.git] / mod / resource / view.php
CommitLineData
aa54ed7b 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/>.
17
18/**
19 * Resource module version information
20 *
702ab58c
PS
21 * @package mod
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
aa54ed7b 25 */
26
27require('../../config.php');
28require_once($CFG->dirroot.'/mod/resource/locallib.php');
516c5eca 29require_once($CFG->libdir.'/completionlib.php');
aa54ed7b 30
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);
34
35if ($r) {
36 if (!$resource = $DB->get_record('resource', array('id'=>$r))) {
37 resource_redirect_if_migrated($r, 0);
baa336f0 38 print_error('invalidaccessparameter');
2a439ba7 39 }
aa54ed7b 40 $cm = get_coursemodule_from_instance('resource', $resource->id, $resource->course, false, MUST_EXIST);
2a439ba7 41
aa54ed7b 42} else {
43 if (!$cm = get_coursemodule_from_id('resource', $id)) {
44 resource_redirect_if_migrated(0, $id);
45 print_error('invalidcoursemodule');
d3787db0 46 }
aa54ed7b 47 $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
48}
49
50$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
51
52require_course_login($course, true, $cm);
53$context = get_context_instance(CONTEXT_MODULE, $cm->id);
ce459060 54require_capability('mod/resource:view', $context);
aa54ed7b 55
56add_to_log($course->id, 'resource', 'view', 'view.php?id='.$cm->id, $resource->id, $cm->id);
57
c15a60e6
SM
58// Update 'viewed' state if required by completion system
59$completion = new completion_info($course);
60$completion->set_module_viewed($cm);
61
a6855934 62$PAGE->set_url('/mod/resource/view.php', array('id' => $cm->id));
aa54ed7b 63
64if ($resource->tobemigrated) {
65 resource_print_tobemigrated($resource, $cm, $course);
66 die;
67}
d3787db0 68
aa54ed7b 69$fs = get_file_storage();
64f93798 70$files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder');
f79321f1 71if (count($files) < 1) {
aa54ed7b 72 resource_print_filenotfound($resource, $cm, $course);
73 die;
f79321f1
DC
74} else {
75 $file = array_pop($files);
aa54ed7b 76}
9f741612 77
aa54ed7b 78if ($redirect) {
79 // coming from course page or url index page
80 // this redirect trick solves caching problems when tracking views ;-)
64f93798 81 $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
aa54ed7b 82 $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
83 redirect($fullurl);
84}
797e9e30 85
f79321f1 86$resource->mainfile = $file->get_filename();
aa54ed7b 87switch (resource_get_final_display_type($resource)) {
88 case RESOURCELIB_DISPLAY_EMBED:
89 resource_display_embed($resource, $cm, $course, $file);
90 break;
91 case RESOURCELIB_DISPLAY_FRAME:
92 resource_display_frame($resource, $cm, $course, $file);
93 break;
94 default:
95 resource_print_workaround($resource, $cm, $course, $file);
96 break;
97}
2a439ba7 98