adding group support for get_role_users
[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
7 * @author J.White AT open.ac.uk
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package groups
10 */
11require_once('../config.php');
12require_once('lib.php');
13require_once($CFG->libdir.'/moodlelib.php');
14
15$success = true;
16
17$courseid = required_param('courseid', PARAM_INT);
18$groupingid = required_param('grouping', PARAM_INT);
19$groupid = required_param('group', PARAM_INT);
20
21// Get the course information so we can print the header and
22// check the course id is valid
23$course = groups_get_course_info($courseid);
24if (! $course) {
25 $success = false;
b39552af 26 print_error('invalidcourse');
27}
28if (empty($groupid)) {
29 $success = false;
30 print_error('errorinvalidgroup', 'group', groups_home_url($courseid));
f3f7610c
ML
31}
32
33if ($success) {
34 // Make sure that the user has permissions to manage groups.
35 require_login($courseid);
36
37 $context = get_context_instance(CONTEXT_COURSE, $courseid);
38 if (! has_capability('moodle/course:managegroups', $context)) {
39 redirect();
40 }
41
42 if ($frm = data_submitted() and confirm_sesskey()) {
43
44 if (isset($frm->cancel)) {
45 redirect('index.php?id='. $courseid
1ecd677e 46 .'&grouping='. $groupingid .'&group='. $groupid);
f3f7610c
ML
47 }
48 elseif (isset($frm->add) and !empty($frm->addselect)) {
49
50 foreach ($frm->addselect as $userid) {
51 if (! $userid = clean_param($userid, PARAM_INT)) {
52 continue;
53 }
f3f7610c
ML
54 $success = groups_add_member($groupid, $userid);
55 if (! $success) {
b39552af 56 print_error('erroraddremoveuser', 'group', groups_home_url($courseid));
f3f7610c
ML
57 }
58 }
59 }
60 elseif (isset($frm->remove) and !empty($frm->removeselect)) {
61
62 foreach ($frm->removeselect as $userid) {
63 if (! $userid = clean_param($userid, PARAM_INT)) {
64 continue;
65 }
66 $success = groups_remove_member($groupid, $userid);
67 if (! $success) {
b39552af 68 print_error('erroraddremoveuser', 'group', groups_home_url($courseid));
f3f7610c
ML
69 }
70 }
71 }
72 }
27c69abe 73
74 $groupmembers = groups_get_members($groupid);
75 $groupmembersoptions = '';
76 $groupmemberscount = 0;
77 if ($groupmembers != false) {
78 // Put the groupings into a hash and sorts them
79 foreach ($groupmembers as $userid) {
80 $listmembers[$userid] = groups_get_user_displayname($userid, $courseid);
81 $groupmemberscount ++;
82 }
83 natcasesort($listmembers);
84
85 // Print out the HTML
86 foreach($listmembers as $id => $name) {
87 $groupmembersoptions .= "<option value=\"$id\">$name</option>\n";
88 }
89 } else {
90 $groupmembersoptions .= '<option>&nbsp;</option>';
91 }
92
93 //TODO: If no 'showall' button, then set true.
94 $showall = true;
95
96 $potentialmembers = array();
97 $potentialmembersoptions = '';
98 $potentialmemberscount = 0;
99 if (!$showall && $groupingid != GROUP_NOT_IN_GROUPING) {
100 $potentialmembers = groups_get_users_not_in_any_group_in_grouping($courseid, $groupingid, $groupid);
101 } else {
102 $potentialmembers = groups_get_users_not_in_group($courseid, $groupid);
103 }
104
105 if ($potentialmembers != false) {
106 // Put the groupings into a hash and sorts them
107 foreach ($potentialmembers as $userid) {
108 $nonmembers[$userid] = groups_get_user_displayname($userid, $courseid);
109 $potentialmemberscount++;
110 }
111 natcasesort($nonmembers);
112
113 // Print out the HTML
114 foreach($nonmembers as $id => $name) {
115 $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
116 }
117 } else {
118 $potentialmembersoptions .= '<option>&nbsp;</option>';
119 }
f3f7610c
ML
120
121 // Print the page and form
122 $strgroups = get_string('groups');
123 $strparticipants = get_string('participants');
124
125 $groupname = groups_get_group_displayname($groupid);
126
127 print_header("$course->shortname: $strgroups",
6ba65fa0 128 $course->fullname,
f3f7610c
ML
129 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
130 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
b4e80a8b 131 '-> <a href="' .format_string(groups_home_url($courseid, $groupid, $groupingid, false)) . "\">$strgroups</a>".
132 '-> '. get_string('adduserstogroup', 'group'), '', '', true, '', user_login_string($course, $USER));
f3f7610c 133
f3f7610c
ML
134?>
135<div id="addmembersform">
136 <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo " $groupname"; ?></h3>
137
b4e80a8b 138 <form id="assignform" method="post" action="">
139 <div>
f3f7610c
ML
140 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
141 <input type="hidden" name="courseid" value="<?php p($courseid); ?>" />
142 <input type="hidden" name="grouping" value="<?php echo $groupingid; ?>" />
143 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
144
b4e80a8b 145 <table summary="" cellpadding="5" cellspacing="0">
f3f7610c
ML
146 <tr>
147 <td valign="top">
27c69abe 148 <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
f3f7610c
ML
149 <br />
150 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
b4e80a8b 151 onfocus="document.getElementById('assignform').add.disabled=true;
152 document.getElementById('assignform').remove.disabled=false;
153 document.getElementById('assignform').addselect.selectedIndex=-1;">
27c69abe 154 <?php echo $groupmembersoptions ?>
f3f7610c
ML
155 </select></td>
156 <td valign="top">
157<?php // Hidden assignment? ?>
158
159 <?php check_theme_arrows(); ?>
160 <p class="arrow_button">
161 <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
162 <br />
163 <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
164 </p>
165 </td>
166 <td valign="top">
27c69abe 167 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
f3f7610c
ML
168 <br />
169 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
b4e80a8b 170 onfocus="document.getElementById('assignform').add.disabled=false;
171 document.getElementById('assignform').remove.disabled=true;
172 document.getElementById('assignform').removeselect.selectedIndex=-1;">
27c69abe 173 <?php echo $potentialmembersoptions ?>
f3f7610c
ML
174 </select>
175 <br />
b39552af 176 <?php //TODO: Search box?
f3f7610c 177
b39552af 178 /*if (!empty($searchtext)) {
179 echo '<input name="showall" type="submit" value="'.get_string('showall').'" />'."\n";
180 }*/
f3f7610c
ML
181 ?>
182 </td>
183 </tr>
184 <tr><td>
e91cc88c 185 <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
f3f7610c
ML
186 </td></tr>
187 </table>
b4e80a8b 188 </div>
f3f7610c
ML
189 </form>
190</div>
191
192<?php
193 print_footer($course);
194}
195
196?>