2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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[]
34 $groupids = array($groupid);
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 navigation_node
::override_active_url($url);
50 $url->param('user', $userid);
53 $url->param('group', $groupid);
57 // Make sure that the user has permissions to manage groups.
58 require_login($course);
60 $context = context_course
::instance($course->id
);
61 require_capability('moodle/course:managegroups', $context);
63 $PAGE->requires
->js('/group/clientlib.js', true);
64 $PAGE->requires
->js('/group/module.js', true);
66 // Check for multiple/no group errors
69 case 'ajax_getmembersingroup':
70 case 'showgroupsettingsform':
71 case 'showaddmembersform':
73 print_error('errorselectone', 'group', $returnurl);
78 case false: //OK, display form.
81 case 'ajax_getmembersingroup':
84 // TODO Does not support custom user profile fields (MDL-70456).
85 $userfieldsapi = \core_user\fields
::for_identity($context, false)->with_userpic();
86 $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects
;
87 $extrafields = $userfieldsapi->get_required_fields([\core_user\fields
::PURPOSE_IDENTITY
]);
88 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid,
89 'u.id, ' . $userfields)) {
91 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
93 foreach($groupmemberroles as $roleid=>$roledata) {
94 $shortroledata = new stdClass();
95 $shortroledata->name
= $roledata->name
;
96 $shortroledata->users
= array();
97 foreach($roledata->users
as $member) {
98 $shortmember = new stdClass();
99 $shortmember->id
= $member->id
;
100 $shortmember->name
= fullname($member, $viewfullnames);
102 $extrafieldsdisplay = [];
103 foreach ($extrafields as $field) {
104 $extrafieldsdisplay[] = s($member->{$field});
106 $shortmember->name
.= ' (' . implode(', ', $extrafieldsdisplay) . ')';
109 $shortroledata->users
[] = $shortmember;
111 $roles[] = $shortroledata;
114 echo json_encode($roles);
115 die; // Client side JavaScript takes it from here.
118 if (count($groupids) == 0) {
119 print_error('errorselectsome','group',$returnurl);
121 $groupidlist = implode(',', $groupids);
122 redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
125 case 'showcreateorphangroupform':
126 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
129 case 'showautocreategroupsform':
130 redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
133 case 'showimportgroups':
134 redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
137 case 'showgroupsettingsform':
138 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
141 case 'updategroups': //Currently reloading.
144 case 'removemembers':
147 case 'showaddmembersform':
148 redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
151 case 'updatemembers': //Currently reloading.
155 print_error('unknowaction', '', $returnurl);
159 // Print the page and form
160 $strgroups = get_string('groups');
161 $strparticipants = get_string('participants');
164 $PAGE->set_title($strgroups);
165 $PAGE->set_heading($course->fullname
);
166 $PAGE->set_pagelayout('standard');
167 echo $OUTPUT->header();
170 $currenttab = 'groups';
173 echo $OUTPUT->heading(format_string($course->shortname
, true, array('context' => $context)) .' '.$strgroups, 3);
175 $groups = groups_get_all_groups($courseid);
176 $selectedname = null;
177 $preventgroupremoval = array();
179 // Get list of groups to render.
180 $groupoptions = array();
182 foreach ($groups as $group) {
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)) {
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 $groupoptions[] = (object) [
198 'value' => $group->id
,
199 'selected' => $selected,
205 // Get list of group members to render if there is a single selected group.
208 // TODO Does not support custom user profile fields (MDL-70456).
209 $userfieldsapi = \core_user\fields
::for_identity($context, false)->with_userpic();
210 $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects
;
211 $extrafields = $userfieldsapi->get_required_fields([\core_user\fields
::PURPOSE_IDENTITY
]);
212 if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid,
213 'u.id, ' . $userfields)) {
215 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
217 foreach ($groupmemberroles as $roleid => $roledata) {
219 foreach ($roledata->users
as $member) {
220 $shortmember = new stdClass();
221 $shortmember->value
= $member->id
;
222 $shortmember->text
= fullname($member, $viewfullnames);
224 $extrafieldsdisplay = [];
225 foreach ($extrafields as $field) {
226 $extrafieldsdisplay[] = s($member->{$field});
228 $shortmember->text
.= ' (' . implode(', ', $extrafieldsdisplay) . ')';
231 $users[] = $shortmember;
233 $members[] = (object)[
234 'role' => s($roledata->name
),
235 'rolemembers' => $users
241 $disableaddedit = !$singlegroup;
242 $disabledelete = !empty($groupids);
243 $renderable = new \core_group\output\
index_page($courseid, $groupoptions, $selectedname, $members, $disableaddedit, $disabledelete,
244 $preventgroupremoval);
245 $output = $PAGE->get_renderer('core_group');
246 echo $output->render($renderable);
248 echo $OUTPUT->footer();
251 * Returns the first button action with the given prefix, taken from
252 * POST or GET, otherwise returns false.
253 * @see /lib/moodlelib.php function optional_param().
254 * @param string $prefix 'act_' as in 'action'.
255 * @return string The action without the prefix, or false if no action found.
257 function groups_param_action($prefix = 'act_') {
259 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
268 foreach ($form_vars as $key => $value) {
269 if (preg_match("/$prefix(.+)/", $key, $matches)) {
270 $action = $matches[1];
275 if ($action && !preg_match('/^\w+$/', $action)) {
277 print_error('unknowaction');
279 ///if (debugging()) echo 'Debug: '.$action;