Added help links to Roles pages MDL-6923
[moodle.git] / admin / roles / assign.php
blob2f98fa4bcadaaf56841c606617cbea742b8b53c0
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 $timestart = optional_param('timestart', 0, PARAM_INT);
19 $timeend = optional_param('timened', 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)");
40 $inmeta = 0;
41 if ($context->contextlevel == CONTEXT_COURSE) {
42 $courseid = $context->instanceid;
43 if ($course = get_record('course', 'id', $courseid)) {
44 $inmeta = $course->metacourse;
45 } else {
46 error('Invalid course id');
48 } else if (!empty($courseid)){ // we need this for user tabs in user context
49 if (!$course = get_record('course', 'id', $courseid)) {
50 error('Invalid course id');
52 } else {
53 $courseid = SITEID;
54 $course = get_site();
57 if ($context->contextlevel == CONTEXT_COURSE) {
58 require_login($context->instanceid);
59 } else {
60 require_login();
63 require_capability('moodle/role:assign', $context);
65 $assignableroles = get_assignable_roles($context);
67 /// Get some language strings
69 $strassignusers = get_string('assignusers', 'role');
70 $strpotentialusers = get_string('potentialusers', 'role');
71 $strexistingusers = get_string('existingusers', 'role');
72 $straction = get_string('assignroles', 'role');
73 $strroletoassign = get_string('roletoassign', 'role');
74 $strcurrentcontext = get_string('currentcontext', 'role');
75 $strsearch = get_string('search');
76 $strshowall = get_string('showall');
77 $strparticipants = get_string('participants');
78 $strsearchresults = get_string('searchresults');
82 /// Make sure this user can assign that role
84 if ($roleid) {
85 if (!user_can_assign($context, $roleid)) {
86 error ('you can not override this role in this context');
90 if ($userid) {
91 $user = get_record('user', 'id', $userid);
92 $fullname = fullname($user, has_capability('moodle/site:viewfullnames', $context));
96 /// Print the header and tabs
98 if ($context->contextlevel == CONTEXT_USER) {
99 /// course header
100 if ($courseid != SITEID) {
101 print_header("$fullname", "$fullname",
102 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->
103 <a href=\"$CFG->wwwroot/user/index.php?id=$course->id\">$strparticipants</a> -> <a href=\"$CFG->wwwroot/user/view.php?id=$userid&course=$courseid\">$fullname</a> ->".$straction,
104 "", "", true, "&nbsp;", navmenu($course));
106 /// site header
107 } else {
108 print_header("$course->fullname: $fullname", "$course->fullname",
109 "<a href=\"$CFG->wwwroot/user/view.php?id=$userid&course=$courseid\">$fullname</a> -> $straction", "", "", true, "&nbsp;", navmenu($course));
112 $showroles = 1;
113 $currenttab = 'assign';
114 include_once($CFG->dirroot.'/user/tabs.php');
115 } else if ($context->contextlevel == CONTEXT_SYSTEM) {
116 $adminroot = admin_get_root();
117 admin_externalpage_setup('assignroles', $adminroot);
118 admin_externalpage_print_header($adminroot);
119 } else {
120 $currenttab = '';
121 $tabsmode = 'assign';
122 include_once('tabs.php');
126 /// Process incoming role assignment
128 if ($frm = data_submitted()) {
130 if ($add and !empty($frm->addselect) and confirm_sesskey()) {
132 $timemodified = time();
134 foreach ($frm->addselect as $adduser) {
135 if (!$adduser = clean_param($adduser, PARAM_INT)) {
136 continue;
138 $allow = true;
139 if ($inmeta) {
140 if (has_capability('moodle/course:managemetacourse', $context, $adduser)) {
141 //ok
142 } else {
143 $managerroles = get_roles_with_capability('moodle/course:managemetacourse', CAP_ALLOW, $context);
144 if (!empty($managerroles) and !array_key_exists($roleid, $managerroles)) {
145 $erruser = get_record('user', 'id', $adduser, '','','','', 'id, firstname, lastname');
146 $errors[] = get_string('metaassignerror', 'role', fullname($erruser));
147 $allow = false;
151 if ($allow) {
152 if (! role_assign($roleid, $adduser, 0, $context->id, $timestart, $timeend, $hidden)) {
153 $errors[] = "Could not add user with id $adduser to this role!";
158 } else if ($remove and !empty($frm->removeselect) and confirm_sesskey()) {
160 foreach ($frm->removeselect as $removeuser) {
161 $removeuser = clean_param($removeuser, PARAM_INT);
162 if (! role_unassign($roleid, $removeuser, 0, $context->id)) {
163 $errors[] = "Could not remove user with id $removeuser from this role!";
164 } else if ($inmeta) {
165 sync_metacourse($courseid);
166 $newroles = get_user_roles($context, $removeuser, false);
167 if (!empty($newroles) and !array_key_exists($roleid, $newroles)) {
168 $erruser = get_record('user', 'id', $removeuser, '','','','', 'id, firstname, lastname');
169 $errors[] = get_string('metaunassignerror', 'role', fullname($erruser));
170 $allow = false;
175 } else if ($showall) {
176 $searchtext = '';
177 $previoussearch = 0;
182 print_heading_with_help(get_string('assignroles', 'role'), 'assignroles');
184 if ($roleid) { /// prints a form to swap roles
186 /// Get all existing participants in this context.
187 // Why is this not done with get_users???
189 if (!$contextusers = get_role_users($roleid, $context, false, 'u.id, u.firstname, u.lastname, u.email')) {
190 $contextusers = array();
193 $select = "username <> 'guest' AND username <> 'changeme' AND deleted = 0 AND confirmed = 1";
195 $usercount = count_records_select('user', $select) - count($contextusers);
197 $searchtext = trim($searchtext);
199 if ($searchtext !== '') { // Search for a subset of remaining users
200 $LIKE = sql_ilike();
201 $FULLNAME = sql_fullname();
203 $select .= " AND ($FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%') ";
206 $availableusers = get_recordset_sql('SELECT id, firstname, lastname, email
207 FROM '.$CFG->prefix.'user
208 WHERE '.$select.'
209 ORDER BY lastname ASC, firstname ASC');
211 /// In the .html file below we loop through these results and exclude any in $contextusers
213 echo '<form name="rolesform" action="assign.php" method="post">';
214 echo '<div align="center">'.$strcurrentcontext.': '.print_context_name($context).'<br/>';
215 if ($userid) {
216 echo '<input type="hidden" name="userid" value="'.$userid.'" />';
218 echo '<input type="hidden" name="courseid" value="'.$courseid.'" />';
219 echo '<input type="hidden" name="contextid" value="'.$context->id.'" />'.$strroletoassign.': ';
220 choose_from_menu ($assignableroles, 'roleid', $roleid, get_string('listallroles', 'role').'...', $script='rolesform.submit()');
221 echo '</div></form>';
223 print_simple_box_start('center');
224 include('assign.html');
225 print_simple_box_end();
227 if (!empty($errors)) {
228 $msg = '<p>';
229 foreach ($errors as $e) {
230 $msg .= $e.'<br />';
232 $msg .= '</p>';
233 print_simple_box_start('center');
234 notify($msg);
235 print_simple_box_end();
238 } else { // Print overview table
240 // sync metacourse enrolments if needed
241 if ($inmeta) {
242 sync_metacourse($course);
245 $table->tablealign = 'center';
246 $table->cellpadding = 5;
247 $table->cellspacing = 0;
248 $table->width = '60%';
249 $table->head = array(get_string('roles', 'role'), get_string('description'), get_string('users'));
250 $table->wrap = array('nowrap', '', 'nowrap');
251 $table->align = array('right', 'left', 'center');
253 foreach ($assignableroles as $roleid => $rolename) {
254 $countusers = count_role_users($roleid, $context);
255 $description = format_string(get_field('role', 'description', 'id', $roleid));
256 $table->data[] = array('<a href="'.$baseurl.'&amp;roleid='.$roleid.'">'.$rolename.'</a>',$description, $countusers);
259 print_table($table);
262 if ($context->contextlevel == CONTEXT_SYSTEM) {
263 admin_externalpage_print_footer($adminroot);
264 } else {
265 print_footer($course);