weekly back-to-dev release 4.5dev
[moodle.git] / course / switchrole.php
blob91e7afdcaf03168452c81834d6568cbec8e73d12
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * The purpose of this file is to allow the user to switch roles and be redirected
19 * back to the page that they were on.
21 * This functionality is also supported in {@link /course/view.php} in order to comply
22 * with backwards compatibility.
23 * The reason that we created this file was so that user didn't get redirected back
24 * to the course view page only to be redirected again.
26 * @since Moodle 2.0
27 * @package course
28 * @copyright 2009 Sam Hemelryk
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 require_once('../config.php');
33 require_once($CFG->dirroot.'/course/lib.php');
35 $id = required_param('id', PARAM_INT);
36 $switchrole = optional_param('switchrole', -1, PARAM_INT);
37 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
39 $PAGE->set_url('/course/switchrole.php', array('id'=>$id, 'switchrole'=>$switchrole));
41 if ($switchrole >= 0) {
42 require_sesskey();
45 if (!$course = $DB->get_record('course', array('id'=>$id))) {
46 redirect(new moodle_url('/'));
49 $context = context_course::instance($course->id);
51 // Remove any switched roles before checking login.
52 if ($switchrole == 0) {
53 role_switch(0, $context);
55 require_login($course);
57 // Switchrole - sanity check in cost-order...
58 if ($switchrole > 0 && has_capability('moodle/role:switchroles', $context)) {
59 // Is this role assignable in this context?
60 // inquiring minds want to know...
61 $aroles = get_switchable_roles($context);
62 if (is_array($aroles) && isset($aroles[$switchrole])) {
63 role_switch($switchrole, $context);
65 } else if ($switchrole < 0) {
67 $PAGE->set_title(get_string('switchroleto'));
68 $PAGE->set_heading($course->fullname);
69 $PAGE->set_pagelayout('incourse');
71 echo $OUTPUT->header();
72 echo $OUTPUT->heading(get_string('switchroleto'));
74 // Overall criteria aggregation.
75 $roles = array();
76 $assumedrole = -1;
77 if (is_role_switched($course->id)) {
78 $roles[0] = get_string('switchrolereturn');
79 $assumedrole = $USER->access['rsw'][$context->path];
81 $availableroles = get_switchable_roles($context, ROLENAME_BOTH);
82 if (is_array($availableroles)) {
83 foreach ($availableroles as $key => $role) {
84 if ($assumedrole == (int)$key) {
85 continue;
87 $roles[$key] = $role;
90 echo $OUTPUT->box(markdown_to_html(get_string('switchroleto_help')));
92 foreach ($roles as $key => $role) {
93 $url = new moodle_url('/course/switchrole.php', array('id' => $id, 'switchrole' => $key, 'returnurl' => $returnurl));
94 // Button encodes special characters, apply htmlspecialchars_decode() to avoid double escaping.
95 echo $OUTPUT->container($OUTPUT->single_button($url, htmlspecialchars_decode($role, ENT_COMPAT)), 'mx-3 mb-1');
98 $url = new moodle_url($returnurl);
99 echo $OUTPUT->container($OUTPUT->action_link($url, get_string('cancel')), 'mx-3 mb-1');
101 echo $OUTPUT->footer();
102 exit;
105 redirect(new moodle_url($returnurl));