MDL-40353 fixed wrong redirection on course group page
[moodle.git] / group / index.php
CommitLineData
fa19c325 1<?php
4d8e2417
AG
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
16
17
fa19c325 18/**
19 * The main group management user interface.
20 *
4d8e2417
AG
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
23 * @package core_group
fa19c325 24 */
25require_once('../config.php');
26require_once('lib.php');
0addb366 27
2524b0f2 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();
f6eece19 32// Support either single group= parameter, or array groups[]
33if ($groupid) {
4f0c2d00 34 $groupids = array($groupid);
f6eece19 35} else {
18bd7573 36 $groupids = optional_param_array('groups', array(), PARAM_INT);
f6eece19 37}
4f0c2d00 38$singlegroup = (count($groupids) == 1);
77ea3330 39
2524b0f2 40$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
77ea3330 41
fa19c325 42// Get the course information so we can print the header and
43// check the course id is valid
2524b0f2 44
4f0c2d00 45$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
fa19c325 46
a6855934 47$url = new moodle_url('/group/index.php', array('id'=>$courseid));
af333695 48if ($userid) {
49 $url->param('user', $userid);
50}
51if ($groupid) {
52 $url->param('group', $groupid);
53}
54$PAGE->set_url($url);
55
778918fd 56// Make sure that the user has permissions to manage groups.
57require_login($course);
fa19c325 58
1d5932bf
MG
59$context = get_context_instance(CONTEXT_COURSE, $course->id);
60require_capability('moodle/course:managegroups', $context);
61
df997f84
PS
62$PAGE->requires->yui2_lib('connection');
63$PAGE->requires->js('/group/clientlib.js');
64
f6eece19 65// Check for multiple/no group errors
4f0c2d00 66if (!$singlegroup) {
f6eece19 67 switch($action) {
68 case 'ajax_getmembersingroup':
69 case 'showgroupsettingsform':
70 case 'showaddmembersform':
71 case 'updatemembers':
4f0c2d00 72 print_error('errorselectone', 'group', $returnurl);
f6eece19 73 }
74}
75
778918fd 76switch ($action) {
77 case false: //OK, display form.
78 break;
79
80 case 'ajax_getmembersingroup':
e254aa34 81 $roles = array();
4f0c2d00 82 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id,u.firstname,u.lastname')) {
e254aa34 83 foreach($groupmemberroles as $roleid=>$roledata) {
4f0c2d00
PS
84 $shortroledata = new stdClass();
85 $shortroledata->name = $roledata->name;
86 $shortroledata->users = array();
e254aa34 87 foreach($roledata->users as $member) {
4f0c2d00
PS
88 $shortmember = new stdClass();
89 $shortmember->id = $member->id;
90 $shortmember->name = fullname($member, true);
91 $shortroledata->users[] = $shortmember;
e254aa34 92 }
4f0c2d00 93 $roles[] = $shortroledata;
3cdc1e28 94 }
778918fd 95 }
1e074660 96 echo json_encode($roles);
778918fd 97 die; // Client side JavaScript takes it from here.
3cdc1e28 98
778918fd 99 case 'deletegroup':
4f0c2d00 100 if (count($groupids) == 0) {
f6eece19 101 print_error('errorselectsome','group',$returnurl);
102 }
4f0c2d00
PS
103 $groupidlist = implode(',', $groupids);
104 redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
778918fd 105 break;
2524b0f2 106
778918fd 107 case 'showcreateorphangroupform':
4f0c2d00 108 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
778918fd 109 break;
2524b0f2 110
acf000b0 111 case 'showautocreategroupsform':
4f0c2d00 112 redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
f16fa0a3 113 break;
acf000b0 114
81ed4632
PS
115 case 'showimportgroups':
116 redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
117 break;
118
778918fd 119 case 'showgroupsettingsform':
4f0c2d00 120 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
778918fd 121 break;
2524b0f2 122
778918fd 123 case 'updategroups': //Currently reloading.
124 break;
3cdc1e28 125
778918fd 126 case 'removemembers':
127 break;
2524b0f2 128
778918fd 129 case 'showaddmembersform':
4f0c2d00 130 redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
778918fd 131 break;
2524b0f2 132
778918fd 133 case 'updatemembers': //Currently reloading.
134 break;
5e4074b6 135
778918fd 136 default: //ERROR.
4f0c2d00 137 print_error('unknowaction', '', $returnurl);
778918fd 138 break;
778918fd 139}
fa19c325 140
778918fd 141// Print the page and form
142$strgroups = get_string('groups');
143$strparticipants = get_string('participants');
fa19c325 144
778918fd 145/// Print header
b87573d7 146$PAGE->set_title($strgroups);
5a47767c 147$PAGE->set_heading($course->fullname);
d6d07a68 148$PAGE->set_pagelayout('standard');
b87573d7 149echo $OUTPUT->header();
150
f16fa0a3 151// Add tabs
152$currenttab = 'groups';
153require('tabs.php');
fa19c325 154
778918fd 155$disabled = 'disabled="disabled"';
f6eece19 156if (ajaxenabled()) {
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;
161} else {
162 // Do not disable buttons. The buttons work based on the selected group,
163 // which you can change without reloading the page, so it is not appropriate
164 // to disable them if no group is selected.
778918fd 165 $showaddmembersform_disabled = '';
166 $showeditgroupsettingsform_disabled = '';
167 $deletegroup_disabled = '';
778918fd 168}
2524b0f2 169
8ebbb06a 170echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$strgroups, 3);
778918fd 171echo '<form id="groupeditform" action="index.php" method="post">'."\n";
172echo '<div>'."\n";
173echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
2524b0f2 174
778918fd 175echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
176echo '<tr>'."\n";
5bf243d1 177
2524b0f2 178
778918fd 179echo "<td>\n";
778918fd 180echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping">&nbsp;</span></label></p>'."\n";
59fe9db0 181
68dc01ab
PS
182if (ajaxenabled()) { // TODO: move this to JS init!
183 $onchange = 'M.core_group.membersCombo.refreshMembers();';
1dd3930b 184} else {
f16fa0a3 185 $onchange = '';
1dd3930b 186}
187
f6eece19 188echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
189echo ' onclick="window.status=this.selectedIndex==-1 ? \'\' : this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
08103c93 190
778918fd 191$groups = groups_get_all_groups($courseid);
f6eece19 192$selectedname = '&nbsp;';
74b714df 193$preventgroupremoval = array();
62d63838 194
778918fd 195if ($groups) {
196 // Print out the HTML
197 foreach ($groups as $group) {
198 $select = '';
f33e1ed4 199 $usercount = $DB->count_records('groups_members', array('groupid'=>$group->id));
778918fd 200 $groupname = format_string($group->name).' ('.$usercount.')';
f6eece19 201 if (in_array($group->id,$groupids)) {
202 $select = ' selected="selected"';
203 if ($singlegroup) {
204 // Only keep selected name if there is one group selected
205 $selectedname = $groupname;
206 }
207 }
74b714df
ARN
208 if (!empty($group->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
209 $preventgroupremoval[$group->id] = true;
210 }
6f5e0852 211
778918fd 212 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
fa19c325 213 }
778918fd 214} else {
215 // Print an empty option to avoid the XHTML error of having an empty select element
216 echo '<option>&nbsp;</option>';
217}
2524b0f2 218
778918fd 219echo '</select>'."\n";
220echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
221 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
222echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
223 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
2e839610 224echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" id="deletegroup" value="'
778918fd 225 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
226
227echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
228 . get_string('creategroup', 'group') . '" /></p>'."\n";
f16fa0a3 229
acf000b0 230echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
231 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
778918fd 232
81ed4632 233echo '<p><input type="submit" name="act_showimportgroups" id="showimportgroups" value="'
b74ceb3f 234 . get_string('importgroups', 'core_group') . '" /></p>'."\n";
81ed4632 235
778918fd 236echo '</td>'."\n";
237echo '<td>'."\n";
f6eece19 238
239echo '<p><label for="members"><span id="memberslabel">'.
240 get_string('membersofselectedgroup', 'group').
241 ' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
778918fd 242//NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
243echo '<select name="user" id="members" size="15" class="select"'."\n";
244echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
245
246$member_names = array();
247
e254aa34 248$atleastonemember = false;
f6eece19 249if ($singlegroup) {
250 if ($groupmemberroles = groups_get_members_by_role($groupids[0],$courseid,'u.id,u.firstname,u.lastname')) {
e254aa34 251 foreach($groupmemberroles as $roleid=>$roledata) {
dfdaabd6 252 echo '<optgroup label="'.s($roledata->name).'">';
e254aa34 253 foreach($roledata->users as $member) {
254 echo '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
255 $atleastonemember = true;
256 }
1e074660 257 echo '</optgroup>';
fa19c325 258 }
1e074660 259 }
778918fd 260}
1e074660 261
e254aa34 262if (!$atleastonemember) {
778918fd 263 // Print an empty option to avoid the XHTML error of having an empty select element
264 echo '<option>&nbsp;</option>';
265}
266
267echo '</select>'."\n";
10bf9730 268
778918fd 269echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
270 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
271echo '</td>'."\n";
272echo '</tr>'."\n";
273echo '</table>'."\n";
10bf9730 274
778918fd 275//<input type="hidden" name="rand" value="om" />
276echo '</div>'."\n";
277echo '</form>'."\n";
2524b0f2 278
b6c2b618 279if (ajaxenabled()) {
80db8e8d 280 $PAGE->requires->js_init_call('M.core_group.init_index', array($CFG->wwwroot, $courseid));
74b714df 281 $PAGE->requires->js_init_call('M.core_group.groupslist', array($preventgroupremoval));
b6c2b618 282}
3cdc1e28 283
653468d4 284echo $OUTPUT->footer();
2524b0f2 285
2c386f82 286/**
287 * Returns the first button action with the given prefix, taken from
288 * POST or GET, otherwise returns false.
4d8e2417
AG
289 * @see /lib/moodlelib.php function optional_param().
290 * @param string $prefix 'act_' as in 'action'.
2c386f82 291 * @return string The action without the prefix, or false if no action found.
292 */
293function groups_param_action($prefix = 'act_') {
294 $action = false;
295//($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
296
297 if ($_POST) {
298 $form_vars = $_POST;
299 }
300 elseif ($_GET) {
ddff2fa8 301 $form_vars = $_GET;
2c386f82 302 }
303 if ($form_vars) {
304 foreach ($form_vars as $key => $value) {
305 if (preg_match("/$prefix(.+)/", $key, $matches)) {
306 $action = $matches[1];
307 break;
308 }
309 }
310 }
311 if ($action && !preg_match('/^\w+$/', $action)) {
312 $action = false;
b121a4ee 313 print_error('unknowaction');
2c386f82 314 }
315 ///if (debugging()) echo 'Debug: '.$action;
316 return $action;
317}