weekly release 3.10.1+
[moodle.git] / group / overview.php
CommitLineData
4ca6cfbf 1<?php
4d8e2417
AG
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.
13//
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/>.
16
17
acf000b0 18/**
19 * Print an overview of groupings & group membership
20 *
4d8e2417
AG
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
acf000b0 24 */
f16fa0a3 25
acf000b0 26require_once('../config.php');
99d19c13 27require_once($CFG->libdir . '/filelib.php');
acf000b0 28
294c4bb0
DS
29define('OVERVIEW_NO_GROUP', -1); // The fake group for users not in a group.
30define('OVERVIEW_GROUPING_GROUP_NO_GROUPING', -1); // The fake grouping for groups that have no grouping.
31define('OVERVIEW_GROUPING_NO_GROUP', -2); // The fake grouping for users with no group.
32
f16fa0a3 33$courseid = required_param('id', PARAM_INT);
34$groupid = optional_param('group', 0, PARAM_INT);
35$groupingid = optional_param('grouping', 0, PARAM_INT);
acf000b0 36
37$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
f16fa0a3 38$rooturl = $CFG->wwwroot.'/group/overview.php?id='.$courseid;
acf000b0 39
dfdaabd6 40if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
5a2a5331 41 print_error('invalidcourse');
acf000b0 42}
43
0dd819c7 44$url = new moodle_url('/group/overview.php', array('id'=>$courseid));
af333695 45if ($groupid !== 0) {
46 $url->param('group', $groupid);
47}
48if ($groupingid !== 0) {
49 $url->param('grouping', $groupingid);
50}
51$PAGE->set_url($url);
52
acf000b0 53// Make sure that the user has permissions to manage groups.
54require_login($course);
55
bf0f06b1 56$context = context_course::instance($courseid);
acf000b0 57require_capability('moodle/course:managegroups', $context);
58
f16fa0a3 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');
294c4bb0
DS
68$strnotingroup = get_string('notingrouplist', 'group');
69$strnogroup = get_string('nogroup', 'group');
70$strnogrouping = get_string('nogrouping', 'group');
f16fa0a3 71
f1f3101b
TH
72// This can show all users and all groups in a course.
73// This is lots of data so allow this script more resources.
74raise_memory_limit(MEMORY_EXTRA);
75
3ba63534 76// Get all groupings and sort them by formatted name.
98da6021 77$groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name');
3ba63534
MG
78foreach ($groupings as $gid => $grouping) {
79 $groupings[$gid]->formattedname = format_string($grouping->name, true, array('context' => $context));
80}
81core_collator::asort_objects_by_property($groupings, 'formattedname');
98da6021
PS
82$members = array();
83foreach ($groupings as $grouping) {
84 $members[$grouping->id] = array();
f16fa0a3 85}
aba3f4f3
AN
86// Groups not in a grouping.
87$members[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = array();
f16fa0a3 88
89// Get all groups
5e2f308b 90$groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
acf000b0 91
dfdaabd6 92$params = array('courseid'=>$courseid);
f33e1ed4 93if ($groupid) {
dfdaabd6 94 $groupwhere = "AND g.id = :groupid";
95 $params['groupid'] = $groupid;
f33e1ed4 96} else {
97 $groupwhere = "";
98}
99
98da6021 100if ($groupingid) {
294c4bb0
DS
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;
106 }
f16fa0a3 107} else {
98da6021 108 $groupingwhere = "";
f16fa0a3 109}
9695ff81
TH
110
111list($sort, $sortparams) = users_order_by_sql('u');
112
21253ed6
PH
113$extrafields = get_extra_user_fields($context);
114$allnames = 'u.id, ' . user_picture::fields('u', $extrafields);
115
a327f25e 116$sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
98da6021
PS
117 FROM {groups} g
118 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
119 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
120 LEFT JOIN {user} u ON gm.userid = u.id
121 WHERE g.courseid = :courseid $groupwhere $groupingwhere
9695ff81 122 ORDER BY g.name, $sort";
f16fa0a3 123
9695ff81 124$rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
fc5016fc 125foreach ($rs as $row) {
21253ed6
PH
126 $user = username_load_fields_from_object((object) [], $row, null,
127 array_merge(['id' => 'userid', 'username', 'idnumber'], $extrafields));
128
fc5016fc 129 if (!$row->groupingid) {
294c4bb0 130 $row->groupingid = OVERVIEW_GROUPING_GROUP_NO_GROUPING;
fc5016fc
EL
131 }
132 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
133 $members[$row->groupingid][$row->groupid] = array();
134 }
93df8076 135 if (!empty($user->id)) {
fc5016fc 136 $members[$row->groupingid][$row->groupid][] = $user;
f16fa0a3 137 }
f16fa0a3 138}
fc5016fc 139$rs->close();
acf000b0 140
294c4bb0
DS
141// Add 'no groupings' / 'no groups' selectors.
142$groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = (object)array(
143 'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING,
144 'formattedname' => $strnogrouping,
145);
146$groups[OVERVIEW_NO_GROUP] = (object)array(
147 'id' => OVERVIEW_NO_GROUP,
148 'courseid' => $courseid,
149 'idnumber' => '',
150 'name' => $strnogroup,
151 'description' => '',
152 'descriptionformat' => FORMAT_HTML,
153 'enrolmentkey' => '',
154 'picture' => 0,
155 'hidepicture' => 0,
156 'timecreated' => 0,
157 'timemodified' => 0,
158);
159
160// Add users who are not in a group.
161if ($groupid <= 0 && $groupingid <= 0) {
162 list($esql, $params) = get_enrolled_sql($context, null, 0, true);
163 $sql = "SELECT u.id, $allnames, u.idnumber, u.username
164 FROM {user} u
165 JOIN ($esql) e ON e.id = u.id
166 LEFT JOIN (
167 SELECT gm.userid
168 FROM {groups_members} gm
169 JOIN {groups} g ON g.id = gm.groupid
170 WHERE g.courseid = :courseid
171 ) grouped ON grouped.userid = u.id
172 WHERE grouped.userid IS NULL";
173 $params['courseid'] = $courseid;
174
175 $nogroupusers = $DB->get_records_sql($sql, $params);
176
177 if ($nogroupusers) {
178 $members[OVERVIEW_GROUPING_NO_GROUP][OVERVIEW_NO_GROUP] = $nogroupusers;
179 }
180}
181
0dd819c7 182navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
d6d07a68 183$PAGE->navbar->add(get_string('overview', 'group'));
acf000b0 184
185/// Print header
b87573d7 186$PAGE->set_title($strgroups);
d6d07a68
SH
187$PAGE->set_heading($course->fullname);
188$PAGE->set_pagelayout('standard');
b87573d7 189echo $OUTPUT->header();
190
f16fa0a3 191// Add tabs
192$currenttab = 'overview';
193require('tabs.php');
acf000b0 194
f16fa0a3 195/// Print overview
8ebbb06a 196echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
acf000b0 197
f16fa0a3 198echo $strfiltergroups;
acf000b0 199
98da6021
PS
200$options = array();
201$options[0] = get_string('all');
202foreach ($groupings as $grouping) {
3ba63534 203 $options[$grouping->id] = strip_tags($grouping->formattedname);
acf000b0 204}
98da6021
PS
205$popupurl = new moodle_url($rooturl.'&group='.$groupid);
206$select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
207$select->label = $strgrouping;
208$select->formid = 'selectgrouping';
209echo $OUTPUT->render($select);
acf000b0 210
f16fa0a3 211$options = array();
212$options[0] = get_string('all');
213foreach ($groups as $group) {
214 $options[$group->id] = strip_tags(format_string($group->name));
acf000b0 215}
f8dab966 216$popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
070dea52 217$select = new single_select($popupurl, 'group', $options, $groupid, array());
f8dab966
PS
218$select->label = $strgroup;
219$select->formid = 'selectgroup';
220echo $OUTPUT->render($select);
f16fa0a3 221
222/// Print table
223$printed = false;
c2489597 224$hoverevents = array();
f16fa0a3 225foreach ($members as $gpgid=>$groupdata) {
226 if ($groupingid and $groupingid != $gpgid) {
294c4bb0
DS
227 if ($groupingid > 0 || $gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
228 continue; // Do not show.
229 }
acf000b0 230 }
768cefa0 231 $table = new html_table();
f16fa0a3 232 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
233 $table->size = array('20%', '70%', '10%');
234 $table->align = array('left', 'left', 'center');
235 $table->width = '90%';
236 $table->data = array();
237 foreach ($groupdata as $gpid=>$users) {
238 if ($groupid and $groupid != $gpid) {
239 continue;
acf000b0 240 }
f16fa0a3 241 $line = array();
e88dd876 242 $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
64f93798 243 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
8bdc9cac
SH
244 $options = new stdClass;
245 $options->noclean = true;
367a75fa 246 $options->overflowdiv = true;
c2489597 247 $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options));
f16fa0a3 248 if (empty($jsdescription)) {
249 $line[] = $name;
250 } else {
fef51794 251 $line[] = html_writer::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid));
def68a43 252 $hoverevents[$gpid] = get_string('descriptiona', null, $jsdescription);
f16fa0a3 253 }
6e8ed1a8 254 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
f16fa0a3 255 $fullnames = array();
256 foreach ($users as $user) {
6e8ed1a8 257 $displayname = fullname($user, $viewfullnames);
21253ed6
PH
258 if ($extrafields) {
259 $extrafieldsdisplay = [];
260 foreach ($extrafields as $field) {
261 $extrafieldsdisplay[] = s($user->{$field});
262 }
263 $displayname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
264 }
265
266 $fullnames[] = html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $course->id]),
267 $displayname);
f16fa0a3 268 }
269 $line[] = implode(', ', $fullnames);
270 $line[] = count($users);
271 $table->data[] = $line;
acf000b0 272 }
f16fa0a3 273 if ($groupid and empty($table->data)) {
274 continue;
acf000b0 275 }
98da6021 276 if ($gpgid < 0) {
294c4bb0
DS
277 // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
278 if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) {
279 echo $OUTPUT->heading($strnotingroup, 3);
280 } else {
281 echo $OUTPUT->heading($strnotingrouping, 3);
282 }
98da6021 283 } else {
3ba63534 284 echo $OUTPUT->heading($groupings[$gpgid]->formattedname, 3);
64f93798 285 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
98da6021 286 $options = new stdClass;
367a75fa 287 $options->overflowdiv = true;
98da6021 288 echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter');
acf000b0 289 }
16be8974 290 echo html_writer::table($table);
f16fa0a3 291 $printed = true;
acf000b0 292}
293
c2489597
SH
294if (count($hoverevents)>0) {
295 $PAGE->requires->string_for_js('description', 'moodle');
296 $PAGE->requires->js_init_call('M.core_group.init_hover_events', array($hoverevents));
297}
298
653468d4 299echo $OUTPUT->footer();