MDL-40992 qbehaviour: new type method, can Qs can finish naturally
[moodle.git] / group / index.php
blobcbbb143db8bf74bdf2d8d76c809e8c21475038b9
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 html_writer::start_tag('div', array('class' => 'groupmanagementtable boxaligncenter'));
167 echo html_writer::start_tag('div', array('class' => 'groups'));
169 echo '<p><label for="groups"><span id="groupslabel">'.get_string('groups').':</span><span id="thegrouping">&nbsp;</span></label></p>'."\n";
171 $onchange = 'M.core_group.membersCombo.refreshMembers();';
173 echo '<select name="groups[]" multiple="multiple" id="groups" size="15" class="select" onchange="'.$onchange.'"'."\n";
174 echo ' onclick="window.status=this.selectedIndex==-1 ? \'\' : this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
176 $groups = groups_get_all_groups($courseid);
177 $selectedname = '&nbsp;';
178 $preventgroupremoval = array();
180 if ($groups) {
181 // Print out the HTML
182 foreach ($groups as $group) {
183 $select = '';
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"';
188 if ($singlegroup) {
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";
199 } else {
200 // Print an empty option to avoid the XHTML error of having an empty select element
201 echo '<option>&nbsp;</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;
234 if ($singlegroup) {
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;
242 echo '</optgroup>';
247 if (!$atleastonemember) {
248 // Print an empty option to avoid the XHTML error of having an empty select element
249 echo '<option>&nbsp;</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" />
260 echo '</div>'."\n";
261 echo '</form>'."\n";
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_') {
276 $action = false;
277 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
279 if ($_POST) {
280 $form_vars = $_POST;
282 elseif ($_GET) {
283 $form_vars = $_GET;
285 if ($form_vars) {
286 foreach ($form_vars as $key => $value) {
287 if (preg_match("/$prefix(.+)/", $key, $matches)) {
288 $action = $matches[1];
289 break;
293 if ($action && !preg_match('/^\w+$/', $action)) {
294 $action = false;
295 print_error('unknowaction');
297 ///if (debugging()) echo 'Debug: '.$action;
298 return $action;