MDL-55999 lib: Update ADOdb to version 5.20.7
[moodle.git] / group / index.php
blob1dfedf4df8ea0372c4d4cad01b4ceab65b457bf4
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";
175 $groups = groups_get_all_groups($courseid);
176 $selectedname = '&nbsp;';
177 $preventgroupremoval = array();
179 if ($groups) {
180 // Print out the HTML
181 foreach ($groups as $group) {
182 $select = '';
183 $usercount = $DB->count_records('groups_members', array('groupid'=>$group->id));
184 $groupname = format_string($group->name).' ('.$usercount.')';
185 if (in_array($group->id,$groupids)) {
186 $select = ' selected="selected"';
187 if ($singlegroup) {
188 // Only keep selected name if there is one group selected
189 $selectedname = $groupname;
192 if (!empty($group->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
193 $preventgroupremoval[$group->id] = true;
196 echo "<option value=\"{$group->id}\"$select title=\"$groupname\">$groupname</option>\n";
198 } else {
199 // Print an empty option to avoid the XHTML error of having an empty select element
200 echo '<option>&nbsp;</option>';
203 echo '</select>'."\n";
204 echo '<p><input type="submit" name="act_updatemembers" id="updatemembers" value="'
205 . get_string('showmembersforgroup', 'group') . '" /></p>'."\n";
206 echo '<p><input type="submit" '. $showeditgroupsettingsform_disabled . ' name="act_showgroupsettingsform" id="showeditgroupsettingsform" value="'
207 . get_string('editgroupsettings', 'group') . '" /></p>'."\n";
208 echo '<p><input type="submit" '. $deletegroup_disabled . ' name="act_deletegroup" id="deletegroup" value="'
209 . get_string('deleteselectedgroup', 'group') . '" /></p>'."\n";
211 echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
212 . get_string('creategroup', 'group') . '" /></p>'."\n";
214 echo '<p><input type="submit" name="act_showautocreategroupsform" id="showautocreategroupsform" value="'
215 . get_string('autocreategroups', 'group') . '" /></p>'."\n";
217 echo '<p><input type="submit" name="act_showimportgroups" id="showimportgroups" value="'
218 . get_string('importgroups', 'core_group') . '" /></p>'."\n";
220 echo html_writer::end_tag('div');
221 echo html_writer::start_tag('div', array('class' => 'members'));
223 echo '<p><label for="members"><span id="memberslabel">'.
224 get_string('membersofselectedgroup', 'group').
225 ' </span><span id="thegroup">'.$selectedname.'</span></label></p>'."\n";
226 //NOTE: the SELECT was, multiple="multiple" name="user[]" - not used and breaks onclick.
227 echo '<select name="user" id="members" size="15" class="select"'."\n";
228 echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
230 $member_names = array();
232 $atleastonemember = false;
233 if ($singlegroup) {
234 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid, 'u.id, ' . get_all_user_name_fields(true, 'u'))) {
235 foreach($groupmemberroles as $roleid=>$roledata) {
236 echo '<optgroup label="'.s($roledata->name).'">';
237 foreach($roledata->users as $member) {
238 echo '<option value="'.$member->id.'">'.fullname($member, true).'</option>';
239 $atleastonemember = true;
241 echo '</optgroup>';
246 if (!$atleastonemember) {
247 // Print an empty option to avoid the XHTML error of having an empty select element
248 echo '<option>&nbsp;</option>';
251 echo '</select>'."\n";
253 echo '<p><input type="submit" ' . $showaddmembersform_disabled . ' name="act_showaddmembersform" '
254 . 'id="showaddmembersform" value="' . get_string('adduserstogroup', 'group'). '" /></p>'."\n";
255 echo html_writer::end_tag('div');
256 echo html_writer::end_tag('div');
258 //<input type="hidden" name="rand" value="om" />
259 echo '</div>'."\n";
260 echo '</form>'."\n";
262 $PAGE->requires->js_init_call('M.core_group.init_index', array($CFG->wwwroot, $courseid));
263 $PAGE->requires->js_init_call('M.core_group.groupslist', array($preventgroupremoval));
265 echo $OUTPUT->footer();
268 * Returns the first button action with the given prefix, taken from
269 * POST or GET, otherwise returns false.
270 * @see /lib/moodlelib.php function optional_param().
271 * @param string $prefix 'act_' as in 'action'.
272 * @return string The action without the prefix, or false if no action found.
274 function groups_param_action($prefix = 'act_') {
275 $action = false;
276 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
278 if ($_POST) {
279 $form_vars = $_POST;
281 elseif ($_GET) {
282 $form_vars = $_GET;
284 if ($form_vars) {
285 foreach ($form_vars as $key => $value) {
286 if (preg_match("/$prefix(.+)/", $key, $matches)) {
287 $action = $matches[1];
288 break;
292 if ($action && !preg_match('/^\w+$/', $action)) {
293 $action = false;
294 print_error('unknowaction');
296 ///if (debugging()) echo 'Debug: '.$action;
297 return $action;