Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/resource/view.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 require('../../config.php');
00028 require_once($CFG->dirroot.'/mod/resource/locallib.php');
00029 require_once($CFG->libdir.'/completionlib.php');
00030 
00031 $id       = optional_param('id', 0, PARAM_INT); // Course Module ID
00032 $r        = optional_param('r', 0, PARAM_INT);  // Resource instance ID
00033 $redirect = optional_param('redirect', 0, PARAM_BOOL);
00034 
00035 if ($r) {
00036     if (!$resource = $DB->get_record('resource', array('id'=>$r))) {
00037         resource_redirect_if_migrated($r, 0);
00038         print_error('invalidaccessparameter');
00039     }
00040     $cm = get_coursemodule_from_instance('resource', $resource->id, $resource->course, false, MUST_EXIST);
00041 
00042 } else {
00043     if (!$cm = get_coursemodule_from_id('resource', $id)) {
00044         resource_redirect_if_migrated(0, $id);
00045         print_error('invalidcoursemodule');
00046     }
00047     $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
00048 }
00049 
00050 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
00051 
00052 require_course_login($course, true, $cm);
00053 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00054 require_capability('mod/resource:view', $context);
00055 
00056 add_to_log($course->id, 'resource', 'view', 'view.php?id='.$cm->id, $resource->id, $cm->id);
00057 
00058 // Update 'viewed' state if required by completion system
00059 $completion = new completion_info($course);
00060 $completion->set_module_viewed($cm);
00061 
00062 $PAGE->set_url('/mod/resource/view.php', array('id' => $cm->id));
00063 
00064 if ($resource->tobemigrated) {
00065     resource_print_tobemigrated($resource, $cm, $course);
00066     die;
00067 }
00068 
00069 $fs = get_file_storage();
00070 $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
00071 if (count($files) < 1) {
00072     resource_print_filenotfound($resource, $cm, $course);
00073     die;
00074 } else {
00075     $file = reset($files);
00076     unset($files);
00077 }
00078 
00079 $resource->mainfile = $file->get_filename();
00080 $displaytype = resource_get_final_display_type($resource);
00081 if ($displaytype == RESOURCELIB_DISPLAY_OPEN || $displaytype == RESOURCELIB_DISPLAY_DOWNLOAD) {
00082     // For 'open' and 'download' links, we always redirect to the content - except
00083     // if the user just chose 'save and display' from the form then that would be
00084     // confusing
00085     if (!isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], 'modedit.php') === false) {
00086         $redirect = true;
00087     }
00088 }
00089 
00090 if ($redirect) {
00091     // coming from course page or url index page
00092     // this redirect trick solves caching problems when tracking views ;-)
00093     $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
00094     $fullurl = moodle_url::make_file_url('/pluginfile.php', $path, $displaytype == RESOURCELIB_DISPLAY_DOWNLOAD);
00095     redirect($fullurl);
00096 }
00097 
00098 switch ($displaytype) {
00099     case RESOURCELIB_DISPLAY_EMBED:
00100         resource_display_embed($resource, $cm, $course, $file);
00101         break;
00102     case RESOURCELIB_DISPLAY_FRAME:
00103         resource_display_frame($resource, $cm, $course, $file);
00104         break;
00105     default:
00106         resource_print_workaround($resource, $cm, $course, $file);
00107         break;
00108 }
00109 
 All Data Structures Namespaces Files Functions Variables Enumerations