Merge branch 'MDL-51434_master-fix-unit-test' of git://github.com/dmonllao/moodle
[moodle.git] / group / overview.php
blob173ab922f6d1cd436148a3ebea2133c333866965
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 $courseid = required_param('id', PARAM_INT);
30 $groupid = optional_param('group', 0, PARAM_INT);
31 $groupingid = optional_param('grouping', 0, PARAM_INT);
33 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
34 $rooturl = $CFG->wwwroot.'/group/overview.php?id='.$courseid;
36 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
37 print_error('invalidcourse');
40 $url = new moodle_url('/group/overview.php', array('id'=>$courseid));
41 if ($groupid !== 0) {
42 $url->param('group', $groupid);
44 if ($groupingid !== 0) {
45 $url->param('grouping', $groupingid);
47 $PAGE->set_url($url);
49 // Make sure that the user has permissions to manage groups.
50 require_login($course);
52 $context = context_course::instance($courseid);
53 require_capability('moodle/course:managegroups', $context);
55 $strgroups = get_string('groups');
56 $strparticipants = get_string('participants');
57 $stroverview = get_string('overview', 'group');
58 $strgrouping = get_string('grouping', 'group');
59 $strgroup = get_string('group', 'group');
60 $strnotingrouping = get_string('notingrouping', 'group');
61 $strfiltergroups = get_string('filtergroups', 'group');
62 $strnogroups = get_string('nogroups', 'group');
63 $strdescription = get_string('description');
65 // Get all groupings and sort them by formatted name.
66 $groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name');
67 foreach ($groupings as $gid => $grouping) {
68 $groupings[$gid]->formattedname = format_string($grouping->name, true, array('context' => $context));
70 core_collator::asort_objects_by_property($groupings, 'formattedname');
71 $members = array();
72 foreach ($groupings as $grouping) {
73 $members[$grouping->id] = array();
75 $members[-1] = array(); //groups not in a grouping
77 // Get all groups
78 $groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
80 $params = array('courseid'=>$courseid);
81 if ($groupid) {
82 $groupwhere = "AND g.id = :groupid";
83 $params['groupid'] = $groupid;
84 } else {
85 $groupwhere = "";
88 if ($groupingid) {
89 $groupingwhere = "AND gg.groupingid = :groupingid";
90 $params['groupingid'] = $groupingid;
91 } else {
92 $groupingwhere = "";
95 list($sort, $sortparams) = users_order_by_sql('u');
97 $allnames = get_all_user_name_fields(true, 'u');
98 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
99 FROM {groups} g
100 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
101 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
102 LEFT JOIN {user} u ON gm.userid = u.id
103 WHERE g.courseid = :courseid $groupwhere $groupingwhere
104 ORDER BY g.name, $sort";
106 $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
107 foreach ($rs as $row) {
108 $user = new stdClass();
109 $user = username_load_fields_from_object($user, $row, null, array('id' => 'userid', 'username', 'idnumber'));
110 if (!$row->groupingid) {
111 $row->groupingid = -1;
113 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
114 $members[$row->groupingid][$row->groupid] = array();
116 if (!empty($user->id)) {
117 $members[$row->groupingid][$row->groupid][] = $user;
120 $rs->close();
122 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
123 $PAGE->navbar->add(get_string('overview', 'group'));
125 /// Print header
126 $PAGE->set_title($strgroups);
127 $PAGE->set_heading($course->fullname);
128 $PAGE->set_pagelayout('standard');
129 echo $OUTPUT->header();
131 // Add tabs
132 $currenttab = 'overview';
133 require('tabs.php');
135 /// Print overview
136 echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
138 echo $strfiltergroups;
140 $options = array();
141 $options[0] = get_string('all');
142 foreach ($groupings as $grouping) {
143 $options[$grouping->id] = strip_tags($grouping->formattedname);
145 $popupurl = new moodle_url($rooturl.'&group='.$groupid);
146 $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
147 $select->label = $strgrouping;
148 $select->formid = 'selectgrouping';
149 echo $OUTPUT->render($select);
151 $options = array();
152 $options[0] = get_string('all');
153 foreach ($groups as $group) {
154 $options[$group->id] = strip_tags(format_string($group->name));
156 $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
157 $select = new single_select($popupurl, 'group', $options, $groupid, array());
158 $select->label = $strgroup;
159 $select->formid = 'selectgroup';
160 echo $OUTPUT->render($select);
162 /// Print table
163 $printed = false;
164 $hoverevents = array();
165 foreach ($members as $gpgid=>$groupdata) {
166 if ($groupingid and $groupingid != $gpgid) {
167 continue; // do not show
169 $table = new html_table();
170 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
171 $table->size = array('20%', '70%', '10%');
172 $table->align = array('left', 'left', 'center');
173 $table->width = '90%';
174 $table->data = array();
175 foreach ($groupdata as $gpid=>$users) {
176 if ($groupid and $groupid != $gpid) {
177 continue;
179 $line = array();
180 $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
181 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
182 $options = new stdClass;
183 $options->noclean = true;
184 $options->overflowdiv = true;
185 $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options));
186 if (empty($jsdescription)) {
187 $line[] = $name;
188 } else {
189 $line[] = html_writer::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid));
190 $hoverevents[$gpid] = $jsdescription;
192 $fullnames = array();
193 foreach ($users as $user) {
194 $fullnames[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user, true).'</a>';
196 $line[] = implode(', ', $fullnames);
197 $line[] = count($users);
198 $table->data[] = $line;
200 if ($groupid and empty($table->data)) {
201 continue;
203 if ($gpgid < 0) {
204 echo $OUTPUT->heading($strnotingrouping, 3);
205 } else {
206 echo $OUTPUT->heading($groupings[$gpgid]->formattedname, 3);
207 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
208 $options = new stdClass;
209 $options->overflowdiv = true;
210 echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter');
212 echo html_writer::table($table);
213 $printed = true;
216 if (count($hoverevents)>0) {
217 $PAGE->requires->string_for_js('description', 'moodle');
218 $PAGE->requires->js_init_call('M.core_group.init_hover_events', array($hoverevents));
221 echo $OUTPUT->footer();