MDL-48624 behat: Added extended timeout to ensure step pass
[moodle.git] / group / index.php
blobf3e7f37f6d376e89235e6012a124a3aec35183d0
1 <?php
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.
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/>.
18 /**
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
23 * @package core_group
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[]
33 if ($groupid) {
34 $groupids = array($groupid);
35 } else {
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 if ($userid) {
49 $url->param('user', $userid);
51 if ($groupid) {
52 $url->param('group', $groupid);
54 $PAGE->set_url($url);
56 // Make sure that the user has permissions to manage groups.
57 require_login($course);
59 $context = context_course::instance($course->id);
60 require_capability('moodle/course:managegroups', $context);
62 $PAGE->requires->js('/group/clientlib.js');
64 // Check for multiple/no group errors
65 if (!$singlegroup) {
66 switch($action) {
67 case 'ajax_getmembersingroup':
68 case 'showgroupsettingsform':
69 case 'showaddmembersform':
70 case 'updatemembers':
71 print_error('errorselectone', 'group', $returnurl);
75 switch ($action) {
76 case false: //OK, display form.
77 break;
79 case 'ajax_getmembersingroup':
80 $roles = array();
81 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, ' . get_all_user_name_fields(true, 'u'))) {
82 foreach($groupmemberroles as $roleid=>$roledata) {
83 $shortroledata = new stdClass();
84 $shortroledata->name = $roledata->name;
85 $shortroledata->users = array();
86 foreach($roledata->users as $member) {
87 $shortmember = new stdClass();
88 $shortmember->id = $member->id;
89 $shortmember->name = fullname($member, true);
90 $shortroledata->users[] = $shortmember;
92 $roles[] = $shortroledata;
95 echo json_encode($roles);
96 die; // Client side JavaScript takes it from here.
98 case 'deletegroup':
99 if (count($groupids) == 0) {
100 print_error('errorselectsome','group',$returnurl);
102 $groupidlist = implode(',', $groupids);
103 redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
104 break;
106 case 'showcreateorphangroupform':
107 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
108 break;
110 case 'showautocreategroupsform':
111 redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
112 break;
114 case 'showimportgroups':
115 redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
116 break;
118 case 'showgroupsettingsform':
119 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
120 break;
122 case 'updategroups': //Currently reloading.
123 break;
125 case 'removemembers':
126 break;
128 case 'showaddmembersform':
129 redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
130 break;
132 case 'updatemembers': //Currently reloading.
133 break;
135 default: //ERROR.
136 print_error('unknowaction', '', $returnurl);
137 break;
140 // Print the page and form
141 $strgroups = get_string('groups');
142 $strparticipants = get_string('participants');
144 /// Print header
145 $PAGE->set_title($strgroups);
146 $PAGE->set_heading($course->fullname);
147 $PAGE->set_pagelayout('standard');
148 echo $OUTPUT->header();
150 // Add tabs
151 $currenttab = 'groups';
152 require('tabs.php');
154 $disabled = 'disabled="disabled"';
156 // Some buttons are enabled if single group selected.
157 $showaddmembersform_disabled = $singlegroup ? '' : $disabled;
158 $showeditgroupsettingsform_disabled = $singlegroup ? '' : $disabled;
159 $deletegroup_disabled = count($groupids) > 0 ? '' : $disabled;
161 echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$strgroups, 3);
162 echo '<form id="groupeditform" action="index.php" method="post">'."\n";
163 echo '<div>'."\n";
164 echo '<input type="hidden" name="id" value="' . $courseid . '" />'."\n";
166 echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
167 echo '<tr>'."\n";
170 echo "<td>\n";
171 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping">&nbsp;</span></label></p>'."\n";
173 $onchange = 'M.core_group.membersCombo.refreshMembers();';
175 echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
176 echo ' onclick="window.status=this.selectedIndex==-1 ? \'\' : this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
178 $groups = groups_get_all_groups($courseid);
179 $selectedname = '&nbsp;';
180 $preventgroupremoval = array();
182 if ($groups) {
183 // Print out the HTML
184 foreach ($groups as $group) {
185 $select = '';
186 $usercount = $DB->count_records('groups_members', array('groupid'=>$group->id));
187 $groupname = format_string($group->name).' ('.$usercount.')';
188 if (in_array($group->id,$groupids)) {
189 $select = ' selected="selected"';
190 if ($singlegroup) {
191 // Only keep selected name if there is one group selected
192 $selectedname = $groupname;
195 if (!empty($group->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
196 $preventgroupremoval[$group->id] = true;
199 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
201 } else {
202 // Print an empty option to avoid the XHTML error of having an empty select element
203 echo '<option>&nbsp;</option>';
206 echo '</select>'."\n";
207 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
208 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
209 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
210 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
211 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" id="deletegroup" value="'
212 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
214 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
215 . get_string('creategroup', 'group') . '" /></p>'."\n";
217 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
218 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
220 echo '<p><input type="submit" name="act_showimportgroups" id="showimportgroups" value="'
221 . get_string('importgroups', 'core_group') . '" /></p>'."\n";
223 echo '</td>'."\n";
224 echo '<td>'."\n";
226 echo '<p><label for="members"><span id="memberslabel">'.
227 get_string('membersofselectedgroup', 'group').
228 ' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
229 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
230 echo '<select name="user" id="members" size="15" class="select"'."\n";
231 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
233 $member_names = array();
235 $atleastonemember = false;
236 if ($singlegroup) {
237 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, ' . get_all_user_name_fields(true, 'u'))) {
238 foreach($groupmemberroles as $roleid=>$roledata) {
239 echo '<optgroup label="'.s($roledata->name).'">';
240 foreach($roledata->users as $member) {
241 echo '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
242 $atleastonemember = true;
244 echo '</optgroup>';
249 if (!$atleastonemember) {
250 // Print an empty option to avoid the XHTML error of having an empty select element
251 echo '<option>&nbsp;</option>';
254 echo '</select>'."\n";
256 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
257 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
258 echo '</td>'."\n";
259 echo '</tr>'."\n";
260 echo '</table>'."\n";
262 //<input type="hidden" name="rand" value="om" />
263 echo '</div>'."\n";
264 echo '</form>'."\n";
266 $PAGE->requires->js_init_call('M.core_group.init_index', array($CFG->wwwroot, $courseid));
267 $PAGE->requires->js_init_call('M.core_group.groupslist', array($preventgroupremoval));
269 echo $OUTPUT->footer();
272 * Returns the first button action with the given prefix, taken from
273 * POST or GET, otherwise returns false.
274 * @see /lib/moodlelib.php function optional_param().
275 * @param string $prefix 'act_' as in 'action'.
276 * @return string The action without the prefix, or false if no action found.
278 function groups_param_action($prefix = 'act_') {
279 $action = false;
280 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
282 if ($_POST) {
283 $form_vars = $_POST;
285 elseif ($_GET) {
286 $form_vars = $_GET;
288 if ($form_vars) {
289 foreach ($form_vars as $key => $value) {
290 if (preg_match("/$prefix(.+)/", $key, $matches)) {
291 $action = $matches[1];
292 break;
296 if ($action && !preg_match('/^\w+$/', $action)) {
297 $action = false;
298 print_error('unknowaction');
300 ///if (debugging()) echo 'Debug: '.$action;
301 return $action;