MDL-73507 course: make current activity highlight darker
[moodle.git] / group / overview.php
blob8a99ba3a890ca105df330090bee4ec67ceb0a2a2
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);
37 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
38 $rooturl = $CFG->wwwroot.'/group/overview.php?id='.$courseid;
40 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
41 print_error('invalidcourse');
44 $url = new moodle_url('/group/overview.php', array('id'=>$courseid));
45 if ($groupid !== 0) {
46 $url->param('group', $groupid);
48 if ($groupingid !== 0) {
49 $url->param('grouping', $groupingid);
51 $PAGE->set_url($url);
53 // Make sure that the user has permissions to manage groups.
54 require_login($course);
56 $context = context_course::instance($courseid);
57 require_capability('moodle/course:managegroups', $context);
59 $strgroups = get_string('groups');
60 $strparticipants = get_string('participants');
61 $stroverview = get_string('overview', 'group');
62 $strgrouping = get_string('grouping', 'group');
63 $strgroup = get_string('group', 'group');
64 $strnotingrouping = get_string('notingrouping', 'group');
65 $strfiltergroups = get_string('filtergroups', 'group');
66 $strnogroups = get_string('nogroups', 'group');
67 $strdescription = get_string('description');
68 $strnotingroup = get_string('notingrouplist', 'group');
69 $strnogroup = get_string('nogroup', 'group');
70 $strnogrouping = get_string('nogrouping', 'group');
72 // This can show all users and all groups in a course.
73 // This is lots of data so allow this script more resources.
74 raise_memory_limit(MEMORY_EXTRA);
76 // Get all groupings and sort them by formatted name.
77 $groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name');
78 foreach ($groupings as $gid => $grouping) {
79 $groupings[$gid]->formattedname = format_string($grouping->name, true, array('context' => $context));
81 core_collator::asort_objects_by_property($groupings, 'formattedname');
82 $members = array();
83 foreach ($groupings as $grouping) {
84 $members[$grouping->id] = array();
86 // Groups not in a grouping.
87 $members[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = array();
89 // Get all groups
90 $groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
92 $params = array('courseid'=>$courseid);
93 if ($groupid) {
94 $groupwhere = "AND g.id = :groupid";
95 $params['groupid'] = $groupid;
96 } else {
97 $groupwhere = "";
100 if ($groupingid) {
101 if ($groupingid < 0) { // No grouping filter.
102 $groupingwhere = "AND gg.groupingid IS NULL";
103 } else {
104 $groupingwhere = "AND gg.groupingid = :groupingid";
105 $params['groupingid'] = $groupingid;
107 } else {
108 $groupingwhere = "";
111 list($sort, $sortparams) = users_order_by_sql('u');
113 $userfieldsapi = \core_user\fields::for_identity($context)->with_userpic();
115 'selects' => $userfieldsselects,
116 'joins' => $userfieldsjoin,
117 'params' => $userfieldsparams
118 ] = (array)$userfieldsapi->get_sql('u', true);
119 $extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
120 $allnames = 'u.id ' . $userfieldsselects;
122 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
123 FROM {groups} g
124 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
125 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
126 LEFT JOIN {user} u ON gm.userid = u.id
127 $userfieldsjoin
128 WHERE g.courseid = :courseid $groupwhere $groupingwhere
129 ORDER BY g.name, $sort";
131 $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams, $userfieldsparams));
132 foreach ($rs as $row) {
133 $user = username_load_fields_from_object((object) [], $row, null,
134 array_merge(['id' => 'userid', 'username', 'idnumber'], $extrafields));
136 if (!$row->groupingid) {
137 $row->groupingid = OVERVIEW_GROUPING_GROUP_NO_GROUPING;
139 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
140 $members[$row->groupingid][$row->groupid] = array();
142 if (!empty($user->id)) {
143 $members[$row->groupingid][$row->groupid][] = $user;
146 $rs->close();
148 // Add 'no groupings' / 'no groups' selectors.
149 $groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = (object)array(
150 'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING,
151 'formattedname' => $strnogrouping,
153 $groups[OVERVIEW_NO_GROUP] = (object)array(
154 'id' => OVERVIEW_NO_GROUP,
155 'courseid' => $courseid,
156 'idnumber' => '',
157 'name' => $strnogroup,
158 'description' => '',
159 'descriptionformat' => FORMAT_HTML,
160 'enrolmentkey' => '',
161 'picture' => 0,
162 'timecreated' => 0,
163 'timemodified' => 0,
166 // Add users who are not in a group.
167 if ($groupid <= 0 && $groupingid <= 0) {
168 list($esql, $params) = get_enrolled_sql($context, null, 0, true);
169 $sql = "SELECT u.id, $allnames, u.idnumber, u.username
170 FROM {user} u
171 JOIN ($esql) e ON e.id = u.id
172 LEFT JOIN (
173 SELECT gm.userid
174 FROM {groups_members} gm
175 JOIN {groups} g ON g.id = gm.groupid
176 WHERE g.courseid = :courseid
177 ) grouped ON grouped.userid = u.id
178 $userfieldsjoin
179 WHERE grouped.userid IS NULL";
180 $params['courseid'] = $courseid;
182 $nogroupusers = $DB->get_records_sql($sql, array_merge($params, $userfieldsparams));
184 if ($nogroupusers) {
185 $members[OVERVIEW_GROUPING_NO_GROUP][OVERVIEW_NO_GROUP] = $nogroupusers;
189 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
190 $PAGE->navbar->add(get_string('overview', 'group'));
192 /// Print header
193 $PAGE->set_title($strgroups);
194 $PAGE->set_heading($course->fullname);
195 $PAGE->set_pagelayout('standard');
196 echo $OUTPUT->header();
198 echo $OUTPUT->render_participants_tertiary_nav($course);
199 /// Print overview
200 echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
202 echo $strfiltergroups;
204 $options = array();
205 $options[0] = get_string('all');
206 foreach ($groupings as $grouping) {
207 $options[$grouping->id] = strip_tags($grouping->formattedname);
209 $popupurl = new moodle_url($rooturl.'&group='.$groupid);
210 $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
211 $select->label = $strgrouping;
212 $select->formid = 'selectgrouping';
213 echo $OUTPUT->render($select);
215 $options = array();
216 $options[0] = get_string('all');
217 foreach ($groups as $group) {
218 $options[$group->id] = strip_tags(format_string($group->name));
220 $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
221 $select = new single_select($popupurl, 'group', $options, $groupid, array());
222 $select->label = $strgroup;
223 $select->formid = 'selectgroup';
224 echo $OUTPUT->render($select);
226 /// Print table
227 $printed = false;
228 foreach ($members as $gpgid=>$groupdata) {
229 if ($groupingid and $groupingid != $gpgid) {
230 if ($groupingid > 0 || $gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
231 continue; // Do not show.
234 $table = new html_table();
235 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
236 $table->size = array('20%', '70%', '10%');
237 $table->align = array('left', 'left', 'center');
238 $table->width = '90%';
239 $table->data = array();
240 foreach ($groupdata as $gpid=>$users) {
241 if ($groupid and $groupid != $gpid) {
242 continue;
244 $line = array();
245 $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
246 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
247 $options = new stdClass;
248 $options->noclean = true;
249 $options->overflowdiv = true;
250 $line[] = $name;
251 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
252 $fullnames = array();
253 foreach ($users as $user) {
254 $displayname = fullname($user, $viewfullnames);
255 if ($extrafields) {
256 $extrafieldsdisplay = [];
257 foreach ($extrafields as $field) {
258 $extrafieldsdisplay[] = s($user->{$field});
260 $displayname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
263 $fullnames[] = html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $course->id]),
264 $displayname);
266 $line[] = implode(', ', $fullnames);
267 $line[] = count($users);
268 $table->data[] = $line;
270 if ($groupid and empty($table->data)) {
271 continue;
273 if ($gpgid < 0) {
274 // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
275 if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) {
276 echo $OUTPUT->heading($strnotingroup, 3);
277 } else {
278 echo $OUTPUT->heading($strnotingrouping, 3);
280 } else {
281 echo $OUTPUT->heading($groupings[$gpgid]->formattedname, 3);
282 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
283 $options = new stdClass;
284 $options->overflowdiv = true;
285 echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter');
287 echo html_writer::table($table);
288 $printed = true;
291 echo $OUTPUT->footer();