MDL-70935 mod_lesson: Custom completion implementation
[moodle.git] / group / overview.php
bloba6e84c0432e23649459a09bcf82ada07b6f2ec83
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 // TODO Does not support custom user profile fields (MDL-70456).
114 $userfieldsapi = \core_user\fields::for_identity($context, false)->with_userpic();
115 $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
116 $extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
117 $allnames = 'u.id, ' . $userfields;
119 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
120 FROM {groups} g
121 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
122 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
123 LEFT JOIN {user} u ON gm.userid = u.id
124 WHERE g.courseid = :courseid $groupwhere $groupingwhere
125 ORDER BY g.name, $sort";
127 $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
128 foreach ($rs as $row) {
129 $user = username_load_fields_from_object((object) [], $row, null,
130 array_merge(['id' => 'userid', 'username', 'idnumber'], $extrafields));
132 if (!$row->groupingid) {
133 $row->groupingid = OVERVIEW_GROUPING_GROUP_NO_GROUPING;
135 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
136 $members[$row->groupingid][$row->groupid] = array();
138 if (!empty($user->id)) {
139 $members[$row->groupingid][$row->groupid][] = $user;
142 $rs->close();
144 // Add 'no groupings' / 'no groups' selectors.
145 $groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = (object)array(
146 'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING,
147 'formattedname' => $strnogrouping,
149 $groups[OVERVIEW_NO_GROUP] = (object)array(
150 'id' => OVERVIEW_NO_GROUP,
151 'courseid' => $courseid,
152 'idnumber' => '',
153 'name' => $strnogroup,
154 'description' => '',
155 'descriptionformat' => FORMAT_HTML,
156 'enrolmentkey' => '',
157 'picture' => 0,
158 'timecreated' => 0,
159 'timemodified' => 0,
162 // Add users who are not in a group.
163 if ($groupid <= 0 && $groupingid <= 0) {
164 list($esql, $params) = get_enrolled_sql($context, null, 0, true);
165 $sql = "SELECT u.id, $allnames, u.idnumber, u.username
166 FROM {user} u
167 JOIN ($esql) e ON e.id = u.id
168 LEFT JOIN (
169 SELECT gm.userid
170 FROM {groups_members} gm
171 JOIN {groups} g ON g.id = gm.groupid
172 WHERE g.courseid = :courseid
173 ) grouped ON grouped.userid = u.id
174 WHERE grouped.userid IS NULL";
175 $params['courseid'] = $courseid;
177 $nogroupusers = $DB->get_records_sql($sql, $params);
179 if ($nogroupusers) {
180 $members[OVERVIEW_GROUPING_NO_GROUP][OVERVIEW_NO_GROUP] = $nogroupusers;
184 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
185 $PAGE->navbar->add(get_string('overview', 'group'));
187 /// Print header
188 $PAGE->set_title($strgroups);
189 $PAGE->set_heading($course->fullname);
190 $PAGE->set_pagelayout('standard');
191 echo $OUTPUT->header();
193 // Add tabs
194 $currenttab = 'overview';
195 require('tabs.php');
197 /// Print overview
198 echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
200 echo $strfiltergroups;
202 $options = array();
203 $options[0] = get_string('all');
204 foreach ($groupings as $grouping) {
205 $options[$grouping->id] = strip_tags($grouping->formattedname);
207 $popupurl = new moodle_url($rooturl.'&group='.$groupid);
208 $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
209 $select->label = $strgrouping;
210 $select->formid = 'selectgrouping';
211 echo $OUTPUT->render($select);
213 $options = array();
214 $options[0] = get_string('all');
215 foreach ($groups as $group) {
216 $options[$group->id] = strip_tags(format_string($group->name));
218 $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
219 $select = new single_select($popupurl, 'group', $options, $groupid, array());
220 $select->label = $strgroup;
221 $select->formid = 'selectgroup';
222 echo $OUTPUT->render($select);
224 /// Print table
225 $printed = false;
226 $hoverevents = array();
227 foreach ($members as $gpgid=>$groupdata) {
228 if ($groupingid and $groupingid != $gpgid) {
229 if ($groupingid > 0 || $gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
230 continue; // Do not show.
233 $table = new html_table();
234 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
235 $table->size = array('20%', '70%', '10%');
236 $table->align = array('left', 'left', 'center');
237 $table->width = '90%';
238 $table->data = array();
239 foreach ($groupdata as $gpid=>$users) {
240 if ($groupid and $groupid != $gpid) {
241 continue;
243 $line = array();
244 $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
245 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
246 $options = new stdClass;
247 $options->noclean = true;
248 $options->overflowdiv = true;
249 $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options));
250 if (empty($jsdescription)) {
251 $line[] = $name;
252 } else {
253 $line[] = html_writer::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid));
254 $hoverevents[$gpid] = get_string('descriptiona', null, $jsdescription);
256 $viewfullnames = has_capability('moodle/site:viewfullnames', $context);
257 $fullnames = array();
258 foreach ($users as $user) {
259 $displayname = fullname($user, $viewfullnames);
260 if ($extrafields) {
261 $extrafieldsdisplay = [];
262 foreach ($extrafields as $field) {
263 $extrafieldsdisplay[] = s($user->{$field});
265 $displayname .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
268 $fullnames[] = html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $course->id]),
269 $displayname);
271 $line[] = implode(', ', $fullnames);
272 $line[] = count($users);
273 $table->data[] = $line;
275 if ($groupid and empty($table->data)) {
276 continue;
278 if ($gpgid < 0) {
279 // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
280 if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) {
281 echo $OUTPUT->heading($strnotingroup, 3);
282 } else {
283 echo $OUTPUT->heading($strnotingrouping, 3);
285 } else {
286 echo $OUTPUT->heading($groupings[$gpgid]->formattedname, 3);
287 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
288 $options = new stdClass;
289 $options->overflowdiv = true;
290 echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter');
292 echo html_writer::table($table);
293 $printed = true;
296 if (count($hoverevents)>0) {
297 $PAGE->requires->string_for_js('description', 'moodle');
298 $PAGE->requires->js_init_call('M.core_group.init_hover_events', array($hoverevents));
301 echo $OUTPUT->footer();