3 * The main group management user interface.
5 * @copyright © 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
11 require_once('../config.php');
12 require_once('lib.php');
13 require_once($CFG->libdir
.'/moodlelib.php');
14 require_once($CFG->libdir
.'/json/JSON.php');
16 require_js('yui_yahoo');
17 require_js('yui_dom');
18 require_js('yui_connection');
19 require_js($CFG->wwwroot
.'/group/lib/clientlib.js');
24 $courseid = required_param('id', PARAM_INT
);
25 $groupingid = optional_param('grouping', GROUP_NOT_IN_GROUPING
, PARAM_INT
);
26 $groupid = optional_param('group', false, PARAM_INT
);
27 $userid = optional_param('user', false, PARAM_INT
);
28 $action = groups_param_action();
31 $groupingsforgroup = groups_get_groupings_for_group($groupid);
32 if ($groupingsforgroup) {
34 // We currently assume that a group can only belong to one grouping.
36 // The UI will have to be fixed if we want to support more than one
37 // groupings per group in the future.
39 // vy-shane AT moodle DOT com
40 $groupingid = array_shift($groupingsforgroup);
45 // Get the course information so we can print the header and
46 // check the course id is valid
47 $course = groups_get_course_info($courseid);
50 print_error('invalidcourse'); //'The course ID is invalid'
54 // Make sure that the user has permissions to manage groups.
55 require_login($courseid);
57 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
58 if (! has_capability('moodle/course:managegroups', $context)) {
59 redirect(); //"group.php?id=$course->id"); // Not allowed to see all groups
62 // Set the session key so we can check this later
63 $sesskey = !empty($USER->id
) ?
$USER->sesskey
: '';
67 case false: //OK, display form.
70 case 'ajax_getgroupsingrouping':
71 if (GROUP_NOT_IN_GROUPING
== $groupingid) {
72 $groupids = groups_get_groups_not_in_any_grouping($courseid);
74 $groupids = groups_get_groups_in_grouping($groupingid);
76 $group_names = groups_groupids_to_group_names($groupids);
77 $json = new Services_JSON();
78 echo $json->encode($group_names);
79 die; // Client side JavaScript takes it from here.
81 case 'ajax_getmembersingroup':
84 if ($memberids = groups_get_members($groupid)) {
85 $member_names = groups_userids_to_user_names($memberids, $courseid);
86 $json = new Services_JSON();
87 echo $json->encode($member_names);
89 die; // Client side JavaScript takes it from here.
91 case 'showgroupingsettingsform':
92 redirect(groups_grouping_edit_url($courseid, $groupingid, false));
94 case 'showgroupingpermsform':
96 case 'deletegrouping':
97 redirect(groups_grouping_edit_url($courseid, $groupingid, $html=false, $param='delete=1'));
99 case 'showcreategroupingform':
100 redirect(groups_grouping_edit_url($courseid, null, false));
102 case 'printerfriendly':
103 redirect('groupui/printgrouping.php?courseid='. $courseid .'&groupingid='. $groupingid);
106 case 'showgroupsettingsform':
107 redirect(groups_group_edit_url($courseid, $groupid, $groupingid, false));
110 redirect(groups_group_edit_url($courseid, $groupid, $groupingid, $html=false, $param='delete=1'));
114 case 'showcreategroupform':
115 // Allow groups to be created outside of groupings
117 if (GROUP_NOT_IN_GROUPING == $groupingid) {
118 print_error('errornotingrouping', 'group', groups_home_url($courseid), get_string('notingrouping', 'group'));
121 redirect(groups_group_edit_url($courseid, null, $groupingid, false));
123 case 'showcreateorphangroupform':
124 redirect(groups_group_edit_url($courseid, null, null, false));
126 case 'addgroupstogroupingform':
128 case 'updategroups': //Currently reloading.
131 case 'removemembers':
133 case 'showaddmembersform':
134 redirect(groups_members_add_url($courseid, $groupid, $groupingid, false));
136 case 'updatemembers': //Currently reloading.
141 error('Error, unknown button/action. Probably a user-interface bug!', groups_home_url($courseid));
146 // Print the page and form
147 $strgroups = get_string('groups');
148 $strparticipants = get_string('participants');
150 print_header("$course->shortname: $strgroups home", //TODO: home
152 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
153 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
154 "-> $strgroups", '', '', true, '', user_login_string($course, $USER));
156 $usehtmleditor = false;
157 //TODO: eventually we'll implement all buttons, meantime hide the ones we haven't finished.
158 $shownotdone = false;
159 $disabled = 'disabled="disabled"';
161 // Pre-disable buttons based on URL variables
162 if (!empty($groupingid) && $groupingid > -1) {
163 $showeditgroupsettingsform_disabled = '';
164 $showeditgroupingsettingsform_disabled = '';
165 $deletegroup_disabled = '';
166 $deletegrouping_disabled = '';
167 $printerfriendly_disabled = '';
168 $showcreategroupform_disabled = '';
170 $showeditgroupsettingsform_disabled = $disabled;
171 $showeditgroupingsettingsform_disabled = $disabled;
172 $deletegroup_disabled = $disabled;
173 $deletegrouping_disabled = $disabled;
174 $printerfriendly_disabled = $disabled;
175 $showcreategroupform_disabled = $disabled;
178 if ($groupingid == -1 && groups_count_groups_in_grouping(GROUP_NOT_IN_GROUPING
, $courseid) > 0) {
179 $printerfriendly_disabled = '';
182 if (!empty($groupid)) {
183 $showaddmembersform_disabled = '';
184 $showeditgroupsettingsform_disabled = '';
185 $deletegroup_disabled = '';
187 $deletegroup_disabled = $disabled;
188 $showeditgroupsettingsform_disabled = $disabled;
189 $showaddmembersform_disabled = $disabled;
192 print_heading(format_string($course->shortname
) .' '.$strgroups, 'center', 3);
193 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
195 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
198 <input type="hidden" name="groupid" value="<?php p($selectedgroup) ?>" />
199 <input type="hidden" name="sesskey" value="<?php p($sesskey) ?>" />
200 <input type="hidden" name="roleid" value="<?php p($roleid) ?>" />
202 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
204 echo '<td class="generalboxcontent">'."\n";
205 echo '<p><label for="groupings">' . get_string('groupings', 'group') . '<span id="dummygrouping"> </span></label></p>'."\n";
206 echo '<select name="grouping" id="groupings" size="15" class="select"';
207 echo ' onchange="groupsCombo.refreshGroups(this.options[this.selectedIndex].value);"';
208 //NOTE: onclick/onmouseout is for long names in IE6 (Firefox/IE7 display OPTION title).
209 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
211 $groupingids = groups_get_groupings($courseid);
212 if (groups_count_groups_in_grouping(GROUP_NOT_IN_GROUPING
, $courseid) > 0) {
213 //NOTE, only show the pseudo-grouping if it has groups.
214 $groupingids[] = GROUP_NOT_IN_GROUPING
;
217 $sel_groupingid = -1;
220 // Put the groupings into a hash and sort them
221 foreach($groupingids as $id) {
222 $listgroupings[$id] = groups_get_grouping_displayname($id, $courseid);
224 natcasesort($listgroupings);
226 // Print out the HTML
228 foreach($listgroupings as $id => $name) {
230 if ($groupingid == $id) { //|| $count <= 1) ??
231 $select = ' selected="selected"';
232 $sel_groupingid = $id;
234 echo "<option value=\"$id\"$select title=\"$name\">$name</option>\n";
238 echo '<option> </option>';
241 echo '</select>'."\n";
244 echo '<p><input type="submit" name="act_updategroups" id="updategroups" value="'
245 . get_string('showgroupsingrouping', 'group') . '" /></p>'."\n";
246 echo '<p><input type="submit" ' . $showeditgroupingsettingsform_disabled . ' name="act_showgroupingsettingsform" id="showeditgroupingsettingsform" value="'
247 . get_string('editgroupingsettings', 'group') . '" /></p>'."\n";
250 echo '<p><input type="submit" '.$disabled.' name="act_showgroupingpermsform" '
251 . 'id="showeditgroupingpermissionsform" value="'
252 . get_string('editgroupingpermissions', 'group') . '" /></p>'."\n";
255 echo '<p><input type="submit" ' . $deletegrouping_disabled . ' name="act_deletegrouping" id="deletegrouping" value="'
256 . get_string('deletegrouping', 'group') . '" /></p>'."\n";
257 echo '<p><input type="submit" name="act_showcreategroupingform" id="showcreategroupingform" value="'
258 . get_string('creategrouping', 'group') . '" /></p>'."\n";
261 echo '<p><input type="submit" '.$disabled.' name="act_createautomaticgroupingform" '
262 . 'id="showcreateautomaticgroupingform" value="'
263 . get_string('createautomaticgrouping', 'group') . '" /></p>'."\n";
266 echo '<p><input type="submit" ' . $printerfriendly_disabled . ' name="act_printerfriendly" id="printerfriendly" value="'
267 . get_string('printerfriendly', 'group') . '" /></p>'."\n";
268 echo "</td>\n<td>\n";
269 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groupsinselectedgrouping', 'group').' </span><span id="thegrouping">'.get_string('grouping', 'group').'</span></label></p>'."\n";
270 echo '<select name="group" id="groups" size="15" class="select" onchange="membersCombo.refreshMembers(this.options[this.selectedIndex].value);"'."\n";
271 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
273 if (GROUP_NOT_IN_GROUPING
== $sel_groupingid) {
274 $groupids = groups_get_groups_not_in_any_grouping($courseid); //$sel_groupingid
276 $groupids = groups_get_groups_in_grouping($sel_groupingid);
279 // Put the groups into a hash and sort them
280 $group_names = groups_groupids_to_group_names($groupids);
282 // Print out the HTML
284 foreach ($group_names as $group) {
286 if ($groupid == $group->id
) { //|| $count <= 1) ??
287 $select = ' selected="selected"';
288 $sel_groupid = $group->id
;
290 echo "<option value=\"{$group->id}\"$select title=\"{$group->name}\">{$group->name}</option>\n";
294 // Print an empty option to avoid the XHTML error of having an empty select element
295 echo '<option> </option>';
298 echo '</select>'."\n";
299 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
300 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
301 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
302 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
303 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" onclick="onDeleteGroup()" id="deletegroup" value="'
304 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
307 echo '<p><input type="submit" '.$disabled.' name="act_removegroup" '
308 . 'id="removegroup" value="' . get_string('removegroupfromselectedgrouping', 'group') . '" /></p>'."\n";
311 echo '<p><input type="submit" ' . $showcreategroupform_disabled . ' name="act_showcreategroupform" id="showcreategroupform" value="'
312 . get_string('creategroupinselectedgrouping', 'group') . '" /></p>'."\n";
314 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
315 . get_string('createorphangroup', 'group') . '" /></p>'."\n";
318 echo '<p><input type="submit" '.$disabled.' name="act_addgroupstogroupingform" '
319 . 'id="showaddgroupstogroupingform" value="' . get_string('addgroupstogrouping', 'group') . '" /></p>'."\n";
324 echo '<p><label for="members"><span id="memberslabel">'.get_string('membersofselectedgroup', 'group').' </span><span id="thegroup">'.get_string('group', 'group').'</span></label></p>'."\n";
325 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
326 echo '<select name="user" id="members" size="15" class="select"'."\n";
327 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
329 if (isset($sel_groupid)) {
330 $userids = groups_get_members($sel_groupid);
332 if (isset($userids)) { //&& is_array($userids)
333 // Put the groupings into a hash and sort them
334 $user_names = groups_userids_to_user_names($userids, $courseid);
335 if(empty($user_names)) {
336 echo '<option> </option>';
338 foreach ($user_names as $user) {
339 echo "<option value=\"{$user->id}\" title=\"{$user->name}\">{$user->name}</option>\n";
343 // Print an empty option to avoid the XHTML error of having an empty select element
344 echo '<option> </option>';
347 echo '</select>'."\n";
350 echo '<p><input type="submit" '.$disabled.' name="act_removemembers" '
351 . 'id="removemembers" value="' . get_string('removeselectedusers', 'group') . '"/></p>'."\n";
354 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
355 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
358 echo '</table>'."\n";
360 //<input type="hidden" name="rand" value="om" />
364 echo '<script type="text/javascript">'."\n";
365 echo '//<![CDATA['."\n";
366 echo 'var groupsCombo = new UpdatableGroupsCombo("'.$CFG->wwwroot
.'", '.$course->id
.');'."\n";
367 echo 'var membersCombo = new UpdatableMembersCombo("'.$CFG->wwwroot
.'", '.$course->id
.');'."\n";
369 echo '</script>'."\n";
371 print_footer($course);