Little strings...
[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 69 }
e5138261 70
71 // MDL-9983
72 $eventdata = new object();
73 $eventdata -> groupid = $groupid;
74 $eventdata -> userid = $userid;
75 events_trigger('group_user_removed', $eventdata);
f3f7610c
ML
76 }
77 }
78 }
27c69abe 79
80 $groupmembers = groups_get_members($groupid);
81 $groupmembersoptions = '';
82 $groupmemberscount = 0;
83 if ($groupmembers != false) {
84 // Put the groupings into a hash and sorts them
85 foreach ($groupmembers as $userid) {
86 $listmembers[$userid] = groups_get_user_displayname($userid, $courseid);
87 $groupmemberscount ++;
88 }
89 natcasesort($listmembers);
90
91 // Print out the HTML
92 foreach($listmembers as $id => $name) {
93 $groupmembersoptions .= "<option value=\"$id\">$name</option>\n";
94 }
95 } else {
96 $groupmembersoptions .= '<option>&nbsp;</option>';
97 }
98
99 //TODO: If no 'showall' button, then set true.
100 $showall = true;
101
102 $potentialmembers = array();
103 $potentialmembersoptions = '';
104 $potentialmemberscount = 0;
105 if (!$showall && $groupingid != GROUP_NOT_IN_GROUPING) {
106 $potentialmembers = groups_get_users_not_in_any_group_in_grouping($courseid, $groupingid, $groupid);
107 } else {
108 $potentialmembers = groups_get_users_not_in_group($courseid, $groupid);
109 }
110
111 if ($potentialmembers != false) {
112 // Put the groupings into a hash and sorts them
113 foreach ($potentialmembers as $userid) {
114 $nonmembers[$userid] = groups_get_user_displayname($userid, $courseid);
115 $potentialmemberscount++;
116 }
117 natcasesort($nonmembers);
118
119 // Print out the HTML
120 foreach($nonmembers as $id => $name) {
121 $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
122 }
123 } else {
124 $potentialmembersoptions .= '<option>&nbsp;</option>';
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
133 print_header("$course->shortname: $strgroups",
6ba65fa0 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> ".
b4e80a8b 137 '-> <a href="' .format_string(groups_home_url($courseid, $groupid, $groupingid, false)) . "\">$strgroups</a>".
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
ML
146 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
147 <input type="hidden" name="courseid" value="<?php p($courseid); ?>" />
148 <input type="hidden" name="grouping" value="<?php echo $groupingid; ?>" />
149 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
150
b4e80a8b 151 <table summary="" cellpadding="5" cellspacing="0">
f3f7610c
ML
152 <tr>
153 <td valign="top">
27c69abe 154 <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
f3f7610c
ML
155 <br />
156 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
b4e80a8b 157 onfocus="document.getElementById('assignform').add.disabled=true;
158 document.getElementById('assignform').remove.disabled=false;
159 document.getElementById('assignform').addselect.selectedIndex=-1;">
27c69abe 160 <?php echo $groupmembersoptions ?>
f3f7610c
ML
161 </select></td>
162 <td valign="top">
163<?php // Hidden assignment? ?>
164
165 <?php check_theme_arrows(); ?>
166 <p class="arrow_button">
167 <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
168 <br />
169 <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
170 </p>
171 </td>
172 <td valign="top">
27c69abe 173 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
f3f7610c
ML
174 <br />
175 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
b4e80a8b 176 onfocus="document.getElementById('assignform').add.disabled=false;
177 document.getElementById('assignform').remove.disabled=true;
178 document.getElementById('assignform').removeselect.selectedIndex=-1;">
27c69abe 179 <?php echo $potentialmembersoptions ?>
f3f7610c
ML
180 </select>
181 <br />
b39552af 182 <?php //TODO: Search box?
f3f7610c 183
b39552af 184 /*if (!empty($searchtext)) {
185 echo '<input name="showall" type="submit" value="'.get_string('showall').'" />'."\n";
186 }*/
f3f7610c
ML
187 ?>
188 </td>
189 </tr>
190 <tr><td>
e91cc88c 191 <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
f3f7610c
ML
192 </td></tr>
193 </table>
b4e80a8b 194 </div>
f3f7610c
ML
195 </form>
196</div>
197
198<?php
199 print_footer($course);
200}
201
202?>