Automatically generated installer lang files
[moodle.git] / group / overview.php
blob785cc6356ff516b38a4402ed12c9d265adf38cb0
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 $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
115 FROM {groups} g
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;
136 $rs->close();
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,
146 'idnumber' => '',
147 'name' => $strnogroup,
148 'description' => '',
149 'descriptionformat' => FORMAT_HTML,
150 'enrolmentkey' => '',
151 'picture' => 0,
152 'hidepicture' => 0,
153 'timecreated' => 0,
154 'timemodified' => 0,
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
161 FROM {user} u
162 JOIN ($esql) e ON e.id = u.id
163 LEFT JOIN (
164 SELECT gm.userid
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);
174 if ($nogroupusers) {
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'));
182 /// Print header
183 $PAGE->set_title($strgroups);
184 $PAGE->set_heading($course->fullname);
185 $PAGE->set_pagelayout('standard');
186 echo $OUTPUT->header();
188 // Add tabs
189 $currenttab = 'overview';
190 require('tabs.php');
192 /// Print overview
193 echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
195 echo $strfiltergroups;
197 $options = array();
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);
208 $options = array();
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);
219 /// Print table
220 $printed = false;
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) {
236 continue;
238 $line = array();
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)) {
246 $line[] = $name;
247 } else {
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.'&amp;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)) {
260 continue;
262 if ($gpgid < 0) {
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);
266 } else {
267 echo $OUTPUT->heading($strnotingrouping, 3);
269 } else {
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);
277 $printed = true;
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();