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 * 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
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));
47 $url->param('group', $groupid);
49 if ($groupingid !== 0) {
50 $url->param('grouping', $groupingid);
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');
84 foreach ($groupings as $grouping) {
85 $members[$grouping->id
] = array();
87 // Groups not in a grouping.
88 $members[OVERVIEW_GROUPING_GROUP_NO_GROUPING
] = array();
91 $groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
93 $params = array('courseid'=>$courseid);
95 $groupwhere = "AND g.id = :groupid";
96 $params['groupid'] = $groupid;
102 if ($groupingid < 0) { // No grouping filter.
103 $groupingwhere = "AND gg.groupingid IS NULL";
105 $groupingwhere = "AND gg.groupingid = :groupingid";
106 $params['groupingid'] = $groupingid;
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
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
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;
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,
158 'name' => $strnogroup,
160 'descriptionformat' => FORMAT_HTML
,
161 'enrolmentkey' => '',
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
172 JOIN ($esql) e ON e.id = u.id
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
180 WHERE grouped.userid IS NULL
182 $params['courseid'] = $courseid;
184 $nogroupusers = $DB->get_records_sql($sql, array_merge($params, $userfieldsparams));
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);
204 // Generate file name.
205 $shortname = format_string($course->shortname
, true, array('context' => $context))."_groups";
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.
215 // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
216 if ($gpgid == OVERVIEW_GROUPING_NO_GROUP
) {
217 $groupingname = $strnotingroup;
219 $groupingname = $strnotingrouping;
222 $groupingname = $groupings[$gpgid]->formattedname
;
224 if (empty($groupdata)) {
225 $alldata[$i] = array_fill_keys(array_keys($columnnames), '');
226 $alldata[$i]['grouping'] = $groupingname;
229 foreach ($groupdata as $gpid => $users) {
230 if ($groupid and $groupid != $gpid) {
234 $alldata[$i] = array_fill_keys(array_keys($columnnames), '');
235 $alldata[$i]['grouping'] = $groupingname;
236 $alldata[$i]['group'] = $groups[$gpid]->name
;
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;
252 \core\dataformat
::download_data(
257 function($record, $supportshtml) use ($extrafields) {
259 foreach ($extrafields as $extrafield) {
260 $record[$extrafield] = s($record[$extrafield]);
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'));
273 $PAGE->set_title($strgroups);
274 $PAGE->set_heading($course->fullname
);
275 $PAGE->set_pagelayout('standard');
276 echo $OUTPUT->header();
278 echo $OUTPUT->render_participants_tertiary_nav($course);
280 echo $OUTPUT->heading(format_string($course->shortname
, true, array('context' => $context)) .' '.$stroverview, 3);
282 echo $strfiltergroups;
285 $options[0] = get_string('all');
286 foreach ($groupings as $grouping) {
287 $options[$grouping->id
] = strip_tags($grouping->formattedname
);
289 $popupurl = new moodle_url($rooturl.'&group='.$groupid);
290 $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
291 $select->label
= $strgrouping;
292 $select->formid
= 'selectgrouping';
293 echo $OUTPUT->render($select);
296 $options[0] = get_string('all');
297 foreach ($groups as $group) {
298 $options[$group->id
] = strip_tags(format_string($group->name
));
300 $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
301 $select = new single_select($popupurl, 'group', $options, $groupid, array());
302 $select->label
= $strgroup;
303 $select->formid
= 'selectgroup';
304 echo $OUTPUT->render($select);
308 foreach ($members as $gpgid=>$groupdata) {
309 if ($groupingid and $groupingid != $gpgid) {
310 if ($groupingid > 0 ||
$gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
311 continue; // Do not show.
314 $table = new html_table();
315 $table->head
= array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
316 $table->size
= array('20%', '70%', '10%');
317 $table->align
= array('left', 'left', 'center');
318 $table->width
= '90%';
319 $table->data
= array();
320 foreach ($groupdata as $gpid=>$users) {
321 if ($groupid and $groupid != $gpid) {
325 $name = print_group_picture($groups[$gpid], $course->id
, false, true, false) . format_string($groups[$gpid]->name
);
326 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description
, 'pluginfile.php', $context->id
, 'group', 'description', $gpid);
327 $options = new stdClass
;
328 $options->noclean
= true;
329 $options->overflowdiv
= true;
331 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
332 $fullnames = array();
333 foreach ($users as $user) {
334 $displayname = fullname($user, $viewfullnames);
336 $extrafieldsdisplay = [];
337 foreach ($extrafields as $field) {
338 $extrafieldsdisplay[] = s($user->{$field});
340 $displayname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
343 $fullnames[] = html_writer
::link(new moodle_url('/user/view.php', ['id' => $user->id
, 'course' => $course->id
]),
346 $line[] = implode(', ', $fullnames);
347 $line[] = count($users);
348 $table->data
[] = $line;
350 if ($groupid and empty($table->data
)) {
354 // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
355 if ($gpgid == OVERVIEW_GROUPING_NO_GROUP
) {
356 echo $OUTPUT->heading($strnotingroup, 3);
358 echo $OUTPUT->heading($strnotingrouping, 3);
361 echo $OUTPUT->heading($groupings[$gpgid]->formattedname
, 3);
362 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description
, 'pluginfile.php', $context->id
, 'grouping', 'description', $gpgid);
363 $options = new stdClass
;
364 $options->overflowdiv
= true;
365 echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat
, $options), 'generalbox boxwidthnarrow boxaligncenter');
367 echo html_writer
::table($table);
371 // Add buttons for exporting groups/groupings.
372 echo $OUTPUT->download_dataformat_selector(get_string('exportgroupsgroupings', 'group'), 'overview.php', 'dataformat', [
375 'grouping' => $groupingid,
378 echo $OUTPUT->footer();