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