Added LinuxChix theme
[moodle-linuxchix.git] / group / members.php
blob85e6d1739fc1e37d404efdfa78b591e7999e9273
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 if ($potentialmemberscount <= MAX_USERS_PER_PAGE) {
105 if ($potentialmemberscount != 0) {
106 // Get other groups user already belongs to
107 $sql = "SELECT u.id AS userid, g.* FROM {$CFG->prefix}user u " .
108 "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid " .
109 "INNER JOIN {$CFG->prefix}groups g ON gm.groupid = g.id " .
110 "WHERE u.id IN (".implode(',',$potentialmembersids).") AND g.courseid = {$course->id} ";
111 $rs = get_recordset_sql($sql);
112 $groups = array();
113 $usergroups = array();
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>';
127 $potentialmembersoptions .= '</optgroup>';
129 } else {
130 $potentialmembersoptions .= '<option>&nbsp;</option>';
134 // Print the page and form
135 $strgroups = get_string('groups');
136 $strparticipants = get_string('participants');
137 $stradduserstogroup = get_string('adduserstogroup', 'group');
138 $strusergroupmembership = get_string('usergroupmembership', 'group');
140 $groupname = format_string($group->name);
142 $navlinks = array();
143 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$courseid", 'type' => 'misc');
144 $navlinks[] = array('name' => $strgroups, 'link' => "$CFG->wwwroot/group/index.php?id=$courseid", 'type' => 'misc');
145 $navlinks[] = array('name' => $stradduserstogroup, 'link' => null, 'type' => 'misc');
146 $navigation = build_navigation($navlinks);
148 print_header("$course->shortname: $strgroups", $course->fullname, $navigation, '', '', true, '', user_login_string($course, $USER));
150 // Print Javascript for showing the selected users group membership
152 <script type="text/javascript">
153 //<![CDATA[
154 var userSummaries = Array(
155 <?php
156 $membercnt = count($nonmembers);
157 $i=1;
158 foreach ($nonmembers as $userid => $potentalmember) {
160 if (isset($usergroups[$userid])) {
161 $usergrouplist = '<ul>';
163 foreach ($usergroups[$userid] as $groupitem) {
164 $usergrouplist .= '<li>'.addslashes_js(format_string($groupitem->name)).'</li>';
166 $usergrouplist .= '</ul>';
168 else {
169 $usergrouplist = '';
171 echo "'$usergrouplist'";
172 if ($i < $membercnt) {
173 echo ', ';
175 $i++;
180 function updateUserSummary() {
182 var selectEl = document.getElementById('addselect');
183 var summaryDiv = document.getElementById('group-usersummary');
184 var length = selectEl.length;
185 var selectCnt = 0;
186 var selectIdx = -1;
188 for(i=0;i<length;i++) {
189 if (selectEl.options[i].selected) {
190 selectCnt++;
191 selectIdx = i;
195 if (selectCnt == 1 && userSummaries[selectIdx]) {
196 summaryDiv.innerHTML = userSummaries[selectIdx];
197 } else {
198 summaryDiv.innerHTML = '';
201 return(true);
203 //]]>
204 </script>
206 <div id="addmembersform">
207 <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo ": $groupname"; ?></h3>
209 <form id="assignform" method="post" action="members.php">
210 <div>
211 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
212 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
214 <table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">
215 <tr>
216 <td valign="top">
218 <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
219 </p>
220 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
221 onfocus="document.getElementById('assignform').add.disabled=true;
222 document.getElementById('assignform').remove.disabled=false;
223 document.getElementById('assignform').addselect.selectedIndex=-1;"
224 onclick="this.focus();updateUserSummary();">
225 <?php echo $groupmembersoptions ?>
226 </select></td>
227 <td valign="top">
228 <?php // Hidden assignment? ?>
230 <?php check_theme_arrows(); ?>
231 <p class="arrow_button">
232 <input name="add" id="add" type="submit" value="<?php echo $THEME->larrow.'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
233 <input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$THEME->rarrow; ?>" title="<?php print_string('remove'); ?>" />
234 </p>
235 </td>
236 <td valign="top">
238 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
239 </p>
240 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
241 onfocus="updateUserSummary();document.getElementById('assignform').add.disabled=false;
242 document.getElementById('assignform').remove.disabled=true;
243 document.getElementById('assignform').removeselect.selectedIndex=-1;"
244 onclick="this.focus();updateUserSummary();">
245 <?php
246 if ($potentialmemberscount > MAX_USERS_PER_PAGE) {
247 echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
248 .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
249 } else {
250 echo $potentialmembersoptions;
253 </select>
254 <br />
255 <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
256 <input type="text" name="searchtext" id="searchtext" size="21" value="<?php p($searchtext, true) ?>"
257 onfocus ="getElementById('assignform').add.disabled=true;
258 getElementById('assignform').remove.disabled=true;
259 getElementById('assignform').removeselect.selectedIndex=-1;
260 getElementById('assignform').addselect.selectedIndex=-1;"
261 onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
262 if (keyCode == 13) {
263 getElementById('assignform').previoussearch.value=1;
264 getElementById('assignform').submit();
265 } " />
266 <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
267 <?php
268 if (!empty($searchtext)) {
269 echo '<br /><input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
272 </td>
273 <td valign="top">
274 <p><?php echo($strusergroupmembership) ?></p>
275 <div id="group-usersummary"></div>
276 </td>
277 </tr>
278 <tr><td>
279 <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
280 </td></tr>
281 </table>
282 </div>
283 </form>
284 </div>
286 <?php
287 print_footer($course);