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]

   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 edit role definitions.
  19   *
  20   * Responds to actions:
  21   *   add       - add a new role (allows import, duplicate, archetype)
  22   *   export    - save xml role definition
  23   *   edit      - edit the definition of a role
  24   *   view      - view the definition of a role
  25   *
  26   * @package    core_role
  27   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  
  31  require_once(__DIR__ . '/../../config.php');
  32  require_once($CFG->libdir.'/adminlib.php');
  33  
  34  $action = required_param('action', PARAM_ALPHA);
  35  if (!in_array($action, array('add', 'export', 'edit', 'reset', 'view'))) {
  36      throw new moodle_exception('invalidaccess');
  37  }
  38  if ($action != 'add') {
  39      $roleid = required_param('roleid', PARAM_INT);
  40  } else {
  41      $roleid = 0;
  42  }
  43  $resettype = optional_param('resettype', '', PARAM_RAW);
  44  $return = optional_param('return', 'manage', PARAM_ALPHA);
  45  
  46  // Get the base URL for this and related pages into a convenient variable.
  47  $baseurl = new moodle_url('/admin/roles/define.php', array('action'=>$action, 'roleid'=>$roleid));
  48  $manageurl = new moodle_url('/admin/roles/manage.php');
  49  if ($return === 'manage') {
  50      $returnurl = $manageurl;
  51  } else {
  52      $returnurl = new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$roleid));;
  53  }
  54  
  55  admin_externalpage_setup('defineroles', '', array('action' => $action, 'roleid' => $roleid),
  56      new moodle_url('/admin/roles/define.php'));
  57  
  58  // Check access permissions.
  59  $systemcontext = context_system::instance();
  60  require_capability('moodle/role:manage', $systemcontext);
  61  
  62  // Export role.
  63  if ($action === 'export') {
  64      core_role_preset::send_export_xml($roleid);
  65      die;
  66  }
  67  
  68  // Handle the toggle advanced mode button.
  69  $showadvanced = get_user_preferences('definerole_showadvanced', false);
  70  if (optional_param('toggleadvanced', false, PARAM_BOOL)) {
  71      $showadvanced = !$showadvanced;
  72      set_user_preference('definerole_showadvanced', $showadvanced);
  73  }
  74  
  75  // Get some basic data we are going to need.
  76  $roles = get_all_roles();
  77  $rolenames = role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
  78  $rolescount = count($roles);
  79  
  80  if ($action === 'add') {
  81      $title = get_string('addinganewrole', 'core_role');
  82  } else if ($action == 'view') {
  83      $title = get_string('viewingdefinitionofrolex', 'core_role', $rolenames[$roleid]->localname);
  84  } else if ($action == 'reset') {
  85      $title = get_string('resettingrole', 'core_role', $rolenames[$roleid]->localname);
  86  } else {
  87      $title = get_string('editingrolex', 'core_role', $rolenames[$roleid]->localname);
  88  }
  89  
  90  $PAGE->set_secondary_active_tab('users');
  91  $PAGE->set_primary_active_tab('siteadminnode');
  92  $PAGE->navbar->add($title, $baseurl);
  93  
  94  // Decide how to create new role.
  95  if ($action === 'add' and $resettype !== 'none') {
  96      $mform = new core_role_preset_form(null, array('action'=>'add', 'roleid'=>0, 'resettype'=>'0', 'return'=>'manage'));
  97      if ($mform->is_cancelled()) {
  98          redirect($manageurl);
  99  
 100      } else if ($data = $mform->get_data()) {
 101          $resettype = $data->resettype;
 102          $options = array(
 103              'shortname'     => 1,
 104              'name'          => 1,
 105              'description'   => 1,
 106              'permissions'   => 1,
 107              'archetype'     => 1,
 108              'contextlevels' => 1,
 109              'allowassign'   => 1,
 110              'allowoverride' => 1,
 111              'allowswitch'   => 1,
 112              'allowview'   => 1);
 113          if ($showadvanced) {
 114              $definitiontable = new core_role_define_role_table_advanced($systemcontext, 0);
 115          } else {
 116              $definitiontable = new core_role_define_role_table_basic($systemcontext, 0);
 117          }
 118          if (is_number($resettype)) {
 119              // Duplicate the role.
 120              $definitiontable->force_duplicate($resettype, $options);
 121          } else {
 122              // Must be an archetype.
 123              $definitiontable->force_archetype($resettype, $options);
 124          }
 125  
 126          if ($xml = $mform->get_file_content('rolepreset')) {
 127              $definitiontable->force_preset($xml, $options);
 128          }
 129  
 130      } else {
 131          echo $OUTPUT->header();
 132          echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
 133          $mform->display();
 134          echo $OUTPUT->footer();
 135          die;
 136      }
 137  
 138  } else if ($action === 'reset' and $resettype !== 'none') {
 139      if (!$role = $DB->get_record('role', array('id'=>$roleid))) {
 140          redirect($manageurl);
 141      }
 142      $resettype = empty($role->archetype) ? '0' : $role->archetype;
 143      $mform = new core_role_preset_form(null,
 144          array('action'=>'reset', 'roleid'=>$roleid, 'resettype'=>$resettype , 'permissions'=>1, 'archetype'=>1, 'contextlevels'=>1, 'return'=>$return));
 145      if ($mform->is_cancelled()) {
 146          redirect($returnurl);
 147  
 148      } else if ($data = $mform->get_data()) {
 149          $resettype = $data->resettype;
 150          $options = array(
 151              'shortname'     => $data->shortname,
 152              'name'          => $data->name,
 153              'description'   => $data->description,
 154              'permissions'   => $data->permissions,
 155              'archetype'     => $data->archetype,
 156              'contextlevels' => $data->contextlevels,
 157              'allowassign'   => $data->allowassign,
 158              'allowoverride' => $data->allowoverride,
 159              'allowswitch'   => $data->allowswitch,
 160              'allowview'     => $data->allowview);
 161          if ($showadvanced) {
 162              $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
 163          } else {
 164              $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
 165          }
 166          if (is_number($resettype)) {
 167              // Duplicate the role.
 168              $definitiontable->force_duplicate($resettype, $options);
 169          } else {
 170              // Must be an archetype.
 171              $definitiontable->force_archetype($resettype, $options);
 172          }
 173  
 174          if ($xml = $mform->get_file_content('rolepreset')) {
 175              $definitiontable->force_preset($xml, $options);
 176          }
 177  
 178      } else {
 179          echo $OUTPUT->header();
 180          echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
 181          $mform->display();
 182          echo $OUTPUT->footer();
 183          die;
 184      }
 185  
 186  } else {
 187      // Create the table object.
 188      if ($action === 'view') {
 189          $definitiontable = new core_role_view_role_definition_table($systemcontext, $roleid);
 190      } else if ($showadvanced) {
 191          $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
 192      } else {
 193          $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
 194      }
 195      $definitiontable->read_submitted_permissions();
 196  }
 197  
 198  // Handle the cancel button.
 199  if (optional_param('cancel', false, PARAM_BOOL)) {
 200      redirect($returnurl);
 201  }
 202  
 203  // Process submission in necessary.
 204  if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey() && $definitiontable->is_submission_valid()) {
 205      $definitiontable->save_changes();
 206      $tableroleid = $definitiontable->get_role_id();
 207  
 208      if ($action === 'add') {
 209          redirect(new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$definitiontable->get_role_id())));
 210      } else {
 211          redirect($returnurl);
 212      }
 213  }
 214  
 215  // Print the page header and tabs.
 216  echo $OUTPUT->header();
 217  
 218  $currenttab = 'manage';
 219  require ('managetabs.php');
 220  
 221  echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
 222  
 223  // Work out some button labels.
 224  if ($action === 'add') {
 225      $submitlabel = get_string('createthisrole', 'core_role');
 226  } else {
 227      $submitlabel = get_string('savechanges');
 228  }
 229  
 230  // On the view page, show some extra controls at the top.
 231  if ($action === 'view') {
 232      echo $OUTPUT->container_start('buttons');
 233      $url = new moodle_url('/admin/roles/define.php', array('action'=>'edit', 'roleid'=>$roleid, 'return'=>'define'));
 234      echo $OUTPUT->single_button(new moodle_url($url), get_string('edit'));
 235      $url = new moodle_url('/admin/roles/define.php', array('action'=>'reset', 'roleid'=>$roleid, 'return'=>'define'));
 236      echo $OUTPUT->single_button(new moodle_url($url), get_string('resetrole', 'core_role'));
 237      $url = new moodle_url('/admin/roles/define.php', array('action'=>'export', 'roleid'=>$roleid));
 238      echo $OUTPUT->single_button(new moodle_url($url), get_string('export', 'core_role'));
 239      echo $OUTPUT->single_button($manageurl, get_string('listallroles', 'core_role'));
 240      echo $OUTPUT->container_end();
 241  }
 242  
 243  // Start the form.
 244  echo $OUTPUT->box_start('generalbox');
 245  if ($action === 'view') {
 246      echo '<div class="mform">';
 247  } else {
 248      ?>
 249  <form id="rolesform" class="mform fcontainer" action="<?php p($baseurl->out(false)); ?>" method="post"><div>
 250  <input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
 251  <input type="hidden" name="return" value="<?php p($return); ?>" />
 252  <input type="hidden" name="resettype" value="none" />
 253  <div class="submitbuttons">
 254      <input type="submit" name="savechanges" class="btn btn-primary" value="<?php p($submitlabel); ?>" />
 255      <input type="submit" name="cancel" class="btn btn-secondary" value="<?php print_string('cancel'); ?>" />
 256  </div>
 257      <?php
 258  }
 259  
 260  // Print the form controls.
 261  $definitiontable->display();
 262  
 263  // Close the stuff we left open above.
 264  if ($action === 'view') {
 265      echo '</div>';
 266  } else {
 267      ?>
 268  <div class="submitbuttons">
 269      <input type="submit" name="savechanges" class="btn btn-primary" value="<?php p($submitlabel); ?>" />
 270      <input type="submit" name="cancel" class="btn btn-secondary" value="<?php print_string('cancel'); ?>" />
 271  </div>
 272  </div></form>
 273  <?php
 274  }
 275  echo $OUTPUT->box_end();
 276  
 277  // Print a link back to the all roles list.
 278  echo '<div class="backlink">';
 279  echo '<p><a href="' . s($manageurl->out(false)) . '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
 280  echo '</div>';
 281  
 282  echo $OUTPUT->footer();