Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   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.
  14  //
  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/>.
  17  
  18  /**
  19   * Switch roles form.
  20   *
  21   * @package     core_course
  22   * @copyright   2016 Damyon Wiese
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->libdir.'/formslib.php');
  29  
  30  /**
  31   * Defines the course completion settings form.
  32   */
  33  class switchrole_form extends moodleform {
  34  
  35      /**
  36       * Determine whether the user is assuming another role
  37       *
  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.
  42       *
  43       * @param context $context
  44       * @return bool|int The role(int) if the user is in another role, false otherwise
  45       */
  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];
  51              }
  52              foreach ($USER->access['rsw'] as $key=>$role) {
  53                  if (strpos($context->path, $key)===0) {
  54                      return $role;
  55                  }
  56              }
  57          }
  58          return false;
  59      }
  60  
  61      /**
  62       * Defines the form fields.
  63       */
  64      public function definition() {
  65          global $USER, $CFG, $DB;
  66  
  67          $mform = $this->_form;
  68          $course = $this->_customdata['course'];
  69  
  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];
  77          }
  78          $availableroles = get_switchable_roles($context);
  79          if (is_array($availableroles)) {
  80              foreach ($availableroles as $key=>$role) {
  81                  if ($assumedrole == (int)$key) {
  82                      continue;
  83                  }
  84                  $roles[$key] = $role;
  85              }
  86          }
  87          $mform->addElement('select', 'switchrole', get_string('role'), $roles);
  88  
  89          // Add common action buttons.
  90          $this->add_action_buttons();
  91  
  92          // Add hidden fields.
  93          $mform->addElement('hidden', 'id', $course->id);
  94          $mform->setType('id', PARAM_INT);
  95      }
  96  }