Small upgrade to fix some guest->mnethostid. MDL-10375
[pfb-moodle.git] / group / assign.php
blob8e379c77c815fe923da5311e5c1b9586b368d853
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));
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 ++;
83 natcasesort($listmembers);
85 // Print out the HTML
86 foreach($listmembers as $id => $name) {
87 $groupmembersoptions .= "<option value=\"$id\">$name</option>\n";
89 } else {
90 $groupmembersoptions .= '<option>&nbsp;</option>';
93 //TODO: If no 'showall' button, then set true.
94 $showall = true;
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);
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++;
111 natcasesort($nonmembers);
113 // Print out the HTML
114 foreach($nonmembers as $id => $name) {
115 $potentialmembersoptions .= "<option value=\"$id\">$name</option>\n";
117 } else {
118 $potentialmembersoptions .= '<option>&nbsp;</option>';
121 // Print the page and form
122 $strgroups = get_string('groups');
123 $strparticipants = get_string('participants');
125 $groupname = groups_get_group_displayname($groupid);
127 print_header("$course->shortname: $strgroups",
128 $course->fullname,
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> ".
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));
135 <div id="addmembersform">
136 <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo " $groupname"; ?></h3>
138 <form id="assignform" method="post" action="">
139 <div>
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; ?>" />
145 <table summary="" cellpadding="5" cellspacing="0">
146 <tr>
147 <td valign="top">
148 <label for="removeselect"><?php print_string('existingmembers', 'group', $groupmemberscount); //count($contextusers) ?></label>
149 <br />
150 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
151 onfocus="document.getElementById('assignform').add.disabled=true;
152 document.getElementById('assignform').remove.disabled=false;
153 document.getElementById('assignform').addselect.selectedIndex=-1;">
154 <?php echo $groupmembersoptions ?>
155 </select></td>
156 <td valign="top">
157 <?php // Hidden assignment? ?>
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">
167 <label for="addselect"><?php print_string('potentialmembers', 'group', $potentialmemberscount); //$usercount ?></label>
168 <br />
169 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
170 onfocus="document.getElementById('assignform').add.disabled=false;
171 document.getElementById('assignform').remove.disabled=true;
172 document.getElementById('assignform').removeselect.selectedIndex=-1;">
173 <?php echo $potentialmembersoptions ?>
174 </select>
175 <br />
176 <?php //TODO: Search box?
178 /*if (!empty($searchtext)) {
179 echo '<input name="showall" type="submit" value="'.get_string('showall').'" />'."\n";
182 </td>
183 </tr>
184 <tr><td>
185 <input type="submit" name="cancel" value="<?php print_string('backtogroups', 'group'); ?>" />
186 </td></tr>
187 </table>
188 </div>
189 </form>
190 </div>
192 <?php
193 print_footer($course);