Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 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  $PAGE->requires->yui_module(
  59      'moodle-tool_capability-search',
  60      'M.tool_capability.init_capability_search',
  61      array(array('strsearch' => get_string('search')))
  62  );
  63  
  64  // Log.
  65  $capabilities = array();
  66  $rolestoshow = array();
  67  $roleids = array('0');
  68  $cleanedroleids = array();
  69  $onlydiff = false;
  70  if ($data = $form->get_data()) {
  71  
  72      $roleids = array();
  73      if (!empty($data->roles)) {
  74          $roleids = $data->roles;
  75      }
  76  
  77      $capabilities = array();
  78      if (!empty($data->capability)) {
  79          $capabilities = $data->capability;
  80      }
  81  
  82      if (in_array('0', $roleids)) {
  83          $rolestoshow = $allroles;
  84      } else {
  85          $cleanedroleids = array_intersect(array_keys($allroles), $roleids);
  86          if (count($cleanedroleids) === 0) {
  87              $rolestoshow = $allroles;
  88          } else {
  89              foreach ($cleanedroleids as $id) {
  90                  $rolestoshow[$id] = $allroles[$id];
  91              }
  92          }
  93      }
  94  
  95      if (isset($data->onlydiff)) {
  96          $onlydiff = $data->onlydiff;
  97      }
  98  }
  99  
 100  \tool_capability\event\report_viewed::create()->trigger();
 101  
 102  $renderer = $PAGE->get_renderer('tool_capability');
 103  
 104  echo $OUTPUT->header();
 105  
 106  $form->display();
 107  
 108  // If we have a capability, generate the report.
 109  if (count($capabilities) && count($rolestoshow)) {
 110      /* @var tool_capability_renderer $renderer */
 111      echo $renderer->capability_comparison_table($capabilities, $context->id, $rolestoshow, $onlydiff);
 112  }
 113  
 114  // Footer.
 115  echo $OUTPUT->footer();
 116  
 117  function print_report_tree($contextid, $contexts, $allroles) {
 118      global $CFG;
 119  
 120      // Array for holding lang strings.
 121      static $strpermissions = null;
 122      if (is_null($strpermissions)) {
 123          $strpermissions = array(
 124              CAP_INHERIT => get_string('notset','role'),
 125              CAP_ALLOW => get_string('allow','role'),
 126              CAP_PREVENT => get_string('prevent','role'),
 127              CAP_PROHIBIT => get_string('prohibit','role')
 128          );
 129      }
 130  
 131      // Start the list item, and print the context name as a link to the place to
 132      // make changes.
 133      if ($contextid == context_system::instance()->id) {
 134          $url = "$CFG->wwwroot/$CFG->admin/roles/manage.php";
 135          $title = get_string('changeroles', 'tool_capability');
 136      } else {
 137          $url = "$CFG->wwwroot/$CFG->admin/roles/override.php?contextid=$contextid";
 138          $title = get_string('changeoverrides', 'tool_capability');
 139      }
 140      $context = context::instance_by_id($contextid);
 141      echo '<h3><a href="' . $url . '" title="' . $title . '">', $context->get_context_name(), '</a></h3>';
 142  
 143      // If there are any role overrides here, print them.
 144      if (!empty($contexts[$contextid]->rolecapabilities)) {
 145          $rowcounter = 0;
 146          echo '<table class="generaltable table-striped"><tbody>';
 147          foreach ($allroles as $role) {
 148              if (isset($contexts[$contextid]->rolecapabilities[$role->id])) {
 149                  $permission = $contexts[$contextid]->rolecapabilities[$role->id];
 150                  echo '<tr class="r' . ($rowcounter % 2) . '"><th class="cell">', $role->localname,
 151                          '</th><td class="cell">' . $strpermissions[$permission] . '</td></tr>';
 152                  $rowcounter++;
 153              }
 154          }
 155          echo '</tbody></table>';
 156      }
 157  
 158      // After we have done the site context, change the string for CAP_INHERIT
 159      // from 'notset' to 'inherit'.
 160      $strpermissions[CAP_INHERIT] = get_string('inherit','role');
 161  
 162      // If there are any child contexts, print them recursively.
 163      if (!empty($contexts[$contextid]->children)) {
 164          echo '<ul>';
 165          foreach ($contexts[$contextid]->children as $childcontextid) {
 166              echo '<li>';
 167              print_report_tree($childcontextid, $contexts, $allroles);
 168              echo '</li>';
 169          }
 170          echo '</ul>';
 171      }
 172  }