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