Commit | Line | Data |
---|---|---|
7d2a0492 | 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. | |
13 | // | |
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/>. | |
16 | ||
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. | |
20 | * | |
21 | * This functionality is also supported in {@link /course/view.php} in order to comply | |
aae028d9 | 22 | * with backwards compatibility. |
7d2a0492 | 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. | |
25 | * | |
5bcfd504 | 26 | * @since Moodle 2.0 |
d9cb06dc | 27 | * @package course |
7d2a0492 | 28 | * @copyright 2009 Sam Hemelryk |
29 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
30 | */ | |
31 | ||
32 | require_once('../config.php'); | |
33 | require_once($CFG->dirroot.'/course/lib.php'); | |
34 | ||
e26507b3 | 35 | $id = required_param('id', PARAM_INT); |
aae028d9 PS |
36 | $switchrole = optional_param('switchrole', -1, PARAM_INT); |
37 | $returnurl = optional_param('returnurl', '', PARAM_RAW); | |
38 | ||
39 | if (strpos($returnurl, '?') === false) { | |
40 | // Looks like somebody did not set proper page url, better go to course page. | |
41 | $returnurl = new moodle_url('/course/view.php', array('id' => $id)); | |
42 | } else { | |
43 | if (strpos($returnurl, $CFG->wwwroot) !== 0) { | |
44 | $returnurl = $CFG->wwwroot.$returnurl; | |
45 | } | |
46 | $returnurl = clean_param($returnurl, PARAM_URL); | |
47 | } | |
7d2a0492 | 48 | |
99061152 | 49 | $PAGE->set_url('/course/switchrole.php', array('id'=>$id, 'switchrole'=>$switchrole)); |
d9cb06dc | 50 | |
99061152 DW |
51 | if ($switchrole >= 0) { |
52 | require_sesskey(); | |
53 | } | |
7d2a0492 | 54 | |
aae028d9 PS |
55 | if (!$course = $DB->get_record('course', array('id'=>$id))) { |
56 | redirect(new moodle_url('/')); | |
7d2a0492 | 57 | } |
58 | ||
1f364c87 | 59 | $context = context_course::instance($course->id); |
7d2a0492 | 60 | |
aae028d9 | 61 | // Remove any switched roles before checking login. |
7d2a0492 | 62 | if ($switchrole == 0) { |
aae028d9 | 63 | role_switch(0, $context); |
7d2a0492 | 64 | } |
65 | require_login($course); | |
66 | ||
67 | // Switchrole - sanity check in cost-order... | |
68 | if ($switchrole > 0 && has_capability('moodle/role:switchroles', $context)) { | |
aae028d9 | 69 | // Is this role assignable in this context? |
7d2a0492 | 70 | // inquiring minds want to know... |
71 | $aroles = get_switchable_roles($context); | |
72 | if (is_array($aroles) && isset($aroles[$switchrole])) { | |
73 | role_switch($switchrole, $context); | |
7d2a0492 | 74 | } |
99061152 DW |
75 | } else if ($switchrole < 0) { |
76 | ||
77 | $PAGE->set_title(get_string('switchroleto')); | |
78 | $PAGE->set_heading($course->fullname); | |
79 | $PAGE->set_pagelayout('incourse'); | |
80 | ||
81 | echo $OUTPUT->header(); | |
82 | echo $OUTPUT->heading(get_string('switchroleto')); | |
83 | ||
70b03eff DW |
84 | // Overall criteria aggregation. |
85 | $roles = array(); | |
86 | $assumedrole = -1; | |
87 | if (is_role_switched($course->id)) { | |
88 | $roles[0] = get_string('switchrolereturn'); | |
89 | $assumedrole = $USER->access['rsw'][$context->path]; | |
90 | } | |
91 | $availableroles = get_switchable_roles($context); | |
92 | if (is_array($availableroles)) { | |
93 | foreach ($availableroles as $key => $role) { | |
94 | if ($assumedrole == (int)$key) { | |
95 | continue; | |
96 | } | |
97 | $roles[$key] = $role; | |
98 | } | |
99 | } | |
100 | echo $OUTPUT->box(markdown_to_html(get_string('switchroleto_help'))); | |
101 | ||
102 | foreach ($roles as $key => $role) { | |
103 | $url = new moodle_url('/course/switchrole.php', array('id' => $id, 'switchrole' => $key, 'returnurl' => $returnurl)); | |
1c4a0506 | 104 | // Button encodes special characters, apply htmlspecialchars_decode() to avoid double escaping. |
5cac5fa4 | 105 | echo $OUTPUT->container($OUTPUT->single_button($url, htmlspecialchars_decode($role)), 'mx-3 mb-1'); |
70b03eff | 106 | } |
99061152 | 107 | |
8c224317 | 108 | $url = new moodle_url($returnurl); |
5cac5fa4 | 109 | echo $OUTPUT->container($OUTPUT->action_link($url, get_string('cancel')), 'mx-3 mb-1'); |
8c224317 | 110 | |
99061152 DW |
111 | echo $OUTPUT->footer(); |
112 | exit; | |
7d2a0492 | 113 | } |
114 | ||
fd1d8295 | 115 | redirect($returnurl); |