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