Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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   * For a given capability, show what permission it has for every role, and everywhere that it is overridden.
  19   *
  20   * @package    tool_capability
  21   * @copyright  2008 Tim Hunt
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/capability/locallib.php');
  27  require_once($CFG->libdir.'/adminlib.php');
  28  
  29  // Get URL parameters.
  30  $systemcontext = context_system::instance();
  31  $contextid = optional_param('context', $systemcontext->id, PARAM_INT);
  32  
  33  // Check permissions.
  34  list($context, $course, $cm) = get_context_info_array($contextid);
  35  require_login($course, false, $cm);
  36  require_capability('moodle/role:manage', $context);
  37  
  38  // Print the header.
  39  admin_externalpage_setup('toolcapability');
  40  
  41  // Prepare the list of capabilities to choose from.
  42  $capabilitychoices = array();
  43  foreach ($context->get_capabilities('name') as $cap) {
  44      $capabilitychoices[$cap->name] = $cap->name . ': ' . get_capability_string($cap->name);
  45  }
  46  
  47  $allroles = role_fix_names(get_all_roles($context));
  48  // Prepare the list of roles to choose from.
  49  $rolechoices = array('0' => get_string('all'));
  50  foreach ($allroles as $role) {
  51      $rolechoices[$role->id] = $role->localname;
  52  }
  53  
  54  $form = new tool_capability_settings_form(null, array(
  55      'capabilities' => $capabilitychoices,
  56      'roles' => $rolechoices
  57  ));
  58  
  59  $PAGE->requires->js_call_amd('tool_capability/search', 'init');
  60  
  61  // Log.
  62  $capabilities = array();
  63  $rolestoshow = array();
  64  $roleids = array('0');
  65  $cleanedroleids = array();
  66  $onlydiff = false;
  67  if ($data = $form->get_data()) {
  68  
  69      $roleids = array();
  70      if (!empty($data->roles)) {
  71          $roleids = $data->roles;
  72      }
  73  
  74      $capabilities = array();
  75      if (!empty($data->capability)) {
  76          $capabilities = $data->capability;
  77      }
  78  
  79      if (in_array('0', $roleids)) {
  80          $rolestoshow = $allroles;
  81      } else {
  82          $cleanedroleids = array_intersect(array_keys($allroles), $roleids);
  83          if (count($cleanedroleids) === 0) {
  84              $rolestoshow = $allroles;
  85          } else {
  86              foreach ($cleanedroleids as $id) {
  87                  $rolestoshow[$id] = $allroles[$id];
  88              }
  89          }
  90      }
  91  
  92      if (isset($data->onlydiff)) {
  93          $onlydiff = $data->onlydiff;
  94      }
  95  }
  96  
  97  \tool_capability\event\report_viewed::create()->trigger();
  98  
  99  $renderer = $PAGE->get_renderer('tool_capability');
 100  
 101  echo $OUTPUT->header();
 102  
 103  $form->display();
 104  
 105  // If we have a capability, generate the report.
 106  if (count($capabilities) && count($rolestoshow)) {
 107      /* @var tool_capability_renderer $renderer */
 108      echo $renderer->capability_comparison_table($capabilities, $context->id, $rolestoshow, $onlydiff);
 109  }
 110  
 111  // Footer.
 112  echo $OUTPUT->footer();
 113  
 114  function print_report_tree($contextid, $contexts, $allroles) {
 115      global $CFG;
 116  
 117      // Array for holding lang strings.
 118      static $strpermissions = null;
 119      if (is_null($strpermissions)) {
 120          $strpermissions = array(
 121              CAP_INHERIT => get_string('notset','role'),
 122              CAP_ALLOW => get_string('allow','role'),
 123              CAP_PREVENT => get_string('prevent','role'),
 124              CAP_PROHIBIT => get_string('prohibit','role')
 125          );
 126      }
 127  
 128      // Start the list item, and print the context name as a link to the place to
 129      // make changes.
 130      if ($contextid == context_system::instance()->id) {
 131          $url = "$CFG->wwwroot/$CFG->admin/roles/manage.php";
 132          $title = get_string('changeroles', 'tool_capability');
 133      } else {
 134          $url = "$CFG->wwwroot/$CFG->admin/roles/override.php?contextid=$contextid";
 135          $title = get_string('changeoverrides', 'tool_capability');
 136      }
 137      $context = context::instance_by_id($contextid);
 138      echo '<h3><a href="' . $url . '" title="' . $title . '">', $context->get_context_name(), '</a></h3>';
 139  
 140      // If there are any role overrides here, print them.
 141      if (!empty($contexts[$contextid]->rolecapabilities)) {
 142          $rowcounter = 0;
 143          echo '<table class="generaltable table-striped"><tbody>';
 144          foreach ($allroles as $role) {
 145              if (isset($contexts[$contextid]->rolecapabilities[$role->id])) {
 146                  $permission = $contexts[$contextid]->rolecapabilities[$role->id];
 147                  echo '<tr class="r' . ($rowcounter % 2) . '"><th class="cell">', $role->localname,
 148                          '</th><td class="cell">' . $strpermissions[$permission] . '</td></tr>';
 149                  $rowcounter++;
 150              }
 151          }
 152          echo '</tbody></table>';
 153      }
 154  
 155      // After we have done the site context, change the string for CAP_INHERIT
 156      // from 'notset' to 'inherit'.
 157      $strpermissions[CAP_INHERIT] = get_string('inherit','role');
 158  
 159      // If there are any child contexts, print them recursively.
 160      if (!empty($contexts[$contextid]->children)) {
 161          echo '<ul>';
 162          foreach ($contexts[$contextid]->children as $childcontextid) {
 163              echo '<li>';
 164              print_report_tree($childcontextid, $contexts, $allroles);
 165              echo '</li>';
 166          }
 167          echo '</ul>';
 168      }
 169  }