3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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');
31 * Defines the course completion settings form.
33 class switchrole_form
extends moodleform
{
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) {
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) {
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
);
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) {
87 $mform->addElement('select', 'switchrole', get_string('role'), $roles);
89 // Add common action buttons.
90 $this->add_action_buttons();
93 $mform->addElement('hidden', 'id', $course->id
);
94 $mform->setType('id', PARAM_INT
);