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);
65 $contextname = $context->get_context_name();
66 $courseid = $course->id
;
68 // These are needed early because of tabs.php.
69 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH
, true);
70 $overridableroles = get_overridable_roles($context, ROLENAME_BOTH
);
72 // Make sure this user can assign this role.
73 if ($roleid && !isset($assignableroles[$roleid])) {
76 $a->context
= $contextname;
77 print_error('cannotassignrolehere', '', $context->get_url(), $a);
80 // Work out an appropriate page title.
83 $a->role
= $assignableroles[$roleid];
84 $a->context
= $contextname;
85 $title = get_string('assignrolenameincontext', 'core_role', $a);
88 $title = get_string('frontpageroles', 'admin');
90 $title = get_string('assignrolesin', 'core_role', $contextname);
94 // Process any incoming role assignments before printing the header.
97 // Create the user selector objects.
98 $options = array('context' => $context, 'roleid' => $roleid);
100 $potentialuserselector = core_role_get_potential_user_selector($context, 'addselect', $options);
101 $currentuserselector = new core_role_existing_role_holders('removeselect', $options);
103 // Process incoming role assignments.
105 if (optional_param('add', false, PARAM_BOOL
) && confirm_sesskey()) {
106 $userstoassign = $potentialuserselector->get_selected_users();
107 if (!empty($userstoassign)) {
109 foreach ($userstoassign as $adduser) {
113 role_assign($roleid, $adduser->id
, $context->id
);
117 $potentialuserselector->invalidate_selected_users();
118 $currentuserselector->invalidate_selected_users();
120 // Counts have changed, so reload.
121 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH
, true);
125 // Process incoming role unassignments.
126 if (optional_param('remove', false, PARAM_BOOL
) && confirm_sesskey()) {
127 $userstounassign = $currentuserselector->get_selected_users();
128 if (!empty($userstounassign)) {
130 foreach ($userstounassign as $removeuser) {
131 // Unassign only roles that are added manually, no messing with other components!!!
132 role_unassign($roleid, $removeuser->id
, $context->id
, '');
135 $potentialuserselector->invalidate_selected_users();
136 $currentuserselector->invalidate_selected_users();
138 // Counts have changed, so reload.
139 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH
, true);
144 if (!empty($user) && ($user->id
!= $USER->id
)) {
145 $PAGE->navigation
->extend_for_user($user);
146 $PAGE->navbar
->includesettingsbase
= true;
149 $PAGE->set_pagelayout('admin');
150 if ($context->contextlevel
== CONTEXT_BLOCK
) {
151 // Do not show blocks when changing block's settings, it is confusing.
152 $PAGE->blocks
->show_only_fake_blocks(true);
154 $PAGE->set_title($title);
156 switch ($context->contextlevel
) {
158 require_once($CFG->libdir
.'/adminlib.php');
159 admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
162 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
163 $PAGE->set_heading($fullname);
166 case CONTEXT_COURSECAT
:
167 $PAGE->set_heading($SITE->fullname
);
171 $PAGE->set_heading(get_string('frontpage', 'admin'));
173 $PAGE->set_heading($course->fullname
);
177 $PAGE->set_heading($context->get_context_name(false));
178 $PAGE->set_cacheable(false);
181 $PAGE->set_heading($PAGE->course
->fullname
);
185 echo $OUTPUT->header();
188 echo $OUTPUT->heading_with_help($title, 'assignroles', 'core_role');
191 // Show UI for assigning a particular role to users.
192 // Print a warning if we are assigning system roles.
193 if ($context->contextlevel
== CONTEXT_SYSTEM
) {
194 echo $OUTPUT->notification(get_string('globalroleswarning', 'core_role'));
198 $assignurl = new moodle_url($PAGE->url
, array('roleid'=>$roleid));
200 <form id
="assignform" method
="post" action
="<?php echo $assignurl ?>"><div
>
201 <input type
="hidden" name
="sesskey" value
="<?php echo sesskey() ?>" />
203 <table id
="assigningrole" summary
="" class="admintable roleassigntable generaltable" cellspacing
="0">
205 <td id
="existingcell">
206 <p
><label
for="removeselect"><?php
print_string('extusers', 'core_role'); ?
></label
></p
>
207 <?php
$currentuserselector->display() ?
>
209 <td id
="buttonscell">
210 <div id
="addcontrols">
211 <input name
="add" id
="add" type
="submit" value
="<?php echo $OUTPUT->larrow().' '.get_string('add'); ?>"
212 title
="<?php print_string('add'); ?>" class="btn btn-secondary"/><br
/>
215 <div id
="removecontrols">
216 <input name
="remove" id
="remove" type
="submit" value
="<?php echo get_string('remove').' '.$OUTPUT->rarrow(); ?>"
217 title
="<?php print_string('remove'); ?>" class="btn btn-secondary"/>
220 <td id
="potentialcell">
221 <p
><label
for="addselect"><?php
print_string('potusers', 'core_role'); ?
></label
></p
>
222 <?php
$potentialuserselector->display() ?
>
229 $PAGE->requires
->js_init_call('M.core_role.init_add_assign_page');
231 if (!empty($errors)) {
233 foreach ($errors as $e) {
237 echo $OUTPUT->box_start();
238 echo $OUTPUT->notification($msg);
239 echo $OUTPUT->box_end();
242 // Print a form to swap roles, and a link back to the all roles list.
243 echo '<div class="backlink">';
245 $select = new single_select($PAGE->url
, 'roleid', $nameswithcounts, $roleid, null);
246 $select->label
= get_string('assignanotherrole', 'core_role');
247 echo $OUTPUT->render($select);
248 echo '<p><a href="' . $PAGE->url
. '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
251 } else if (empty($assignableroles)) {
252 // Print a message that there are no roles that can me assigned here.
253 echo $OUTPUT->heading(get_string('notabletoassignroleshere', 'core_role'), 3);
256 // Show UI for choosing a role to assign.
258 // Print a warning if we are assigning system roles.
259 if ($context->contextlevel
== CONTEXT_SYSTEM
) {
260 echo $OUTPUT->notification(get_string('globalroleswarning', 'core_role'));
263 // Print instruction.
264 echo $OUTPUT->heading(get_string('chooseroletoassign', 'core_role'), 3);
266 // Get the names of role holders for roles with between 1 and MAX_USERS_TO_LIST_PER_ROLE users,
267 // and so determine whether to show the extra column.
268 $roleholdernames = array();
269 $strmorethanmax = get_string('morethan', 'core_role', MAX_USERS_TO_LIST_PER_ROLE
);
270 $showroleholders = false;
271 foreach ($assignableroles as $roleid => $notused) {
273 if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE
) {
274 $userfields = 'u.id, u.username, ' . get_all_user_name_fields(true, 'u');
275 $roleusers = get_role_users($roleid, $context, false, $userfields);
276 if (!empty($roleusers)) {
277 $strroleusers = array();
278 foreach ($roleusers as $user) {
279 $strroleusers[] = '<a href="' . $CFG->wwwroot
. '/user/view.php?id=' . $user->id
. '" >' . fullname($user) . '</a>';
281 $roleholdernames[$roleid] = implode('<br />', $strroleusers);
282 $showroleholders = true;
284 } else if ($assigncounts[$roleid] > MAX_USERS_TO_LIST_PER_ROLE
) {
285 $assignurl = new moodle_url($PAGE->url
, array('roleid'=>$roleid));
286 $roleholdernames[$roleid] = '<a href="'.$assignurl.'">'.$strmorethanmax.'</a>';
288 $roleholdernames[$roleid] = '';
292 // Print overview table.
293 $table = new html_table();
294 $table->id
= 'assignrole';
295 $table->head
= array(get_string('role'), get_string('description'), get_string('userswiththisrole', 'core_role'));
296 $table->colclasses
= array('leftalign role', 'leftalign', 'centeralign userrole');
297 $table->attributes
['class'] = 'admintable generaltable';
298 if ($showroleholders) {
299 $table->headspan
= array(1, 1, 2);
300 $table->colclasses
[] = 'leftalign roleholder';
303 foreach ($assignableroles as $roleid => $rolename) {
304 $description = format_string($DB->get_field('role', 'description', array('id'=>$roleid)));
305 $assignurl = new moodle_url($PAGE->url
, array('roleid'=>$roleid));
306 $row = array('<a href="'.$assignurl.'">'.$rolename.'</a>',
307 $description, $assigncounts[$roleid]);
308 if ($showroleholders) {
309 $row[] = $roleholdernames[$roleid];
311 $table->data
[] = $row;
314 echo html_writer
::table($table);
316 if ($context->contextlevel
> CONTEXT_USER
) {
319 $url = new moodle_url($returnurl);
321 $url = $context->get_url();
324 echo html_writer
::start_tag('div', array('class'=>'backlink'));
325 echo html_writer
::tag('a', get_string('backto', '', $contextname), array('href' => $url));
326 echo html_writer
::end_tag('div');
330 echo $OUTPUT->footer();