2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * The main group management user interface.
21 * @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
26 require_once('lib.php');
28 $courseid = required_param('id', PARAM_INT
);
29 $groupid = optional_param('group', false, PARAM_INT
);
30 $userid = optional_param('user', false, PARAM_INT
);
31 $action = groups_param_action();
32 // Support either single group= parameter, or array groups[]
34 $groupids = array($groupid);
36 $groupids = optional_param_array('groups', array(), PARAM_INT
);
38 $singlegroup = (count($groupids) == 1);
40 $returnurl = $CFG->wwwroot
.'/group/index.php?id='.$courseid;
42 // Get the course information so we can print the header and
43 // check the course id is valid
45 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST
);
47 $url = new moodle_url('/group/index.php', array('id'=>$courseid));
49 $url->param('user', $userid);
52 $url->param('group', $groupid);
56 // Make sure that the user has permissions to manage groups.
57 require_login($course);
59 $PAGE->requires
->js('/group/clientlib.js');
61 $context = context_course
::instance($course->id
);
62 if (!has_capability('moodle/course:managegroups', $context)) {
63 redirect('/course/view.php', array('id'=>$course->id
)); // Not allowed to manage all groups
66 // Check for multiple/no group errors
69 case 'ajax_getmembersingroup':
70 case 'showgroupsettingsform':
71 case 'showaddmembersform':
73 print_error('errorselectone', 'group', $returnurl);
78 case false: //OK, display form.
81 case 'ajax_getmembersingroup':
83 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, u.firstname, u.lastname')) {
84 foreach($groupmemberroles as $roleid=>$roledata) {
85 $shortroledata = new stdClass();
86 $shortroledata->name
= $roledata->name
;
87 $shortroledata->users
= array();
88 foreach($roledata->users
as $member) {
89 $shortmember = new stdClass();
90 $shortmember->id
= $member->id
;
91 $shortmember->name
= fullname($member, true);
92 $shortroledata->users
[] = $shortmember;
94 $roles[] = $shortroledata;
97 echo json_encode($roles);
98 die; // Client side JavaScript takes it from here.
101 if (count($groupids) == 0) {
102 print_error('errorselectsome','group',$returnurl);
104 $groupidlist = implode(',', $groupids);
105 redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
108 case 'showcreateorphangroupform':
109 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
112 case 'showautocreategroupsform':
113 redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
116 case 'showimportgroups':
117 redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
120 case 'showgroupsettingsform':
121 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
124 case 'updategroups': //Currently reloading.
127 case 'removemembers':
130 case 'showaddmembersform':
131 redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
134 case 'updatemembers': //Currently reloading.
138 print_error('unknowaction', '', $returnurl);
142 // Print the page and form
143 $strgroups = get_string('groups');
144 $strparticipants = get_string('participants');
147 $PAGE->set_title($strgroups);
148 $PAGE->set_heading($course->fullname
);
149 $PAGE->set_pagelayout('admin');
150 echo $OUTPUT->header();
153 $currenttab = 'groups';
156 $disabled = 'disabled="disabled"';
158 // Some buttons are enabled if single group selected
159 $showaddmembersform_disabled = $singlegroup ?
'' : $disabled;
160 $showeditgroupsettingsform_disabled = $singlegroup ?
'' : $disabled;
161 $deletegroup_disabled = count($groupids)>0 ?
'' : $disabled;
163 // Do not disable buttons. The buttons work based on the selected group,
164 // which you can change without reloading the page, so it is not appropriate
165 // to disable them if no group is selected.
166 $showaddmembersform_disabled = '';
167 $showeditgroupsettingsform_disabled = '';
168 $deletegroup_disabled = '';
171 echo $OUTPUT->heading(format_string($course->shortname
, true, array('context' => $context)) .' '.$strgroups, 3);
172 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
174 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
176 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
181 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping"> </span></label></p>'."\n";
183 if (ajaxenabled()) { // TODO: move this to JS init!
184 $onchange = 'M.core_group.membersCombo.refreshMembers();';
189 echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
190 echo ' onclick="window.status=this.selectedIndex==-1 ? \'\' : this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
192 $groups = groups_get_all_groups($courseid);
193 $selectedname = ' ';
194 $preventgroupremoval = array();
197 // Print out the HTML
198 foreach ($groups as $group) {
200 $usercount = $DB->count_records('groups_members', array('groupid'=>$group->id
));
201 $groupname = format_string($group->name
).' ('.$usercount.')';
202 if (in_array($group->id
,$groupids)) {
203 $select = ' selected="selected"';
205 // Only keep selected name if there is one group selected
206 $selectedname = $groupname;
209 if (!empty($group->idnumber
) && !has_capability('moodle/course:changeidnumber', $context)) {
210 $preventgroupremoval[$group->id
] = true;
213 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
216 // Print an empty option to avoid the XHTML error of having an empty select element
217 echo '<option> </option>';
220 echo '</select>'."\n";
221 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
222 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
223 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
224 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
225 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" id="deletegroup" value="'
226 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
228 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
229 . get_string('creategroup', 'group') . '" /></p>'."\n";
231 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
232 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
234 echo '<p><input type="submit" name="act_showimportgroups" id="showimportgroups" value="'
235 . get_string('importgroups', 'core_group') . '" /></p>'."\n";
240 echo '<p><label for="members"><span id="memberslabel">'.
241 get_string('membersofselectedgroup', 'group').
242 ' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
243 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
244 echo '<select name="user" id="members" size="15" class="select"'."\n";
245 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
247 $member_names = array();
249 $atleastonemember = false;
251 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, u.firstname, u.lastname')) {
252 foreach($groupmemberroles as $roleid=>$roledata) {
253 echo '<optgroup label="'.s($roledata->name
).'">';
254 foreach($roledata->users
as $member) {
255 echo '<option value="'.$member->id
.'">'.fullname($member, true).'</option>';
256 $atleastonemember = true;
263 if (!$atleastonemember) {
264 // Print an empty option to avoid the XHTML error of having an empty select element
265 echo '<option> </option>';
268 echo '</select>'."\n";
270 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
271 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
274 echo '</table>'."\n";
276 //<input type="hidden" name="rand" value="om" />
281 $PAGE->requires
->js_init_call('M.core_group.init_index', array($CFG->wwwroot
, $courseid));
282 $PAGE->requires
->js_init_call('M.core_group.groupslist', array($preventgroupremoval));
285 echo $OUTPUT->footer();
288 * Returns the first button action with the given prefix, taken from
289 * POST or GET, otherwise returns false.
290 * @see /lib/moodlelib.php function optional_param().
291 * @param string $prefix 'act_' as in 'action'.
292 * @return string The action without the prefix, or false if no action found.
294 function groups_param_action($prefix = 'act_') {
296 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
305 foreach ($form_vars as $key => $value) {
306 if (preg_match("/$prefix(.+)/", $key, $matches)) {
307 $action = $matches[1];
312 if ($action && !preg_match('/^\w+$/', $action)) {
314 print_error('unknowaction');
316 ///if (debugging()) echo 'Debug: '.$action;