Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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   * Lets the user define and edit roles.
  19   *
  20   * Responds to actions:
  21   *   [blank]   - list roles.
  22   *   delete    - delete a role (with are-you-sure)
  23   *   moveup    - change the sort order
  24   *   movedown  - change the sort order
  25   *
  26   * For all but the first two of those, you also need a roleid parameter, and
  27   * possibly some other data.
  28   *
  29   * @package    core_role
  30   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  
  34  require_once(__DIR__ . '/../../config.php');
  35  require_once($CFG->libdir.'/adminlib.php');
  36  require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
  37  
  38  $action = optional_param('action', '', PARAM_ALPHA);
  39  if ($action) {
  40      $roleid = required_param('roleid', PARAM_INT);
  41  } else {
  42      $roleid = 0;
  43  }
  44  
  45  // Get the base URL for this and related pages into a convenient variable.
  46  $baseurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/manage.php';
  47  $defineurl = $CFG->wwwroot . '/' . $CFG->admin . '/roles/define.php';
  48  
  49  admin_externalpage_setup('defineroles');
  50  
  51  // Check access permissions.
  52  $systemcontext = context_system::instance();
  53  require_capability('moodle/role:manage', $systemcontext);
  54  
  55  // Get some basic data we are going to need.
  56  $roles = role_fix_names(get_all_roles(), $systemcontext, ROLENAME_ORIGINAL);
  57  
  58  $undeletableroles = array();
  59  $undeletableroles[$CFG->notloggedinroleid] = 1;
  60  $undeletableroles[$CFG->guestroleid] = 1;
  61  $undeletableroles[$CFG->defaultuserroleid] = 1;
  62  
  63  $PAGE->set_primary_active_tab('siteadminnode');
  64  $PAGE->navbar->add(get_string('defineroles', 'role'), $PAGE->url);
  65  
  66  // Process submitted data.
  67  $confirmed = (optional_param('confirm', false, PARAM_BOOL) && data_submitted() && confirm_sesskey());
  68  switch ($action) {
  69      case 'delete':
  70          if (isset($undeletableroles[$roleid])) {
  71              throw new \moodle_exception('cannotdeletethisrole', '', $baseurl);
  72          }
  73          if (!$confirmed) {
  74              // Show confirmation.
  75              echo $OUTPUT->header();
  76              $optionsyes = array('action'=>'delete', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
  77              $a = new stdClass();
  78              $a->id = $roleid;
  79              $a->name = $roles[$roleid]->localname;
  80              $a->shortname = $roles[$roleid]->shortname;
  81              $a->count = $DB->count_records_select('role_assignments',
  82                  'roleid = ?', array($roleid), 'COUNT(DISTINCT userid)');
  83  
  84              $formcontinue = new single_button(new moodle_url($baseurl, $optionsyes), get_string('yes'));
  85              $formcancel = new single_button(new moodle_url($baseurl), get_string('no'), 'get');
  86              echo $OUTPUT->confirm(get_string('deleterolesure', 'core_role', $a), $formcontinue, $formcancel);
  87              echo $OUTPUT->footer();
  88              die;
  89          }
  90          if (!delete_role($roleid)) {
  91              // The delete failed.
  92              throw new \moodle_exception('cannotdeleterolewithid', 'error', $baseurl, $roleid);
  93          }
  94          // Deleted a role sitewide...
  95          redirect($baseurl);
  96          break;
  97  
  98      case 'moveup':
  99          if (confirm_sesskey()) {
 100              $prevrole = null;
 101              $thisrole = null;
 102              foreach ($roles as $role) {
 103                  if ($role->id == $roleid) {
 104                      $thisrole = $role;
 105                      break;
 106                  } else {
 107                      $prevrole = $role;
 108                  }
 109              }
 110              if (is_null($thisrole) || is_null($prevrole)) {
 111                  throw new \moodle_exception('cannotmoverolewithid', 'error', '', $roleid);
 112              }
 113              if (!switch_roles($thisrole, $prevrole)) {
 114                  throw new \moodle_exception('cannotmoverolewithid', 'error', '', $roleid);
 115              }
 116          }
 117  
 118          redirect($baseurl);
 119          break;
 120  
 121      case 'movedown':
 122          if (confirm_sesskey()) {
 123              $thisrole = null;
 124              $nextrole = null;
 125              foreach ($roles as $role) {
 126                  if ($role->id == $roleid) {
 127                      $thisrole = $role;
 128                  } else if (!is_null($thisrole)) {
 129                      $nextrole = $role;
 130                      break;
 131                  }
 132              }
 133              if (is_null($nextrole)) {
 134                  throw new \moodle_exception('cannotmoverolewithid', 'error', '', $roleid);
 135              }
 136              if (!switch_roles($thisrole, $nextrole)) {
 137                  throw new \moodle_exception('cannotmoverolewithid', 'error', '', $roleid);
 138              }
 139          }
 140  
 141          redirect($baseurl);
 142          break;
 143  
 144  }
 145  
 146  // Print the page header and tabs.
 147  echo $OUTPUT->header();
 148  
 149  $currenttab = 'manage';
 150  require ('managetabs.php');
 151  
 152  // Initialise table.
 153  $table = new html_table();
 154  $table->colclasses = array('leftalign', 'leftalign', 'leftalign', 'leftalign');
 155  $table->id = 'roles';
 156  $table->attributes['class'] = 'admintable generaltable';
 157  $table->head = array(
 158      get_string('role') . ' ' . $OUTPUT->help_icon('roles', 'core_role'),
 159      get_string('description'),
 160      get_string('roleshortname', 'core_role'),
 161      get_string('edit')
 162  );
 163  
 164  // Get some strings outside the loop.
 165  $stredit = get_string('edit');
 166  $strdelete = get_string('delete');
 167  $strmoveup = get_string('moveup');
 168  $strmovedown = get_string('movedown');
 169  
 170  // Print a list of roles with edit/copy/delete/reorder icons.
 171  $table->data = array();
 172  $firstrole = reset($roles);
 173  $lastrole = end($roles);
 174  foreach ($roles as $role) {
 175      // Basic data.
 176      $row = array(
 177          '<a href="' . $defineurl . '?action=view&amp;roleid=' . $role->id . '">' . $role->localname . '</a>',
 178          role_get_description($role),
 179          s($role->shortname),
 180          '',
 181      );
 182  
 183      // Move up.
 184      if ($role->sortorder != $firstrole->sortorder) {
 185          $row[3] .= get_action_icon($baseurl . '?action=moveup&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
 186      } else {
 187          $row[3] .= get_spacer();
 188      }
 189      // Move down.
 190      if ($role->sortorder != $lastrole->sortorder) {
 191          $row[3] .= get_action_icon($baseurl . '?action=movedown&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
 192      } else {
 193          $row[3] .= get_spacer();
 194      }
 195      // Edit.
 196      $row[3] .= get_action_icon($defineurl . '?action=edit&amp;roleid=' . $role->id,
 197              'edit', $stredit, get_string('editxrole', 'core_role', $role->localname));
 198      // Delete.
 199      if (isset($undeletableroles[$role->id])) {
 200          $row[3] .= get_spacer();
 201      } else {
 202          $row[3] .= get_action_icon($baseurl . '?action=delete&amp;roleid=' . $role->id,
 203                'delete', $strdelete, get_string('deletexrole', 'core_role', $role->localname));
 204      }
 205  
 206      $table->data[] = $row;
 207  }
 208  echo html_writer::table($table);
 209  
 210  echo $OUTPUT->container_start('buttons');
 211  echo $OUTPUT->single_button(new moodle_url($defineurl, array('action' => 'add')), get_string('addrole', 'core_role'), 'get');
 212  echo $OUTPUT->container_end();
 213  
 214  echo $OUTPUT->footer();
 215  die;
 216  
 217  function get_action_icon($url, $icon, $alt, $tooltip) {
 218      global $OUTPUT;
 219      return '<a title="' . $tooltip . '" href="'. $url . '">' .
 220              $OUTPUT->pix_icon('t/' . $icon, $alt) . '</a> ';
 221  }
 222  function get_spacer() {
 223      global $OUTPUT;
 224      return $OUTPUT->spacer();
 225  }