MDL-30005 fix general URI support in URL module
[moodle.git] / mod / url / view.php
1 <?php
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/>.
18 /**
19  * URL module main user interface
20  *
21  * @package    mod
22  * @subpackage url
23  * @copyright  2009 Petr Skoda  {@link http://skodak.org}
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 require('../../config.php');
28 require_once("$CFG->dirroot/mod/url/locallib.php");
29 require_once($CFG->libdir . '/completionlib.php');
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);
35 if ($u) {  // Two ways to specify the module
36     $url = $DB->get_record('url', array('id'=>$u), '*', MUST_EXIST);
37     $cm = get_coursemodule_from_instance('url', $url->id, $url->course, false, MUST_EXIST);
39 } else {
40     $cm = get_coursemodule_from_id('url', $id, 0, false, MUST_EXIST);
41     $url = $DB->get_record('url', array('id'=>$cm->instance), '*', MUST_EXIST);
42 }
44 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
46 require_course_login($course, true, $cm);
47 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
48 require_capability('mod/url:view', $context);
50 add_to_log($course->id, 'url', 'view', 'view.php?id='.$cm->id, $url->id, $cm->id);
52 // Update 'viewed' state if required by completion system
53 $completion = new completion_info($course);
54 $completion->set_module_viewed($cm);
56 $PAGE->set_url('/mod/url/view.php', array('id' => $cm->id));
58 // Make sure URL exists before generating output - some older sites may contain empty urls
59 // Do not use PARAM_URL here, it is too strict and does not support general URIs!
60 $exturl = trim($url->externalurl);
61 if (empty($exturl) or $exturl === 'http://') {
62     url_print_header($url, $cm, $course);
63     url_print_heading($url, $cm, $course);
64     url_print_intro($url, $cm, $course);
65     notice(get_string('invalidstoredurl', 'url'), new moodle_url('/course/view.php', array('id'=>$cm->course)));
66     die;
67 }
68 unset($exturl);
70 if ($redirect) {
71     // coming from course page or url index page,
72     // the redirection is needed for completion tracking and logging
73     $fullurl = url_get_full_url($url, $cm, $course);
74     redirect(str_replace('&amp;', '&', $fullurl));
75 }
77 switch (url_get_final_display_type($url)) {
78     case RESOURCELIB_DISPLAY_EMBED:
79         url_display_embed($url, $cm, $course);
80         break;
81     case RESOURCELIB_DISPLAY_FRAME:
82         url_display_frame($url, $cm, $course);
83         break;
84     default:
85         url_print_workaround($url, $cm, $course);
86         break;
87 }