MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / group / overview.php
blob006c3772df69f69185af645e10b0e1fd6402e906
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 * Print an overview of groupings & group membership
21 * @copyright Matt Clarkson mattc@catalyst.net.nz
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @package core_group
26 require_once('../config.php');
27 require_once($CFG->libdir . '/filelib.php');
29 define('OVERVIEW_NO_GROUP', -1); // The fake group for users not in a group.
30 define('OVERVIEW_GROUPING_GROUP_NO_GROUPING', -1); // The fake grouping for groups that have no grouping.
31 define('OVERVIEW_GROUPING_NO_GROUP', -2); // The fake grouping for users with no group.
33 $courseid = required_param('id', PARAM_INT);
34 $groupid = optional_param('group', 0, PARAM_INT);
35 $groupingid = optional_param('grouping', 0, PARAM_INT);
36 $dataformat = optional_param('dataformat', '', PARAM_ALPHA);
38 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
39 $rooturl = $CFG->wwwroot.'/group/overview.php?id='.$courseid;
41 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
42 throw new \moodle_exception('invalidcourse');
45 $url = new moodle_url('/group/overview.php', array('id'=>$courseid));
46 if ($groupid !== 0) {
47 $url->param('group', $groupid);
49 if ($groupingid !== 0) {
50 $url->param('grouping', $groupingid);
52 $PAGE->set_url($url);
54 // Make sure that the user has permissions to manage groups.
55 require_login($course);
57 $context = context_course::instance($courseid);
58 require_capability('moodle/course:managegroups', $context);
60 $strgroups = get_string('groups');
61 $strparticipants = get_string('participants');
62 $stroverview = get_string('overview', 'group');
63 $strgrouping = get_string('grouping', 'group');
64 $strgroup = get_string('group', 'group');
65 $strnotingrouping = get_string('notingrouping', 'group');
66 $strfiltergroups = get_string('filtergroups', 'group');
67 $strnogroups = get_string('nogroups', 'group');
68 $strdescription = get_string('description');
69 $strnotingroup = get_string('notingrouplist', 'group');
70 $strnogroup = get_string('nogroup', 'group');
71 $strnogrouping = get_string('nogrouping', 'group');
73 // This can show all users and all groups in a course.
74 // This is lots of data so allow this script more resources.
75 raise_memory_limit(MEMORY_EXTRA);
77 // Get all groupings and sort them by formatted name.
78 $groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name');
79 foreach ($groupings as $gid => $grouping) {
80 $groupings[$gid]->formattedname = format_string($grouping->name, true, array('context' => $context));
82 core_collator::asort_objects_by_property($groupings, 'formattedname');
83 $members = array();
84 foreach ($groupings as $grouping) {
85 $members[$grouping->id] = array();
87 // Groups not in a grouping.
88 $members[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = array();
90 // Get all groups
91 $groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
93 $params = array('courseid'=>$courseid);
94 if ($groupid) {
95 $groupwhere = "AND g.id = :groupid";
96 $params['groupid'] = $groupid;
97 } else {
98 $groupwhere = "";
101 if ($groupingid) {
102 if ($groupingid < 0) { // No grouping filter.
103 $groupingwhere = "AND gg.groupingid IS NULL";
104 } else {
105 $groupingwhere = "AND gg.groupingid = :groupingid";
106 $params['groupingid'] = $groupingid;
108 } else {
109 $groupingwhere = "";
112 list($sort, $sortparams) = users_order_by_sql('u');
114 $userfieldsapi = \core_user\fields::for_identity($context)->with_userpic();
116 'selects' => $userfieldsselects,
117 'joins' => $userfieldsjoin,
118 'params' => $userfieldsparams
119 ] = (array)$userfieldsapi->get_sql('u', true);
120 $extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
121 $allnames = 'u.id ' . $userfieldsselects;
123 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
124 FROM {groups} g
125 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
126 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
127 LEFT JOIN {user} u ON gm.userid = u.id
128 $userfieldsjoin
129 WHERE g.courseid = :courseid $groupwhere $groupingwhere
130 ORDER BY g.name, $sort";
132 $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams, $userfieldsparams));
133 foreach ($rs as $row) {
134 $user = username_load_fields_from_object((object) [], $row, null,
135 array_merge(['id' => 'userid', 'username', 'idnumber'], $extrafields));
137 if (!$row->groupingid) {
138 $row->groupingid = OVERVIEW_GROUPING_GROUP_NO_GROUPING;
140 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
141 $members[$row->groupingid][$row->groupid] = array();
143 if (!empty($user->id)) {
144 $members[$row->groupingid][$row->groupid][] = $user;
147 $rs->close();
149 // Add 'no groupings' / 'no groups' selectors.
150 $groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = (object)array(
151 'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING,
152 'formattedname' => $strnogrouping,
154 $groups[OVERVIEW_NO_GROUP] = (object)array(
155 'id' => OVERVIEW_NO_GROUP,
156 'courseid' => $courseid,
157 'idnumber' => '',
158 'name' => $strnogroup,
159 'description' => '',
160 'descriptionformat' => FORMAT_HTML,
161 'enrolmentkey' => '',
162 'picture' => 0,
163 'timecreated' => 0,
164 'timemodified' => 0,
167 // Add users who are not in a group.
168 if ($groupid <= 0 && $groupingid <= 0) {
169 list($esql, $params) = get_enrolled_sql($context, null, 0, true);
170 $sql = "SELECT u.id, $allnames, u.idnumber, u.username
171 FROM {user} u
172 JOIN ($esql) e ON e.id = u.id
173 LEFT JOIN (
174 SELECT gm.userid
175 FROM {groups_members} gm
176 JOIN {groups} g ON g.id = gm.groupid
177 WHERE g.courseid = :courseid
178 ) grouped ON grouped.userid = u.id
179 $userfieldsjoin
180 WHERE grouped.userid IS NULL
181 ORDER BY $sort";
182 $params['courseid'] = $courseid;
184 $nogroupusers = $DB->get_records_sql($sql, array_merge($params, $userfieldsparams));
186 if ($nogroupusers) {
187 $members[OVERVIEW_GROUPING_NO_GROUP][OVERVIEW_NO_GROUP] = $nogroupusers;
191 // Export groups if requested.
192 if ($dataformat !== '') {
193 $columnnames = array(
194 'grouping' => $strgrouping,
195 'group' => $strgroup,
196 'firstname' => get_string('firstname'),
197 'lastname' => get_string('lastname'),
199 $extrafields = \core_user\fields::get_identity_fields($context, false);
200 foreach ($extrafields as $field) {
201 $columnnames[$field] = \core_user\fields::get_display_name($field);
203 $alldata = array();
204 // Generate file name.
205 $shortname = format_string($course->shortname, true, array('context' => $context))."_groups";
206 $i = 0;
207 foreach ($members as $gpgid => $groupdata) {
208 if ($groupingid and $groupingid != $gpgid) {
209 if ($groupingid > 0 || $gpgid > 0) {
210 // Still show 'not in group' when 'no grouping' selected.
211 continue; // Do not export.
214 if ($gpgid < 0) {
215 // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
216 if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) {
217 $groupingname = $strnotingroup;
218 } else {
219 $groupingname = $strnotingrouping;
221 } else {
222 $groupingname = $groupings[$gpgid]->formattedname;
224 if (empty($groupdata)) {
225 $alldata[$i] = array_fill_keys(array_keys($columnnames), '');
226 $alldata[$i]['grouping'] = $groupingname;
227 $i++;
229 foreach ($groupdata as $gpid => $users) {
230 if ($groupid and $groupid != $gpid) {
231 continue;
233 if (empty($users)) {
234 $alldata[$i] = array_fill_keys(array_keys($columnnames), '');
235 $alldata[$i]['grouping'] = $groupingname;
236 $alldata[$i]['group'] = $groups[$gpid]->name;
237 $i++;
239 foreach ($users as $option => $user) {
240 $alldata[$i]['grouping'] = $groupingname;
241 $alldata[$i]['group'] = $groups[$gpid]->name;
242 $alldata[$i]['firstname'] = $user->firstname;
243 $alldata[$i]['lastname'] = $user->lastname;
244 foreach ($extrafields as $field) {
245 $alldata[$i][$field] = $user->$field;
247 $i++;
252 \core\dataformat::download_data(
253 $shortname,
254 $dataformat,
255 $columnnames,
256 $alldata,
257 function($record, $supportshtml) use ($extrafields) {
258 if ($supportshtml) {
259 foreach ($extrafields as $extrafield) {
260 $record[$extrafield] = s($record[$extrafield]);
263 return $record;
265 die;
268 // Main page content.
269 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
270 $PAGE->navbar->add(get_string('overview', 'group'));
272 /// Print header
273 $PAGE->set_title($strgroups);
274 $PAGE->set_heading($course->fullname);
275 $PAGE->set_pagelayout('standard');
277 echo $OUTPUT->header();
278 echo $OUTPUT->render_participants_tertiary_nav($course);
280 echo $strfiltergroups;
282 $options = array();
283 $options[0] = get_string('all');
284 foreach ($groupings as $grouping) {
285 $options[$grouping->id] = strip_tags($grouping->formattedname);
287 $popupurl = new moodle_url($rooturl.'&group='.$groupid);
288 $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
289 $select->label = $strgrouping;
290 $select->formid = 'selectgrouping';
291 echo $OUTPUT->render($select);
293 $options = array();
294 $options[0] = get_string('all');
295 foreach ($groups as $group) {
296 $options[$group->id] = strip_tags(format_string($group->name));
298 $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
299 $select = new single_select($popupurl, 'group', $options, $groupid, array());
300 $select->label = $strgroup;
301 $select->formid = 'selectgroup';
302 echo $OUTPUT->render($select);
304 /// Print table
305 $printed = false;
306 foreach ($members as $gpgid=>$groupdata) {
307 if ($groupingid and $groupingid != $gpgid) {
308 if ($groupingid > 0 || $gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
309 continue; // Do not show.
312 $table = new html_table();
313 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
314 $table->size = array('20%', '70%', '10%');
315 $table->align = array('left', 'left', 'center');
316 $table->width = '90%';
317 $table->data = array();
318 foreach ($groupdata as $gpid=>$users) {
319 if ($groupid and $groupid != $gpid) {
320 continue;
322 $line = array();
323 $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
324 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
325 $options = new stdClass;
326 $options->noclean = true;
327 $options->overflowdiv = true;
328 $line[] = $name;
329 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
330 $fullnames = array();
331 foreach ($users as $user) {
332 $displayname = fullname($user, $viewfullnames);
333 if ($extrafields) {
334 $extrafieldsdisplay = [];
335 foreach ($extrafields as $field) {
336 $extrafieldsdisplay[] = s($user->{$field});
338 $displayname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
341 $fullnames[] = html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $course->id]),
342 $displayname);
344 $line[] = implode(', ', $fullnames);
345 $line[] = count($users);
346 $table->data[] = $line;
348 if ($groupid and empty($table->data)) {
349 continue;
351 if ($gpgid < 0) {
352 // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
353 if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) {
354 echo $OUTPUT->heading($strnotingroup, 3);
355 } else {
356 echo $OUTPUT->heading($strnotingrouping, 3);
358 } else {
359 echo $OUTPUT->heading($groupings[$gpgid]->formattedname, 3);
360 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
361 $options = new stdClass;
362 $options->overflowdiv = true;
363 echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter');
365 echo html_writer::table($table);
366 $printed = true;
369 // Add buttons for exporting groups/groupings.
370 echo $OUTPUT->download_dataformat_selector(get_string('exportgroupsgroupings', 'group'), 'overview.php', 'dataformat', [
371 'id' => $courseid,
372 'group' => $groupid,
373 'grouping' => $groupingid,
376 echo $OUTPUT->footer();