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 // Get all groupings and sort them by formatted name.
73 $groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name');
74 foreach ($groupings as $gid => $grouping) {
75 $groupings[$gid]->formattedname
= format_string($grouping->name
, true, array('context' => $context));
77 core_collator
::asort_objects_by_property($groupings, 'formattedname');
79 foreach ($groupings as $grouping) {
80 $members[$grouping->id
] = array();
82 // Groups not in a grouping.
83 $members[OVERVIEW_GROUPING_GROUP_NO_GROUPING
] = array();
86 $groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
88 $params = array('courseid'=>$courseid);
90 $groupwhere = "AND g.id = :groupid";
91 $params['groupid'] = $groupid;
97 if ($groupingid < 0) { // No grouping filter.
98 $groupingwhere = "AND gg.groupingid IS NULL";
100 $groupingwhere = "AND gg.groupingid = :groupingid";
101 $params['groupingid'] = $groupingid;
107 list($sort, $sortparams) = users_order_by_sql('u');
109 $allnames = get_all_user_name_fields(true, 'u');
110 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
112 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
113 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
114 LEFT JOIN {user} u ON gm.userid = u.id
115 WHERE g.courseid = :courseid $groupwhere $groupingwhere
116 ORDER BY g.name, $sort";
118 $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
119 foreach ($rs as $row) {
120 $user = new stdClass();
121 $user = username_load_fields_from_object($user, $row, null, array('id' => 'userid', 'username', 'idnumber'));
122 if (!$row->groupingid
) {
123 $row->groupingid
= OVERVIEW_GROUPING_GROUP_NO_GROUPING
;
125 if (!array_key_exists($row->groupid
, $members[$row->groupingid
])) {
126 $members[$row->groupingid
][$row->groupid
] = array();
128 if (!empty($user->id
)) {
129 $members[$row->groupingid
][$row->groupid
][] = $user;
134 // Add 'no groupings' / 'no groups' selectors.
135 $groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING
] = (object)array(
136 'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING
,
137 'formattedname' => $strnogrouping,
139 $groups[OVERVIEW_NO_GROUP
] = (object)array(
140 'id' => OVERVIEW_NO_GROUP
,
141 'courseid' => $courseid,
143 'name' => $strnogroup,
145 'descriptionformat' => FORMAT_HTML
,
146 'enrolmentkey' => '',
153 // Add users who are not in a group.
154 if ($groupid <= 0 && $groupingid <= 0) {
155 list($esql, $params) = get_enrolled_sql($context, null, 0, true);
156 $sql = "SELECT u.id, $allnames, u.idnumber, u.username
158 JOIN ($esql) e ON e.id = u.id
161 FROM {groups_members} gm
162 JOIN {groups} g ON g.id = gm.groupid
163 WHERE g.courseid = :courseid
164 ) grouped ON grouped.userid = u.id
165 WHERE grouped.userid IS NULL";
166 $params['courseid'] = $courseid;
168 $nogroupusers = $DB->get_records_sql($sql, $params);
171 $members[OVERVIEW_GROUPING_NO_GROUP
][OVERVIEW_NO_GROUP
] = $nogroupusers;
175 navigation_node
::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
176 $PAGE->navbar
->add(get_string('overview', 'group'));
179 $PAGE->set_title($strgroups);
180 $PAGE->set_heading($course->fullname
);
181 $PAGE->set_pagelayout('standard');
182 echo $OUTPUT->header();
185 $currenttab = 'overview';
189 echo $OUTPUT->heading(format_string($course->shortname
, true, array('context' => $context)) .' '.$stroverview, 3);
191 echo $strfiltergroups;
194 $options[0] = get_string('all');
195 foreach ($groupings as $grouping) {
196 $options[$grouping->id
] = strip_tags($grouping->formattedname
);
198 $popupurl = new moodle_url($rooturl.'&group='.$groupid);
199 $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
200 $select->label
= $strgrouping;
201 $select->formid
= 'selectgrouping';
202 echo $OUTPUT->render($select);
205 $options[0] = get_string('all');
206 foreach ($groups as $group) {
207 $options[$group->id
] = strip_tags(format_string($group->name
));
209 $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
210 $select = new single_select($popupurl, 'group', $options, $groupid, array());
211 $select->label
= $strgroup;
212 $select->formid
= 'selectgroup';
213 echo $OUTPUT->render($select);
217 $hoverevents = array();
218 foreach ($members as $gpgid=>$groupdata) {
219 if ($groupingid and $groupingid != $gpgid) {
220 if ($groupingid > 0 ||
$gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
221 continue; // Do not show.
224 $table = new html_table();
225 $table->head
= array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
226 $table->size
= array('20%', '70%', '10%');
227 $table->align
= array('left', 'left', 'center');
228 $table->width
= '90%';
229 $table->data
= array();
230 foreach ($groupdata as $gpid=>$users) {
231 if ($groupid and $groupid != $gpid) {
235 $name = print_group_picture($groups[$gpid], $course->id
, false, true, false) . format_string($groups[$gpid]->name
);
236 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description
, 'pluginfile.php', $context->id
, 'group', 'description', $gpid);
237 $options = new stdClass
;
238 $options->noclean
= true;
239 $options->overflowdiv
= true;
240 $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat
, $options));
241 if (empty($jsdescription)) {
244 $line[] = html_writer
::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid));
245 $hoverevents[$gpid] = get_string('descriptiona', null, $jsdescription);
247 $fullnames = array();
248 foreach ($users as $user) {
249 $fullnames[] = '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$user->id
.'&course='.$course->id
.'">'.fullname($user, true).'</a>';
251 $line[] = implode(', ', $fullnames);
252 $line[] = count($users);
253 $table->data
[] = $line;
255 if ($groupid and empty($table->data
)) {
259 // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
260 if ($gpgid == OVERVIEW_GROUPING_NO_GROUP
) {
261 echo $OUTPUT->heading($strnotingroup, 3);
263 echo $OUTPUT->heading($strnotingrouping, 3);
266 echo $OUTPUT->heading($groupings[$gpgid]->formattedname
, 3);
267 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description
, 'pluginfile.php', $context->id
, 'grouping', 'description', $gpgid);
268 $options = new stdClass
;
269 $options->overflowdiv
= true;
270 echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat
, $options), 'generalbox boxwidthnarrow boxaligncenter');
272 echo html_writer
::table($table);
276 if (count($hoverevents)>0) {
277 $PAGE->requires
->string_for_js('description', 'moodle');
278 $PAGE->requires
->js_init_call('M.core_group.init_hover_events', array($hoverevents));
281 echo $OUTPUT->footer();