Merge branch 'MDL-36927-m24' of git://github.com/ankitagarwal/moodle into MOODLE_24_S...
[moodle.git] / course / switchrole.php
blob2b756b6b7450b5c2467ccaddc5c6aa7ac2832d8a
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_LOCALURL);
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 $context = context_course::instance($course->id);
52 // Remove any switched roles before checking login
53 if ($switchrole == 0) {
54 role_switch($switchrole, $context);
56 require_login($course);
58 // Switchrole - sanity check in cost-order...
59 if ($switchrole > 0 && has_capability('moodle/role:switchroles', $context)) {
60 // is this role assignable in this context?
61 // inquiring minds want to know...
62 $aroles = get_switchable_roles($context);
63 if (is_array($aroles) && isset($aroles[$switchrole])) {
64 role_switch($switchrole, $context);
65 // Double check that this role is allowed here
66 require_login($course);
70 // TODO: Using SESSION->returnurl is deprecated and should be removed in the future.
71 // Till then this code remains to support any external applications calling this script.
72 if (!empty($returnurl) && is_numeric($returnurl)) {
73 $returnurl = false;
74 if (!empty($SESSION->returnurl) && strpos($SESSION->returnurl, 'moodle_url')!==false) {
75 debugging('Code calling switchrole should be passing a URL as a param.', DEBUG_DEVELOPER);
76 $returnurl = @unserialize($SESSION->returnurl);
77 if (!($returnurl instanceof moodle_url)) {
78 $returnurl = false;
83 if ($returnurl === false) {
84 $returnurl = new moodle_url('/course/view.php', array('id' => $course->id));
87 redirect($returnurl);