MDL-14627 new mod/url:view capability
[moodle.git] / mod / url / view.php
CommitLineData
58a27a74 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 *
2b641d15
PS
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
58a27a74 25 */
26
27require('../../config.php');
28require_once("$CFG->dirroot/mod/url/locallib.php");
2ea5705f 29require_once($CFG->libdir . '/completionlib.php');
58a27a74 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
35if ($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);
38
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}
43
44$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
45
46require_course_login($course, true, $cm);
47$context = get_context_instance(CONTEXT_MODULE, $cm->id);
ccd7189a 48require_capability('mod/url:view', $context);
58a27a74 49
50add_to_log($course->id, 'url', 'view', 'view.php?id='.$cm->id, $url->id, $cm->id);
51
c15a60e6
SM
52// Update 'viewed' state if required by completion system
53$completion = new completion_info($course);
54$completion->set_module_viewed($cm);
55
a6855934 56$PAGE->set_url('/mod/url/view.php', array('id' => $cm->id));
58a27a74 57
58if ($redirect) {
59 // coming from course page or url index page,
60 // the redirection is needed for completion tracking and logging
61 $fullurl = url_get_full_url($url, $cm, $course);
62 redirect($fullurl);
63}
64
65switch (url_get_final_display_type($url)) {
66 case RESOURCELIB_DISPLAY_EMBED:
67 url_display_embed($url, $cm, $course);
68 break;
69 case RESOURCELIB_DISPLAY_FRAME:
70 url_display_frame($url, $cm, $course);
71 break;
72 default:
73 url_print_workaround($url, $cm, $course);
74 break;
75}