Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/mod/url/ -> view.php (source)

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * URL module main user interface
  20   *
  21   * @package    mod_url
  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/url/lib.php");
  28  require_once("$CFG->dirroot/mod/url/locallib.php");
  29  require_once($CFG->libdir . '/completionlib.php');
  30  
  31  $id       = optional_param('id', 0, PARAM_INT);        // Course module ID
  32  $u        = optional_param('u', 0, PARAM_INT);         // URL instance id
  33  $redirect = optional_param('redirect', 0, PARAM_BOOL);
  34  $forceview = optional_param('forceview', 0, PARAM_BOOL);
  35  
  36  if ($u) {  // Two ways to specify the module
  37      $url = $DB->get_record('url', array('id'=>$u), '*', MUST_EXIST);
  38      $cm = get_coursemodule_from_instance('url', $url->id, $url->course, false, MUST_EXIST);
  39  
  40  } else {
  41      $cm = get_coursemodule_from_id('url', $id, 0, false, MUST_EXIST);
  42      $url = $DB->get_record('url', array('id'=>$cm->instance), '*', MUST_EXIST);
  43  }
  44  
  45  $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
  46  
  47  require_course_login($course, true, $cm);
  48  $context = context_module::instance($cm->id);
  49  require_capability('mod/url:view', $context);
  50  
  51  // Completion and trigger events.
  52  url_view($url, $course, $cm, $context);
  53  
  54  $PAGE->set_url('/mod/url/view.php', array('id' => $cm->id));
  55  
  56  // Make sure URL exists before generating output - some older sites may contain empty urls
  57  // Do not use PARAM_URL here, it is too strict and does not support general URIs!
  58  $exturl = trim($url->externalurl);
  59  if (empty($exturl) or $exturl === 'http://') {
  60      url_print_header($url, $cm, $course);
  61      url_print_heading($url, $cm, $course);
  62      url_print_intro($url, $cm, $course);
  63      notice(get_string('invalidstoredurl', 'url'), new moodle_url('/course/view.php', array('id'=>$cm->course)));
  64      die;
  65  }
  66  unset($exturl);
  67  
  68  $displaytype = url_get_final_display_type($url);
  69  if ($displaytype == RESOURCELIB_DISPLAY_OPEN) {
  70      $redirect = true;
  71  }
  72  
  73  if ($redirect && !$forceview) {
  74      // coming from course page or url index page,
  75      // the redirection is needed for completion tracking and logging
  76      $fullurl = str_replace('&amp;', '&', url_get_full_url($url, $cm, $course));
  77  
  78      if (!course_get_format($course)->has_view_page()) {
  79          // If course format does not have a view page, add redirection delay with a link to the edit page.
  80          // Otherwise teacher is redirected to the external URL without any possibility to edit activity or course settings.
  81          $editurl = null;
  82          if (has_capability('moodle/course:manageactivities', $context)) {
  83              $editurl = new moodle_url('/course/modedit.php', array('update' => $cm->id));
  84              $edittext = get_string('editthisactivity');
  85          } else if (has_capability('moodle/course:update', $context->get_course_context())) {
  86              $editurl = new moodle_url('/course/edit.php', array('id' => $course->id));
  87              $edittext = get_string('editcoursesettings');
  88          }
  89          if ($editurl) {
  90              redirect($fullurl, html_writer::link($editurl, $edittext)."<br/>".
  91                      get_string('pageshouldredirect'), 10);
  92          }
  93      }
  94      redirect($fullurl);
  95  }
  96  
  97  switch ($displaytype) {
  98      case RESOURCELIB_DISPLAY_EMBED:
  99          url_display_embed($url, $cm, $course);
 100          break;
 101      case RESOURCELIB_DISPLAY_FRAME:
 102          url_display_frame($url, $cm, $course);
 103          break;
 104      default:
 105          url_print_workaround($url, $cm, $course);
 106          break;
 107  }