MDL-10604:
[moodle-linuxchix.git] / group / assign.php
blob3a6cefe1e7465c949cb61cdde2c2161350a5126d
1 <?php
2 /**
3 * Add/remove members from group.
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
11 require_once('../config.php');
12 require_once('lib.php');
13 require_once($CFG->libdir.'/moodlelib.php');
15 $success = true;
17 $courseid = required_param('courseid', PARAM_INT);
18 $groupingid = required_param('grouping', PARAM_INT);
19 $groupid = required_param('group', PARAM_INT);
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);
24 if (! $course) {
25 $success = false;
26 print_error('invalidcourse');
28 if (empty($groupid)) {
29 $success = false;
30 print_error('errorinvalidgroup', 'group', groups_home_url($courseid));
33 if ($success) {
34 // Make sure that the user has permissions to manage groups.
35 require_login($courseid);
37 $context = get_context_instance(CONTEXT_COURSE, $courseid);
38 if (! has_capability('moodle/course:managegroups', $context)) {
39 redirect();
42 if ($frm = data_submitted() and confirm_sesskey()) {
44 if (isset($frm->cancel)) {
45 redirect('index.php?id='. $courseid
46 .'&grouping='. $groupingid .'&group='. $groupid);
48 elseif (isset($frm->add) and !empty($frm->addselect)) {
50 foreach ($frm->addselect as $userid) {
51 if (! $userid = clean_param($userid, PARAM_INT)) {
52 continue;
54 $success = groups_add_member($groupid, $userid);
55 if (! $success) {
56 print_error('erroraddremoveuser', 'group', groups_home_url($courseid));
60 elseif (isset($frm->remove) and !empty($frm->removeselect)) {
62 foreach ($frm->removeselect as $userid) {
63 if (! $userid = clean_param($userid, PARAM_INT)) {
64 continue;
66 $success = groups_remove_member($groupid, $userid);
67 if (! $success) {
68 print_error('erroraddremoveuser', 'group', groups_home_url($courseid));
71 // MDL-9983
72 $eventdata = new object();
73 $eventdata -> groupid = $groupid;
74 $eventdata -> userid = $userid;
75 events_trigger('group_user_removed', $eventdata);
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 ++;
89 natcasesort($listmembers);
91 // Print out the HTML
92 foreach($listmembers as $id => $name) {
93 $groupmembersoptions .= "<option value=\"$id\">$name</option>\n";
95 } else {
96 $groupmembersoptions .= '<option>&nbsp;</option>';
99 //TODO: If no 'showall' button, then set true.
100 $showall = true;
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);
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++;
117 natcasesort($nonmembers);
119 // Print out the HTML
120 foreach($nonmembers as $id => $name) {
121 $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
123 } else {
124 $potentialmembersoptions .= '<option>&nbsp;</option>';
127 // Print the page and form
128 $strgroups = get_string('groups');
129 $strparticipants = get_string('participants');
131 $groupname = groups_get_group_displayname($groupid);
133 print_header("$course->shortname: $strgroups",
134 $course->fullname,
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> ".
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));
141 <div id="addmembersform">
142 <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo " $groupname"; ?></h3>
144 <form id="assignform" method="post" action="">
145 <div>
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; ?>" />
151 <table summary="" cellpadding="5" cellspacing="0">
152 <tr>
153 <td valign="top">
154 <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
155 <br />
156 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
157 onfocus="document.getElementById('assignform').add.disabled=true;
158 document.getElementById('assignform').remove.disabled=false;
159 document.getElementById('assignform').addselect.selectedIndex=-1;">
160 <?php echo $groupmembersoptions ?>
161 </select></td>
162 <td valign="top">
163 <?php // Hidden assignment? ?>
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">
173 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
174 <br />
175 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
176 onfocus="document.getElementById('assignform').add.disabled=false;
177 document.getElementById('assignform').remove.disabled=true;
178 document.getElementById('assignform').removeselect.selectedIndex=-1;">
179 <?php echo $potentialmembersoptions ?>
180 </select>
181 <br />
182 <?php //TODO: Search box?
184 /*if (!empty($searchtext)) {
185 echo '<input name="showall" type="submit" value="'.get_string('showall').'" />'."\n";
188 </td>
189 </tr>
190 <tr><td>
191 <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
192 </td></tr>
193 </table>
194 </div>
195 </form>
196 </div>
198 <?php
199 print_footer($course);