MDL-37033 environment: add 2.5 brach details
[moodle.git] / group / overview.php
blob7e6fa2cede5ec2dde94a1d745fa6b9b9506132d0
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
66 $groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name');
67 $members = array();
68 foreach ($groupings as $grouping) {
69 $members[$grouping->id] = array();
71 $members[-1] = array(); //groups not in a grouping
73 // Get all groups
74 $groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
76 $params = array('courseid'=>$courseid);
77 if ($groupid) {
78 $groupwhere = "AND g.id = :groupid";
79 $params['groupid'] = $groupid;
80 } else {
81 $groupwhere = "";
84 if ($groupingid) {
85 $groupingwhere = "AND gg.groupingid = :groupingid";
86 $params['groupingid'] = $groupingid;
87 } else {
88 $groupingwhere = "";
91 list($sort, $sortparams) = users_order_by_sql('u');
93 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, u.firstname, u.lastname, u.idnumber, u.username
94 FROM {groups} g
95 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
96 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
97 LEFT JOIN {user} u ON gm.userid = u.id
98 WHERE g.courseid = :courseid $groupwhere $groupingwhere
99 ORDER BY g.name, $sort";
101 $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
102 foreach ($rs as $row) {
103 $user = new stdClass();
104 $user->id = $row->userid;
105 $user->firstname = $row->firstname;
106 $user->lastname = $row->lastname;
107 $user->username = $row->username;
108 $user->idnumber = $row->idnumber;
109 if (!$row->groupingid) {
110 $row->groupingid = -1;
112 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
113 $members[$row->groupingid][$row->groupid] = array();
115 if(isset($user->id)){
116 $members[$row->groupingid][$row->groupid][] = $user;
119 $rs->close();
121 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
122 $PAGE->navbar->add(get_string('overview', 'group'));
124 /// Print header
125 $PAGE->set_title($strgroups);
126 $PAGE->set_heading($course->fullname);
127 $PAGE->set_pagelayout('standard');
128 echo $OUTPUT->header();
130 // Add tabs
131 $currenttab = 'overview';
132 require('tabs.php');
134 /// Print overview
135 echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
137 echo $strfiltergroups;
139 $options = array();
140 $options[0] = get_string('all');
141 foreach ($groupings as $grouping) {
142 $options[$grouping->id] = strip_tags(format_string($grouping->name));
144 $popupurl = new moodle_url($rooturl.'&group='.$groupid);
145 $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
146 $select->label = $strgrouping;
147 $select->formid = 'selectgrouping';
148 echo $OUTPUT->render($select);
150 $options = array();
151 $options[0] = get_string('all');
152 foreach ($groups as $group) {
153 $options[$group->id] = strip_tags(format_string($group->name));
155 $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
156 $select = new single_select($popupurl, 'group', $options, $groupid, array());
157 $select->label = $strgroup;
158 $select->formid = 'selectgroup';
159 echo $OUTPUT->render($select);
161 /// Print table
162 $printed = false;
163 $hoverevents = array();
164 foreach ($members as $gpgid=>$groupdata) {
165 if ($groupingid and $groupingid != $gpgid) {
166 continue; // do not show
168 $table = new html_table();
169 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
170 $table->size = array('20%', '70%', '10%');
171 $table->align = array('left', 'left', 'center');
172 $table->width = '90%';
173 $table->data = array();
174 foreach ($groupdata as $gpid=>$users) {
175 if ($groupid and $groupid != $gpid) {
176 continue;
178 $line = array();
179 $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
180 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
181 $options = new stdClass;
182 $options->noclean = true;
183 $options->overflowdiv = true;
184 $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options));
185 if (empty($jsdescription)) {
186 $line[] = $name;
187 } else {
188 $line[] = html_writer::tag('span', $name, array('id'=>'group_'.$gpid));
189 $hoverevents['group_'.$gpid] = $jsdescription;
191 $fullnames = array();
192 foreach ($users as $user) {
193 $fullnames[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user, true).'</a>';
195 $line[] = implode(', ', $fullnames);
196 $line[] = count($users);
197 $table->data[] = $line;
199 if ($groupid and empty($table->data)) {
200 continue;
202 if ($gpgid < 0) {
203 echo $OUTPUT->heading($strnotingrouping, 3);
204 } else {
205 echo $OUTPUT->heading(format_string($groupings[$gpgid]->name), 3);
206 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
207 $options = new stdClass;
208 $options->noclean = true;
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();