Updated the 19 build version to 20081103
[moodle.git] / group / members.php
blob0ca98d099aa3e871b4337fcb241c49fcb1609902
1 <?php // $Id$
2 /**
3 * Add/remove members from group.
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
11 require_once('../config.php');
12 require_once('lib.php');
14 define("MAX_USERS_PER_PAGE", 5000);
16 $groupid = required_param('group', PARAM_INT);
17 $searchtext = optional_param('searchtext', '', PARAM_RAW); // search string
18 $showall = optional_param('showall', 0, PARAM_BOOL);
20 if ($showall) {
21 $searchtext = '';
24 if (!$group = get_record('groups', 'id', $groupid)) {
25 error('Incorrect group id');
28 if (!$course = get_record('course', 'id', $group->courseid)) {
29 print_error('invalidcourse');
31 $courseid = $course->id;
33 require_login($course);
34 $context = get_context_instance(CONTEXT_COURSE, $courseid);
35 require_capability('moodle/course:managegroups', $context);
37 $strsearch = get_string('search');
38 $strshowall = get_string('showall');
39 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid.'&group='.$group->id;
42 if ($frm = data_submitted() and confirm_sesskey()) {
44 if (isset($frm->cancel)) {
45 redirect($returnurl);
47 } else if (isset($frm->add) and !empty($frm->addselect)) {
49 foreach ($frm->addselect as $userid) {
50 if (! $userid = clean_param($userid, PARAM_INT)) {
51 continue;
53 if (!groups_add_member($groupid, $userid)) {
54 print_error('erroraddremoveuser', 'group', $returnurl);
58 } else if (isset($frm->remove) and !empty($frm->removeselect)) {
60 foreach ($frm->removeselect as $userid) {
61 if (! $userid = clean_param($userid, PARAM_INT)) {
62 continue;
64 if (!groups_remove_member($groupid, $userid)) {
65 print_error('erroraddremoveuser', 'group', $returnurl);
71 $groupmembersoptions = '';
72 $groupmemberscount = 0;
74 // Get members, organised by role, and display
75 if ($groupmemberroles = groups_get_members_by_role($groupid,$courseid,'u.id,u.firstname,u.lastname')) {
76 foreach($groupmemberroles as $roleid=>$roledata) {
77 $groupmembersoptions .= '<optgroup label="'.htmlspecialchars($roledata->name).'">';
78 foreach($roledata->users as $member) {
79 $groupmembersoptions .= '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
80 $groupmemberscount ++;
82 $groupmembersoptions .= '</optgroup>';
84 } else {
85 $groupmembersoptions .= '<option>&nbsp;</option>';
88 $potentialmembers = array();
89 $potentialmembersoptions = '';
90 $potentialmemberscount = 0;
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();
96 if (!empty($potentialmembersbyrole)) {
97 foreach($potentialmembersbyrole as $roledata) {
98 $potentialmemberscount+=count($roledata->users);
99 $potentialmembersids=array_merge($potentialmembersids,array_keys($roledata->users));
103 $usergroups = array();
105 if ($potentialmemberscount <= MAX_USERS_PER_PAGE) {
107 if ($potentialmemberscount != 0) {
108 // Get other groups user already belongs to
109 $sql = "SELECT u.id AS userid, g.* FROM {$CFG->prefix}user u " .
110 "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid " .
111 "INNER JOIN {$CFG->prefix}groups g ON gm.groupid = g.id " .
112 "WHERE u.id IN (".implode(',',$potentialmembersids).") AND g.courseid = {$course->id} ";
113 $rs = get_recordset_sql($sql);
114 while ($usergroup = rs_fetch_next_record($rs)) {
115 $usergroups[$usergroup->userid][$usergroup->id] = $usergroup;
117 rs_close($rs);
119 foreach($potentialmembersbyrole as $roleid=>$roledata) {
120 $potentialmembersoptions .= '<optgroup label="'.htmlspecialchars($roledata->name).'">';
121 foreach($roledata->users as $member) {
122 $name=htmlspecialchars(fullname($member, true));
123 $potentialmembersoptions .= '<option value="'.$member->id.
124 '" title="'.$name.'">'.$name.
125 ' ('.@count($usergroups[$member->id]).')</option>';
126 $potentialmembers[$member->id] = $member;
128 $potentialmembersoptions .= '</optgroup>';
130 } else {
131 $potentialmembersoptions .= '<option>&nbsp;</option>';
135 // Print the page and form
136 $strgroups = get_string('groups');
137 $strparticipants = get_string('participants');
138 $stradduserstogroup = get_string('adduserstogroup', 'group');
139 $strusergroupmembership = get_string('usergroupmembership', 'group');
141 $groupname = format_string($group->name);
143 $navlinks = array();
144 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
145 $navlinks[] = array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.php?id=$courseid", 'type' => 'misc');
146 $navlinks[] = array('name' => $stradduserstogroup, 'link' => null, 'type' => 'misc');
147 $navigation = build_navigation($navlinks);
149 print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '', '', true, '', user_login_string($course, $USER));
151 // Print Javascript for showing the selected users group membership
153 <script type="text/javascript">
154 //<![CDATA[
155 var userSummaries = Array(
156 <?php
157 $membercnt = count($potentialmembers);
158 $i=1;
159 foreach ($potentialmembers as $userid => $potentalmember) {
161 if (isset($usergroups[$userid])) {
162 $usergrouplist = '<ul>';
164 foreach ($usergroups[$userid] as $groupitem) {
165 $usergrouplist .= '<li>'.addslashes_js(format_string($groupitem->name)).'</li>';
167 $usergrouplist .= '</ul>';
169 else {
170 $usergrouplist = '';
172 echo "'$usergrouplist'";
173 if ($i < $membercnt) {
174 echo ', ';
176 $i++;
181 function updateUserSummary() {
183 var selectEl = document.getElementById('addselect');
184 var summaryDiv = document.getElementById('group-usersummary');
185 var length = selectEl.length;
186 var selectCnt = 0;
187 var selectIdx = -1;
189 for(i=0;i<length;i++) {
190 if (selectEl.options[i].selected) {
191 selectCnt++;
192 selectIdx = i;
196 if (selectCnt == 1 && userSummaries[selectIdx]) {
197 summaryDiv.innerHTML = userSummaries[selectIdx];
198 } else {
199 summaryDiv.innerHTML = '';
202 return(true);
204 //]]>
205 </script>
207 <div id="addmembersform">
208 <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo ": $groupname"; ?></h3>
210 <form id="assignform" method="post" action="members.php">
211 <div>
212 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
213 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
215 <table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
216 <tr>
217 <td valign="top">
219 <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
220 </p>
221 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
222 onfocus="document.getElementById('assignform').add.disabled=true;
223 document.getElementById('assignform').remove.disabled=false;
224 document.getElementById('assignform').addselect.selectedIndex=-1;"
225 onclick="this.focus();updateUserSummary();">
226 <?php echo $groupmembersoptions ?>
227 </select></td>
228 <td valign="top">
229 <?php // Hidden assignment? ?>
231 <?php check_theme_arrows(); ?>
232 <p class="arrow_button">
233 <input name="add" id="add" type="submit" value="<?php echo $THEME->larrow.'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
234 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$THEME->rarrow; ?>" title="<?php print_string('remove'); ?>" />
235 </p>
236 </td>
237 <td valign="top">
239 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
240 </p>
241 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
242 onfocus="updateUserSummary();document.getElementById('assignform').add.disabled=false;
243 document.getElementById('assignform').remove.disabled=true;
244 document.getElementById('assignform').removeselect.selectedIndex=-1;"
245 onclick="this.focus();updateUserSummary();">
246 <?php
247 if ($potentialmemberscount > MAX_USERS_PER_PAGE) {
248 echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
249 .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
250 } else {
251 echo $potentialmembersoptions;
254 </select>
255 <br />
256 <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
257 <input type="text" name="searchtext" id="searchtext" size="21" value="<?php p($searchtext, true) ?>"
258 onfocus ="getElementById('assignform').add.disabled=true;
259 getElementById('assignform').remove.disabled=true;
260 getElementById('assignform').removeselect.selectedIndex=-1;
261 getElementById('assignform').addselect.selectedIndex=-1;"
262 onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
263 if (keyCode == 13) {
264 getElementById('assignform').previoussearch.value=1;
265 getElementById('assignform').submit();
266 } " />
267 <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
268 <?php
269 if (!empty($searchtext)) {
270 echo '<br /><input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
273 </td>
274 <td valign="top">
275 <p><?php echo($strusergroupmembership) ?></p>
276 <div id="group-usersummary"></div>
277 </td>
278 </tr>
279 <tr><td>
280 <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
281 </td></tr>
282 </table>
283 </div>
284 </form>
285 </div>
287 <?php
288 print_footer($course);