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 $returnto = optional_param('return', null, PARAM_ALPHANUMEXT
);
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 $PAGE->set_context($context);
59 $contextname = $context->get_context_name();
60 $courseid = $course->id
;
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
);
66 // Make sure this user can assign this role.
67 if ($roleid && !isset($assignableroles[$roleid])) {
70 $a->context
= $contextname;
71 print_error('cannotassignrolehere', '', $context->get_url(), $a);
74 // Work out an appropriate page title.
77 $a->role
= $assignableroles[$roleid];
78 $a->context
= $contextname;
79 $title = get_string('assignrolenameincontext', 'core_role', $a);
82 $title = get_string('frontpageroles', 'admin');
84 $title = get_string('assignrolesin', 'core_role', $contextname);
88 // Process any incoming role assignments before printing the header.
91 // Create the user selector objects.
92 $options = array('context' => $context, 'roleid' => $roleid);
94 $potentialuserselector = core_role_get_potential_user_selector($context, 'addselect', $options);
95 $currentuserselector = new core_role_existing_role_holders('removeselect', $options);
97 // Process incoming role assignments.
99 if (optional_param('add', false, PARAM_BOOL
) && confirm_sesskey()) {
100 $userstoassign = $potentialuserselector->get_selected_users();
101 if (!empty($userstoassign)) {
103 foreach ($userstoassign as $adduser) {
107 role_assign($roleid, $adduser->id
, $context->id
);
111 $potentialuserselector->invalidate_selected_users();
112 $currentuserselector->invalidate_selected_users();
114 // Counts have changed, so reload.
115 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH
, true);
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)) {
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
, '');
129 $potentialuserselector->invalidate_selected_users();
130 $currentuserselector->invalidate_selected_users();
132 // Counts have changed, so reload.
133 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH
, true);
138 if (!empty($user) && ($user->id
!= $USER->id
)) {
139 $PAGE->navigation
->extend_for_user($user);
140 $PAGE->navbar
->includesettingsbase
= true;
143 $PAGE->set_pagelayout('admin');
144 $PAGE->set_title($title);
146 switch ($context->contextlevel
) {
148 require_once($CFG->libdir
.'/adminlib.php');
149 admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
152 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
153 $PAGE->set_heading($fullname);
156 case CONTEXT_COURSECAT
:
157 $PAGE->set_heading($SITE->fullname
);
161 $PAGE->set_heading(get_string('frontpage', 'admin'));
163 $PAGE->set_heading($course->fullname
);
167 $PAGE->set_heading($context->get_context_name(false));
168 $PAGE->set_cacheable(false);
171 $PAGE->set_heading($PAGE->course
->fullname
);
175 echo $OUTPUT->header();
178 echo $OUTPUT->heading_with_help($title, 'assignroles', 'core_role');
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'));
188 $assignurl = new moodle_url($PAGE->url
, array('roleid'=>$roleid));
189 if ($returnto !== null) {
190 $assignurl->param('return', $returnto);
193 <form id
="assignform" method
="post" action
="<?php echo $assignurl ?>"><div
>
194 <input type
="hidden" name
="sesskey" value
="<?php echo sesskey() ?>" />
196 <table id
="assigningrole" summary
="" class="admintable roleassigntable generaltable" cellspacing
="0">
198 <td id
="existingcell">
199 <p
><label
for="removeselect"><?php
print_string('extusers', 'core_role'); ?
></label
></p
>
200 <?php
$currentuserselector->display() ?
>
202 <td id
="buttonscell">
203 <div id
="addcontrols">
204 <input name
="add" id
="add" type
="submit" value
="<?php echo $OUTPUT->larrow().' '.get_string('add'); ?>" title
="<?php print_string('add'); ?>" /><br
/>
207 <div id
="removecontrols">
208 <input name
="remove" id
="remove" type
="submit" value
="<?php echo get_string('remove').' '.$OUTPUT->rarrow(); ?>" title
="<?php print_string('remove'); ?>" />
211 <td id
="potentialcell">
212 <p
><label
for="addselect"><?php
print_string('potusers', 'core_role'); ?
></label
></p
>
213 <?php
$potentialuserselector->display() ?
>
220 $PAGE->requires
->js_init_call('M.core_role.init_add_assign_page');
222 if (!empty($errors)) {
224 foreach ($errors as $e) {
228 echo $OUTPUT->box_start();
229 echo $OUTPUT->notification($msg);
230 echo $OUTPUT->box_end();
233 // Print a form to swap roles, and a link back to the all roles list.
234 echo '<div class="backlink">';
236 $newroleurl = new moodle_url($PAGE->url
);
237 if ($returnto !== null) {
238 $newroleurl->param('return', $returnto);
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);
247 echo '<p><a href="' . $backurl->out() . '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
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);
255 // Show UI for choosing a role to assign.
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'));
262 // Print instruction.
263 echo $OUTPUT->heading(get_string('chooseroletoassign', 'core_role'), 3);
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) {
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>';
280 $roleholdernames[$roleid] = implode('<br />', $strroleusers);
281 $showroleholders = true;
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);
288 $roleholdernames[$roleid] = '<a href="'.$assignurl.'">'.$strmorethanmax.'</a>';
290 $roleholdernames[$roleid] = '';
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';
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);
311 $row = array('<a href="'.$assignurl.'">'.$rolename.'</a>',
312 $description, $assigncounts[$roleid]);
313 if ($showroleholders) {
314 $row[] = $roleholdernames[$roleid];
316 $table->data
[] = $row;
319 echo html_writer
::table($table);
321 if ($context->contextlevel
> CONTEXT_USER
) {
323 if ($context->contextlevel
=== CONTEXT_COURSECAT
&& $returnto === 'management') {
324 $url = new moodle_url('/course/management.php', array('categoryid' => $context->instanceid
));
326 $url = $context->get_url();
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');
335 echo $OUTPUT->footer();