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();
30 if (empty($CFG->enablegroupings
)) {
32 $groupingid = GROUP_NOT_IN_GROUPING
;
36 $groupingsforgroup = groups_get_groupings_for_group($groupid);
37 if ($groupingsforgroup) {
39 // We currently assume that a group can only belong to one grouping.
41 // The UI will have to be fixed if we want to support more than one
42 // groupings per group in the future.
44 // vy-shane AT moodle DOT com
45 $groupingid = array_shift($groupingsforgroup);
50 // Get the course information so we can print the header and
51 // check the course id is valid
52 $course = groups_get_course_info($courseid);
55 print_error('invalidcourse'); //'The course ID is invalid'
59 // Make sure that the user has permissions to manage groups.
60 require_login($courseid);
62 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
63 if (! has_capability('moodle/course:managegroups', $context)) {
64 redirect(); //"group.php?id=$course->id"); // Not allowed to see all groups
67 // Set the session key so we can check this later
68 $sesskey = !empty($USER->id
) ?
$USER->sesskey
: '';
72 case false: //OK, display form.
75 case 'ajax_getgroupsingrouping':
76 if (GROUP_NOT_IN_GROUPING
== $groupingid) {
77 $groupids = groups_get_groups_not_in_any_grouping($courseid);
79 $groupids = groups_get_groups_in_grouping($groupingid);
81 $group_names = groups_groupids_to_group_names($groupids);
82 $json = new Services_JSON();
83 echo $json->encode($group_names);
84 die; // Client side JavaScript takes it from here.
86 case 'ajax_getmembersingroup':
89 if ($memberids = groups_get_members($groupid)) {
90 $member_names = groups_userids_to_user_names($memberids, $courseid);
91 $json = new Services_JSON();
92 echo $json->encode($member_names);
94 die; // Client side JavaScript takes it from here.
96 case 'showgroupingsettingsform':
97 redirect(groups_grouping_edit_url($courseid, $groupingid, false));
99 case 'showgroupingpermsform':
101 case 'deletegrouping':
102 redirect(groups_grouping_edit_url($courseid, $groupingid, $html=false, $param='delete=1'));
104 case 'showcreategroupingform':
105 redirect(groups_grouping_edit_url($courseid, null, false));
107 case 'printerfriendly':
108 redirect('printgrouping.php?courseid='. $courseid .'&groupingid='. $groupingid);
111 case 'showgroupsettingsform':
112 redirect(groups_group_edit_url($courseid, $groupid, $groupingid, false));
115 redirect(groups_group_edit_url($courseid, $groupid, $groupingid, $html=false, $param='delete=1'));
119 case 'showcreategroupform':
120 // Allow groups to be created outside of groupings
122 if (GROUP_NOT_IN_GROUPING == $groupingid) {
123 print_error('errornotingrouping', 'group', groups_home_url($courseid), get_string('notingrouping', 'group'));
126 redirect(groups_group_edit_url($courseid, null, $groupingid, false));
128 case 'showcreateorphangroupform':
129 redirect(groups_group_edit_url($courseid, null, null, false));
131 case 'addgroupstogroupingform':
133 case 'updategroups': //Currently reloading.
136 case 'removemembers':
138 case 'showaddmembersform':
139 redirect(groups_members_add_url($courseid, $groupid, $groupingid, false));
141 case 'updatemembers': //Currently reloading.
146 error('Error, unknown button/action. Probably a user-interface bug!', groups_home_url($courseid));
151 // Print the page and form
152 $strgroups = get_string('groups');
153 $strparticipants = get_string('participants');
155 print_header("$course->shortname: $strgroups home", //TODO: home
157 "<a href=\"$CFG->wwwroot/course/view.php?id=$courseid\">$course->shortname</a> ".
158 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$courseid\">$strparticipants</a> ".
159 "-> $strgroups", '', '', true, '', user_login_string($course, $USER));
161 $usehtmleditor = false;
162 //TODO: eventually we'll implement all buttons, meantime hide the ones we haven't finished.
163 $shownotdone = false;
164 $disabled = 'disabled="disabled"';
166 // Pre-disable buttons based on URL variables
167 if (!empty($groupingid) && $groupingid > -1) {
168 $showeditgroupsettingsform_disabled = '';
169 $showeditgroupingsettingsform_disabled = '';
170 $deletegroup_disabled = '';
171 $deletegrouping_disabled = '';
172 $printerfriendly_disabled = '';
173 $showcreategroupform_disabled = '';
175 $showeditgroupsettingsform_disabled = $disabled;
176 $showeditgroupingsettingsform_disabled = $disabled;
177 $deletegroup_disabled = $disabled;
178 $deletegrouping_disabled = $disabled;
179 $printerfriendly_disabled = $disabled;
180 $showcreategroupform_disabled = $disabled;
183 if ($groupingid == -1 && groups_count_groups_in_grouping(GROUP_NOT_IN_GROUPING
, $courseid) > 0) {
184 $printerfriendly_disabled = '';
187 if (!empty($groupid)) {
188 $showaddmembersform_disabled = '';
189 $showeditgroupsettingsform_disabled = '';
190 $deletegroup_disabled = '';
192 $deletegroup_disabled = $disabled;
193 $showeditgroupsettingsform_disabled = $disabled;
194 $showaddmembersform_disabled = $disabled;
197 print_heading(format_string($course->shortname
) .' '.$strgroups, 'center', 3);
198 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
200 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
203 <input type="hidden" name="groupid" value="<?php p($selectedgroup) ?>" />
204 <input type="hidden" name="sesskey" value="<?php p($sesskey) ?>" />
205 <input type="hidden" name="roleid" value="<?php p($roleid) ?>" />
207 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
210 if (empty($CFG->enablegroupings
)) {
212 $sel_groupingid = -1;
214 echo '<td class="generalboxcontent">'."\n";
215 echo '<p><label for="groupings">' . get_string('groupings', 'group') . '<span id="dummygrouping"> </span></label></p>'."\n";
216 echo '<select name="grouping" id="groupings" size="15" class="select"';
217 echo ' onchange="groupsCombo.refreshGroups(this.options[this.selectedIndex].value);"';
218 //NOTE: onclick/onmouseout is for long names in IE6 (Firefox/IE7 display OPTION title).
219 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
221 $groupingids = groups_get_groupings($courseid);
222 if (groups_count_groups_in_grouping(GROUP_NOT_IN_GROUPING
, $courseid) > 0) {
223 //NOTE, only show the pseudo-grouping if it has groups.
224 $groupingids[] = GROUP_NOT_IN_GROUPING
;
227 $sel_groupingid = -1;
230 // Put the groupings into a hash and sort them
231 foreach($groupingids as $id) {
232 $listgroupings[$id] = groups_get_grouping_displayname($id, $courseid);
234 natcasesort($listgroupings);
236 // Print out the HTML
238 foreach($listgroupings as $id => $name) {
240 if ($groupingid == $id) { //|| $count <= 1) ??
241 $select = ' selected="selected"';
242 $sel_groupingid = $id;
244 echo "<option value=\"$id\"$select title=\"$name\">$name</option>\n";
248 echo '<option> </option>';
251 echo '</select>'."\n";
254 echo '<p><input type="submit" name="act_updategroups" id="updategroups" value="'
255 . get_string('showgroupsingrouping', 'group') . '" /></p>'."\n";
256 echo '<p><input type="submit" ' . $showeditgroupingsettingsform_disabled . ' name="act_showgroupingsettingsform" id="showeditgroupingsettingsform" value="'
257 . get_string('editgroupingsettings', 'group') . '" /></p>'."\n";
260 echo '<p><input type="submit" '.$disabled.' name="act_showgroupingpermsform" '
261 . 'id="showeditgroupingpermissionsform" value="'
262 . get_string('editgroupingpermissions', 'group') . '" /></p>'."\n";
265 echo '<p><input type="submit" ' . $deletegrouping_disabled . ' name="act_deletegrouping" id="deletegrouping" value="'
266 . get_string('deletegrouping', 'group') . '" /></p>'."\n";
267 echo '<p><input type="submit" name="act_showcreategroupingform" id="showcreategroupingform" value="'
268 . get_string('creategrouping', 'group') . '" /></p>'."\n";
271 echo '<p><input type="submit" '.$disabled.' name="act_createautomaticgroupingform" '
272 . 'id="showcreateautomaticgroupingform" value="'
273 . get_string('createautomaticgrouping', 'group') . '" /></p>'."\n";
276 echo '<p><input type="submit" ' . $printerfriendly_disabled . ' name="act_printerfriendly" id="printerfriendly" value="'
277 . get_string('printerfriendly', 'group') . '" /></p>'."\n";
281 if (empty($CFG->enablegroupings
)) {
283 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping"> </span></label></p>'."\n";
285 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groupsinselectedgrouping', 'group').' </span><span id="thegrouping">'.get_string('grouping', 'group').'</span></label></p>'."\n";
287 echo '<select name="group" id="groups" size="15" class="select" onchange="membersCombo.refreshMembers(this.options[this.selectedIndex].value);"'."\n";
288 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
290 if (GROUP_NOT_IN_GROUPING
== $sel_groupingid) {
291 $groupids = groups_get_groups_not_in_any_grouping($courseid); //$sel_groupingid
293 $groupids = groups_get_groups_in_grouping($sel_groupingid);
296 // Put the groups into a hash and sort them
297 $group_names = groups_groupids_to_group_names($groupids);
299 // Print out the HTML
301 foreach ($group_names as $group) {
303 if ($groupid == $group->id
) { //|| $count <= 1) ??
304 $select = ' selected="selected"';
305 $sel_groupid = $group->id
;
307 echo "<option value=\"{$group->id}\"$select title=\"{$group->name}\">{$group->name}</option>\n";
311 // Print an empty option to avoid the XHTML error of having an empty select element
312 echo '<option> </option>';
315 echo '</select>'."\n";
316 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
317 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
318 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
319 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
320 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" onclick="onDeleteGroup()" id="deletegroup" value="'
321 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
324 echo '<p><input type="submit" '.$disabled.' name="act_removegroup" '
325 . 'id="removegroup" value="' . get_string('removegroupfromselectedgrouping', 'group') . '" /></p>'."\n";
328 if (empty($CFG->enablegroupings
)) {
330 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
331 . get_string('creategroup', 'group') . '" /></p>'."\n";
332 echo '<p><input type="submit" name="act_printerfriendly" id="printerfriendly" value="'
333 . get_string('printerfriendly', 'group') . '" /></p>'."\n";
335 echo '<p><input type="submit" ' . $showcreategroupform_disabled . ' name="act_showcreategroupform" id="showcreategroupform" value="'
336 . get_string('creategroupinselectedgrouping', 'group') . '" /></p>'."\n";
338 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
339 . get_string('createorphangroup', 'group') . '" /></p>'."\n";
342 echo '<p><input type="submit" '.$disabled.' name="act_addgroupstogroupingform" '
343 . 'id="showaddgroupstogroupingform" value="' . get_string('addgroupstogrouping', 'group') . '" /></p>'."\n";
349 echo '<p><label for="members"><span id="memberslabel">'.get_string('membersofselectedgroup', 'group').' </span><span id="thegroup"> </span></label></p>'."\n";
350 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
351 echo '<select name="user" id="members" size="15" class="select"'."\n";
352 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
354 if (isset($sel_groupid)) {
355 $userids = groups_get_members($sel_groupid);
357 if (isset($userids)) { //&& is_array($userids)
358 // Put the groupings into a hash and sort them
359 $user_names = groups_userids_to_user_names($userids, $courseid);
360 if(empty($user_names)) {
361 echo '<option> </option>';
363 foreach ($user_names as $user) {
364 echo "<option value=\"{$user->id}\" title=\"{$user->name}\">{$user->name}</option>\n";
368 // Print an empty option to avoid the XHTML error of having an empty select element
369 echo '<option> </option>';
372 echo '</select>'."\n";
375 echo '<p><input type="submit" '.$disabled.' name="act_removemembers" '
376 . 'id="removemembers" value="' . get_string('removeselectedusers', 'group') . '"/></p>'."\n";
379 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
380 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
383 echo '</table>'."\n";
385 //<input type="hidden" name="rand" value="om" />
389 echo '<script type="text/javascript">'."\n";
390 echo '//<![CDATA['."\n";
391 echo 'var groupsCombo = new UpdatableGroupsCombo("'.$CFG->wwwroot
.'", '.$course->id
.');'."\n";
392 echo 'var membersCombo = new UpdatableMembersCombo("'.$CFG->wwwroot
.'", '.$course->id
.');'."\n";
394 echo '</script>'."\n";
396 print_footer($course);