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');
14 $courseid = required_param('id', PARAM_INT
);
15 $groupid = optional_param('group', false, PARAM_INT
);
16 $userid = optional_param('user', false, PARAM_INT
);
17 $action = groups_param_action();
18 // Support either single group= parameter, or array groups[]
20 $groupids = array($groupid);
22 $groupids = optional_param_array('groups', array(), PARAM_INT
);
24 $singlegroup = (count($groupids) == 1);
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 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST
);
33 $url = new moodle_url('/group/index.php', array('id'=>$courseid));
35 $url->param('user', $userid);
38 $url->param('group', $groupid);
42 // Make sure that the user has permissions to manage groups.
43 require_login($course);
45 $PAGE->requires
->yui2_lib('connection');
46 $PAGE->requires
->js('/group/clientlib.js');
48 $context = get_context_instance(CONTEXT_COURSE
, $course->id
);
49 if (!has_capability('moodle/course:managegroups', $context)) {
50 redirect('/course/view.php', array('id'=>$course->id
)); // Not allowed to manage all groups
53 // Check for multiple/no group errors
56 case 'ajax_getmembersingroup':
57 case 'showgroupsettingsform':
58 case 'showaddmembersform':
60 print_error('errorselectone', 'group', $returnurl);
65 case false: //OK, display form.
68 case 'ajax_getmembersingroup':
70 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id,u.firstname,u.lastname')) {
71 foreach($groupmemberroles as $roleid=>$roledata) {
72 $shortroledata = new stdClass();
73 $shortroledata->name
= $roledata->name
;
74 $shortroledata->users
= array();
75 foreach($roledata->users
as $member) {
76 $shortmember = new stdClass();
77 $shortmember->id
= $member->id
;
78 $shortmember->name
= fullname($member, true);
79 $shortroledata->users
[] = $shortmember;
81 $roles[] = $shortroledata;
84 echo json_encode($roles);
85 die; // Client side JavaScript takes it from here.
88 if (count($groupids) == 0) {
89 print_error('errorselectsome','group',$returnurl);
91 $groupidlist = implode(',', $groupids);
92 redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
95 case 'showcreateorphangroupform':
96 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
99 case 'showautocreategroupsform':
100 redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
103 case 'showimportgroups':
104 redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
107 case 'showgroupsettingsform':
108 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
111 case 'updategroups': //Currently reloading.
114 case 'removemembers':
117 case 'showaddmembersform':
118 redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
121 case 'updatemembers': //Currently reloading.
125 print_error('unknowaction', '', $returnurl);
129 // Print the page and form
130 $strgroups = get_string('groups');
131 $strparticipants = get_string('participants');
134 $PAGE->set_title($strgroups);
135 $PAGE->set_heading($course->fullname
);
136 $PAGE->set_pagelayout('standard');
137 echo $OUTPUT->header();
140 $currenttab = 'groups';
143 $disabled = 'disabled="disabled"';
145 // Some buttons are enabled if single group selected
146 $showaddmembersform_disabled = $singlegroup ?
'' : $disabled;
147 $showeditgroupsettingsform_disabled = $singlegroup ?
'' : $disabled;
148 $deletegroup_disabled = count($groupids)>0 ?
'' : $disabled;
150 // Do not disable buttons. The buttons work based on the selected group,
151 // which you can change without reloading the page, so it is not appropriate
152 // to disable them if no group is selected.
153 $showaddmembersform_disabled = '';
154 $showeditgroupsettingsform_disabled = '';
155 $deletegroup_disabled = '';
158 echo $OUTPUT->heading(format_string($course->shortname
, true, array('context' => $context)) .' '.$strgroups, 3);
159 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
161 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
163 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
168 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping"> </span></label></p>'."\n";
170 if (ajaxenabled()) { // TODO: move this to JS init!
171 $onchange = 'M.core_group.membersCombo.refreshMembers();';
176 echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
177 echo ' onclick="window.status=this.selectedIndex==-1 ? \'\' : this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
179 $groups = groups_get_all_groups($courseid);
180 $selectedname = ' ';
183 // Print out the HTML
184 foreach ($groups as $group) {
186 $usercount = $DB->count_records('groups_members', array('groupid'=>$group->id
));
187 $groupname = format_string($group->name
).' ('.$usercount.')';
188 if (in_array($group->id
,$groupids)) {
189 $select = ' selected="selected"';
191 // Only keep selected name if there is one group selected
192 $selectedname = $groupname;
196 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
199 // Print an empty option to avoid the XHTML error of having an empty select element
200 echo '<option> </option>';
203 echo '</select>'."\n";
204 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
205 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
206 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
207 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
208 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" id="deletegroup" value="'
209 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
211 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
212 . get_string('creategroup', 'group') . '" /></p>'."\n";
214 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
215 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
217 echo '<p><input type="submit" name="act_showimportgroups" id="showimportgroups" value="'
218 . get_string('importgroups', 'core_group') . '" /></p>'."\n";
223 echo '<p><label for="members"><span id="memberslabel">'.
224 get_string('membersofselectedgroup', 'group').
225 ' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
226 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
227 echo '<select name="user" id="members" size="15" class="select"'."\n";
228 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
230 $member_names = array();
232 $atleastonemember = false;
234 if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
235 foreach($groupmemberroles as $roleid=>$roledata) {
236 echo '<optgroup label="'.s($roledata->name
).'">';
237 foreach($roledata->users
as $member) {
238 echo '<option value="'.$member->id
.'">'.fullname($member, true).'</option>';
239 $atleastonemember = true;
246 if (!$atleastonemember) {
247 // Print an empty option to avoid the XHTML error of having an empty select element
248 echo '<option> </option>';
251 echo '</select>'."\n";
253 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
254 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
257 echo '</table>'."\n";
259 //<input type="hidden" name="rand" value="om" />
264 $PAGE->requires
->js_init_call('M.core_group.init_index', array($CFG->wwwroot
, $courseid));
267 echo $OUTPUT->footer();
270 * Returns the first button action with the given prefix, taken from
271 * POST or GET, otherwise returns false.
272 * See /lib/moodlelib.php function optional_param.
273 * @param $prefix 'act_' as in 'action'.
274 * @return string The action without the prefix, or false if no action found.
276 function groups_param_action($prefix = 'act_') {
278 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
287 foreach ($form_vars as $key => $value) {
288 if (preg_match("/$prefix(.+)/", $key, $matches)) {
289 $action = $matches[1];
294 if ($action && !preg_match('/^\w+$/', $action)) {
296 print_error('unknowaction');
298 ///if (debugging()) echo 'Debug: '.$action;