Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/roles/assign.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 require_once(dirname(__FILE__) . '/../../config.php');
00028 require_once($CFG->dirroot . '/' . $CFG->admin . '/roles/lib.php');
00029 
00030 define("MAX_USERS_TO_LIST_PER_ROLE", 10);
00031 
00032 $contextid      = required_param('contextid',PARAM_INT);
00033 $roleid         = optional_param('roleid', 0, PARAM_INT);
00034 
00035 list($context, $course, $cm) = get_context_info_array($contextid);
00036 
00037 $url = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
00038 
00039 if ($course) {
00040     $isfrontpage = ($course->id == SITEID);
00041 } else {
00042     $isfrontpage = false;
00043     if ($context->contextlevel == CONTEXT_USER) {
00044         $course = $DB->get_record('course', array('id'=>optional_param('courseid', SITEID, PARAM_INT)), '*', MUST_EXIST);
00045         $user = $DB->get_record('user', array('id'=>$context->instanceid), '*', MUST_EXIST);
00046         $url->param('courseid', $course->id);
00047         $url->param('userid', $user->id);
00048     } else {
00049         $course = $SITE;
00050     }
00051 }
00052 
00053 
00054 // security
00055 require_login($course, false, $cm);
00056 require_capability('moodle/role:assign', $context);
00057 $PAGE->set_url($url);
00058 $PAGE->set_context($context);
00059 
00060 $contextname = print_context_name($context);
00061 $courseid = $course->id;
00062 
00063 // These are needed early because of tabs.php
00064 list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
00065 $overridableroles = get_overridable_roles($context, ROLENAME_BOTH);
00066 
00067 // Make sure this user can assign this role
00068 if ($roleid && !isset($assignableroles[$roleid])) {
00069     $a = new stdClass;
00070     $a->roleid = $roleid;
00071     $a->context = $contextname;
00072     print_error('cannotassignrolehere', '', get_context_url($context), $a);
00073 }
00074 
00075 // Work out an appropriate page title.
00076 if ($roleid) {
00077     $a = new stdClass;
00078     $a->role = $assignableroles[$roleid];
00079     $a->context = $contextname;
00080     $title = get_string('assignrolenameincontext', 'role', $a);
00081 } else {
00082     if ($isfrontpage) {
00083         $title = get_string('frontpageroles', 'admin');
00084     } else {
00085         $title = get_string('assignrolesin', 'role', $contextname);
00086     }
00087 }
00088 
00089 // Process any incoming role assignments before printing the header.
00090 if ($roleid) {
00091 
00092     // Create the user selector objects.
00093     $options = array('context' => $context, 'roleid' => $roleid);
00094 
00095     $potentialuserselector = roles_get_potential_user_selector($context, 'addselect', $options);
00096     $currentuserselector = new existing_role_holders('removeselect', $options);
00097 
00098     // Process incoming role assignments
00099     $errors = array();
00100     if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
00101         $userstoassign = $potentialuserselector->get_selected_users();
00102         if (!empty($userstoassign)) {
00103 
00104             foreach ($userstoassign as $adduser) {
00105                 $allow = true;
00106 
00107                 if ($allow) {
00108                     role_assign($roleid, $adduser->id, $context->id);
00109                 }
00110             }
00111 
00112             $potentialuserselector->invalidate_selected_users();
00113             $currentuserselector->invalidate_selected_users();
00114 
00115             $rolename = $assignableroles[$roleid];
00116             add_to_log($course->id, 'role', 'assign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
00117             // Counts have changed, so reload.
00118             list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
00119         }
00120     }
00121 
00122     // Process incoming role unassignments
00123     if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
00124         $userstounassign = $currentuserselector->get_selected_users();
00125         if (!empty($userstounassign)) {
00126 
00127             foreach ($userstounassign as $removeuser) {
00128                 //unassign only roles that are added manually, no messing with other components!!!
00129                 role_unassign($roleid, $removeuser->id, $context->id, '');
00130             }
00131 
00132             $potentialuserselector->invalidate_selected_users();
00133             $currentuserselector->invalidate_selected_users();
00134 
00135             $rolename = $assignableroles[$roleid];
00136             add_to_log($course->id, 'role', 'unassign', 'admin/roles/assign.php?contextid='.$context->id.'&roleid='.$roleid, $rolename, '', $USER->id);
00137             // Counts have changed, so reload.
00138             list($assignableroles, $assigncounts, $nameswithcounts) = get_assignable_roles($context, ROLENAME_BOTH, true);
00139         }
00140     }
00141 }
00142 
00143 $PAGE->set_pagelayout('admin');
00144 $PAGE->set_title($title);
00145 
00146 switch ($context->contextlevel) {
00147     case CONTEXT_SYSTEM:
00148         admin_externalpage_setup('assignroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
00149         break;
00150     case CONTEXT_USER:
00151         $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
00152         $PAGE->set_heading($fullname);
00153         $showroles = 1;
00154         break;
00155     case CONTEXT_COURSECAT:
00156         $PAGE->set_heading("$SITE->fullname: ".get_string("categories"));
00157         break;
00158     case CONTEXT_COURSE:
00159         if ($isfrontpage) {
00160             admin_externalpage_setup('frontpageroles', '', array('contextid' => $contextid, 'roleid' => $roleid));
00161         } else {
00162             $PAGE->set_heading($course->fullname);
00163         }
00164         break;
00165     case CONTEXT_MODULE:
00166         $PAGE->set_heading(print_context_name($context, false));
00167         $PAGE->set_cacheable(false);
00168         break;
00169     case CONTEXT_BLOCK:
00170         $PAGE->set_heading($PAGE->course->fullname);
00171         break;
00172 }
00173 
00174 echo $OUTPUT->header();
00175 
00176 // Print heading.
00177 echo $OUTPUT->heading_with_help($title, 'assignroles', 'role');
00178 
00179 if ($roleid) {
00180     // Show UI for assigning a particular role to users.
00181     // Print a warning if we are assigning system roles.
00182     if ($context->contextlevel == CONTEXT_SYSTEM) {
00183         echo $OUTPUT->box(get_string('globalroleswarning', 'role'));
00184     }
00185 
00186     // Print the form.
00187 $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
00188 ?>
00189 <form id="assignform" method="post" action="<?php echo $assignurl ?>"><div>
00190   <input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
00191 
00192   <table summary="" class="roleassigntable generaltable generalbox boxaligncenter" cellspacing="0">
00193     <tr>
00194       <td id="existingcell">
00195           <p><label for="removeselect"><?php print_string('extusers', 'role'); ?></label></p>
00196           <?php $currentuserselector->display() ?>
00197       </td>
00198       <td id="buttonscell">
00199           <div id="addcontrols">
00200               <input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
00201           </div>
00202 
00203           <div id="removecontrols">
00204               <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
00205           </div>
00206       </td>
00207       <td id="potentialcell">
00208           <p><label for="addselect"><?php print_string('potusers', 'role'); ?></label></p>
00209           <?php $potentialuserselector->display() ?>
00210       </td>
00211     </tr>
00212   </table>
00213 </div></form>
00214 
00215 <?php
00216     $PAGE->requires->js_init_call('M.core_role.init_add_assign_page');
00217 
00218     if (!empty($errors)) {
00219         $msg = '<p>';
00220         foreach ($errors as $e) {
00221             $msg .= $e.'<br />';
00222         }
00223         $msg .= '</p>';
00224         echo $OUTPUT->box_start();
00225         echo $OUTPUT->notification($msg);
00226         echo $OUTPUT->box_end();
00227     }
00228 
00229     // Print a form to swap roles, and a link back to the all roles list.
00230     echo '<div class="backlink">';
00231 
00232     $select = new single_select($PAGE->url, 'roleid', $nameswithcounts, $roleid, null);
00233     $select->label = get_string('assignanotherrole', 'role');
00234     echo $OUTPUT->render($select);
00235     $backurl = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
00236     echo '<p><a href="' . $backurl->out() . '">' . get_string('backtoallroles', 'role') . '</a></p>';
00237     echo '</div>';
00238 
00239 } else if (empty($assignableroles)) {
00240     // Print a message that there are no roles that can me assigned here.
00241     echo $OUTPUT->heading(get_string('notabletoassignroleshere', 'role'), 3);
00242 
00243 } else {
00244     // Show UI for choosing a role to assign.
00245 
00246     // Print a warning if we are assigning system roles.
00247     if ($context->contextlevel == CONTEXT_SYSTEM) {
00248         echo $OUTPUT->box(get_string('globalroleswarning', 'role'));
00249     }
00250 
00251     // Print instruction
00252     echo $OUTPUT->heading(get_string('chooseroletoassign', 'role'), 3);
00253 
00254     // Get the names of role holders for roles with between 1 and MAX_USERS_TO_LIST_PER_ROLE users,
00255     // and so determine whether to show the extra column.
00256     $roleholdernames = array();
00257     $strmorethanmax = get_string('morethan', 'role', MAX_USERS_TO_LIST_PER_ROLE);
00258     $showroleholders = false;
00259     foreach ($assignableroles as $roleid => $notused) {
00260         $roleusers = '';
00261         if (0 < $assigncounts[$roleid] && $assigncounts[$roleid] <= MAX_USERS_TO_LIST_PER_ROLE) {
00262             $roleusers = get_role_users($roleid, $context, false, 'u.id, u.lastname, u.firstname');
00263             if (!empty($roleusers)) {
00264                 $strroleusers = array();
00265                 foreach ($roleusers as $user) {
00266                     $strroleusers[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '" >' . fullname($user) . '</a>';
00267                 }
00268                 $roleholdernames[$roleid] = implode('<br />', $strroleusers);
00269                 $showroleholders = true;
00270             }
00271         } else if ($assigncounts[$roleid] > MAX_USERS_TO_LIST_PER_ROLE) {
00272             $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
00273             $roleholdernames[$roleid] = '<a href="'.$assignurl.'">'.$strmorethanmax.'</a>';
00274         } else {
00275             $roleholdernames[$roleid] = '';
00276         }
00277     }
00278 
00279     // Print overview table
00280     $table = new html_table();
00281     $table->tablealign = 'center';
00282     $table->width = '60%';
00283     $table->head = array(get_string('role'), get_string('description'), get_string('userswiththisrole', 'role'));
00284     $table->wrap = array('nowrap', '', 'nowrap');
00285     $table->align = array('left', 'left', 'center');
00286     if ($showroleholders) {
00287         $table->headspan = array(1, 1, 2);
00288         $table->wrap[] = 'nowrap';
00289         $table->align[] = 'left';
00290     }
00291 
00292     foreach ($assignableroles as $roleid => $rolename) {
00293         $description = format_string($DB->get_field('role', 'description', array('id'=>$roleid)));
00294         $assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
00295         $row = array('<a href="'.$assignurl.'">'.$rolename.'</a>',
00296                 $description, $assigncounts[$roleid]);
00297         if ($showroleholders) {
00298             $row[] = $roleholdernames[$roleid];
00299         }
00300         $table->data[] = $row;
00301     }
00302 
00303     echo html_writer::table($table);
00304 
00305     if ($context->contextlevel > CONTEXT_USER) {
00306         echo html_writer::start_tag('div', array('class'=>'backlink'));
00307         echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>get_context_url($context)));
00308         echo html_writer::end_tag('div');
00309     }
00310 }
00311 
00312 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations