Moodle release 2.7.10
[moodle.git] / mod / url / view.php
bloba4600f4281fb7e0f9a90fd74adb0ec767405c804
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/locallib.php");
28 require_once($CFG->libdir . '/completionlib.php');
30 $id = optional_param('id', 0, PARAM_INT); // Course module ID
31 $u = optional_param('u', 0, PARAM_INT); // URL instance id
32 $redirect = optional_param('redirect', 0, PARAM_BOOL);
34 if ($u) { // Two ways to specify the module
35 $url = $DB->get_record('url', array('id'=>$u), '*', MUST_EXIST);
36 $cm = get_coursemodule_from_instance('url', $url->id, $url->course, false, MUST_EXIST);
38 } else {
39 $cm = get_coursemodule_from_id('url', $id, 0, false, MUST_EXIST);
40 $url = $DB->get_record('url', array('id'=>$cm->instance), '*', MUST_EXIST);
43 $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
45 require_course_login($course, true, $cm);
46 $context = context_module::instance($cm->id);
47 require_capability('mod/url:view', $context);
49 $params = array(
50 'context' => $context,
51 'objectid' => $url->id
53 $event = \mod_url\event\course_module_viewed::create($params);
54 $event->add_record_snapshot('course_modules', $cm);
55 $event->add_record_snapshot('course', $course);
56 $event->add_record_snapshot('url', $url);
57 $event->trigger();
59 // Update 'viewed' state if required by completion system
60 $completion = new completion_info($course);
61 $completion->set_module_viewed($cm);
63 $PAGE->set_url('/mod/url/view.php', array('id' => $cm->id));
65 // Make sure URL exists before generating output - some older sites may contain empty urls
66 // Do not use PARAM_URL here, it is too strict and does not support general URIs!
67 $exturl = trim($url->externalurl);
68 if (empty($exturl) or $exturl === 'http://') {
69 url_print_header($url, $cm, $course);
70 url_print_heading($url, $cm, $course);
71 url_print_intro($url, $cm, $course);
72 notice(get_string('invalidstoredurl', 'url'), new moodle_url('/course/view.php', array('id'=>$cm->course)));
73 die;
75 unset($exturl);
77 $displaytype = url_get_final_display_type($url);
78 if ($displaytype == RESOURCELIB_DISPLAY_OPEN) {
79 // For 'open' links, we always redirect to the content - except if the user
80 // just chose 'save and display' from the form then that would be confusing
81 if (!isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], 'modedit.php') === false) {
82 $redirect = true;
86 if ($redirect) {
87 // coming from course page or url index page,
88 // the redirection is needed for completion tracking and logging
89 $fullurl = str_replace('&amp;', '&', url_get_full_url($url, $cm, $course));
91 if (!course_get_format($course)->has_view_page()) {
92 // If course format does not have a view page, add redirection delay with a link to the edit page.
93 // Otherwise teacher is redirected to the external URL without any possibility to edit activity or course settings.
94 $editurl = null;
95 if (has_capability('moodle/course:manageactivities', $context)) {
96 $editurl = new moodle_url('/course/modedit.php', array('update' => $cm->id));
97 $edittext = get_string('editthisactivity');
98 } else if (has_capability('moodle/course:update', $context->get_course_context())) {
99 $editurl = new moodle_url('/course/edit.php', array('id' => $course->id));
100 $edittext = get_string('editcoursesettings');
102 if ($editurl) {
103 redirect($fullurl, html_writer::link($editurl, $edittext)."<br/>".
104 get_string('pageshouldredirect'), 10);
107 redirect($fullurl);
110 switch ($displaytype) {
111 case RESOURCELIB_DISPLAY_EMBED:
112 url_display_embed($url, $cm, $course);
113 break;
114 case RESOURCELIB_DISPLAY_FRAME:
115 url_display_frame($url, $cm, $course);
116 break;
117 default:
118 url_print_workaround($url, $cm, $course);
119 break;