Moodle release 3.2.5
[moodle.git] / admin / roles / assign.php
blob889a2d111dce87887abd3e758d2432e3fb7450fe
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.
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/>.
17 /**
18 * Assign roles to users.
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
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));
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;
53 // Security.
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);
59 if ($returnurl) {
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])) {
74 $a = new stdClass;
75 $a->roleid = $roleid;
76 $a->context = $contextname;
77 print_error('cannotassignrolehere', '', $context->get_url(), $a);
80 // Work out an appropriate page title.
81 if ($roleid) {
82 $a = new stdClass;
83 $a->role = $assignableroles[$roleid];
84 $a->context = $contextname;
85 $title = get_string('assignrolenameincontext', 'core_role', $a);
86 } else {
87 if ($isfrontpage) {
88 $title = get_string('frontpageroles', 'admin');
89 } else {
90 $title = get_string('assignrolesin', 'core_role', $contextname);
94 // Process any incoming role assignments before printing the header.
95 if ($roleid) {
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.
104 $errors = array();
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) {
110 $allow = true;
112 if ($allow) {
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) {
157 case CONTEXT_SYSTEM:
158 require_once($CFG->libdir.'/adminlib.php');
159 admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
160 break;
161 case CONTEXT_USER:
162 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
163 $PAGE->set_heading($fullname);
164 $showroles = 1;
165 break;
166 case CONTEXT_COURSECAT:
167 $PAGE->set_heading($SITE->fullname);
168 break;
169 case CONTEXT_COURSE:
170 if ($isfrontpage) {
171 $PAGE->set_heading(get_string('frontpage', 'admin'));
172 } else {
173 $PAGE->set_heading($course->fullname);
175 break;
176 case CONTEXT_MODULE:
177 $PAGE->set_heading($context->get_context_name(false));
178 $PAGE->set_cacheable(false);
179 break;
180 case CONTEXT_BLOCK:
181 $PAGE->set_heading($PAGE->course->fullname);
182 break;
185 echo $OUTPUT->header();
187 // Print heading.
188 echo $OUTPUT->heading_with_help($title, 'assignroles', 'core_role');
190 if ($roleid) {
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'));
197 // Print the form.
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">
204 <tr>
205 <td id="existingcell">
206 <p><label for="removeselect"><?php print_string('extusers', 'core_role'); ?></label></p>
207 <?php $currentuserselector->display() ?>
208 </td>
209 <td id="buttonscell">
210 <div id="addcontrols">
211 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>"
212 title="<?php print_string('add'); ?>" class="btn btn-secondary"/><br />
213 </div>
215 <div id="removecontrols">
216 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>"
217 title="<?php print_string('remove'); ?>" class="btn btn-secondary"/>
218 </div>
219 </td>
220 <td id="potentialcell">
221 <p><label for="addselect"><?php print_string('potusers', 'core_role'); ?></label></p>
222 <?php $potentialuserselector->display() ?>
223 </td>
224 </tr>
225 </table>
226 </div></form>
228 <?php
229 $PAGE->requires->js_init_call('M.core_role.init_add_assign_page');
231 if (!empty($errors)) {
232 $msg = '<p>';
233 foreach ($errors as $e) {
234 $msg .= $e.'<br />';
236 $msg .= '</p>';
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>';
249 echo '</div>';
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);
255 } else {
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) {
272 $roleusers = '';
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>';
287 } else {
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) {
318 if ($returnurl) {
319 $url = new moodle_url($returnurl);
320 } else {
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();