2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Assign roles to users.
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__
. '/../../config.php');
26 require_once($CFG->dirroot
. '/' . $CFG->admin
. '/roles/lib.php');
28 define("MAX_USERS_TO_LIST_PER_ROLE", 10);
30 $contextid = required_param('contextid', PARAM_INT
);
31 $roleid = optional_param('roleid', 0, PARAM_INT
);
32 $returnurl = optional_param('returnurl', null, PARAM_LOCALURL
);
34 list($context, $course, $cm) = get_context_info_array($contextid);
36 $url = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
39 $isfrontpage = ($course->id
== SITEID
);
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
);
54 require_login($course, false, $cm);
55 require_capability('moodle/role:assign', $context);
57 navigation_node
::override_active_url($url);
58 $pageurl = new moodle_url($url);
60 $pageurl->param('returnurl', $returnurl);
62 $PAGE->set_url($pageurl);
63 $PAGE->set_context($context);
64 $PAGE->activityheader
->disable();
66 $contextname = $context->get_context_name();
67 $courseid = $course->id
;
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
);
73 // Make sure this user can assign this role.
74 if ($roleid && !isset($assignableroles[$roleid])) {
77 $a->context
= $contextname;
78 print_error('cannotassignrolehere', '', $context->get_url(), $a);
81 // Work out an appropriate page title.
84 $a->role
= $assignableroles[$roleid];
85 $a->context
= $contextname;
86 $title = get_string('assignrolenameincontext', 'core_role', $a);
89 $title = get_string('frontpageroles', 'admin');
91 $title = get_string('assignrolesin', 'core_role', $contextname);
95 // Process any incoming role assignments before printing the header.
98 // Create the user selector objects.
99 $options = array('context' => $context, 'roleid' => $roleid);
101 $potentialuserselector = core_role_get_potential_user_selector($context, 'addselect', $options);
102 $currentuserselector = new core_role_existing_role_holders('removeselect', $options);
104 // Process incoming role assignments.
106 if (optional_param('add', false, PARAM_BOOL
) && confirm_sesskey()) {
107 $userstoassign = $potentialuserselector->get_selected_users();
108 if (!empty($userstoassign)) {
110 foreach ($userstoassign as $adduser) {
114 role_assign($roleid, $adduser->id
, $context->id
);
118 $potentialuserselector->invalidate_selected_users();
119 $currentuserselector->invalidate_selected_users();
121 // Counts have changed, so reload.
122 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH
, true);
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)) {
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
, '');
136 $potentialuserselector->invalidate_selected_users();
137 $currentuserselector->invalidate_selected_users();
139 // Counts have changed, so reload.
140 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH
, true);
145 if (!empty($user) && ($user->id
!= $USER->id
)) {
146 $PAGE->navigation
->extend_for_user($user);
147 $PAGE->navbar
->includesettingsbase
= true;
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);
155 $PAGE->set_title($title);
157 switch ($context->contextlevel
) {
159 require_once($CFG->libdir
.'/adminlib.php');
160 admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
163 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
164 $PAGE->set_heading($fullname);
167 case CONTEXT_COURSECAT
:
168 $PAGE->set_heading($SITE->fullname
);
172 $PAGE->set_heading(get_string('frontpage', 'admin'));
174 $PAGE->set_heading($course->fullname
);
178 $PAGE->set_heading($context->get_context_name(false));
179 $PAGE->set_cacheable(false);
182 $PAGE->set_heading($PAGE->course
->fullname
);
186 $PAGE->set_navigation_overflow_state(false);
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');
192 echo $OUTPUT->header();
195 // We are looking at a particular role. The page URL has been set correctly.
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);
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);
216 echo $OUTPUT->heading_with_help($title, 'assignroles', 'core_role');
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'));
226 $assignurl = new moodle_url($PAGE->url
, array('roleid'=>$roleid));
228 <form id
="assignform" method
="post" action
="<?php echo $assignurl ?>"><div
>
229 <input type
="hidden" name
="sesskey" value
="<?php echo sesskey() ?>" />
231 <table id
="assigningrole" summary
="" class="admintable roleassigntable generaltable" cellspacing
="0">
233 <td id
="existingcell">
234 <p
><label
for="removeselect"><?php
print_string('extusers', 'core_role'); ?
></label
></p
>
235 <?php
$currentuserselector->display() ?
>
237 <td id
="buttonscell">
238 <div id
="addcontrols">
239 <input name
="add" id
="add" type
="submit" value
="<?php echo $OUTPUT->larrow().' '.get_string('add'); ?>"
240 title
="<?php print_string('add'); ?>" class="btn btn-secondary"/><br
/>
243 <div id
="removecontrols">
244 <input name
="remove" id
="remove" type
="submit" value
="<?php echo get_string('remove').' '.$OUTPUT->rarrow(); ?>"
245 title
="<?php print_string('remove'); ?>" class="btn btn-secondary"/>
248 <td id
="potentialcell">
249 <p
><label
for="addselect"><?php
print_string('potusers', 'core_role'); ?
></label
></p
>
250 <?php
$potentialuserselector->display() ?
>
257 $PAGE->requires
->js_init_call('M.core_role.init_add_assign_page');
259 if (!empty($errors)) {
261 foreach ($errors as $e) {
265 echo $OUTPUT->box_start();
266 echo $OUTPUT->notification($msg);
267 echo $OUTPUT->box_end();
270 // Print a form to swap roles, and a link back to the all roles list.
271 echo '<div class="backlink">';
273 $select = new single_select($PAGE->url
, 'roleid', $nameswithcounts, $roleid, null);
274 $select->label
= get_string('assignanotherrole', 'core_role');
275 echo $OUTPUT->render($select);
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);
283 // Show UI for choosing a role to assign.
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'));
290 // Print instruction.
291 echo $OUTPUT->heading(get_string('chooseroletoassign', 'core_role'), 3);
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) {
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>';
309 $roleholdernames[$roleid] = implode('<br />', $strroleusers);
310 $showroleholders = true;
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>';
316 $roleholdernames[$roleid] = '';
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';
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];
339 $table->data
[] = $row;
342 echo html_writer
::table($table);
344 if (!$PAGE->has_secondary_navigation() && $context->contextlevel
> CONTEXT_USER
) {
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');
354 echo $OUTPUT->footer();