Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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  $returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
  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  
  57  navigation_node::override_active_url($url);
  58  $pageurl = new moodle_url($url);
  59  if ($returnurl) {
  60      $pageurl->param('returnurl', $returnurl);
  61  }
  62  $PAGE->set_url($pageurl);
  63  $PAGE->set_context($context);
  64  $PAGE->activityheader->disable();
  65  
  66  $contextname = $context->get_context_name();
  67  $courseid = $course->id;
  68  
  69  // These are needed early because of tabs.php.
  70  list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
  71  $overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
  72  
  73  // Make sure this user can assign this role.
  74  if ($roleid && !isset($assignableroles[$roleid])) {
  75      $a = new stdClass;
  76      $a->roleid = $roleid;
  77      $a->context = $contextname;
  78      throw new \moodle_exception('cannotassignrolehere', '', $context->get_url(), $a);
  79  }
  80  
  81  // Work out an appropriate page title.
  82  if ($roleid) {
  83      $a = new stdClass;
  84      $a->role = $assignableroles[$roleid];
  85      $a->context = $contextname;
  86      $title = get_string('assignrolenameincontext', 'core_role', $a);
  87  } else {
  88      if ($isfrontpage) {
  89          $title = get_string('frontpageroles', 'admin');
  90      } else {
  91          $title = get_string('assignrolesin', 'core_role', $contextname);
  92      }
  93  }
  94  
  95  // Process any incoming role assignments before printing the header.
  96  if ($roleid) {
  97  
  98      // Create the user selector objects.
  99      $options = array('context' => $context, 'roleid' => $roleid);
 100  
 101      $potentialuserselector = core_role_get_potential_user_selector($context, 'addselect', $options);
 102      $currentuserselector = new core_role_existing_role_holders('removeselect', $options);
 103  
 104      // Process incoming role assignments.
 105      $errors = array();
 106      if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
 107          $userstoassign = $potentialuserselector->get_selected_users();
 108          if (!empty($userstoassign)) {
 109  
 110              foreach ($userstoassign as $adduser) {
 111                  $allow = true;
 112  
 113                  if ($allow) {
 114                      role_assign($roleid, $adduser->id, $context->id);
 115                  }
 116              }
 117  
 118              $potentialuserselector->invalidate_selected_users();
 119              $currentuserselector->invalidate_selected_users();
 120  
 121              // Counts have changed, so reload.
 122              list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
 123          }
 124      }
 125  
 126      // Process incoming role unassignments.
 127      if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
 128          $userstounassign = $currentuserselector->get_selected_users();
 129          if (!empty($userstounassign)) {
 130  
 131              foreach ($userstounassign as $removeuser) {
 132                  // Unassign only roles that are added manually, no messing with other components!!!
 133                  role_unassign($roleid, $removeuser->id, $context->id, '');
 134              }
 135  
 136              $potentialuserselector->invalidate_selected_users();
 137              $currentuserselector->invalidate_selected_users();
 138  
 139              // Counts have changed, so reload.
 140              list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
 141          }
 142      }
 143  }
 144  
 145  if (!empty($user) && ($user->id != $USER->id)) {
 146      $PAGE->navigation->extend_for_user($user);
 147      $PAGE->navbar->includesettingsbase = true;
 148  }
 149  
 150  $PAGE->set_pagelayout('admin');
 151  if ($context->contextlevel == CONTEXT_BLOCK) {
 152      // Do not show blocks when changing block's settings, it is confusing.
 153      $PAGE->blocks->show_only_fake_blocks(true);
 154  }
 155  $PAGE->set_title($title);
 156  
 157  switch ($context->contextlevel) {
 158      case CONTEXT_SYSTEM:
 159          require_once($CFG->libdir.'/adminlib.php');
 160          admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
 161          break;
 162      case CONTEXT_USER:
 163          $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
 164          $PAGE->set_heading($fullname);
 165          $showroles = 1;
 166          break;
 167      case CONTEXT_COURSECAT:
 168          core_course_category::page_setup();
 169          break;
 170      case CONTEXT_COURSE:
 171          if ($isfrontpage) {
 172              $PAGE->set_heading(get_string('frontpage', 'admin'));
 173          } else {
 174              $PAGE->set_heading($course->fullname);
 175          }
 176          break;
 177      case CONTEXT_MODULE:
 178          $PAGE->set_heading($context->get_context_name(false));
 179          $PAGE->set_cacheable(false);
 180          break;
 181      case CONTEXT_BLOCK:
 182          $PAGE->set_heading($PAGE->course->fullname);
 183          break;
 184  }
 185  
 186  $PAGE->set_navigation_overflow_state(false);
 187  
 188  // Within a course context we need to explicitly set active tab as there isn't a reference in the nav tree.
 189  if ($context->contextlevel == CONTEXT_COURSE) {
 190      $PAGE->set_secondary_active_tab('participants');
 191  }
 192  echo $OUTPUT->header();
 193  
 194  $backurl = null;
 195  // We are looking at a particular role. The page URL has been set correctly.
 196  if ($roleid) {
 197      $backurl = $pageurl;
 198  } else if ($context->contextlevel == CONTEXT_COURSE && !$isfrontpage) {
 199      // Return to the intermediary page when within the course context.
 200      $backurl = new moodle_url('/enrol/otherusers.php', ['id' => $course->id]);
 201  } else if ($returnurl) {
 202      // Factor in for $returnurl being passed.
 203      $backurl = new moodle_url($returnurl);
 204  }
 205  
 206  if ($backurl) {
 207      $backbutton = new single_button($backurl, get_string('back'), 'get');
 208      $backbutton->class = 'singlebutton navitem';
 209      echo html_writer::tag('div', $OUTPUT->render($backbutton), ['class' => 'tertiary-navigation']);
 210  } else if (in_array($context->contextlevel, [CONTEXT_COURSE, CONTEXT_MODULE, CONTEXT_COURSECAT])) {
 211      // The front page doesn't have an intermediate page 'other users' but needs similar tertiary nav like a standard course.
 212      echo $OUTPUT->render_participants_tertiary_nav($course);
 213  }
 214  
 215  // Print heading.
 216  echo $OUTPUT->heading_with_help($title, 'assignroles', 'core_role');
 217  
 218  if ($roleid) {
 219      // Show UI for assigning a particular role to users.
 220      // Print a warning if we are assigning system roles.
 221      if ($context->contextlevel == CONTEXT_SYSTEM) {
 222          echo $OUTPUT->notification(get_string('globalroleswarning', 'core_role'));
 223      }
 224  
 225      // Print the form.
 226      $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
 227  ?>
 228  <form id="assignform" method="post" action="<?php echo $assignurl ?>"><div>
 229    <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
 230  
 231    <table id="assigningrole" summary="" class="admintable roleassigntable generaltable" cellspacing="0">
 232      <tr>
 233        <td id="existingcell">
 234            <p><label for="removeselect"><?php print_string('extusers', 'core_role'); ?></label></p>
 235            <?php $currentuserselector->display() ?>
 236        </td>
 237        <td id="buttonscell">
 238            <div id="addcontrols">
 239                <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
 240                       title="<?php print_string('add'); ?>" class="btn btn-secondary"/><br />
 241            </div>
 242  
 243            <div id="removecontrols">
 244                <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
 245                       title="<?php print_string('remove'); ?>" class="btn btn-secondary"/>
 246            </div>
 247        </td>
 248        <td id="potentialcell">
 249            <p><label for="addselect"><?php print_string('potusers', 'core_role'); ?></label></p>
 250            <?php $potentialuserselector->display() ?>
 251        </td>
 252      </tr>
 253    </table>
 254  </div></form>
 255  
 256  <?php
 257      $PAGE->requires->js_init_call('M.core_role.init_add_assign_page');
 258  
 259      if (!empty($errors)) {
 260          $msg = '<p>';
 261          foreach ($errors as $e) {
 262              $msg .= $e.'<br />';
 263          }
 264          $msg .= '</p>';
 265          echo $OUTPUT->box_start();
 266          echo $OUTPUT->notification($msg);
 267          echo $OUTPUT->box_end();
 268      }
 269  
 270      // Print a form to swap roles, and a link back to the all roles list.
 271      echo '<div class="backlink">';
 272  
 273      $select = new single_select($PAGE->url, 'roleid', $nameswithcounts, $roleid, null);
 274      $select->label = get_string('assignanotherrole', 'core_role');
 275      echo $OUTPUT->render($select);
 276      echo '</div>';
 277  
 278  } else if (empty($assignableroles)) {
 279      // Print a message that there are no roles that can me assigned here.
 280      echo $OUTPUT->heading(get_string('notabletoassignroleshere', 'core_role'), 3);
 281  
 282  } else {
 283      // Show UI for choosing a role to assign.
 284  
 285      // Print a warning if we are assigning system roles.
 286      if ($context->contextlevel == CONTEXT_SYSTEM) {
 287          echo $OUTPUT->notification(get_string('globalroleswarning', 'core_role'));
 288      }
 289  
 290      // Print instruction.
 291      echo $OUTPUT->heading(get_string('chooseroletoassign', 'core_role'), 3);
 292  
 293      // Get the names of role holders for roles with between 1 and MAX_USERS_TO_LIST_PER_ROLE users,
 294      // and so determine whether to show the extra column.
 295      $roleholdernames = array();
 296      $strmorethanmax = get_string('morethan', 'core_role', MAX_USERS_TO_LIST_PER_ROLE);
 297      $showroleholders = false;
 298      foreach ($assignableroles as $roleid => $notused) {
 299          $roleusers = '';
 300          if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE) {
 301              $userfieldsapi = \core_user\fields::for_name();
 302              $userfields = 'u.id, u.username' . $userfieldsapi->get_sql('u')->selects;
 303              $roleusers = get_role_users($roleid, $context, false, $userfields);
 304              if (!empty($roleusers)) {
 305                  $strroleusers = array();
 306                  foreach ($roleusers as $user) {
 307                      $strroleusers[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '" >' . fullname($user) . '</a>';
 308                  }
 309                  $roleholdernames[$roleid] = implode('<br />', $strroleusers);
 310                  $showroleholders = true;
 311              }
 312          } else if ($assigncounts[$roleid] > MAX_USERS_TO_LIST_PER_ROLE) {
 313              $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
 314              $roleholdernames[$roleid] = '<a href="'.$assignurl.'">'.$strmorethanmax.'</a>';
 315          } else {
 316              $roleholdernames[$roleid] = '';
 317          }
 318      }
 319  
 320      // Print overview table.
 321      $table = new html_table();
 322      $table->id = 'assignrole';
 323      $table->head = array(get_string('role'), get_string('description'), get_string('userswiththisrole', 'core_role'));
 324      $table->colclasses = array('leftalign role', 'leftalign', 'centeralign userrole');
 325      $table->attributes['class'] = 'admintable generaltable';
 326      if ($showroleholders) {
 327          $table->headspan = array(1, 1, 2);
 328          $table->colclasses[] = 'leftalign roleholder';
 329      }
 330  
 331      foreach ($assignableroles as $roleid => $rolename) {
 332          $description = format_string($DB->get_field('role', 'description', array('id'=>$roleid)));
 333          $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
 334          $row = array('<a href="'.$assignurl.'">'.$rolename.'</a>',
 335                  $description, $assigncounts[$roleid]);
 336          if ($showroleholders) {
 337              $row[] = $roleholdernames[$roleid];
 338          }
 339          $table->data[] = $row;
 340      }
 341  
 342      echo html_writer::table($table);
 343  
 344      if (!$PAGE->has_secondary_navigation() && $context->contextlevel > CONTEXT_USER) {
 345          if (!$returnurl) {
 346              $url = $context->get_url();
 347              echo html_writer::start_tag('div', array('class' => 'backlink'));
 348              echo html_writer::tag('a', get_string('backto', '', $contextname), array('href' => $url));
 349              echo html_writer::end_tag('div');
 350          }
 351      }
 352  }
 353  
 354  echo $OUTPUT->footer();