[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/roles/ -> assign.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   * Assign roles to users.
  19   *
  20   * @package    core_role
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  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 . '/roles/lib.php');
  27  
  28  define("MAX_USERS_TO_LIST_PER_ROLE", 10);
  29  
  30  $contextid = required_param('contextid', PARAM_INT);
  31  $roleid    = optional_param('roleid', 0, PARAM_INT);
  32  $returnto  = optional_param('return', null, PARAM_ALPHANUMEXT);
  33  
  34  list($context, $course, $cm) = get_context_info_array($contextid);
  35  
  36  $url = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
  37  
  38  if ($course) {
  39      $isfrontpage = ($course->id == SITEID);
  40  } else {
  41      $isfrontpage = false;
  42      if ($context->contextlevel == CONTEXT_USER) {
  43          $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
  44          $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
  45          $url->param('courseid', $course->id);
  46          $url->param('userid', $user->id);
  47      } else {
  48          $course = $SITE;
  49      }
  50  }
  51  
  52  
  53  // Security.
  54  require_login($course, false, $cm);
  55  require_capability('moodle/role:assign', $context);
  56  $PAGE->set_url($url);
  57  $PAGE->set_context($context);
  58  
  59  $contextname = $context->get_context_name();
  60  $courseid = $course->id;
  61  
  62  // These are needed early because of tabs.php.
  63  list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
  64  $overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
  65  
  66  // Make sure this user can assign this role.
  67  if ($roleid && !isset($assignableroles[$roleid])) {
  68      $a = new stdClass;
  69      $a->roleid = $roleid;
  70      $a->context = $contextname;
  71      print_error('cannotassignrolehere', '', $context->get_url(), $a);
  72  }
  73  
  74  // Work out an appropriate page title.
  75  if ($roleid) {
  76      $a = new stdClass;
  77      $a->role = $assignableroles[$roleid];
  78      $a->context = $contextname;
  79      $title = get_string('assignrolenameincontext', 'core_role', $a);
  80  } else {
  81      if ($isfrontpage) {
  82          $title = get_string('frontpageroles', 'admin');
  83      } else {
  84          $title = get_string('assignrolesin', 'core_role', $contextname);
  85      }
  86  }
  87  
  88  // Process any incoming role assignments before printing the header.
  89  if ($roleid) {
  90  
  91      // Create the user selector objects.
  92      $options = array('context' => $context, 'roleid' => $roleid);
  93  
  94      $potentialuserselector = core_role_get_potential_user_selector($context, 'addselect', $options);
  95      $currentuserselector = new core_role_existing_role_holders('removeselect', $options);
  96  
  97      // Process incoming role assignments.
  98      $errors = array();
  99      if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
 100          $userstoassign = $potentialuserselector->get_selected_users();
 101          if (!empty($userstoassign)) {
 102  
 103              foreach ($userstoassign as $adduser) {
 104                  $allow = true;
 105  
 106                  if ($allow) {
 107                      role_assign($roleid, $adduser->id, $context->id);
 108                  }
 109              }
 110  
 111              $potentialuserselector->invalidate_selected_users();
 112              $currentuserselector->invalidate_selected_users();
 113  
 114              // Counts have changed, so reload.
 115              list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
 116          }
 117      }
 118  
 119      // Process incoming role unassignments.
 120      if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
 121          $userstounassign = $currentuserselector->get_selected_users();
 122          if (!empty($userstounassign)) {
 123  
 124              foreach ($userstounassign as $removeuser) {
 125                  // Unassign only roles that are added manually, no messing with other components!!!
 126                  role_unassign($roleid, $removeuser->id, $context->id, '');
 127              }
 128  
 129              $potentialuserselector->invalidate_selected_users();
 130              $currentuserselector->invalidate_selected_users();
 131  
 132              // Counts have changed, so reload.
 133              list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
 134          }
 135      }
 136  }
 137  
 138  if (!empty($user) && ($user->id != $USER->id)) {
 139      $PAGE->navigation->extend_for_user($user);
 140      $PAGE->navbar->includesettingsbase = true;
 141  }
 142  
 143  $PAGE->set_pagelayout('admin');
 144  $PAGE->set_title($title);
 145  
 146  switch ($context->contextlevel) {
 147      case CONTEXT_SYSTEM:
 148          require_once($CFG->libdir.'/adminlib.php');
 149          admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
 150          break;
 151      case CONTEXT_USER:
 152          $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
 153          $PAGE->set_heading($fullname);
 154          $showroles = 1;
 155          break;
 156      case CONTEXT_COURSECAT:
 157          $PAGE->set_heading($SITE->fullname);
 158          break;
 159      case CONTEXT_COURSE:
 160          if ($isfrontpage) {
 161              $PAGE->set_heading(get_string('frontpage', 'admin'));
 162          } else {
 163              $PAGE->set_heading($course->fullname);
 164          }
 165          break;
 166      case CONTEXT_MODULE:
 167          $PAGE->set_heading($context->get_context_name(false));
 168          $PAGE->set_cacheable(false);
 169          break;
 170      case CONTEXT_BLOCK:
 171          $PAGE->set_heading($PAGE->course->fullname);
 172          break;
 173  }
 174  
 175  echo $OUTPUT->header();
 176  
 177  // Print heading.
 178  echo $OUTPUT->heading_with_help($title, 'assignroles', 'core_role');
 179  
 180  if ($roleid) {
 181      // Show UI for assigning a particular role to users.
 182      // Print a warning if we are assigning system roles.
 183      if ($context->contextlevel == CONTEXT_SYSTEM) {
 184          echo $OUTPUT->notification(get_string('globalroleswarning', 'core_role'));
 185      }
 186  
 187      // Print the form.
 188      $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
 189      if ($returnto !== null) {
 190          $assignurl->param('return', $returnto);
 191      }
 192  ?>
 193  <form id="assignform" method="post" action="<?php echo $assignurl ?>"><div>
 194    <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
 195  
 196    <table id="assigningrole" summary="" class="admintable roleassigntable generaltable" cellspacing="0">
 197      <tr>
 198        <td id="existingcell">
 199            <p><label for="removeselect"><?php print_string('extusers', 'core_role'); ?></label></p>
 200            <?php $currentuserselector->display() ?>
 201        </td>
 202        <td id="buttonscell">
 203            <div id="addcontrols">
 204                <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
 205            </div>
 206  
 207            <div id="removecontrols">
 208                <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
 209            </div>
 210        </td>
 211        <td id="potentialcell">
 212            <p><label for="addselect"><?php print_string('potusers', 'core_role'); ?></label></p>
 213            <?php $potentialuserselector->display() ?>
 214        </td>
 215      </tr>
 216    </table>
 217  </div></form>
 218  
 219  <?php
 220      $PAGE->requires->js_init_call('M.core_role.init_add_assign_page');
 221  
 222      if (!empty($errors)) {
 223          $msg = '<p>';
 224          foreach ($errors as $e) {
 225              $msg .= $e.'<br />';
 226          }
 227          $msg .= '</p>';
 228          echo $OUTPUT->box_start();
 229          echo $OUTPUT->notification($msg);
 230          echo $OUTPUT->box_end();
 231      }
 232  
 233      // Print a form to swap roles, and a link back to the all roles list.
 234      echo '<div class="backlink">';
 235  
 236      $newroleurl = new moodle_url($PAGE->url);
 237      if ($returnto !== null) {
 238          $newroleurl->param('return', $returnto);
 239      }
 240      $select = new single_select($newroleurl, 'roleid', $nameswithcounts, $roleid, null);
 241      $select->label = get_string('assignanotherrole', 'core_role');
 242      echo $OUTPUT->render($select);
 243      $backurl = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
 244      if ($returnto !== null) {
 245          $backurl->param('return', $returnto);
 246      }
 247      echo '<p><a href="' . $backurl->out() . '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
 248      echo '</div>';
 249  
 250  } else if (empty($assignableroles)) {
 251      // Print a message that there are no roles that can me assigned here.
 252      echo $OUTPUT->heading(get_string('notabletoassignroleshere', 'core_role'), 3);
 253  
 254  } else {
 255      // Show UI for choosing a role to assign.
 256  
 257      // Print a warning if we are assigning system roles.
 258      if ($context->contextlevel == CONTEXT_SYSTEM) {
 259          echo $OUTPUT->notification(get_string('globalroleswarning', 'core_role'));
 260      }
 261  
 262      // Print instruction.
 263      echo $OUTPUT->heading(get_string('chooseroletoassign', 'core_role'), 3);
 264  
 265      // Get the names of role holders for roles with between 1 and MAX_USERS_TO_LIST_PER_ROLE users,
 266      // and so determine whether to show the extra column.
 267      $roleholdernames = array();
 268      $strmorethanmax = get_string('morethan', 'core_role', MAX_USERS_TO_LIST_PER_ROLE);
 269      $showroleholders = false;
 270      foreach ($assignableroles as $roleid => $notused) {
 271          $roleusers = '';
 272          if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE) {
 273              $userfields = 'u.id, u.username, ' . get_all_user_name_fields(true, 'u');
 274              $roleusers = get_role_users($roleid, $context, false, $userfields);
 275              if (!empty($roleusers)) {
 276                  $strroleusers = array();
 277                  foreach ($roleusers as $user) {
 278                      $strroleusers[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '" >' . fullname($user) . '</a>';
 279                  }
 280                  $roleholdernames[$roleid] = implode('<br />', $strroleusers);
 281                  $showroleholders = true;
 282              }
 283          } else if ($assigncounts[$roleid] > MAX_USERS_TO_LIST_PER_ROLE) {
 284              $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
 285              if ($returnto !== null) {
 286                  $assignurl->param('return', $returnto);
 287              }
 288              $roleholdernames[$roleid] = '<a href="'.$assignurl.'">'.$strmorethanmax.'</a>';
 289          } else {
 290              $roleholdernames[$roleid] = '';
 291          }
 292      }
 293  
 294      // Print overview table.
 295      $table = new html_table();
 296      $table->id = 'assignrole';
 297      $table->head = array(get_string('role'), get_string('description'), get_string('userswiththisrole', 'core_role'));
 298      $table->colclasses = array('leftalign role', 'leftalign', 'centeralign userrole');
 299      $table->attributes['class'] = 'admintable generaltable';
 300      if ($showroleholders) {
 301          $table->headspan = array(1, 1, 2);
 302          $table->colclasses[] = 'leftalign roleholder';
 303      }
 304  
 305      foreach ($assignableroles as $roleid => $rolename) {
 306          $description = format_string($DB->get_field('role', 'description', array('id'=>$roleid)));
 307          $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
 308          if ($returnto !== null) {
 309              $assignurl->param('return', $returnto);
 310          }
 311          $row = array('<a href="'.$assignurl.'">'.$rolename.'</a>',
 312                  $description, $assigncounts[$roleid]);
 313          if ($showroleholders) {
 314              $row[] = $roleholdernames[$roleid];
 315          }
 316          $table->data[] = $row;
 317      }
 318  
 319      echo html_writer::table($table);
 320  
 321      if ($context->contextlevel > CONTEXT_USER) {
 322  
 323          if ($context->contextlevel === CONTEXT_COURSECAT && $returnto === 'management') {
 324              $url = new moodle_url('/course/management.php', array('categoryid' => $context->instanceid));
 325          } else {
 326              $url = $context->get_url();
 327          }
 328  
 329          echo html_writer::start_tag('div', array('class'=>'backlink'));
 330          echo html_writer::tag('a', get_string('backto', '', $contextname), array('href' => $url));
 331          echo html_writer::end_tag('div');
 332      }
 333  }
 334  
 335  echo $OUTPUT->footer();


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