Commit | Line | Data |
---|---|---|
7d2a0492 | 1 | <?php |
d9cb06dc | 2 | |
7d2a0492 | 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 | * The purpose of this file is to allow the user to switch roles and be redirected | |
20 | * back to the page that they were on. | |
21 | * | |
22 | * This functionality is also supported in {@link /course/view.php} in order to comply | |
23 | * with backwards compatibility | |
24 | * The reason that we created this file was so that user didn't get redirected back | |
25 | * to the course view page only to be redirected again. | |
26 | * | |
27 | * @since 2.0 | |
d9cb06dc | 28 | * @package course |
7d2a0492 | 29 | * @copyright 2009 Sam Hemelryk |
30 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
31 | */ | |
32 | ||
33 | require_once('../config.php'); | |
34 | require_once($CFG->dirroot.'/course/lib.php'); | |
35 | ||
e26507b3 SH |
36 | $id = required_param('id', PARAM_INT); |
37 | $switchrole = optional_param('switchrole',-1, PARAM_INT); | |
38 | $returnurl = optional_param('returnurl', false, PARAM_URL); | |
7d2a0492 | 39 | |
a6855934 | 40 | $PAGE->set_url('/course/switchrole.php', array('id'=>$id)); |
d9cb06dc | 41 | |
7d2a0492 | 42 | if (!confirm_sesskey()) { |
43 | print_error('confirmsesskeybad', 'error'); | |
44 | } | |
45 | ||
46 | if (! ($course = $DB->get_record('course', array('id'=>$id)))) { | |
47 | print_error('invalidcourseid', 'error'); | |
48 | } | |
49 | ||
50 | if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) { | |
51 | print_error('nocontext'); | |
52 | } | |
53 | ||
54 | // Remove any switched roles before checking login | |
55 | if ($switchrole == 0) { | |
56 | role_switch($switchrole, $context); | |
57 | } | |
58 | require_login($course); | |
59 | ||
60 | // Switchrole - sanity check in cost-order... | |
61 | if ($switchrole > 0 && has_capability('moodle/role:switchroles', $context)) { | |
62 | // is this role assignable in this context? | |
63 | // inquiring minds want to know... | |
64 | $aroles = get_switchable_roles($context); | |
65 | if (is_array($aroles) && isset($aroles[$switchrole])) { | |
66 | role_switch($switchrole, $context); | |
67 | // Double check that this role is allowed here | |
68 | require_login($course); | |
69 | } | |
70 | } | |
71 | ||
e26507b3 SH |
72 | // TODO: Using SESSION->returnurl is deprecated and should be removed in the future. |
73 | // Till then this code remains to support any external applications calling this script. | |
74 | if (!empty($returnurl) && is_numeric($returnurl)) { | |
75 | $returnurl = false; | |
76 | if (!empty($SESSION->returnurl) && strpos($SESSION->returnurl, 'moodle_url')!==false) { | |
77 | debugging('Code calling switchrole should be passing a URL as a param.', DEBUG_DEVELOPER); | |
78 | $returnurl = @unserialize($SESSION->returnurl); | |
79 | if (!($returnurl instanceof moodle_url)) { | |
80 | $returnurl = false; | |
81 | } | |
7d2a0492 | 82 | } |
7d2a0492 | 83 | } |
84 | ||
e26507b3 SH |
85 | if ($returnurl === false) { |
86 | $returnurl = new moodle_url('/course/view.php', array('id' => $course->id)); | |
7d2a0492 | 87 | } |
7d2a0492 | 88 | |
e26507b3 | 89 | redirect($returnurl); |