weekly release 2.1.6+
[moodle.git] / course / switchrole.php
blob12cba2041e893523463cd09488ba248aaa75b3ce
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 * 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.
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.
27 * @since 2.0
28 * @package course
29 * @copyright 2009 Sam Hemelryk
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 require_once('../config.php');
34 require_once($CFG->dirroot.'/course/lib.php');
36 $id = required_param('id', PARAM_INT);
37 $switchrole = optional_param('switchrole',-1, PARAM_INT);
38 $returnurl = optional_param('returnurl', false, PARAM_URL);
40 $PAGE->set_url('/course/switchrole.php', array('id'=>$id));
42 if (!confirm_sesskey()) {
43 print_error('confirmsesskeybad', 'error');
46 if (! ($course = $DB->get_record('course', array('id'=>$id)))) {
47 print_error('invalidcourseid', 'error');
50 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
51 print_error('nocontext');
54 // Remove any switched roles before checking login
55 if ($switchrole == 0) {
56 role_switch($switchrole, $context);
58 require_login($course);
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);
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;
85 if ($returnurl === false) {
86 $returnurl = new moodle_url('/course/view.php', array('id' => $course->id));
89 redirect($returnurl);