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 throw new \
moodle_exception('errorselectone', 'group', $returnurl);
78 case false: //OK, display form.
81 case 'ajax_getmembersingroup':
84 $userfieldsapi = \core_user\fields
::for_identity($context)->with_userpic();
86 'selects' => $userfieldsselects,
87 'joins' => $userfieldsjoin,
88 'params' => $userfieldsparams
89 ] = (array)$userfieldsapi->get_sql('u', true, '', '', false);
90 $extrafields = $userfieldsapi->get_required_fields([\core_user\fields
::PURPOSE_IDENTITY
]);
91 if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid,
92 'u.id, ' . $userfieldsselects, null, '', $userfieldsparams, $userfieldsjoin)) {
94 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
96 foreach($groupmemberroles as $roleid=>$roledata) {
97 $shortroledata = new stdClass();
98 $shortroledata->name
= $roledata->name
;
99 $shortroledata->users
= array();
100 foreach($roledata->users
as $member) {
101 $shortmember = new stdClass();
102 $shortmember->id
= $member->id
;
103 $shortmember->name
= fullname($member, $viewfullnames);
105 $extrafieldsdisplay = [];
106 foreach ($extrafields as $field) {
107 // No escaping here, handled client side in response to AJAX request.
108 $extrafieldsdisplay[] = $member->{$field};
110 $shortmember->name
.= ' (' . implode(', ', $extrafieldsdisplay) . ')';
113 $shortroledata->users
[] = $shortmember;
115 $roles[] = $shortroledata;
118 echo json_encode($roles);
119 die; // Client side JavaScript takes it from here.
122 if (count($groupids) == 0) {
123 throw new \
moodle_exception('errorselectsome', 'group', $returnurl);
125 $groupidlist = implode(',', $groupids);
126 redirect(new moodle_url('/group/delete.php', array('courseid'=>$courseid, 'groups'=>$groupidlist)));
129 case 'showcreateorphangroupform':
130 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid)));
133 case 'showautocreategroupsform':
134 redirect(new moodle_url('/group/autogroup.php', array('courseid'=>$courseid)));
137 case 'showimportgroups':
138 redirect(new moodle_url('/group/import.php', array('id'=>$courseid)));
141 case 'showgroupsettingsform':
142 redirect(new moodle_url('/group/group.php', array('courseid'=>$courseid, 'id'=>$groupids[0])));
145 case 'updategroups': //Currently reloading.
148 case 'removemembers':
151 case 'showaddmembersform':
152 redirect(new moodle_url('/group/members.php', array('group'=>$groupids[0])));
155 case 'updatemembers': //Currently reloading.
159 throw new \
moodle_exception('unknowaction', '', $returnurl);
163 // Print the page and form
164 $strgroups = get_string('groups');
165 $strparticipants = get_string('participants');
168 $PAGE->set_title($strgroups);
169 $PAGE->set_heading($course->fullname
);
170 $PAGE->set_pagelayout('standard');
171 echo $OUTPUT->header();
173 echo $OUTPUT->render_participants_tertiary_nav($course);
174 echo $OUTPUT->heading(format_string($course->shortname
, true, array('context' => $context)) .' '.$strgroups, 3);
176 $groups = groups_get_all_groups($courseid);
177 $selectedname = null;
178 $preventgroupremoval = array();
180 // Get list of groups to render.
181 $groupoptions = array();
183 foreach ($groups as $group) {
185 $usercount = $DB->count_records('groups_members', array('groupid' => $group->id
));
186 $groupname = format_string($group->name
) . ' (' . $usercount . ')';
187 if (in_array($group->id
, $groupids)) {
190 // Only keep selected name if there is one group selected.
191 $selectedname = $groupname;
194 if (!empty($group->idnumber
) && !has_capability('moodle/course:changeidnumber', $context)) {
195 $preventgroupremoval[$group->id
] = true;
198 $groupoptions[] = (object) [
199 'value' => $group->id
,
200 'selected' => $selected,
201 'text' => s($groupname)
206 // Get list of group members to render if there is a single selected group.
209 $userfieldsapi = \core_user\fields
::for_identity($context)->with_userpic();
211 'selects' => $userfieldsselects,
212 'joins' => $userfieldsjoin,
213 'params' => $userfieldsparams
214 ] = (array)$userfieldsapi->get_sql('u', true, '', '', false);
215 $extrafields = $userfieldsapi->get_required_fields([\core_user\fields
::PURPOSE_IDENTITY
]);
216 if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid,
217 'u.id, ' . $userfieldsselects, null, '', $userfieldsparams, $userfieldsjoin)) {
219 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
221 foreach ($groupmemberroles as $roleid => $roledata) {
223 foreach ($roledata->users
as $member) {
224 $shortmember = new stdClass();
225 $shortmember->value
= $member->id
;
226 $shortmember->text
= fullname($member, $viewfullnames);
228 $extrafieldsdisplay = [];
229 foreach ($extrafields as $field) {
230 $extrafieldsdisplay[] = s($member->{$field});
232 $shortmember->text
.= ' (' . implode(', ', $extrafieldsdisplay) . ')';
235 $users[] = $shortmember;
237 $members[] = (object)[
238 'role' => s($roledata->name
),
239 'rolemembers' => $users
245 $disableaddedit = !$singlegroup;
246 $disabledelete = !empty($groupids);
247 $renderable = new \core_group\output\
index_page($courseid, $groupoptions, $selectedname, $members, $disableaddedit, $disabledelete,
248 $preventgroupremoval);
249 $output = $PAGE->get_renderer('core_group');
250 echo $output->render($renderable);
252 echo $OUTPUT->footer();
255 * Returns the first button action with the given prefix, taken from
256 * POST or GET, otherwise returns false.
257 * @see /lib/moodlelib.php function optional_param().
258 * @param string $prefix 'act_' as in 'action'.
259 * @return string The action without the prefix, or false if no action found.
261 function groups_param_action($prefix = 'act_') {
263 //($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/
272 foreach ($form_vars as $key => $value) {
273 if (preg_match("/$prefix(.+)/", $key, $matches)) {
274 $action = $matches[1];
279 if ($action && !preg_match('/^\w+$/', $action)) {
281 throw new \
moodle_exception('unknowaction');
283 ///if (debugging()) echo 'Debug: '.$action;