[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/roles/ -> define.php (source)

   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  // Check access permissions.
  56  $systemcontext = context_system::instance();
  57  require_login();
  58  require_capability('moodle/role:manage', $systemcontext);
  59  admin_externalpage_setup('defineroles', '', array('action' => $action, 'roleid' => $roleid), new moodle_url('/admin/roles/define.php'));
  60  
  61  // Export role.
  62  if ($action === 'export') {
  63      core_role_preset::send_export_xml($roleid);
  64      die;
  65  }
  66  
  67  // Handle the toggle advanced mode button.
  68  $showadvanced = get_user_preferences('definerole_showadvanced', false);
  69  if (optional_param('toggleadvanced', false, PARAM_BOOL)) {
  70      $showadvanced = !$showadvanced;
  71      set_user_preference('definerole_showadvanced', $showadvanced);
  72  }
  73  
  74  // Get some basic data we are going to need.
  75  $roles = get_all_roles();
  76  $rolenames = role_fix_names($roles, $systemcontext, ROLENAME_ORIGINAL);
  77  $rolescount = count($roles);
  78  
  79  if ($action === 'add') {
  80      $title = get_string('addinganewrole', 'core_role');
  81  } else if ($action == 'view') {
  82      $title = get_string('viewingdefinitionofrolex', 'core_role', $rolenames[$roleid]->localname);
  83  } else if ($action == 'reset') {
  84      $title = get_string('resettingrole', 'core_role', $rolenames[$roleid]->localname);
  85  } else {
  86      $title = get_string('editingrolex', 'core_role', $rolenames[$roleid]->localname);
  87  }
  88  
  89  // Decide how to create new role.
  90  if ($action === 'add' and $resettype !== 'none') {
  91      $mform = new core_role_preset_form(null, array('action'=>'add', 'roleid'=>0, 'resettype'=>'0', 'return'=>'manage'));
  92      if ($mform->is_cancelled()) {
  93          redirect($manageurl);
  94  
  95      } else if ($data = $mform->get_data()) {
  96          $resettype = $data->resettype;
  97          $options = array(
  98              'shortname'     => 1,
  99              'name'          => 1,
 100              'description'   => 1,
 101              'permissions'   => 1,
 102              'archetype'     => 1,
 103              'contextlevels' => 1,
 104              'allowassign'   => 1,
 105              'allowoverride' => 1,
 106              'allowswitch'   => 1);
 107          if ($showadvanced) {
 108              $definitiontable = new core_role_define_role_table_advanced($systemcontext, 0);
 109          } else {
 110              $definitiontable = new core_role_define_role_table_basic($systemcontext, 0);
 111          }
 112          if (is_number($resettype)) {
 113              // Duplicate the role.
 114              $definitiontable->force_duplicate($resettype, $options);
 115          } else {
 116              // Must be an archetype.
 117              $definitiontable->force_archetype($resettype, $options);
 118          }
 119  
 120          if ($xml = $mform->get_file_content('rolepreset')) {
 121              $definitiontable->force_preset($xml, $options);
 122          }
 123  
 124      } else {
 125          echo $OUTPUT->header();
 126          echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
 127          $mform->display();
 128          echo $OUTPUT->footer();
 129          die;
 130      }
 131  
 132  } else if ($action === 'reset' and $resettype !== 'none') {
 133      if (!$role = $DB->get_record('role', array('id'=>$roleid))) {
 134          redirect($manageurl);
 135      }
 136      $resettype = empty($role->archetype) ? '0' : $role->archetype;
 137      $mform = new core_role_preset_form(null,
 138          array('action'=>'reset', 'roleid'=>$roleid, 'resettype'=>$resettype , 'permissions'=>1, 'archetype'=>1, 'contextlevels'=>1, 'return'=>$return));
 139      if ($mform->is_cancelled()) {
 140          redirect($returnurl);
 141  
 142      } else if ($data = $mform->get_data()) {
 143          $resettype = $data->resettype;
 144          $options = array(
 145              'shortname'     => $data->shortname,
 146              'name'          => $data->name,
 147              'description'   => $data->description,
 148              'permissions'   => $data->permissions,
 149              'archetype'     => $data->archetype,
 150              'contextlevels' => $data->contextlevels,
 151              'allowassign'   => $data->allowassign,
 152              'allowoverride' => $data->allowoverride,
 153              'allowswitch'   => $data->allowswitch);
 154          if ($showadvanced) {
 155              $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
 156          } else {
 157              $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
 158          }
 159          if (is_number($resettype)) {
 160              // Duplicate the role.
 161              $definitiontable->force_duplicate($resettype, $options);
 162          } else {
 163              // Must be an archetype.
 164              $definitiontable->force_archetype($resettype, $options);
 165          }
 166  
 167          if ($xml = $mform->get_file_content('rolepreset')) {
 168              $definitiontable->force_preset($xml, $options);
 169          }
 170  
 171      } else {
 172          echo $OUTPUT->header();
 173          echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
 174          $mform->display();
 175          echo $OUTPUT->footer();
 176          die;
 177      }
 178  
 179  } else {
 180      // Create the table object.
 181      if ($action === 'view') {
 182          $definitiontable = new core_role_view_role_definition_table($systemcontext, $roleid);
 183      } else if ($showadvanced) {
 184          $definitiontable = new core_role_define_role_table_advanced($systemcontext, $roleid);
 185      } else {
 186          $definitiontable = new core_role_define_role_table_basic($systemcontext, $roleid);
 187      }
 188      $definitiontable->read_submitted_permissions();
 189  }
 190  
 191  // Handle the cancel button.
 192  if (optional_param('cancel', false, PARAM_BOOL)) {
 193      redirect($returnurl);
 194  }
 195  
 196  // Process submission in necessary.
 197  if (optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey() && $definitiontable->is_submission_valid()) {
 198      $definitiontable->save_changes();
 199      $tableroleid = $definitiontable->get_role_id();
 200      // Trigger event.
 201      $event = \core\event\role_capabilities_updated::create(
 202          array(
 203              'context' => $systemcontext,
 204              'objectid' => $tableroleid
 205          )
 206      );
 207      $event->set_legacy_logdata(array(SITEID, 'role', $action, 'admin/roles/define.php?action=view&roleid=' . $tableroleid,
 208          $definitiontable->get_role_name(), '', $USER->id));
 209      if (!empty($role)) {
 210          $event->add_record_snapshot('role', $role);
 211      }
 212      $event->trigger();
 213  
 214      if ($action === 'add') {
 215          redirect(new moodle_url('/admin/roles/define.php', array('action'=>'view', 'roleid'=>$definitiontable->get_role_id())));
 216      } else {
 217          redirect($returnurl);
 218      }
 219  }
 220  
 221  // Print the page header and tabs.
 222  echo $OUTPUT->header();
 223  
 224  $currenttab = 'manage';
 225  require ('managetabs.php');
 226  
 227  echo $OUTPUT->heading_with_help($title, 'roles', 'core_role');
 228  
 229  // Work out some button labels.
 230  if ($action === 'add') {
 231      $submitlabel = get_string('createthisrole', 'core_role');
 232  } else {
 233      $submitlabel = get_string('savechanges');
 234  }
 235  
 236  // On the view page, show some extra controls at the top.
 237  if ($action === 'view') {
 238      echo $OUTPUT->container_start('buttons');
 239      $url = new moodle_url('/admin/roles/define.php', array('action'=>'edit', 'roleid'=>$roleid, 'return'=>'define'));
 240      echo $OUTPUT->single_button(new moodle_url($url), get_string('edit'));
 241      $url = new moodle_url('/admin/roles/define.php', array('action'=>'reset', 'roleid'=>$roleid, 'return'=>'define'));
 242      echo $OUTPUT->single_button(new moodle_url($url), get_string('resetrole', 'core_role'));
 243      $url = new moodle_url('/admin/roles/define.php', array('action'=>'export', 'roleid'=>$roleid));
 244      echo $OUTPUT->single_button(new moodle_url($url), get_string('export', 'core_role'));
 245      echo $OUTPUT->single_button($manageurl, get_string('listallroles', 'core_role'));
 246      echo $OUTPUT->container_end();
 247  }
 248  
 249  // Start the form.
 250  echo $OUTPUT->box_start('generalbox');
 251  if ($action === 'view') {
 252      echo '<div class="mform">';
 253  } else {
 254      ?>
 255  <form id="rolesform" class="mform" action="<?php p($baseurl->out(false)); ?>" method="post"><div>
 256  <input type="hidden" name="sesskey" value="<?php p(sesskey()) ?>" />
 257  <input type="hidden" name="return" value="<?php p($return); ?>" />
 258  <input type="hidden" name="resettype" value="none" />
 259  <div class="submit buttons">
 260      <input type="submit" name="savechanges" value="<?php p($submitlabel); ?>" />
 261      <input type="submit" name="cancel" value="<?php print_string('cancel'); ?>" />
 262  </div>
 263      <?php
 264  }
 265  
 266  // Print the form controls.
 267  $definitiontable->display();
 268  
 269  // Close the stuff we left open above.
 270  if ($action === 'view') {
 271      echo '</div>';
 272  } else {
 273      ?>
 274  <div class="submit buttons">
 275      <input type="submit" name="savechanges" value="<?php p($submitlabel); ?>" />
 276      <input type="submit" name="cancel" value="<?php print_string('cancel'); ?>" />
 277  </div>
 278  </div></form>
 279  <?php
 280  }
 281  echo $OUTPUT->box_end();
 282  
 283  // Print a link back to the all roles list.
 284  echo '<div class="backlink">';
 285  echo '<p><a href="' . s($manageurl->out(false)) . '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
 286  echo '</div>';
 287  
 288  echo $OUTPUT->footer();


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1