MDL-10092:
[moodle-linuxchix.git] / admin / roles / assign.php
blobc166444a3373ed20b49ba7255acac793887ab5c8
1 <?php // $Id$
2 // Script to assign users to contexts
4 require_once('../../config.php');
5 require_once($CFG->dirroot.'/mod/forum/lib.php');
6 require_once($CFG->libdir.'/adminlib.php');
8 define("MAX_USERS_PER_PAGE", 5000);
10 $contextid = required_param('contextid',PARAM_INT); // context id
11 $roleid = optional_param('roleid', 0, PARAM_INT); // required role id
12 $add = optional_param('add', 0, PARAM_BOOL);
13 $remove = optional_param('remove', 0, PARAM_BOOL);
14 $showall = optional_param('showall', 0, PARAM_BOOL);
15 $searchtext = optional_param('searchtext', '', PARAM_RAW); // search string
16 $previoussearch = optional_param('previoussearch', 0, PARAM_BOOL);
17 $hidden = optional_param('hidden', 0, PARAM_BOOL); // whether this assignment is hidden
18 $extendperiod = optional_param('extendperiod', 0, PARAM_INT);
19 $extendbase = optional_param('extendbase', 0, PARAM_INT);
20 $userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs
21 $courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs
23 $errors = array();
25 $previoussearch = ($searchtext != '') or ($previoussearch) ? 1:0;
27 $baseurl = 'assign.php?contextid='.$contextid;
28 if (!empty($userid)) {
29 $baseurl .= '&amp;userid='.$userid;
31 if (!empty($courseid)) {
32 $baseurl .= '&amp;courseid='.$courseid;
35 if (! $context = get_context_instance_by_id($contextid)) {
36 error("Context ID was incorrect (can't find it)");
39 $inmeta = 0;
40 if ($context->contextlevel == CONTEXT_COURSE) {
41 $courseid = $context->instanceid;
42 if ($course = get_record('course', 'id', $courseid)) {
43 $inmeta = $course->metacourse;
44 } else {
45 error('Invalid course id');
47 } else if (!empty($courseid)){ // we need this for user tabs in user context
48 if (!$course = get_record('course', 'id', $courseid)) {
49 error('Invalid course id');
51 } else {
52 $courseid = SITEID;
53 $course = clone($SITE);
56 require_login($course);
58 if ($context->contextlevel == CONTEXT_COURSE) {
59 require_login($context->instanceid);
60 } else {
61 require_login();
64 require_capability('moodle/role:assign', $context);
66 /// needed for tabs.php
67 $overridableroles = get_overridable_roles($context);
68 $assignableroles = get_assignable_roles($context);
70 /// Get some language strings
72 $strassignusers = get_string('assignusers', 'role');
73 $strpotentialusers = get_string('potentialusers', 'role');
74 $strexistingusers = get_string('existingusers', 'role');
75 $straction = get_string('assignroles', 'role');
76 $strroletoassign = get_string('roletoassign', 'role');
77 $strcurrentcontext = get_string('currentcontext', 'role');
78 $strsearch = get_string('search');
79 $strshowall = get_string('showall');
80 $strparticipants = get_string('participants');
81 $strsearchresults = get_string('searchresults');
83 $unlimitedperiod = get_string('unlimited');
84 $defaultperiod = $course->enrolperiod;
85 for ($i=1; $i<=365; $i++) {
86 $seconds = $i * 86400;
87 $periodmenu[$seconds] = get_string('numdays', '', $i);
90 $timeformat = get_string('strftimedate');
91 $today = time();
92 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
94 $basemenu[0] = get_string('startdate') . ' (' . userdate($course->startdate, $timeformat) . ')';
95 if ($course->enrollable != 2 || ($course->enrolstartdate == 0 || $course->enrolstartdate <= $today) && ($course->enrolenddate == 0 || $course->enrolenddate > $today)) {
96 $basemenu[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ;
98 if($course->enrollable == 2) {
99 if($course->enrolstartdate > 0) {
100 $basemenu[4] = get_string('courseenrolstartdate') . ' (' . userdate($course->enrolstartdate, $timeformat) . ')';
102 if($course->enrolenddate > 0) {
103 $basemenu[5] = get_string('courseenrolenddate') . ' (' . userdate($course->enrolenddate, $timeformat) . ')';
107 /// Make sure this user can assign that role
109 if ($roleid) {
110 if (!user_can_assign($context, $roleid)) {
111 error ('you can not override this role in this context');
115 if ($userid) {
116 $user = get_record('user', 'id', $userid);
117 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
121 /// Print the header and tabs
123 if ($context->contextlevel == CONTEXT_USER) {
124 /// course header
125 $navlinks = array();
126 if ($courseid != SITEID) {
127 $navlinks[] = array('name' => $course->shortname, 'link' => "$CFG->wwwroot/course/view.php?id=$course->id", 'type' => 'course');
128 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
129 $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid", 'type' => 'misc');
130 $navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc');
131 $navigation = build_navigation($navlinks);
133 print_header("$fullname", "$fullname", $navigation, "", "", true, "&nbsp;", navmenu($course));
135 /// site header
136 } else {
137 $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid", 'type' => 'misc');
138 $navlinks[] = array('name' => $straction, 'link' => null, 'type' => 'misc');
139 $navigation = build_navigation($navlinks);
140 print_header("$course->fullname: $fullname", $course->fullname, $navigation, "", "", true, "&nbsp;", navmenu($course));
143 $showroles = 1;
144 $currenttab = 'assign';
145 include_once($CFG->dirroot.'/user/tabs.php');
146 } else if ($context->contextlevel == CONTEXT_SYSTEM) {
147 admin_externalpage_setup('assignroles');
148 admin_externalpage_print_header();
149 } else if ($context->contextlevel==CONTEXT_COURSE and $context->instanceid == SITEID) {
150 admin_externalpage_setup('frontpageroles');
151 admin_externalpage_print_header();
152 $currenttab = '';
153 $tabsmode = 'assign';
154 include_once('tabs.php');
155 } else {
156 $currenttab = '';
157 $tabsmode = 'assign';
158 include_once('tabs.php');
161 /// Process incoming role assignment
163 if ($frm = data_submitted()) {
165 if ($add and !empty($frm->addselect) and confirm_sesskey()) {
167 foreach ($frm->addselect as $adduser) {
168 if (!$adduser = clean_param($adduser, PARAM_INT)) {
169 continue;
171 $allow = true;
172 if ($inmeta) {
173 if (has_capability('moodle/course:managemetacourse', $context, $adduser)) {
174 //ok
175 } else {
176 $managerroles = get_roles_with_capability('moodle/course:managemetacourse', CAP_ALLOW, $context);
177 if (!empty($managerroles) and !array_key_exists($roleid, $managerroles)) {
178 $erruser = get_record('user', 'id', $adduser, '','','','', 'id, firstname, lastname');
179 $errors[] = get_string('metaassignerror', 'role', fullname($erruser));
180 $allow = false;
184 if ($allow) {
185 switch($extendbase) {
186 case 0:
187 $timestart = $course->startdate;
188 break;
189 case 3:
190 $timestart = $today;
191 break;
192 case 4:
193 $timestart = $course->enrolstartdate;
194 break;
195 case 5:
196 $timestart = $course->enrolenddate;
197 break;
200 if($extendperiod > 0) {
201 $timeend = $timestart + $extendperiod;
202 } else {
203 $timeend = 0;
205 if (! role_assign($roleid, $adduser, 0, $context->id, $timestart, $timeend, $hidden)) {
206 $errors[] = "Could not add user with id $adduser to this role!";
211 } else if ($remove and !empty($frm->removeselect) and confirm_sesskey()) {
213 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
214 $topleveladmin = false;
216 // we only worry about this if the role has doanything capability at site level
217 if ($context->id == $sitecontext->id && $adminroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext)) {
218 foreach ($adminroles as $adminrole) {
219 if ($adminrole->id == $roleid) {
220 $topleveladmin = true;
225 foreach ($frm->removeselect as $removeuser) {
226 $removeuser = clean_param($removeuser, PARAM_INT);
228 if ($topleveladmin && ($removeuser == $USER->id)) { // Prevent unassigning oneself from being admin
229 continue;
232 if (! role_unassign($roleid, $removeuser, 0, $context->id)) {
233 $errors[] = "Could not remove user with id $removeuser from this role!";
234 } else if ($inmeta) {
235 sync_metacourse($courseid);
236 $newroles = get_user_roles($context, $removeuser, false);
237 if (!empty($newroles) and !array_key_exists($roleid, $newroles)) {
238 $erruser = get_record('user', 'id', $removeuser, '','','','', 'id, firstname, lastname');
239 $errors[] = get_string('metaunassignerror', 'role', fullname($erruser));
240 $allow = false;
245 } else if ($showall) {
246 $searchtext = '';
247 $previoussearch = 0;
251 if ($context->contextlevel==CONTEXT_COURSE and $context->instanceid == SITEID) {
252 print_heading_with_help(get_string('frontpageroles', 'admin'), 'assignroles');
253 } else {
254 print_heading_with_help(get_string('assignroles', 'role'), 'assignroles');
257 if ($context->contextlevel==CONTEXT_SYSTEM) {
258 print_box(get_string('globalroleswarning', 'role'));
261 if ($roleid) { /// prints a form to swap roles
263 /// Get all existing participants in this context.
264 // Why is this not done with get_users???
266 if (!$contextusers = get_role_users($roleid, $context, false, 'u.id, u.firstname, u.lastname, u.email, r.hidden')) {
267 $contextusers = array();
270 $select = "username <> 'guest' AND deleted = 0 AND confirmed = 1";
272 $usercount = count_records_select('user', $select) - count($contextusers);
274 $searchtext = trim($searchtext);
276 if ($searchtext !== '') { // Search for a subset of remaining users
277 $LIKE = sql_ilike();
278 $FULLNAME = sql_fullname();
280 $selectsql = " AND ($FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%') ";
281 $select .= $selectsql;
282 } else {
283 $selectsql = "";
286 /// MDL-11111 do not include user already assigned this role in this context as available users
287 /// so that the number of available users is right and we save time looping later
288 $availableusers = get_recordset_sql('SELECT id, firstname, lastname, email
289 FROM '.$CFG->prefix.'user
290 WHERE '.$select.'
291 AND id NOT IN (
292 SELECT u.id
293 FROM '.$CFG->prefix.'role_assignments r,
294 '.$CFG->prefix.'user u
295 WHERE r.contextid = '.$contextid.'
296 AND u.id = r.userid
297 AND r.roleid = '.$roleid.'
298 '.$selectsql.')
299 ORDER BY lastname ASC, firstname ASC');
301 echo '<div style="text-align:center">'.$strcurrentcontext.': '.print_context_name($context).'<br/>';
302 $assignableroles = array('0'=>get_string('listallroles', 'role').'...') + $assignableroles;
303 popup_form("$CFG->wwwroot/$CFG->admin/roles/assign.php?userid=$userid&amp;courseid=$courseid&amp;contextid=$contextid&amp;roleid=",
304 $assignableroles, 'switchrole', $roleid, '', '', '', false, 'self', $strroletoassign);
305 echo '</div>';
307 print_simple_box_start('center');
308 include('assign.html');
309 print_simple_box_end();
311 if (!empty($errors)) {
312 $msg = '<p>';
313 foreach ($errors as $e) {
314 $msg .= $e.'<br />';
316 $msg .= '</p>';
317 print_simple_box_start('center');
318 notify($msg);
319 print_simple_box_end();
322 } else { // Print overview table
324 // sync metacourse enrolments if needed
325 if ($inmeta) {
326 sync_metacourse($course);
329 $table->tablealign = 'center';
330 $table->cellpadding = 5;
331 $table->cellspacing = 0;
332 $table->width = '60%';
333 $table->head = array(get_string('roles', 'role'), get_string('description'), get_string('users'));
334 $table->wrap = array('nowrap', '', 'nowrap');
335 $table->align = array('right', 'left', 'center');
337 foreach ($assignableroles as $roleid => $rolename) {
338 $countusers = count_role_users($roleid, $context);
339 $description = format_string(get_field('role', 'description', 'id', $roleid));
340 $table->data[] = array('<a href="'.$baseurl.'&amp;roleid='.$roleid.'">'.$rolename.'</a>',$description, $countusers);
343 print_table($table);
346 print_footer($course);