Merge branch 'MDL-73165' of https://github.com/paulholden/moodle
[moodle.git] / group / index.php
blob94bc4b13c25af055239aad296f8762dd63f90521
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 = optional_param('action', false, PARAM_TEXT);
33 // Support either single group= parameter, or array groups[].
34 if ($groupid) {
35 $groupids = array($groupid);
36 } else {
37 $groupids = optional_param_array('groups', array(), PARAM_INT);
39 $singlegroup = (count($groupids) == 1);
41 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
43 // Get the course information so we can print the header and
44 // 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);
49 if ($userid) {
50 $url->param('user', $userid);
52 if ($groupid) {
53 $url->param('group', $groupid);
55 $PAGE->set_url($url);
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.
67 if (!$singlegroup) {
68 switch($action) {
69 case 'ajax_getmembersingroup':
70 case 'showgroupsettingsform':
71 case 'showaddmembersform':
72 case 'updatemembers':
73 throw new \moodle_exception('errorselectone', 'group', $returnurl);
77 switch ($action) {
78 case false: // OK, display form.
79 break;
81 case 'ajax_getmembersingroup':
82 $roles = array();
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 = html_entity_decode($roledata->name, ENT_QUOTES, 'UTF-8');
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);
104 if ($extrafields) {
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.
121 case 'deletegroup':
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)));
127 break;
129 case 'showcreateorphangroupform':
130 redirect(new moodle_url('/group/group.php', array('courseid' => $courseid)));
131 break;
133 case 'showautocreategroupsform':
134 redirect(new moodle_url('/group/autogroup.php', array('courseid' => $courseid)));
135 break;
137 case 'showimportgroups':
138 redirect(new moodle_url('/group/import.php', array('id' => $courseid)));
139 break;
141 case 'showgroupsettingsform':
142 redirect(new moodle_url('/group/group.php', array('courseid' => $courseid, 'id' => $groupids[0])));
143 break;
145 case 'updategroups': // Currently reloading.
146 break;
148 case 'removemembers':
149 break;
151 case 'showaddmembersform':
152 redirect(new moodle_url('/group/members.php', array('group' => $groupids[0])));
153 break;
155 case 'updatemembers': // Currently reloading.
156 break;
158 case 'enablemessaging':
159 set_groups_messaging($groupids, true);
160 redirect($returnurl, get_string('messagingenabled', 'group', count($groupids)), null,
161 \core\output\notification::NOTIFY_SUCCESS);
162 break;
164 case 'disablemessaging':
165 set_groups_messaging($groupids, false);
166 redirect($returnurl, get_string('messagingdisabled', 'group', count($groupids)), null,
167 \core\output\notification::NOTIFY_SUCCESS);
168 break;
170 default: // ERROR.
171 throw new \moodle_exception('unknowaction', '', $returnurl);
172 break;
175 // Print the page and form.
176 $strgroups = get_string('groups');
177 $strparticipants = get_string('participants');
179 // Print header.
180 $PAGE->set_title($strgroups);
181 $PAGE->set_heading($course->fullname);
182 $PAGE->set_pagelayout('standard');
183 echo $OUTPUT->header();
185 echo $OUTPUT->render_participants_tertiary_nav($course);
187 $groups = groups_get_all_groups($courseid);
188 $selectedname = null;
189 $preventgroupremoval = array();
191 // Get list of groups to render.
192 $groupoptions = array();
193 if ($groups) {
194 foreach ($groups as $group) {
195 $selected = false;
196 $usercount = $DB->count_records('groups_members', array('groupid' => $group->id));
197 $groupname = format_string($group->name, true, ['context' => $context, 'escape' => false]) . ' (' . $usercount . ')';
198 if (in_array($group->id, $groupids)) {
199 $selected = true;
200 if ($singlegroup) {
201 // Only keep selected name if there is one group selected.
202 $selectedname = $groupname;
205 if (!empty($group->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
206 $preventgroupremoval[$group->id] = true;
209 $groupoptions[] = (object) [
210 'value' => $group->id,
211 'selected' => $selected,
212 'text' => s($groupname)
217 // Get list of group members to render if there is a single selected group.
218 $members = array();
219 if ($singlegroup) {
220 $userfieldsapi = \core_user\fields::for_identity($context)->with_userpic();
222 'selects' => $userfieldsselects,
223 'joins' => $userfieldsjoin,
224 'params' => $userfieldsparams
225 ] = (array)$userfieldsapi->get_sql('u', true, '', '', false);
226 $extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
227 if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid,
228 'u.id, ' . $userfieldsselects, null, '', $userfieldsparams, $userfieldsjoin)) {
230 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
232 foreach ($groupmemberroles as $roleid => $roledata) {
233 $users = array();
234 foreach ($roledata->users as $member) {
235 $shortmember = new stdClass();
236 $shortmember->value = $member->id;
237 $shortmember->text = fullname($member, $viewfullnames);
238 if ($extrafields) {
239 $extrafieldsdisplay = [];
240 foreach ($extrafields as $field) {
241 $extrafieldsdisplay[] = s($member->{$field});
243 $shortmember->text .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
246 $users[] = $shortmember;
249 $members[] = (object)[
250 'role' => html_entity_decode($roledata->name, ENT_QUOTES, 'UTF-8'),
251 'rolemembers' => $users
257 $disableaddedit = !$singlegroup;
258 $disabledelete = !empty($groupids);
259 $caneditmessaging = \core_message\api::can_create_group_conversation($USER->id, $context);
261 $renderable = new \core_group\output\index_page($courseid, $groupoptions, $selectedname, $members, $disableaddedit, $disabledelete,
262 $preventgroupremoval, $caneditmessaging);
263 $output = $PAGE->get_renderer('core_group');
264 echo $output->render($renderable);
266 echo $OUTPUT->footer();