1. Added proper breadcrumb to group/edit|create page (preserves previous grouping...
[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 }
73
74 // Print the page and form
75 $strgroups = get_string('groups');
76 $strparticipants = get_string('participants');
77
78 $groupname = groups_get_group_displayname($groupid);
79
80 print_header("$course->shortname: $strgroups",
6ba65fa0 81 $course->fullname,
f3f7610c
ML
82 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
83 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
84 "-> $strgroups", '', '', true, '', user_login_string($course, $USER));
85
f3f7610c
ML
86?>
87<div id="addmembersform">
88 <h3 class="main"><?php print_string('adduserstogroup', 'group'); echo " $groupname"; ?></h3>
89
90 <form name="assignform" id="assignform" method="post" action="">
91
92 <input type="hidden" name="sesskey" value="<?php p(sesskey()); ?>" />
93 <input type="hidden" name="courseid" value="<?php p($courseid); ?>" />
94 <input type="hidden" name="grouping" value="<?php echo $groupingid; ?>" />
95 <input type="hidden" name="group" value="<?php echo $groupid; ?>" />
96
97 <table summary="" align="center" cellpadding="5" cellspacing="0">
98 <tr>
99 <td valign="top">
100 <label for="removeselect"><?php print_string('existingusers', 'role'); //count($contextusers) ?></label>
101 <br />
102 <select name="removeselect[]" size="20" id="removeselect" multiple="multiple"
103 onfocus="document.assignform.add.disabled=true;
104 document.assignform.remove.disabled=false;
105 document.assignform.addselect.selectedIndex=-1;">
106<?php
107 $userids = groups_get_members($groupid);
108
109 if ($userids != false) {
110 // Put the groupings into a hash and sorts them
111 foreach ($userids as $userid) {
112 $listmembers[$userid] = groups_get_user_displayname($userid, $courseid);
113 }
114 natcasesort($listmembers);
115
116 // Print out the HTML
117 foreach($listmembers as $id => $name) {
118 echo "<option value=\"$id\">$name</option>\n";
119 }
120 }
121?>
122 </select></td>
123 <td valign="top">
124<?php // Hidden assignment? ?>
125
126 <?php check_theme_arrows(); ?>
127 <p class="arrow_button">
128 <input name="add" id="add" type="submit" value="<?php echo '&nbsp;'.$THEME->larrow.' &nbsp; &nbsp; '.get_string('add'); ?>" title="<?php print_string('add'); ?>" />
129 <br />
130 <input name="remove" id="remove" type="submit" value="<?php echo '&nbsp; '.$THEME->rarrow.' &nbsp; &nbsp; '.get_string('remove'); ?>" title="<?php print_string('remove'); ?>" />
131 </p>
132 </td>
133 <td valign="top">
134 <label for="addselect"><?php print_string('potentialusers', 'role'); //$usercount ?></label>
135 <br />
136 <select name="addselect[]" size="20" id="addselect" multiple="multiple"
137 onfocus="document.assignform.add.disabled=false;
138 document.assignform.remove.disabled=true;
139 document.assignform.removeselect.selectedIndex=-1;">
140 <?php
b39552af 141 //TODO: If no 'showall' button, then set true.
142 $showall = true;
f3f7610c 143 unset($userids);
b39552af 144 if (!$showall && $groupingid != GROUP_NOT_IN_GROUPING) {
f3f7610c
ML
145 $userids = groups_get_users_not_in_any_group_in_grouping($courseid, $groupingid, $groupid);
146 } else {
147 $userids = groups_get_users_not_in_group($courseid, $groupid);
148 }
149
150 if ($userids != false) {
151 // Put the groupings into a hash and sorts them
152 foreach ($userids as $userid) {
153 $nonmembers[$userid] = groups_get_user_displayname($userid, $courseid);
154 }
155 natcasesort($nonmembers);
156
157 // Print out the HTML
158 foreach($nonmembers as $id => $name) {
159 echo "<option value=\"$id\">$name</option>\n";
160 }
161 }
162 ?>
163 </select>
164 <br />
b39552af 165 <?php //TODO: Search box?
f3f7610c 166
b39552af 167 /*if (!empty($searchtext)) {
168 echo '<input name="showall" type="submit" value="'.get_string('showall').'" />'."\n";
169 }*/
f3f7610c
ML
170 ?>
171 </td>
172 </tr>
173 <tr><td>
174 <input type="submit" name="cancel" value="<?php print_string('return', 'group'); ?>" />
175 </td></tr>
176 </table>
177
178 </form>
179</div>
180
181<?php
182 print_footer($course);
183}
184
185?>