Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   *
  21   * @package    mod_resource
  22   * @copyright  2009 Petr Skoda  {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require('../../config.php');
  27  require_once($CFG->dirroot.'/mod/resource/lib.php');
  28  require_once($CFG->dirroot.'/mod/resource/locallib.php');
  29  require_once($CFG->libdir.'/completionlib.php');
  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  $forceview = optional_param('forceview', 0, PARAM_BOOL);
  35  
  36  if ($r) {
  37      if (!$resource = $DB->get_record('resource', array('id'=>$r))) {
  38          resource_redirect_if_migrated($r, 0);
  39          print_error('invalidaccessparameter');
  40      }
  41      $cm = get_coursemodule_from_instance('resource', $resource->id, $resource->course, false, MUST_EXIST);
  42  
  43  } else {
  44      if (!$cm = get_coursemodule_from_id('resource', $id)) {
  45          resource_redirect_if_migrated(0, $id);
  46          print_error('invalidcoursemodule');
  47      }
  48      $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
  49  }
  50  
  51  $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  52  
  53  require_course_login($course, true, $cm);
  54  $context = context_module::instance($cm->id);
  55  require_capability('mod/resource:view', $context);
  56  
  57  // Completion and trigger events.
  58  resource_view($resource, $course, $cm, $context);
  59  
  60  $PAGE->set_url('/mod/resource/view.php', array('id' => $cm->id));
  61  
  62  if ($resource->tobemigrated) {
  63      resource_print_tobemigrated($resource, $cm, $course);
  64      die;
  65  }
  66  
  67  $fs = get_file_storage();
  68  $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
  69  if (count($files) < 1) {
  70      resource_print_filenotfound($resource, $cm, $course);
  71      die;
  72  } else {
  73      $file = reset($files);
  74      unset($files);
  75  }
  76  
  77  $resource->mainfile = $file->get_filename();
  78  $displaytype = resource_get_final_display_type($resource);
  79  if ($displaytype == RESOURCELIB_DISPLAY_OPEN || $displaytype == RESOURCELIB_DISPLAY_DOWNLOAD) {
  80      $redirect = true;
  81  }
  82  
  83  // Don't redirect teachers, otherwise they can not access course or module settings.
  84  if ($redirect && !course_get_format($course)->has_view_page() &&
  85          (has_capability('moodle/course:manageactivities', $context) ||
  86          has_capability('moodle/course:update', context_course::instance($course->id)))) {
  87      $redirect = false;
  88  }
  89  
  90  if ($redirect && !$forceview) {
  91      // coming from course page or url index page
  92      // this redirect trick solves caching problems when tracking views ;-)
  93      $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
  94      $fullurl = moodle_url::make_file_url('/pluginfile.php', $path, $displaytype == RESOURCELIB_DISPLAY_DOWNLOAD);
  95      redirect($fullurl);
  96  }
  97  
  98  switch ($displaytype) {
  99      case RESOURCELIB_DISPLAY_EMBED:
 100          resource_display_embed($resource, $cm, $course, $file);
 101          break;
 102      case RESOURCELIB_DISPLAY_FRAME:
 103          resource_display_frame($resource, $cm, $course, $file);
 104          break;
 105      default:
 106          resource_print_workaround($resource, $cm, $course, $file);
 107          break;
 108  }
 109