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  // 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   * This file processes AJAX requests and returns JSON
  19   *
  20   * This is a server part of yui permissions manager module
  21   *
  22   * @package core_role
  23   * @copyright 2015 Martin Mastny
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  define('AJAX_SCRIPT', true);
  27  
  28  require(__DIR__ . '/../../config.php');
  29  
  30  $contextid = required_param('contextid', PARAM_INT);
  31  $getroles = optional_param('getroles', 0, PARAM_BOOL);
  32  
  33  list($context, $course, $cm) = get_context_info_array($contextid);
  34  
  35  $PAGE->set_context($context);
  36  
  37  require_login($course, false, $cm);
  38  require_capability('moodle/role:review', $context);
  39  require_sesskey();
  40  
  41  $OUTPUT->header();
  42  
  43  list($overridableroles, $overridecounts, $nameswithcounts) = get_overridable_roles($context,
  44          ROLENAME_BOTH, true);
  45  
  46  if ($getroles) {
  47      echo json_encode($overridableroles);
  48      die();
  49  }
  50  
  51  $capability = required_param('capability', PARAM_CAPABILITY);
  52  $roleid = required_param('roleid', PARAM_INT);
  53  $action = required_param('action', PARAM_ALPHA);
  54  
  55  $capability = $DB->get_record('capabilities', array('name' => $capability), '*', MUST_EXIST);
  56  
  57  if (!isset($overridableroles[$roleid])) {
  58      throw new moodle_exception('invalidarguments');
  59  }
  60  
  61  if (!has_capability('moodle/role:override', $context)) {
  62      if (!has_capability('moodle/role:safeoverride', $context) || !is_safe_capability($capability)) {
  63          require_capability('moodle/role:override', $context);
  64      }
  65  }
  66  
  67  switch ($action) {
  68      case 'allow':
  69          role_change_permission($roleid, $context, $capability->name, CAP_ALLOW);
  70          break;
  71      case 'prevent':
  72          role_change_permission($roleid, $context, $capability->name, CAP_PREVENT);
  73          break;
  74      case 'prohibit':
  75          role_change_permission($roleid, $context, $capability->name, CAP_PROHIBIT);
  76          break;
  77      case 'unprohibit':
  78          role_change_permission($roleid, $context, $capability->name, CAP_INHERIT);
  79          break;
  80      default:
  81          throw new moodle_exception('invalidarguments');
  82  }
  83  
  84  echo json_encode($action);
  85  die();