3 * Add/remove members from group.
5 * @copyright © 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
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
);
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
)) {
47 } else if (isset($frm->add
) and !empty($frm->addselect
)) {
49 foreach ($frm->addselect
as $userid) {
50 if (! $userid = clean_param($userid, PARAM_INT
)) {
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
)) {
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>';
85 $groupmembersoptions .= '<option> </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);
113 $usergroups = array();
114 while ($usergroup = rs_fetch_next_record($rs)) {
115 $usergroups[$usergroup->userid
][$usergroup->id
] = $usergroup;
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>';
130 $potentialmembersoptions .= '<option> </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
);
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">
154 var userSummaries
= Array(
156 $membercnt = count($nonmembers);
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>';
171 echo "'$usergrouplist'";
172 if ($i < $membercnt) {
180 function updateUserSummary() {
182 var selectEl
= document
.getElementById('addselect');
183 var summaryDiv
= document
.getElementById('group-usersummary');
184 var length
= selectEl
.length
;
188 for(i
=0;i
<length
;i++
) {
189 if (selectEl
.options
[i
].selected
) {
195 if (selectCnt
== 1 && userSummaries
[selectIdx
]) {
196 summaryDiv
.innerHTML
= userSummaries
[selectIdx
];
198 summaryDiv
.innerHTML
= '';
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">
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
="">
218 <label
for="removeselect"><?php
print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
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 ?
>
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.' '.get_string('add'); ?>" title
="<?php print_string('add'); ?>" /><br
/>
233 <input name
="remove" id
="remove" type
="submit" value
="<?php echo get_string('remove').' '.$THEME->rarrow; ?>" title
="<?php print_string('remove'); ?>" />
238 <label
for="addselect"><?php
print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
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();">
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";
250 echo $potentialmembersoptions;
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;
263 getElementById('assignform').previoussearch.value=1;
264 getElementById('assignform').submit();
266 <input name
="search" id
="search" type
="submit" value
="<?php p($strsearch) ?>" />
268 if (!empty($searchtext)) {
269 echo '<br /><input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
274 <p
><?php
echo($strusergroupmembership) ?
></p
>
275 <div id
="group-usersummary"></div
>
279 <input type
="submit" name
="cancel" value
="<?php print_string('backtogroups', 'group'); ?>" />
287 print_footer($course);