[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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. 13 // 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/>. 16 17 /** 18 * Assign roles to users. 19 * 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 23 */ 24 25 require_once(__DIR__ . '/../../config.php'); 26 require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php'); 27 28 define("MAX_USERS_TO_LIST_PER_ROLE", 10); 29 30 $contextid = required_param('contextid', PARAM_INT); 31 $roleid = optional_param('roleid', 0, PARAM_INT); 32 $returnto = optional_param('return', null, PARAM_ALPHANUMEXT); 33 34 list($context, $course, $cm) = get_context_info_array($contextid); 35 36 $url = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid)); 37 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; 49 } 50 } 51 52 53 // Security. 54 require_login($course, false, $cm); 55 require_capability('moodle/role:assign', $context); 56 $PAGE->set_url($url); 57 $PAGE->set_context($context); 58 59 $contextname = $context->get_context_name(); 60 $courseid = $course->id; 61 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); 65 66 // Make sure this user can assign this role. 67 if ($roleid && !isset($assignableroles[$roleid])) { 68 $a = new stdClass; 69 $a->roleid = $roleid; 70 $a->context = $contextname; 71 print_error('cannotassignrolehere', '', $context->get_url(), $a); 72 } 73 74 // Work out an appropriate page title. 75 if ($roleid) { 76 $a = new stdClass; 77 $a->role = $assignableroles[$roleid]; 78 $a->context = $contextname; 79 $title = get_string('assignrolenameincontext', 'core_role', $a); 80 } else { 81 if ($isfrontpage) { 82 $title = get_string('frontpageroles', 'admin'); 83 } else { 84 $title = get_string('assignrolesin', 'core_role', $contextname); 85 } 86 } 87 88 // Process any incoming role assignments before printing the header. 89 if ($roleid) { 90 91 // Create the user selector objects. 92 $options = array('context' => $context, 'roleid' => $roleid); 93 94 $potentialuserselector = core_role_get_potential_user_selector($context, 'addselect', $options); 95 $currentuserselector = new core_role_existing_role_holders('removeselect', $options); 96 97 // Process incoming role assignments. 98 $errors = array(); 99 if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) { 100 $userstoassign = $potentialuserselector->get_selected_users(); 101 if (!empty($userstoassign)) { 102 103 foreach ($userstoassign as $adduser) { 104 $allow = true; 105 106 if ($allow) { 107 role_assign($roleid, $adduser->id, $context->id); 108 } 109 } 110 111 $potentialuserselector->invalidate_selected_users(); 112 $currentuserselector->invalidate_selected_users(); 113 114 // Counts have changed, so reload. 115 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true); 116 } 117 } 118 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)) { 123 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, ''); 127 } 128 129 $potentialuserselector->invalidate_selected_users(); 130 $currentuserselector->invalidate_selected_users(); 131 132 // Counts have changed, so reload. 133 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true); 134 } 135 } 136 } 137 138 $PAGE->set_pagelayout('admin'); 139 $PAGE->set_title($title); 140 141 switch ($context->contextlevel) { 142 case CONTEXT_SYSTEM: 143 require_once($CFG->libdir.'/adminlib.php'); 144 admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid)); 145 break; 146 case CONTEXT_USER: 147 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context)); 148 $PAGE->set_heading($fullname); 149 $showroles = 1; 150 break; 151 case CONTEXT_COURSECAT: 152 $PAGE->set_heading($SITE->fullname); 153 break; 154 case CONTEXT_COURSE: 155 if ($isfrontpage) { 156 $PAGE->set_heading(get_string('frontpage', 'admin')); 157 } else { 158 $PAGE->set_heading($course->fullname); 159 } 160 break; 161 case CONTEXT_MODULE: 162 $PAGE->set_heading($context->get_context_name(false)); 163 $PAGE->set_cacheable(false); 164 break; 165 case CONTEXT_BLOCK: 166 $PAGE->set_heading($PAGE->course->fullname); 167 break; 168 } 169 170 echo $OUTPUT->header(); 171 172 // Print heading. 173 echo $OUTPUT->heading_with_help($title, 'assignroles', 'core_role'); 174 175 if ($roleid) { 176 // Show UI for assigning a particular role to users. 177 // Print a warning if we are assigning system roles. 178 if ($context->contextlevel == CONTEXT_SYSTEM) { 179 echo $OUTPUT->notification(get_string('globalroleswarning', 'core_role')); 180 } 181 182 // Print the form. 183 $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid)); 184 if ($returnto !== null) { 185 $assignurl->param('return', $returnto); 186 } 187 ?> 188 <form id="assignform" method="post" action="<?php echo $assignurl ?>"><div> 189 <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" /> 190 191 <table id="assigningrole" summary="" class="admintable roleassigntable generaltable" cellspacing="0"> 192 <tr> 193 <td id="existingcell"> 194 <p><label for="removeselect"><?php print_string('extusers', 'core_role'); ?></label></p> 195 <?php $currentuserselector->display() ?> 196 </td> 197 <td id="buttonscell"> 198 <div id="addcontrols"> 199 <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().' '.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br /> 200 </div> 201 202 <div id="removecontrols"> 203 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').' '.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" /> 204 </div> 205 </td> 206 <td id="potentialcell"> 207 <p><label for="addselect"><?php print_string('potusers', 'core_role'); ?></label></p> 208 <?php $potentialuserselector->display() ?> 209 </td> 210 </tr> 211 </table> 212 </div></form> 213 214 <?php 215 $PAGE->requires->js_init_call('M.core_role.init_add_assign_page'); 216 217 if (!empty($errors)) { 218 $msg = '<p>'; 219 foreach ($errors as $e) { 220 $msg .= $e.'<br />'; 221 } 222 $msg .= '</p>'; 223 echo $OUTPUT->box_start(); 224 echo $OUTPUT->notification($msg); 225 echo $OUTPUT->box_end(); 226 } 227 228 // Print a form to swap roles, and a link back to the all roles list. 229 echo '<div class="backlink">'; 230 231 $newroleurl = new moodle_url($PAGE->url); 232 if ($returnto !== null) { 233 $newroleurl->param('return', $returnto); 234 } 235 $select = new single_select($newroleurl, 'roleid', $nameswithcounts, $roleid, null); 236 $select->label = get_string('assignanotherrole', 'core_role'); 237 echo $OUTPUT->render($select); 238 $backurl = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid)); 239 if ($returnto !== null) { 240 $backurl->param('return', $returnto); 241 } 242 echo '<p><a href="' . $backurl->out() . '">' . get_string('backtoallroles', 'core_role') . '</a></p>'; 243 echo '</div>'; 244 245 } else if (empty($assignableroles)) { 246 // Print a message that there are no roles that can me assigned here. 247 echo $OUTPUT->heading(get_string('notabletoassignroleshere', 'core_role'), 3); 248 249 } else { 250 // Show UI for choosing a role to assign. 251 252 // Print a warning if we are assigning system roles. 253 if ($context->contextlevel == CONTEXT_SYSTEM) { 254 echo $OUTPUT->notification(get_string('globalroleswarning', 'core_role')); 255 } 256 257 // Print instruction. 258 echo $OUTPUT->heading(get_string('chooseroletoassign', 'core_role'), 3); 259 260 // Get the names of role holders for roles with between 1 and MAX_USERS_TO_LIST_PER_ROLE users, 261 // and so determine whether to show the extra column. 262 $roleholdernames = array(); 263 $strmorethanmax = get_string('morethan', 'core_role', MAX_USERS_TO_LIST_PER_ROLE); 264 $showroleholders = false; 265 foreach ($assignableroles as $roleid => $notused) { 266 $roleusers = ''; 267 if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE) { 268 $userfields = 'u.id, u.username, ' . get_all_user_name_fields(true, 'u'); 269 $roleusers = get_role_users($roleid, $context, false, $userfields); 270 if (!empty($roleusers)) { 271 $strroleusers = array(); 272 foreach ($roleusers as $user) { 273 $strroleusers[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '" >' . fullname($user) . '</a>'; 274 } 275 $roleholdernames[$roleid] = implode('<br />', $strroleusers); 276 $showroleholders = true; 277 } 278 } else if ($assigncounts[$roleid] > MAX_USERS_TO_LIST_PER_ROLE) { 279 $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid)); 280 if ($returnto !== null) { 281 $assignurl->param('return', $returnto); 282 } 283 $roleholdernames[$roleid] = '<a href="'.$assignurl.'">'.$strmorethanmax.'</a>'; 284 } else { 285 $roleholdernames[$roleid] = ''; 286 } 287 } 288 289 // Print overview table. 290 $table = new html_table(); 291 $table->id = 'assignrole'; 292 $table->head = array(get_string('role'), get_string('description'), get_string('userswiththisrole', 'core_role')); 293 $table->colclasses = array('leftalign role', 'leftalign', 'centeralign userrole'); 294 $table->attributes['class'] = 'admintable generaltable'; 295 if ($showroleholders) { 296 $table->headspan = array(1, 1, 2); 297 $table->colclasses[] = 'leftalign roleholder'; 298 } 299 300 foreach ($assignableroles as $roleid => $rolename) { 301 $description = format_string($DB->get_field('role', 'description', array('id'=>$roleid))); 302 $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid)); 303 if ($returnto !== null) { 304 $assignurl->param('return', $returnto); 305 } 306 $row = array('<a href="'.$assignurl.'">'.$rolename.'</a>', 307 $description, $assigncounts[$roleid]); 308 if ($showroleholders) { 309 $row[] = $roleholdernames[$roleid]; 310 } 311 $table->data[] = $row; 312 } 313 314 echo html_writer::table($table); 315 316 if ($context->contextlevel > CONTEXT_USER) { 317 318 if ($context->contextlevel === CONTEXT_COURSECAT && $returnto === 'management') { 319 $url = new moodle_url('/course/management.php', array('categoryid' => $context->instanceid)); 320 } else { 321 $url = $context->get_url(); 322 } 323 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'); 327 } 328 } 329 330 echo $OUTPUT->footer();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |