Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / admin / roles / assign.php
blobb1d507144bc2766cf5a80ef3f47b49d54e847726
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 if ($courseid != SITEID) {
126 print_header("$fullname", "$fullname",
127 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->
128 <a href=\"$CFG->wwwroot/user/index.php?id=$course->id\">$strparticipants</a> -> <a href=\"$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid\">$fullname</a> ->".$straction,
129 "", "", true, "&nbsp;", navmenu($course));
131 /// site header
132 } else {
133 print_header("$course->fullname: $fullname", $course->fullname,
134 "<a href=\"$CFG->wwwroot/user/view.php?id=$userid&amp;course=$courseid\">$fullname</a> -> $straction", "", "", true, "&nbsp;", navmenu($course));
137 $showroles = 1;
138 $currenttab = 'assign';
139 include_once($CFG->dirroot.'/user/tabs.php');
140 } else if ($context->contextlevel == CONTEXT_SYSTEM) {
141 admin_externalpage_setup('assignroles');
142 admin_externalpage_print_header();
143 } else if ($context->contextlevel==CONTEXT_COURSE and $context->instanceid == SITEID) {
144 admin_externalpage_setup('frontpageroles');
145 admin_externalpage_print_header();
146 $currenttab = '';
147 $tabsmode = 'assign';
148 include_once('tabs.php');
149 } else {
150 $currenttab = '';
151 $tabsmode = 'assign';
152 include_once('tabs.php');
155 /// Process incoming role assignment
157 if ($frm = data_submitted()) {
159 if ($add and !empty($frm->addselect) and confirm_sesskey()) {
161 foreach ($frm->addselect as $adduser) {
162 if (!$adduser = clean_param($adduser, PARAM_INT)) {
163 continue;
165 $allow = true;
166 if ($inmeta) {
167 if (has_capability('moodle/course:managemetacourse', $context, $adduser)) {
168 //ok
169 } else {
170 $managerroles = get_roles_with_capability('moodle/course:managemetacourse', CAP_ALLOW, $context);
171 if (!empty($managerroles) and !array_key_exists($roleid, $managerroles)) {
172 $erruser = get_record('user', 'id', $adduser, '','','','', 'id, firstname, lastname');
173 $errors[] = get_string('metaassignerror', 'role', fullname($erruser));
174 $allow = false;
178 if ($allow) {
179 switch($extendbase) {
180 case 0:
181 $timestart = $course->startdate;
182 break;
183 case 3:
184 $timestart = $today;
185 break;
186 case 4:
187 $timestart = $course->enrolstartdate;
188 break;
189 case 5:
190 $timestart = $course->enrolenddate;
191 break;
194 if($extendperiod > 0) {
195 $timeend = $timestart + $extendperiod;
196 } else {
197 $timeend = 0;
199 if (! role_assign($roleid, $adduser, 0, $context->id, $timestart, $timeend, $hidden)) {
200 $errors[] = "Could not add user with id $adduser to this role!";
205 } else if ($remove and !empty($frm->removeselect) and confirm_sesskey()) {
207 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
208 $topleveladmin = false;
210 // we only worry about this if the role has doanything capability at site level
211 if ($context->id == $sitecontext->id && $adminroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext)) {
212 foreach ($adminroles as $adminrole) {
213 if ($adminrole->id == $roleid) {
214 $topleveladmin = true;
219 foreach ($frm->removeselect as $removeuser) {
220 $removeuser = clean_param($removeuser, PARAM_INT);
222 if ($topleveladmin && ($removeuser == $USER->id)) { // Prevent unassigning oneself from being admin
223 continue;
226 if (! role_unassign($roleid, $removeuser, 0, $context->id)) {
227 $errors[] = "Could not remove user with id $removeuser from this role!";
228 } else if ($inmeta) {
229 sync_metacourse($courseid);
230 $newroles = get_user_roles($context, $removeuser, false);
231 if (!empty($newroles) and !array_key_exists($roleid, $newroles)) {
232 $erruser = get_record('user', 'id', $removeuser, '','','','', 'id, firstname, lastname');
233 $errors[] = get_string('metaunassignerror', 'role', fullname($erruser));
234 $allow = false;
239 } else if ($showall) {
240 $searchtext = '';
241 $previoussearch = 0;
245 if ($context->contextlevel==CONTEXT_COURSE and $context->instanceid == SITEID) {
246 print_heading_with_help(get_string('frontpageroles', 'admin'), 'assignroles');
247 } else {
248 print_heading_with_help(get_string('assignroles', 'role'), 'assignroles');
251 if ($context->contextlevel==CONTEXT_SYSTEM) {
252 print_box(get_string('globalroleswarning', 'role'));
255 if ($roleid) { /// prints a form to swap roles
257 /// Get all existing participants in this context.
258 // Why is this not done with get_users???
260 if (!$contextusers = get_role_users($roleid, $context, false, 'u.id, u.firstname, u.lastname, u.email, r.hidden')) {
261 $contextusers = array();
264 $select = "username <> 'guest' AND deleted = 0 AND confirmed = 1";
266 $usercount = count_records_select('user', $select) - count($contextusers);
268 $searchtext = trim($searchtext);
270 if ($searchtext !== '') { // Search for a subset of remaining users
271 $LIKE = sql_ilike();
272 $FULLNAME = sql_fullname();
274 $select .= " AND ($FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%') ";
277 $availableusers = get_recordset_sql('SELECT id, firstname, lastname, email
278 FROM '.$CFG->prefix.'user
279 WHERE '.$select.'
280 ORDER BY lastname ASC, firstname ASC');
282 /// In the .html file below we loop through these results and exclude any in $contextusers
284 echo '<div style="text-align:center">'.$strcurrentcontext.': '.print_context_name($context).'<br/>';
285 $assignableroles = array('0'=>get_string('listallroles', 'role').'...') + $assignableroles;
286 popup_form("$CFG->wwwroot/$CFG->admin/roles/assign.php?userid=$userid&amp;courseid=$courseid&amp;contextid=$contextid&amp;roleid=",
287 $assignableroles, 'switchrole', $roleid, '', '', '', false, 'self', $strroletoassign);
288 echo '</div>';
290 print_simple_box_start('center');
291 include('assign.html');
292 print_simple_box_end();
294 if (!empty($errors)) {
295 $msg = '<p>';
296 foreach ($errors as $e) {
297 $msg .= $e.'<br />';
299 $msg .= '</p>';
300 print_simple_box_start('center');
301 notify($msg);
302 print_simple_box_end();
305 } else { // Print overview table
307 // sync metacourse enrolments if needed
308 if ($inmeta) {
309 sync_metacourse($course);
312 $table->tablealign = 'center';
313 $table->cellpadding = 5;
314 $table->cellspacing = 0;
315 $table->width = '60%';
316 $table->head = array(get_string('roles', 'role'), get_string('description'), get_string('users'));
317 $table->wrap = array('nowrap', '', 'nowrap');
318 $table->align = array('right', 'left', 'center');
320 foreach ($assignableroles as $roleid => $rolename) {
321 $countusers = count_role_users($roleid, $context);
322 $description = format_string(get_field('role', 'description', 'id', $roleid));
323 $table->data[] = array('<a href="'.$baseurl.'&amp;roleid='.$roleid.'">'.$rolename.'</a>',$description, $countusers);
326 print_table($table);
329 print_footer($course);