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