MDL-63086 block_rss_client: Move skip time calculation to task
[moodle.git] / mod / url / view.php
blobcc5c12fb94fa5d05f72e18b14ab75336bf61923c
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.
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
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
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');
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);
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);
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);
45 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
47 require_course_login($course, true, $cm);
48 $context = context_module::instance($cm->id);
49 require_capability('mod/url:view', $context);
51 // Completion and trigger events.
52 url_view($url, $course, $cm, $context);
54 $PAGE->set_url('/mod/url/view.php', array('id' => $cm->id));
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;
66 unset($exturl);
68 $displaytype = url_get_final_display_type($url);
69 if ($displaytype == RESOURCELIB_DISPLAY_OPEN) {
70 $redirect = true;
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));
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');
89 if ($editurl) {
90 redirect($fullurl, html_writer::link($editurl, $edittext)."<br/>".
91 get_string('pageshouldredirect'), 10);
94 redirect($fullurl);
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;