MDL-58543 upgrade: Replay 4 steps missed for some combinations
[moodle.git] / course / switchrole_form.php
blob57886564c04dcabdf96c3d2615ad80cdd0b1dc3e
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 * Switch roles form.
21 * @package core_course
22 * @copyright 2016 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir.'/formslib.php');
30 /**
31 * Defines the course completion settings form.
33 class switchrole_form extends moodleform {
35 /**
36 * Determine whether the user is assuming another role
38 * This function checks to see if the user is assuming another role by means of
39 * role switching. In doing this we compare each RSW key (context path) against
40 * the current context path. This ensures that we can provide the switching
41 * options against both the course and any page shown under the course.
43 * @param context $context
44 * @return bool|int The role(int) if the user is in another role, false otherwise
46 protected function in_alternative_role($context) {
47 global $USER, $PAGE;
48 if (!empty($USER->access['rsw']) && is_array($USER->access['rsw'])) {
49 if (!empty($PAGE->context) && !empty($USER->access['rsw'][$PAGE->context->path])) {
50 return $USER->access['rsw'][$PAGE->context->path];
52 foreach ($USER->access['rsw'] as $key=>$role) {
53 if (strpos($context->path, $key)===0) {
54 return $role;
58 return false;
61 /**
62 * Defines the form fields.
64 public function definition() {
65 global $USER, $CFG, $DB;
67 $mform = $this->_form;
68 $course = $this->_customdata['course'];
70 // Overall criteria aggregation.
71 $context = context_course::instance($course->id);
72 $roles = array();
73 $assumedrole = -1;
74 if (is_role_switched($course->id)) {
75 $roles[0] = get_string('switchrolereturn');
76 $assumedrole = $USER->access['rsw'][$context->path];
78 $availableroles = get_switchable_roles($context);
79 if (is_array($availableroles)) {
80 foreach ($availableroles as $key=>$role) {
81 if ($assumedrole == (int)$key) {
82 continue;
84 $roles[$key] = $role;
87 $mform->addElement('select', 'switchrole', get_string('role'), $roles);
89 // Add common action buttons.
90 $this->add_action_buttons();
92 // Add hidden fields.
93 $mform->addElement('hidden', 'id', $course->id);
94 $mform->setType('id', PARAM_INT);