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
.'/json/JSON.php');
15 require_js('yui_yahoo');
16 require_js('yui_dom');
17 require_js('yui_utilities');
18 require_js('yui_connection');
19 require_js($CFG->wwwroot
.'/group/clientlib.js');
21 $courseid = required_param('id', PARAM_INT
);
22 $groupid = optional_param('group', false, PARAM_INT
);
23 $userid = optional_param('user', false, PARAM_INT
);
24 $action = groups_param_action();
26 $returnurl = $CFG->wwwroot
.'/group/index.php?id='.$courseid;
28 // Get the course information so we can print the header and
29 // check the course id is valid
31 if (!$course = get_record('course', 'id',$courseid)) {
33 print_error('invalidcourse'); //'The course ID is invalid'
36 // Make sure that the user has permissions to manage groups.
37 require_login($course);
39 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
40 if (! has_capability('moodle/course:managegroups', $context)) {
41 redirect(); //"group.php?id=$course->id"); // Not allowed to see all groups
45 case false: //OK, display form.
48 case 'ajax_getmembersingroup':
50 if ($members = groups_get_members($groupid)) {
51 $member_names = array();
52 foreach($members as $member) {
54 $user->id
= $member->id
;
55 $user->name
= fullname($member, true);
56 $member_names[] = $user;
58 $json = new Services_JSON();
59 echo $json->encode($member_names);
61 die; // Client side JavaScript takes it from here.
64 redirect('group.php?delete=1&courseid='.$courseid.'&id='.$groupid);
67 case 'showcreateorphangroupform':
68 redirect('group.php?courseid='.$courseid);
71 case 'showautocreategroupsform':
72 redirect('autogroup.php?courseid='.$courseid);
75 case 'showgroupsettingsform':
76 redirect('group.php?courseid='.$courseid.'&id='.$groupid);
79 case 'updategroups': //Currently reloading.
85 case 'showaddmembersform':
86 redirect('members.php?group='.$groupid);
89 case 'updatemembers': //Currently reloading.
94 error('Error, unknown button/action. Probably a user-interface bug!', $returnurl);
99 // Print the page and form
100 $strgroups = get_string('groups');
101 $strparticipants = get_string('participants');
103 $navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot
.'/user/index.php?id='.$courseid, 'type'=>'misc'),
104 array('name'=>$strgroups, 'link'=>'', 'type'=>'misc'));
105 $navigation = build_navigation($navlinks);
108 print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
110 $currenttab = 'groups';
113 $disabled = 'disabled="disabled"';
115 $showeditgroupsettingsform_disabled = $disabled;
116 $deletegroup_disabled = $disabled;
117 $showcreategroupform_disabled = $disabled;
119 if (!empty($groupid)) {
120 $showaddmembersform_disabled = '';
121 $showeditgroupsettingsform_disabled = '';
122 $deletegroup_disabled = '';
124 $deletegroup_disabled = $disabled;
125 $showeditgroupsettingsform_disabled = $disabled;
126 $showaddmembersform_disabled = $disabled;
129 print_heading(format_string($course->shortname
) .' '.$strgroups, 'center', 3);
130 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
132 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
134 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
140 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping"> </span></label></p>'."\n";
143 $onchange = 'membersCombo.refreshMembers(this.options[this.selectedIndex].value);';
149 echo '<select name="group" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
150 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
152 $groups = groups_get_all_groups($courseid);
157 // Print out the HTML
158 foreach ($groups as $group) {
160 if ($groupid == $group->id
) {
161 $select = ' selected="selected"';
162 $sel_groupid = $group->id
;
164 $usercount = (int)count_records('groups_members', 'groupid', $group->id
);
165 $groupname = format_string($group->name
).' ('.$usercount.')';
167 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
170 // Print an empty option to avoid the XHTML error of having an empty select element
171 echo '<option> </option>';
174 echo '</select>'."\n";
175 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
176 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
177 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
178 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
179 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" onclick="onDeleteGroup()" id="deletegroup" value="'
180 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
182 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
183 . get_string('creategroup', 'group') . '" /></p>'."\n";
185 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
186 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
190 echo '<p><label for="members"><span id="memberslabel">'.get_string('membersofselectedgroup', 'group').' </span><span id="thegroup"> </span></label></p>'."\n";
191 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
192 echo '<select name="user" id="members" size="15" class="select"'."\n";
193 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
195 $member_names = array();
198 if ($members = groups_get_members($groupid)) {
199 foreach($members as $member) {
200 $member_names[$member->id
] = fullname($member, true);
206 // Put the groupings into a hash and sort them
207 foreach ($member_names as $userid=>$username) {
208 echo "<option value=\"{$userid}\" title=\"{$username}\">{$username}</option>\n";
212 // Print an empty option to avoid the XHTML error of having an empty select element
213 echo '<option> </option>';
216 echo '</select>'."\n";
218 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
219 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
222 echo '</table>'."\n";
224 //<input type="hidden" name="rand" value="om" />
229 echo '<script type="text/javascript">'."\n";
230 echo '//<![CDATA['."\n";
231 echo 'var groupsCombo = new UpdatableGroupsCombo("'.$CFG->wwwroot
.'", '.$course->id
.');'."\n";
232 echo 'var membersCombo = new UpdatableMembersCombo("'.$CFG->wwwroot
.'", '.$course->id
.');'."\n";
234 echo '</script>'."\n";
237 print_footer($course);
240 * Returns the first button action with the given prefix, taken from
241 * POST or GET, otherwise returns false.
242 * See /lib/moodlelib.php function optional_param.
243 * @param $prefix 'act_' as in 'action'.
244 * @return string The action without the prefix, or false if no action found.
246 function groups_param_action($prefix = 'act_') {
248 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
257 foreach ($form_vars as $key => $value) {
258 if (preg_match("/$prefix(.+)/", $key, $matches)) {
259 $action = $matches[1];
264 if ($action && !preg_match('/^\w+$/', $action)) {
266 error('Action had wrong type.');
268 ///if (debugging()) echo 'Debug: '.$action;