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
);
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));
46 $url->param('group', $groupid);
48 if ($groupingid !== 0) {
49 $url->param('grouping', $groupingid);
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');
83 foreach ($groupings as $grouping) {
84 $members[$grouping->id
] = array();
86 // Groups not in a grouping.
87 $members[OVERVIEW_GROUPING_GROUP_NO_GROUPING
] = array();
90 $groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
92 $params = array('courseid'=>$courseid);
94 $groupwhere = "AND g.id = :groupid";
95 $params['groupid'] = $groupid;
101 if ($groupingid < 0) { // No grouping filter.
102 $groupingwhere = "AND gg.groupingid IS NULL";
104 $groupingwhere = "AND gg.groupingid = :groupingid";
105 $params['groupingid'] = $groupingid;
111 list($sort, $sortparams) = users_order_by_sql('u');
113 $allnames = get_all_user_name_fields(true, 'u');
114 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
116 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
117 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
118 LEFT JOIN {user} u ON gm.userid = u.id
119 WHERE g.courseid = :courseid $groupwhere $groupingwhere
120 ORDER BY g.name, $sort";
122 $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
123 foreach ($rs as $row) {
124 $user = new stdClass();
125 $user = username_load_fields_from_object($user, $row, null, array('id' => 'userid', 'username', 'idnumber'));
126 if (!$row->groupingid
) {
127 $row->groupingid
= OVERVIEW_GROUPING_GROUP_NO_GROUPING
;
129 if (!array_key_exists($row->groupid
, $members[$row->groupingid
])) {
130 $members[$row->groupingid
][$row->groupid
] = array();
132 if (!empty($user->id
)) {
133 $members[$row->groupingid
][$row->groupid
][] = $user;
138 // Add 'no groupings' / 'no groups' selectors.
139 $groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING
] = (object)array(
140 'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING
,
141 'formattedname' => $strnogrouping,
143 $groups[OVERVIEW_NO_GROUP
] = (object)array(
144 'id' => OVERVIEW_NO_GROUP
,
145 'courseid' => $courseid,
147 'name' => $strnogroup,
149 'descriptionformat' => FORMAT_HTML
,
150 'enrolmentkey' => '',
157 // Add users who are not in a group.
158 if ($groupid <= 0 && $groupingid <= 0) {
159 list($esql, $params) = get_enrolled_sql($context, null, 0, true);
160 $sql = "SELECT u.id, $allnames, u.idnumber, u.username
162 JOIN ($esql) e ON e.id = u.id
165 FROM {groups_members} gm
166 JOIN {groups} g ON g.id = gm.groupid
167 WHERE g.courseid = :courseid
168 ) grouped ON grouped.userid = u.id
169 WHERE grouped.userid IS NULL";
170 $params['courseid'] = $courseid;
172 $nogroupusers = $DB->get_records_sql($sql, $params);
175 $members[OVERVIEW_GROUPING_NO_GROUP
][OVERVIEW_NO_GROUP
] = $nogroupusers;
179 navigation_node
::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
180 $PAGE->navbar
->add(get_string('overview', 'group'));
183 $PAGE->set_title($strgroups);
184 $PAGE->set_heading($course->fullname
);
185 $PAGE->set_pagelayout('standard');
186 echo $OUTPUT->header();
189 $currenttab = 'overview';
193 echo $OUTPUT->heading(format_string($course->shortname
, true, array('context' => $context)) .' '.$stroverview, 3);
195 echo $strfiltergroups;
198 $options[0] = get_string('all');
199 foreach ($groupings as $grouping) {
200 $options[$grouping->id
] = strip_tags($grouping->formattedname
);
202 $popupurl = new moodle_url($rooturl.'&group='.$groupid);
203 $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
204 $select->label
= $strgrouping;
205 $select->formid
= 'selectgrouping';
206 echo $OUTPUT->render($select);
209 $options[0] = get_string('all');
210 foreach ($groups as $group) {
211 $options[$group->id
] = strip_tags(format_string($group->name
));
213 $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
214 $select = new single_select($popupurl, 'group', $options, $groupid, array());
215 $select->label
= $strgroup;
216 $select->formid
= 'selectgroup';
217 echo $OUTPUT->render($select);
221 $hoverevents = array();
222 foreach ($members as $gpgid=>$groupdata) {
223 if ($groupingid and $groupingid != $gpgid) {
224 if ($groupingid > 0 ||
$gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
225 continue; // Do not show.
228 $table = new html_table();
229 $table->head
= array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
230 $table->size
= array('20%', '70%', '10%');
231 $table->align
= array('left', 'left', 'center');
232 $table->width
= '90%';
233 $table->data
= array();
234 foreach ($groupdata as $gpid=>$users) {
235 if ($groupid and $groupid != $gpid) {
239 $name = print_group_picture($groups[$gpid], $course->id
, false, true, false) . format_string($groups[$gpid]->name
);
240 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description
, 'pluginfile.php', $context->id
, 'group', 'description', $gpid);
241 $options = new stdClass
;
242 $options->noclean
= true;
243 $options->overflowdiv
= true;
244 $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat
, $options));
245 if (empty($jsdescription)) {
248 $line[] = html_writer
::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid));
249 $hoverevents[$gpid] = get_string('descriptiona', null, $jsdescription);
251 $fullnames = array();
252 foreach ($users as $user) {
253 $fullnames[] = '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$user->id
.'&course='.$course->id
.'">'.fullname($user, true).'</a>';
255 $line[] = implode(', ', $fullnames);
256 $line[] = count($users);
257 $table->data
[] = $line;
259 if ($groupid and empty($table->data
)) {
263 // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
264 if ($gpgid == OVERVIEW_GROUPING_NO_GROUP
) {
265 echo $OUTPUT->heading($strnotingroup, 3);
267 echo $OUTPUT->heading($strnotingrouping, 3);
270 echo $OUTPUT->heading($groupings[$gpgid]->formattedname
, 3);
271 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description
, 'pluginfile.php', $context->id
, 'grouping', 'description', $gpgid);
272 $options = new stdClass
;
273 $options->overflowdiv
= true;
274 echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat
, $options), 'generalbox boxwidthnarrow boxaligncenter');
276 echo html_writer
::table($table);
280 if (count($hoverevents)>0) {
281 $PAGE->requires
->string_for_js('description', 'moodle');
282 $PAGE->requires
->js_init_call('M.core_group.init_hover_events', array($hoverevents));
285 echo $OUTPUT->footer();