Make developer debug mode available to JavaScript.
[moodle.git] / group / members.php
CommitLineData
acf000b0 1<?php // $Id$
62d63838 2/**
3 * Add/remove members from group.
4 *
5 * @copyright &copy; 2006 The Open University
6 * @author N.D.Freear AT open.ac.uk
7 * @author J.White AT open.ac.uk
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package groups
10 */
11require_once('../config.php');
12require_once('lib.php');
13
14define("MAX_USERS_PER_PAGE", 5000);
15
16$groupid = required_param('group', PARAM_INT);
17$searchtext = optional_param('searchtext', '', PARAM_RAW); // search string
18$showall = optional_param('showall', 0, PARAM_BOOL);
19
20if ($showall) {
21 $searchtext = '';
22}
23
dfdaabd6 24if (!$group = $DB->get_record('groups', array('id'=>$groupid))) {
b121a4ee 25 print_error('invalidgroupid');
62d63838 26}
27
dfdaabd6 28if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
62d63838 29 print_error('invalidcourse');
30}
31$courseid = $course->id;
32
33require_login($course);
34$context = get_context_instance(CONTEXT_COURSE, $courseid);
35require_capability('moodle/course:managegroups', $context);
36
37$strsearch = get_string('search');
38$strshowall = get_string('showall');
39$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid.'&group='.$group->id;
40
41
42if ($frm = data_submitted() and confirm_sesskey()) {
43
44 if (isset($frm->cancel)) {
45 redirect($returnurl);
46
47 } else if (isset($frm->add) and !empty($frm->addselect)) {
48
49 foreach ($frm->addselect as $userid) {
50 if (! $userid = clean_param($userid, PARAM_INT)) {
51 continue;
52 }
53 if (!groups_add_member($groupid, $userid)) {
54 print_error('erroraddremoveuser', 'group', $returnurl);
55 }
56 }
57
58 } else if (isset($frm->remove) and !empty($frm->removeselect)) {
59
60 foreach ($frm->removeselect as $userid) {
61 if (! $userid = clean_param($userid, PARAM_INT)) {
62 continue;
63 }
64 if (!groups_remove_member($groupid, $userid)) {
65 print_error('erroraddremoveuser', 'group', $returnurl);
66 }
62d63838 67 }
68 }
69}
70
71$groupmembersoptions = '';
72$groupmemberscount = 0;
73
e254aa34 74// Get members, organised by role, and display
75if ($groupmemberroles = groups_get_members_by_role($groupid,$courseid,'u.id,u.firstname,u.lastname')) {
76 foreach($groupmemberroles as $roleid=>$roledata) {
dfdaabd6 77 $groupmembersoptions .= '<optgroup label="'.s($roledata->name).'">';
e254aa34 78 foreach($roledata->users as $member) {
79 $groupmembersoptions .= '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
80 $groupmemberscount ++;
81 }
82 $groupmembersoptions .= '</optgroup>';
62d63838 83 }
62d63838 84} else {
85 $groupmembersoptions .= '<option>&nbsp;</option>';
86}
87
88$potentialmembers = array();
89$potentialmembersoptions = '';
90$potentialmemberscount = 0;
91
e254aa34 92// Get potential members, organised by role, and count them
93$potentialmembersbyrole = groups_get_users_not_in_group_by_role($courseid, $groupid, $searchtext);
94$potentialmemberscount=0;
95$potentialmembersids=array();
96if (!empty($potentialmembersbyrole)) {
97 foreach($potentialmembersbyrole as $roledata) {
dfdaabd6 98 $potentialmemberscount += count($roledata->users);
99 $potentialmembersids = array_merge($potentialmembersids, array_keys($roledata->users));
e254aa34 100 }
62d63838 101}
e254aa34 102
09803dfc 103$usergroups = array();
104
62d63838 105if ($potentialmemberscount <= MAX_USERS_PER_PAGE) {
dfdaabd6 106 if ($potentialmemberscount > 0) {
acf000b0 107 // Get other groups user already belongs to
dfdaabd6 108 list($potentialmembersids, $params) = $DB->get_in_or_equal($potentialmembersids, SQL_PARAMS_NAMED, 'pm0');
109 $sql = "SELECT u.id AS userid, g.*
110 FROM {user} u
111 JOIN {groups_members} gm ON u.id = gm.userid
112 JOIN {groups} g ON gm.groupid = g.id
113 WHERE u.id $potentialmembersids AND g.courseid = :courseid ";
114 $params['courseid'] = $course->id;
dfdaabd6 115 if ($rs = $DB->get_recordset_sql($sql, $params)) {
116 foreach ($rs as $usergroup) {
117 $usergroups[$usergroup->userid][$usergroup->id] = $usergroup;
118 }
119 $rs->close();
acf000b0 120 }
e254aa34 121
dfdaabd6 122 foreach ($potentialmembersbyrole as $roleid=>$roledata) {
123 $potentialmembersoptions .= '<optgroup label="'.s($roledata->name).'">';
e254aa34 124 foreach($roledata->users as $member) {
dfdaabd6 125 $name = s(fullname($member, true));
e254aa34 126 $potentialmembersoptions .= '<option value="'.$member->id.
127 '" title="'.$name.'">'.$name.
128 ' ('.@count($usergroups[$member->id]).')</option>';
129 }
130 $potentialmembersoptions .= '</optgroup>';
62d63838 131 }
132 } else {
133 $potentialmembersoptions .= '<option>&nbsp;</option>';
134 }
135}
136
137// Print the page and form
138$strgroups = get_string('groups');
139$strparticipants = get_string('participants');
0be6f678 140$stradduserstogroup = get_string('adduserstogroup', 'group');
acf000b0 141$strusergroupmembership = get_string('usergroupmembership', 'group');
62d63838 142
143$groupname = format_string($group->name);
144
0be6f678 145$navlinks = array();
146$navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
147$navlinks[] = array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.php?id=$courseid", 'type' => 'misc');
148$navlinks[] = array('name' => $stradduserstogroup, 'link' => null, 'type' => 'misc');
149$navigation = build_navigation($navlinks);
150
151print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '', '', true, '', user_login_string($course, $USER));
62d63838 152
acf000b0 153// Print Javascript for showing the selected users group membership
154?>
155<script type="text/javascript">
156//<![CDATA[
f16fa0a3 157var userSummaries = Array(
acf000b0 158<?php
09803dfc 159$membercnt = count($potentialmembers);
acf000b0 160$i=1;
09803dfc 161foreach ($potentialmembers as $userid => $potentalmember) {
acf000b0 162
163 if (isset($usergroups[$userid])) {
164 $usergrouplist = '<ul>';
f16fa0a3 165
acf000b0 166 foreach ($usergroups[$userid] as $groupitem) {
dfdaabd6 167 $usergrouplist .= '<li>'.addslashes_js(format_string($groupitem->name)).'</li>';
acf000b0 168 }
169 $usergrouplist .= '</ul>';
170 }
171 else {
172 $usergrouplist = '';
173 }
174 echo "'$usergrouplist'";
175 if ($i < $membercnt) {
176 echo ', ';
177 }
178 $i++;
179}
62d63838 180?>
acf000b0 181);
182
183function updateUserSummary() {
184
185 var selectEl = document.getElementById('addselect');
186 var summaryDiv = document.getElementById('group-usersummary');
187 var length = selectEl.length;
188 var selectCnt = 0;
189 var selectIdx = -1;
f16fa0a3 190
acf000b0 191 for(i=0;i<length;i++) {
192 if (selectEl.options[i].selected) {
193 selectCnt++;
194 selectIdx = i;
195 }
196 }
197
198 if (selectCnt == 1 && userSummaries[selectIdx]) {
199 summaryDiv.innerHTML = userSummaries[selectIdx];
200 } else {
201 summaryDiv.innerHTML = '';
202 }
f16fa0a3 203
acf000b0 204 return(true);
205}
206//]]>
207</script>
208
62d63838 209<div id="addmembersform">
210 <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo ": $groupname"; ?></h3>
211
212 <form id="assignform" method="post" action="members.php">
213 <div>
214 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
215 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
216
acf000b0 217 <table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
62d63838 218 <tr>
219 <td valign="top">
acf000b0 220 <p>
dfdaabd6 221 <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); ?></label>
acf000b0 222 </p>
62d63838 223 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
224 onfocus="document.getElementById('assignform').add.disabled=true;
225 document.getElementById('assignform').remove.disabled=false;
acf000b0 226 document.getElementById('assignform').addselect.selectedIndex=-1;"
227 onclick="this.focus();updateUserSummary();">
62d63838 228 <?php echo $groupmembersoptions ?>
229 </select></td>
230 <td valign="top">
231<?php // Hidden assignment? ?>
232
233 <?php check_theme_arrows(); ?>
234 <p class="arrow_button">
d3789c64 235 <input name="add" id="add" type="submit" value="<?php echo $THEME->larrow.'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
236 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$THEME->rarrow; ?>" title="<?php print_string('remove'); ?>" />
62d63838 237 </p>
238 </td>
239 <td valign="top">
acf000b0 240 <p>
dfdaabd6 241 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); ?></label>
acf000b0 242 </p>
62d63838 243 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
acf000b0 244 onfocus="updateUserSummary();document.getElementById('assignform').add.disabled=false;
62d63838 245 document.getElementById('assignform').remove.disabled=true;
acf000b0 246 document.getElementById('assignform').removeselect.selectedIndex=-1;"
247 onclick="this.focus();updateUserSummary();">
62d63838 248 <?php
249 if ($potentialmemberscount > MAX_USERS_PER_PAGE) {
250 echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
251 .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
252 } else {
253 echo $potentialmembersoptions;
254 }
255 ?>
256 </select>
257 <br />
258 <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
dfdaabd6 259 <input type="text" name="searchtext" id="searchtext" size="21" value="<?php p($searchtext) ?>"
62d63838 260 onfocus ="getElementById('assignform').add.disabled=true;
261 getElementById('assignform').remove.disabled=true;
262 getElementById('assignform').removeselect.selectedIndex=-1;
263 getElementById('assignform').addselect.selectedIndex=-1;"
264 onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
265 if (keyCode == 13) {
266 getElementById('assignform').previoussearch.value=1;
267 getElementById('assignform').submit();
268 } " />
269 <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
270 <?php
271 if (!empty($searchtext)) {
dfdaabd6 272 echo '<br /><input name="showall" id="showall" type="submit" value="'.s($strshowall).'" />'."\n";
62d63838 273 }
274 ?>
275 </td>
acf000b0 276 <td valign="top">
aceb739d 277 <p><?php echo($strusergroupmembership) ?></p>
acf000b0 278 <div id="group-usersummary"></div>
279 </td>
62d63838 280 </tr>
281 <tr><td>
282 <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
283 </td></tr>
284 </table>
285 </div>
286 </form>
287</div>
288
289<?php
290 print_footer($course);
291?>