[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/roles/ -> manage.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 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  // Check access permissions.
  50  $systemcontext = context_system::instance();
  51  require_login();
  52  require_capability('moodle/role:manage', $systemcontext);
  53  admin_externalpage_setup('defineroles');
  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  // Process submitted data.
  64  $confirmed = (optional_param('confirm', false, PARAM_BOOL) && data_submitted() && confirm_sesskey());
  65  switch ($action) {
  66      case 'delete':
  67          if (isset($undeletableroles[$roleid])) {
  68              print_error('cannotdeletethisrole', '', $baseurl);
  69          }
  70          if (!$confirmed) {
  71              // Show confirmation.
  72              echo $OUTPUT->header();
  73              $optionsyes = array('action'=>'delete', 'roleid'=>$roleid, 'sesskey'=>sesskey(), 'confirm'=>1);
  74              $a = new stdClass();
  75              $a->id = $roleid;
  76              $a->name = $roles[$roleid]->name;
  77              $a->shortname = $roles[$roleid]->shortname;
  78              $a->count = $DB->count_records_select('role_assignments',
  79                  'roleid = ?', array($roleid), 'COUNT(DISTINCT userid)');
  80  
  81              $formcontinue = new single_button(new moodle_url($baseurl, $optionsyes), get_string('yes'));
  82              $formcancel = new single_button(new moodle_url($baseurl), get_string('no'), 'get');
  83              echo $OUTPUT->confirm(get_string('deleterolesure', 'core_role', $a), $formcontinue, $formcancel);
  84              echo $OUTPUT->footer();
  85              die;
  86          }
  87          if (!delete_role($roleid)) {
  88              // The delete failed, but mark the context dirty in case.
  89              $systemcontext->mark_dirty();
  90              print_error('cannotdeleterolewithid', 'error', $baseurl, $roleid);
  91          }
  92          // Deleted a role sitewide...
  93          $systemcontext->mark_dirty();
  94          redirect($baseurl);
  95          break;
  96  
  97      case 'moveup':
  98          if (confirm_sesskey()) {
  99              $prevrole = null;
 100              $thisrole = null;
 101              foreach ($roles as $role) {
 102                  if ($role->id == $roleid) {
 103                      $thisrole = $role;
 104                      break;
 105                  } else {
 106                      $prevrole = $role;
 107                  }
 108              }
 109              if (is_null($thisrole) || is_null($prevrole)) {
 110                  print_error('cannotmoverolewithid', 'error', '', $roleid);
 111              }
 112              if (!switch_roles($thisrole, $prevrole)) {
 113                  print_error('cannotmoverolewithid', 'error', '', $roleid);
 114              }
 115          }
 116  
 117          redirect($baseurl);
 118          break;
 119  
 120      case 'movedown':
 121          if (confirm_sesskey()) {
 122              $thisrole = null;
 123              $nextrole = null;
 124              foreach ($roles as $role) {
 125                  if ($role->id == $roleid) {
 126                      $thisrole = $role;
 127                  } else if (!is_null($thisrole)) {
 128                      $nextrole = $role;
 129                      break;
 130                  }
 131              }
 132              if (is_null($nextrole)) {
 133                  print_error('cannotmoverolewithid', 'error', '', $roleid);
 134              }
 135              if (!switch_roles($thisrole, $nextrole)) {
 136                  print_error('cannotmoverolewithid', 'error', '', $roleid);
 137              }
 138          }
 139  
 140          redirect($baseurl);
 141          break;
 142  
 143  }
 144  
 145  // Print the page header and tabs.
 146  echo $OUTPUT->header();
 147  
 148  $currenttab = 'manage';
 149  require ('managetabs.php');
 150  
 151  // Initialise table.
 152  $table = new html_table();
 153  $table->colclasses = array('leftalign', 'leftalign', 'leftalign', 'leftalign');
 154  $table->id = 'roles';
 155  $table->attributes['class'] = 'admintable generaltable';
 156  $table->head = array(
 157      get_string('role') . ' ' . $OUTPUT->help_icon('roles', 'core_role'),
 158      get_string('description'),
 159      get_string('roleshortname', 'core_role'),
 160      get_string('edit')
 161  );
 162  
 163  // Get some strings outside the loop.
 164  $stredit = get_string('edit');
 165  $strdelete = get_string('delete');
 166  $strmoveup = get_string('moveup');
 167  $strmovedown = get_string('movedown');
 168  
 169  // Print a list of roles with edit/copy/delete/reorder icons.
 170  $table->data = array();
 171  $firstrole = reset($roles);
 172  $lastrole = end($roles);
 173  foreach ($roles as $role) {
 174      // Basic data.
 175      $row = array(
 176          '<a href="' . $defineurl . '?action=view&amp;roleid=' . $role->id . '">' . $role->localname . '</a>',
 177          role_get_description($role),
 178          s($role->shortname),
 179          '',
 180      );
 181  
 182      // Move up.
 183      if ($role->sortorder != $firstrole->sortorder) {
 184          $row[3] .= get_action_icon($baseurl . '?action=moveup&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'up', $strmoveup, $strmoveup);
 185      } else {
 186          $row[3] .= get_spacer();
 187      }
 188      // Move down.
 189      if ($role->sortorder != $lastrole->sortorder) {
 190          $row[3] .= get_action_icon($baseurl . '?action=movedown&amp;roleid=' . $role->id . '&amp;sesskey=' . sesskey(), 'down', $strmovedown, $strmovedown);
 191      } else {
 192          $row[3] .= get_spacer();
 193      }
 194      // Edit.
 195      $row[3] .= get_action_icon($defineurl . '?action=edit&amp;roleid=' . $role->id,
 196              'edit', $stredit, get_string('editxrole', 'core_role', $role->localname));
 197      // Delete.
 198      if (isset($undeletableroles[$role->id])) {
 199          $row[3] .= get_spacer();
 200      } else {
 201          $row[3] .= get_action_icon($baseurl . '?action=delete&amp;roleid=' . $role->id,
 202                'delete', $strdelete, get_string('deletexrole', 'core_role', $role->localname));
 203      }
 204  
 205      $table->data[] = $row;
 206  }
 207  echo html_writer::table($table);
 208  
 209  echo $OUTPUT->container_start('buttons');
 210  echo $OUTPUT->single_button(new moodle_url($defineurl, array('action' => 'add')), get_string('addrole', 'core_role'), 'get');
 211  echo $OUTPUT->container_end();
 212  
 213  echo $OUTPUT->footer();
 214  die;
 215  
 216  function get_action_icon($url, $icon, $alt, $tooltip) {
 217      global $OUTPUT;
 218      return '<a title="' . $tooltip . '" href="'. $url . '">' .
 219              '<img src="' . $OUTPUT->pix_url('t/' . $icon) . '" class="iconsmall" alt="' . $alt . '" /></a> ';
 220  }
 221  function get_spacer() {
 222      global $OUTPUT;
 223      return '<img src="' . $OUTPUT->pix_url('spacer') . '" class="iconsmall" alt="" /> ';
 224  }


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