Merge branch 'MDL-31829_22' of git://github.com/timhunt/moodle into MOODLE_22_STABLE
[moodle.git] / admin / roles / assign.php
blob65aa22159a1b32446faf3999cf782baf6689bf37
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Lets you assign roles to users in a particular context.
21 * @package core
22 * @subpackage role
23 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once(dirname(__FILE__) . '/../../config.php');
28 require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
30 define("MAX_USERS_TO_LIST_PER_ROLE", 10);
32 $contextid = required_param('contextid',PARAM_INT);
33 $roleid = optional_param('roleid', 0, PARAM_INT);
35 list($context, $course, $cm) = get_context_info_array($contextid);
37 $url = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
39 if ($course) {
40 $isfrontpage = ($course->id == SITEID);
41 } else {
42 $isfrontpage = false;
43 if ($context->contextlevel == CONTEXT_USER) {
44 $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
45 $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
46 $url->param('courseid', $course->id);
47 $url->param('userid', $user->id);
48 } else {
49 $course = $SITE;
54 // security
55 require_login($course, false, $cm);
56 require_capability('moodle/role:assign', $context);
57 $PAGE->set_url($url);
58 $PAGE->set_context($context);
60 $contextname = print_context_name($context);
61 $courseid = $course->id;
63 // These are needed early because of tabs.php
64 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
65 $overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
67 // Make sure this user can assign this role
68 if ($roleid && !isset($assignableroles[$roleid])) {
69 $a = new stdClass;
70 $a->roleid = $roleid;
71 $a->context = $contextname;
72 print_error('cannotassignrolehere', '', get_context_url($context), $a);
75 // Work out an appropriate page title.
76 if ($roleid) {
77 $a = new stdClass;
78 $a->role = $assignableroles[$roleid];
79 $a->context = $contextname;
80 $title = get_string('assignrolenameincontext', 'role', $a);
81 } else {
82 if ($isfrontpage) {
83 $title = get_string('frontpageroles', 'admin');
84 } else {
85 $title = get_string('assignrolesin', 'role', $contextname);
89 // Process any incoming role assignments before printing the header.
90 if ($roleid) {
92 // Create the user selector objects.
93 $options = array('context' => $context, 'roleid' => $roleid);
95 $potentialuserselector = roles_get_potential_user_selector($context, 'addselect', $options);
96 $currentuserselector = new existing_role_holders('removeselect', $options);
98 // Process incoming role assignments
99 $errors = array();
100 if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
101 $userstoassign = $potentialuserselector->get_selected_users();
102 if (!empty($userstoassign)) {
104 foreach ($userstoassign as $adduser) {
105 $allow = true;
107 if ($allow) {
108 role_assign($roleid, $adduser->id, $context->id);
112 $potentialuserselector->invalidate_selected_users();
113 $currentuserselector->invalidate_selected_users();
115 $rolename = $assignableroles[$roleid];
116 add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
117 // Counts have changed, so reload.
118 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
122 // Process incoming role unassignments
123 if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
124 $userstounassign = $currentuserselector->get_selected_users();
125 if (!empty($userstounassign)) {
127 foreach ($userstounassign as $removeuser) {
128 //unassign only roles that are added manually, no messing with other components!!!
129 role_unassign($roleid, $removeuser->id, $context->id, '');
132 $potentialuserselector->invalidate_selected_users();
133 $currentuserselector->invalidate_selected_users();
135 $rolename = $assignableroles[$roleid];
136 add_to_log($course->id, 'role', 'unassign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
137 // Counts have changed, so reload.
138 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
143 $PAGE->set_pagelayout('admin');
144 $PAGE->set_title($title);
146 switch ($context->contextlevel) {
147 case CONTEXT_SYSTEM:
148 admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
149 break;
150 case CONTEXT_USER:
151 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
152 $PAGE->set_heading($fullname);
153 $showroles = 1;
154 break;
155 case CONTEXT_COURSECAT:
156 $PAGE->set_heading("$SITE->fullname: ".get_string("categories"));
157 break;
158 case CONTEXT_COURSE:
159 if ($isfrontpage) {
160 admin_externalpage_setup('frontpageroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
161 } else {
162 $PAGE->set_heading($course->fullname);
164 break;
165 case CONTEXT_MODULE:
166 $PAGE->set_heading(print_context_name($context, false));
167 $PAGE->set_cacheable(false);
168 break;
169 case CONTEXT_BLOCK:
170 $PAGE->set_heading($PAGE->course->fullname);
171 break;
174 echo $OUTPUT->header();
176 // Print heading.
177 echo $OUTPUT->heading_with_help($title, 'assignroles', 'role');
179 if ($roleid) {
180 // Show UI for assigning a particular role to users.
181 // Print a warning if we are assigning system roles.
182 if ($context->contextlevel == CONTEXT_SYSTEM) {
183 echo $OUTPUT->box(get_string('globalroleswarning', 'role'));
186 // Print the form.
187 $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
189 <form id="assignform" method="post" action="<?php echo $assignurl ?>"><div>
190 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
192 <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
193 <tr>
194 <td id="existingcell">
195 <p><label for="removeselect"><?php print_string('extusers', 'role'); ?></label></p>
196 <?php $currentuserselector->display() ?>
197 </td>
198 <td id="buttonscell">
199 <div id="addcontrols">
200 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
201 </div>
203 <div id="removecontrols">
204 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
205 </div>
206 </td>
207 <td id="potentialcell">
208 <p><label for="addselect"><?php print_string('potusers', 'role'); ?></label></p>
209 <?php $potentialuserselector->display() ?>
210 </td>
211 </tr>
212 </table>
213 </div></form>
215 <?php
216 $PAGE->requires->js_init_call('M.core_role.init_add_assign_page');
218 if (!empty($errors)) {
219 $msg = '<p>';
220 foreach ($errors as $e) {
221 $msg .= $e.'<br />';
223 $msg .= '</p>';
224 echo $OUTPUT->box_start();
225 echo $OUTPUT->notification($msg);
226 echo $OUTPUT->box_end();
229 // Print a form to swap roles, and a link back to the all roles list.
230 echo '<div class="backlink">';
232 $select = new single_select($PAGE->url, 'roleid', $nameswithcounts, $roleid, null);
233 $select->label = get_string('assignanotherrole', 'role');
234 echo $OUTPUT->render($select);
235 $backurl = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
236 echo '<p><a href="' . $backurl->out() . '">' . get_string('backtoallroles', 'role') . '</a></p>';
237 echo '</div>';
239 } else if (empty($assignableroles)) {
240 // Print a message that there are no roles that can me assigned here.
241 echo $OUTPUT->heading(get_string('notabletoassignroleshere', 'role'), 3);
243 } else {
244 // Show UI for choosing a role to assign.
246 // Print a warning if we are assigning system roles.
247 if ($context->contextlevel == CONTEXT_SYSTEM) {
248 echo $OUTPUT->box(get_string('globalroleswarning', 'role'));
251 // Print instruction
252 echo $OUTPUT->heading(get_string('chooseroletoassign', 'role'), 3);
254 // Get the names of role holders for roles with between 1 and MAX_USERS_TO_LIST_PER_ROLE users,
255 // and so determine whether to show the extra column.
256 $roleholdernames = array();
257 $strmorethanmax = get_string('morethan', 'role', MAX_USERS_TO_LIST_PER_ROLE);
258 $showroleholders = false;
259 foreach ($assignableroles as $roleid => $notused) {
260 $roleusers = '';
261 if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE) {
262 $roleusers = get_role_users($roleid, $context, false, 'u.id, u.lastname, u.firstname');
263 if (!empty($roleusers)) {
264 $strroleusers = array();
265 foreach ($roleusers as $user) {
266 $strroleusers[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '" >' . fullname($user) . '</a>';
268 $roleholdernames[$roleid] = implode('<br />', $strroleusers);
269 $showroleholders = true;
271 } else if ($assigncounts[$roleid] > MAX_USERS_TO_LIST_PER_ROLE) {
272 $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
273 $roleholdernames[$roleid] = '<a href="'.$assignurl.'">'.$strmorethanmax.'</a>';
274 } else {
275 $roleholdernames[$roleid] = '';
279 // Print overview table
280 $table = new html_table();
281 $table->tablealign = 'center';
282 $table->width = '60%';
283 $table->head = array(get_string('role'), get_string('description'), get_string('userswiththisrole', 'role'));
284 $table->wrap = array('nowrap', '', 'nowrap');
285 $table->align = array('left', 'left', 'center');
286 if ($showroleholders) {
287 $table->headspan = array(1, 1, 2);
288 $table->wrap[] = 'nowrap';
289 $table->align[] = 'left';
292 foreach ($assignableroles as $roleid => $rolename) {
293 $description = format_string($DB->get_field('role', 'description', array('id'=>$roleid)));
294 $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
295 $row = array('<a href="'.$assignurl.'">'.$rolename.'</a>',
296 $description, $assigncounts[$roleid]);
297 if ($showroleholders) {
298 $row[] = $roleholdernames[$roleid];
300 $table->data[] = $row;
303 echo html_writer::table($table);
305 if ($context->contextlevel > CONTEXT_USER) {
306 echo html_writer::start_tag('div', array('class'=>'backlink'));
307 echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
308 echo html_writer::end_tag('div');
312 echo $OUTPUT->footer();