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));
48 navigation_node
::override_active_url($url);
50 $url->param('user', $userid);
53 $url->param('group', $groupid);
57 // Make sure that the user has permissions to manage groups.
58 require_login($course);
60 $context = context_course
::instance($course->id
);
61 require_capability('moodle/course:managegroups', $context);
63 $PAGE->requires
->js('/group/clientlib.js');
65 // Check for multiple/no group errors
68 case 'ajax_getmembersingroup':
69 case 'showgroupsettingsform':
70 case 'showaddmembersform':
72 print_error('errorselectone', 'group', $returnurl);
77 case false: //OK, display form.
80 case 'ajax_getmembersingroup':
82 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, ' . get_all_user_name_fields(true, 'u'))) {
83 foreach($groupmemberroles as $roleid=>$roledata) {
84 $shortroledata = new stdClass();
85 $shortroledata->name
= $roledata->name
;
86 $shortroledata->users
= array();
87 foreach($roledata->users
as $member) {
88 $shortmember = new stdClass();
89 $shortmember->id
= $member->id
;
90 $shortmember->name
= fullname($member, true);
91 $shortroledata->users
[] = $shortmember;
93 $roles[] = $shortroledata;
96 echo json_encode($roles);
97 die; // Client side JavaScript takes it from here.
100 if (count($groupids) == 0) {
101 print_error('errorselectsome','group',$returnurl);
103 $groupidlist = implode(',', $groupids);
104 redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
107 case 'showcreateorphangroupform':
108 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
111 case 'showautocreategroupsform':
112 redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
115 case 'showimportgroups':
116 redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
119 case 'showgroupsettingsform':
120 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
123 case 'updategroups': //Currently reloading.
126 case 'removemembers':
129 case 'showaddmembersform':
130 redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
133 case 'updatemembers': //Currently reloading.
137 print_error('unknowaction', '', $returnurl);
141 // Print the page and form
142 $strgroups = get_string('groups');
143 $strparticipants = get_string('participants');
146 $PAGE->set_title($strgroups);
147 $PAGE->set_heading($course->fullname
);
148 $PAGE->set_pagelayout('standard');
149 echo $OUTPUT->header();
152 $currenttab = 'groups';
155 $disabled = 'disabled="disabled"';
157 // Some buttons are enabled if single group selected.
158 $showaddmembersform_disabled = $singlegroup ?
'' : $disabled;
159 $showeditgroupsettingsform_disabled = $singlegroup ?
'' : $disabled;
160 $deletegroup_disabled = count($groupids) > 0 ?
'' : $disabled;
162 echo $OUTPUT->heading(format_string($course->shortname
, true, array('context' => $context)) .' '.$strgroups, 3);
163 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
165 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
167 echo html_writer
::start_tag('div', array('class' => 'groupmanagementtable boxaligncenter'));
168 echo html_writer
::start_tag('div', array('class' => 'groups'));
170 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping"> </span></label></p>'."\n";
172 $onchange = 'M.core_group.membersCombo.refreshMembers();';
174 echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'">'."\n";
176 $groups = groups_get_all_groups($courseid);
177 $selectedname = ' ';
178 $preventgroupremoval = array();
181 // Print out the HTML
182 foreach ($groups as $group) {
184 $usercount = $DB->count_records('groups_members', array('groupid'=>$group->id
));
185 $groupname = format_string($group->name
).' ('.$usercount.')';
186 if (in_array($group->id
,$groupids)) {
187 $select = ' selected="selected"';
189 // Only keep selected name if there is one group selected
190 $selectedname = $groupname;
193 if (!empty($group->idnumber
) && !has_capability('moodle/course:changeidnumber', $context)) {
194 $preventgroupremoval[$group->id
] = true;
197 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
200 // Print an empty option to avoid the XHTML error of having an empty select element
201 echo '<option> </option>';
204 echo '</select>'."\n";
205 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
206 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
207 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
208 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
209 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" id="deletegroup" value="'
210 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
212 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
213 . get_string('creategroup', 'group') . '" /></p>'."\n";
215 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
216 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
218 echo '<p><input type="submit" name="act_showimportgroups" id="showimportgroups" value="'
219 . get_string('importgroups', 'core_group') . '" /></p>'."\n";
221 echo html_writer
::end_tag('div');
222 echo html_writer
::start_tag('div', array('class' => 'members'));
224 echo '<p><label for="members"><span id="memberslabel">'.
225 get_string('membersofselectedgroup', 'group').
226 ' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
227 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
228 echo '<select name="user" id="members" size="15" class="select"'."\n";
229 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
231 $member_names = array();
233 $atleastonemember = false;
235 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, ' . get_all_user_name_fields(true, 'u'))) {
236 foreach($groupmemberroles as $roleid=>$roledata) {
237 echo '<optgroup label="'.s($roledata->name
).'">';
238 foreach($roledata->users
as $member) {
239 echo '<option value="'.$member->id
.'">'.fullname($member, true).'</option>';
240 $atleastonemember = true;
247 if (!$atleastonemember) {
248 // Print an empty option to avoid the XHTML error of having an empty select element
249 echo '<option> </option>';
252 echo '</select>'."\n";
254 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
255 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
256 echo html_writer
::end_tag('div');
257 echo html_writer
::end_tag('div');
259 //<input type="hidden" name="rand" value="om" />
263 $PAGE->requires
->js_init_call('M.core_group.init_index', array($CFG->wwwroot
, $courseid));
264 $PAGE->requires
->js_init_call('M.core_group.groupslist', array($preventgroupremoval));
266 echo $OUTPUT->footer();
269 * Returns the first button action with the given prefix, taken from
270 * POST or GET, otherwise returns false.
271 * @see /lib/moodlelib.php function optional_param().
272 * @param string $prefix 'act_' as in 'action'.
273 * @return string The action without the prefix, or false if no action found.
275 function groups_param_action($prefix = 'act_') {
277 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
286 foreach ($form_vars as $key => $value) {
287 if (preg_match("/$prefix(.+)/", $key, $matches)) {
288 $action = $matches[1];
293 if ($action && !preg_match('/^\w+$/', $action)) {
295 print_error('unknowaction');
297 ///if (debugging()) echo 'Debug: '.$action;