some changes for better capabilities list display
[moodle.git] / group / assign.php
CommitLineData
f3f7610c
ML
1<?php
2/**
3 * Add/remove members from group.
4 *
5 * @copyright &copy; 2006 The Open University
6 * @author N.D.Freear AT open.ac.uk
2524b0f2 7 * @author J.White AT open.ac.uk
f3f7610c
ML
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package groups
10 */
11require_once('../config.php');
12require_once('lib.php');
f3f7610c 13
23431567 14define("MAX_USERS_PER_PAGE", 5000);
15
16$groupid = required_param('group', PARAM_INT);
17$searchtext = optional_param('searchtext', '', PARAM_RAW); // search string
18$showall = optional_param('showall', 0, PARAM_BOOL);
19
20if ($showall) {
21 $searchtext = '';
22}
23
f3f7610c 24
2524b0f2 25require_login();
26if (!$group = get_record('groups', 'id', $groupid)) {
27 error('Incorrect group id');
28}
f3f7610c 29
2524b0f2 30if (! $course = get_record('course', 'id', $group->courseid)) {
b39552af 31 print_error('invalidcourse');
32}
f3f7610c 33
2524b0f2 34require_login($course);
35$courseid = $course->id;
f3f7610c 36
23431567 37$strsearch = get_string('search');
38$strshowall = get_string('showall');
2524b0f2 39$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid.'&group='.$groupid;
40
41$context = get_context_instance(CONTEXT_COURSE, $courseid);
42require_capability('moodle/course:managegroups', $context);
43
44 if ($frm = data_submitted() and confirm_sesskey()) {
f3f7610c
ML
45
46 if (isset($frm->cancel)) {
2524b0f2 47 redirect($returnurl);
48
49 } else if (isset($frm->add) and !empty($frm->addselect)) {
f3f7610c
ML
50
51 foreach ($frm->addselect as $userid) {
52 if (! $userid = clean_param($userid, PARAM_INT)) {
53 continue;
54 }
2524b0f2 55 if (!groups_add_member($groupid, $userid)) {
56 print_error('erroraddremoveuser', 'group', $returnurl);
f3f7610c
ML
57 }
58 }
2524b0f2 59
60 } else if (isset($frm->remove) and !empty($frm->removeselect)) {
f3f7610c
ML
61
62 foreach ($frm->removeselect as $userid) {
63 if (! $userid = clean_param($userid, PARAM_INT)) {
64 continue;
65 }
2524b0f2 66 if (!groups_remove_member($groupid, $userid)) {
67 print_error('erroraddremoveuser', 'group', $returnurl);
f3f7610c 68 }
2524b0f2 69
e5138261 70 // MDL-9983
71 $eventdata = new object();
72 $eventdata -> groupid = $groupid;
73 $eventdata -> userid = $userid;
2524b0f2 74 events_trigger('group_user_removed', $eventdata);
f3f7610c
ML
75 }
76 }
77 }
2524b0f2 78
27c69abe 79 $groupmembers = groups_get_members($groupid);
80 $groupmembersoptions = '';
81 $groupmemberscount = 0;
82 if ($groupmembers != false) {
83 // Put the groupings into a hash and sorts them
84 foreach ($groupmembers as $userid) {
85 $listmembers[$userid] = groups_get_user_displayname($userid, $courseid);
86 $groupmemberscount ++;
87 }
88 natcasesort($listmembers);
89
90 // Print out the HTML
91 foreach($listmembers as $id => $name) {
92 $groupmembersoptions .= "<option value=\"$id\">$name</option>\n";
93 }
94 } else {
95 $groupmembersoptions .= '<option>&nbsp;</option>';
96 }
2524b0f2 97
98 $potentialmembers = array();
27c69abe 99 $potentialmembersoptions = '';
100 $potentialmemberscount = 0;
2524b0f2 101
23431567 102 $potentialmembers = groups_get_users_not_in_group($courseid, $groupid, $searchtext);
103 if (!empty($potentialmembers)) {
104 $potentialmemberscount = count($potentialmembers);
105 } else {
106 $potentialmemberscount = 0;
107 }
108 if ($potentialmemberscount <= MAX_USERS_PER_PAGE) {
27c69abe 109
23431567 110 if ($potentialmembers != false) {
111 // Put the groupings into a hash and sorts them
112 foreach ($potentialmembers as $userid => $user) {
113 $nonmembers[$userid] = fullname($user);
114 //$nonmembers[$userid] = groups_get_user_displayname($userid, $courseid);
115 }
116 natcasesort($nonmembers);
117
118 // Print out the HTML
119 foreach($nonmembers as $id => $name) {
120 $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
121 }
122 } else {
123 $potentialmembersoptions .= '<option>&nbsp;</option>';
27c69abe 124 }
27c69abe 125 }
f3f7610c
ML
126
127 // Print the page and form
128 $strgroups = get_string('groups');
129 $strparticipants = get_string('participants');
130
131 $groupname = groups_get_group_displayname($groupid);
132
2524b0f2 133 print_header("$course->shortname: $strgroups",
134 $course->fullname,
f3f7610c
ML
135 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
136 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
2524b0f2 137 "-> <a href=\"$CFG->wwwroot/group/index.php?id=$courseid\">$strgroups</a>".
b4e80a8b 138 '-> '. get_string('adduserstogroup', 'group'), '', '', true, '', user_login_string($course, $USER));
f3f7610c 139
f3f7610c
ML
140?>
141<div id="addmembersform">
142 <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo " $groupname"; ?></h3>
143
b4e80a8b 144 <form id="assignform" method="post" action="">
145 <div>
f3f7610c 146 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
f3f7610c
ML
147 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
148
b4e80a8b 149 <table summary="" cellpadding="5" cellspacing="0">
f3f7610c
ML
150 <tr>
151 <td valign="top">
27c69abe 152 <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
f3f7610c
ML
153 <br />
154 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
b4e80a8b 155 onfocus="document.getElementById('assignform').add.disabled=true;
156 document.getElementById('assignform').remove.disabled=false;
157 document.getElementById('assignform').addselect.selectedIndex=-1;">
27c69abe 158 <?php echo $groupmembersoptions ?>
f3f7610c
ML
159 </select></td>
160 <td valign="top">
161<?php // Hidden assignment? ?>
162
163 <?php check_theme_arrows(); ?>
164 <p class="arrow_button">
165 <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
166 <br />
167 <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
168 </p>
169 </td>
170 <td valign="top">
27c69abe 171 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
f3f7610c
ML
172 <br />
173 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
b4e80a8b 174 onfocus="document.getElementById('assignform').add.disabled=false;
175 document.getElementById('assignform').remove.disabled=true;
176 document.getElementById('assignform').removeselect.selectedIndex=-1;">
23431567 177 <?php
178 if ($potentialmemberscount > MAX_USERS_PER_PAGE) {
179 echo '<optgroup label="'.get_string('toomanytoshow').'"><option></option></optgroup>'."\n"
180 .'<optgroup label="'.get_string('trysearching').'"><option></option></optgroup>'."\n";
181 } else {
182 echo $potentialmembersoptions;
183 }
184 ?>
f3f7610c
ML
185 </select>
186 <br />
23431567 187 <label for="searchtext" class="accesshide"><?php p($strsearch) ?></label>
188 <input type="text" name="searchtext" id="searchtext" size="30" value="<?php p($searchtext, true) ?>"
189 onfocus ="getElementById('assignform').add.disabled=true;
190 getElementById('assignform').remove.disabled=true;
191 getElementById('assignform').removeselect.selectedIndex=-1;
192 getElementById('assignform').addselect.selectedIndex=-1;"
193 onkeydown = "var keyCode = event.which ? event.which : event.keyCode;
194 if (keyCode == 13) {
195 getElementById('assignform').previoussearch.value=1;
196 getElementById('assignform').submit();
197 } " />
198 <input name="search" id="search" type="submit" value="<?php p($strsearch) ?>" />
199 <?php
200 if (!empty($searchtext)) {
201 echo '<input name="showall" id="showall" type="submit" value="'.$strshowall.'" />'."\n";
202 }
f3f7610c
ML
203 ?>
204 </td>
205 </tr>
206 <tr><td>
2524b0f2 207 <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
f3f7610c
ML
208 </td></tr>
209 </table>
b4e80a8b 210 </div>
f3f7610c
ML
211 </form>
212</div>
213
214<?php
215 print_footer($course);
f3f7610c 216?>