"MDL-15251, see tracker"
[moodle-linuxchix.git] / group / members.php
blob2f92c8afdf432262552ad00df2972afdf165e310
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);
68 // MDL-9983
69 $eventdata = new object();
70 $eventdata -> groupid = $groupid;
71 $eventdata -> userid = $userid;
72 events_trigger('group_user_removed', $eventdata);
77 $groupmembersoptions = '';
78 $groupmemberscount = 0;
80 // Get members, organised by role, and display
81 if ($groupmemberroles = groups_get_members_by_role($groupid,$courseid,'u.id,u.firstname,u.lastname')) {
82 foreach($groupmemberroles as $roleid=>$roledata) {
83 $groupmembersoptions .= '<optgroup label="'.htmlspecialchars($roledata->name).'">';
84 foreach($roledata->users as $member) {
85 $groupmembersoptions .= '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
86 $groupmemberscount ++;
88 $groupmembersoptions .= '</optgroup>';
90 } else {
91 $groupmembersoptions .= '<option>&nbsp;</option>';
94 $potentialmembers = array();
95 $potentialmembersoptions = '';
96 $potentialmemberscount = 0;
98 // Get potential members, organised by role, and count them
99 $potentialmembersbyrole = groups_get_users_not_in_group_by_role($courseid, $groupid, $searchtext);
100 $potentialmemberscount=0;
101 $potentialmembersids=array();
102 if (!empty($potentialmembersbyrole)) {
103 foreach($potentialmembersbyrole as $roledata) {
104 $potentialmemberscount+=count($roledata->users);
105 $potentialmembersids=array_merge($potentialmembersids,array_keys($roledata->users));
109 if ($potentialmemberscount <= MAX_USERS_PER_PAGE) {
111 if ($potentialmemberscount != 0) {
112 // Get other groups user already belongs to
113 $sql = "SELECT u.id AS userid, g.* FROM {$CFG->prefix}user u " .
114 "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid " .
115 "INNER JOIN {$CFG->prefix}groups g ON gm.groupid = g.id " .
116 "WHERE u.id IN (".implode(',',$potentialmembersids).") AND g.courseid = {$course->id} ";
117 $rs = get_recordset_sql($sql);
118 $groups = array();
119 $usergroups = array();
120 while ($usergroup = rs_fetch_next_record($rs)) {
121 $usergroups[$usergroup->userid][$usergroup->id] = $usergroup;
123 rs_close($rs);
125 foreach($potentialmembersbyrole as $roleid=>$roledata) {
126 $potentialmembersoptions .= '<optgroup label="'.htmlspecialchars($roledata->name).'">';
127 foreach($roledata->users as $member) {
128 $name=htmlspecialchars(fullname($member, true));
129 $potentialmembersoptions .= '<option value="'.$member->id.
130 '" title="'.$name.'">'.$name.
131 ' ('.@count($usergroups[$member->id]).')</option>';
133 $potentialmembersoptions .= '</optgroup>';
135 } else {
136 $potentialmembersoptions .= '<option>&nbsp;</option>';
140 // Print the page and form
141 $strgroups = get_string('groups');
142 $strparticipants = get_string('participants');
143 $stradduserstogroup = get_string('adduserstogroup', 'group');
144 $strusergroupmembership = get_string('usergroupmembership', 'group');
146 $groupname = format_string($group->name);
148 $navlinks = array();
149 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
150 $navlinks[] = array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.php?id=$courseid", 'type' => 'misc');
151 $navlinks[] = array('name' => $stradduserstogroup, 'link' => null, 'type' => 'misc');
152 $navigation = build_navigation($navlinks);
154 print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '', '', true, '', user_login_string($course, $USER));
156 // Print Javascript for showing the selected users group membership
158 <script type="text/javascript">
159 //<![CDATA[
160 var userSummaries = Array(
161 <?php
162 $membercnt = count($nonmembers);
163 $i=1;
164 foreach ($nonmembers as $userid => $potentalmember) {
166 if (isset($usergroups[$userid])) {
167 $usergrouplist = '<ul>';
169 foreach ($usergroups[$userid] as $groupitem) {
170 $usergrouplist .= '<li>'.addslashes_js(format_string($groupitem->name)).'</li>';
172 $usergrouplist .= '</ul>';
174 else {
175 $usergrouplist = '';
177 echo "'$usergrouplist'";
178 if ($i < $membercnt) {
179 echo ', ';
181 $i++;
186 function updateUserSummary() {
188 var selectEl = document.getElementById('addselect');
189 var summaryDiv = document.getElementById('group-usersummary');
190 var length = selectEl.length;
191 var selectCnt = 0;
192 var selectIdx = -1;
194 for(i=0;i<length;i++) {
195 if (selectEl.options[i].selected) {
196 selectCnt++;
197 selectIdx = i;
201 if (selectCnt == 1 && userSummaries[selectIdx]) {
202 summaryDiv.innerHTML = userSummaries[selectIdx];
203 } else {
204 summaryDiv.innerHTML = '';
207 return(true);
209 //]]>
210 </script>
212 <div id="addmembersform">
213 <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo ": $groupname"; ?></h3>
215 <form id="assignform" method="post" action="members.php">
216 <div>
217 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
218 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
220 <table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
221 <tr>
222 <td valign="top">
224 <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
225 </p>
226 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
227 onfocus="document.getElementById('assignform').add.disabled=true;
228 document.getElementById('assignform').remove.disabled=false;
229 document.getElementById('assignform').addselect.selectedIndex=-1;"
230 onclick="this.focus();updateUserSummary();">
231 <?php echo $groupmembersoptions ?>
232 </select></td>
233 <td valign="top">
234 <?php // Hidden assignment? ?>
236 <?php check_theme_arrows(); ?>
237 <p class="arrow_button">
238 <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
239 <br />
240 <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
241 </p>
242 </td>
243 <td valign="top">
245 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
246 </p>
247 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
248 onfocus="updateUserSummary();document.getElementById('assignform').add.disabled=false;
249 document.getElementById('assignform').remove.disabled=true;
250 document.getElementById('assignform').removeselect.selectedIndex=-1;"
251 onclick="this.focus();updateUserSummary();">
252 <?php
253 if ($potentialmemberscount > MAX_USERS_PER_PAGE) {
254 echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
255 .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
256 } else {
257 echo $potentialmembersoptions;
260 </select>
261 <br />
262 <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
263 <input type="text" name="searchtext" id="searchtext" size="21" value="<?php p($searchtext, true) ?>"
264 onfocus ="getElementById('assignform').add.disabled=true;
265 getElementById('assignform').remove.disabled=true;
266 getElementById('assignform').removeselect.selectedIndex=-1;
267 getElementById('assignform').addselect.selectedIndex=-1;"
268 onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
269 if (keyCode == 13) {
270 getElementById('assignform').previoussearch.value=1;
271 getElementById('assignform').submit();
272 } " />
273 <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
274 <?php
275 if (!empty($searchtext)) {
276 echo '<br /><input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
279 </td>
280 <td valign="top">
281 <p><?php echo($strusergroupmembership) ?></p>
282 <div id="group-usersummary"></div>
283 </td>
284 </tr>
285 <tr><td>
286 <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
287 </td></tr>
288 </table>
289 </div>
290 </form>
291 </div>
293 <?php
294 print_footer($course);