Merge branch 'MDL-26365' of git://github.com/timhunt/moodle
[moodle.git] / course / switchrole.php
blob2a4842b09f3c6b0fbf9fb882c21a88992bf33c20
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 $returnurlkey = optional_param('returnurl', false, PARAM_INT);
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 $returnurl = false;
73 if ($returnurlkey && !empty($SESSION->returnurl) && strpos($SESSION->returnurl, 'moodle_url')!==false) {
74 $returnurl = @unserialize($SESSION->returnurl);
75 if (!($returnurl instanceof moodle_url)) {
76 $returnurl = false;
78 unset($SESSION->returnurl);
81 if ($returnurl===false) {
82 $returnurl = new moodle_url('/course/view.php', array('id'=>$course->id));
84 redirect($returnurl);