MDL-40897 avoid extra DB queries in groups_get_activity_groupmode()
[moodle.git] / group / overview.php
blob134261e027f71a64b9d4b473915054271b4969e2
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 $allnames = get_all_user_name_fields(true, 'u');
94 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
95 FROM {groups} g
96 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
97 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
98 LEFT JOIN {user} u ON gm.userid = u.id
99 WHERE g.courseid = :courseid $groupwhere $groupingwhere
100 ORDER BY g.name, $sort";
102 $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
103 foreach ($rs as $row) {
104 $user = new stdClass();
105 $user->id = $row->userid;
106 $user->firstname = $row->firstname;
107 $user->lastname = $row->lastname;
108 $user->username = $row->username;
109 $user->idnumber = $row->idnumber;
110 foreach (get_all_user_name_fields() as $addname) {
111 $user->$addname = $row->$addname;
113 if (!$row->groupingid) {
114 $row->groupingid = -1;
116 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
117 $members[$row->groupingid][$row->groupid] = array();
119 if(isset($user->id)){
120 $members[$row->groupingid][$row->groupid][] = $user;
123 $rs->close();
125 navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
126 $PAGE->navbar->add(get_string('overview', 'group'));
128 /// Print header
129 $PAGE->set_title($strgroups);
130 $PAGE->set_heading($course->fullname);
131 $PAGE->set_pagelayout('standard');
132 echo $OUTPUT->header();
134 // Add tabs
135 $currenttab = 'overview';
136 require('tabs.php');
138 /// Print overview
139 echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
141 echo $strfiltergroups;
143 $options = array();
144 $options[0] = get_string('all');
145 foreach ($groupings as $grouping) {
146 $options[$grouping->id] = strip_tags(format_string($grouping->name));
148 $popupurl = new moodle_url($rooturl.'&group='.$groupid);
149 $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
150 $select->label = $strgrouping;
151 $select->formid = 'selectgrouping';
152 echo $OUTPUT->render($select);
154 $options = array();
155 $options[0] = get_string('all');
156 foreach ($groups as $group) {
157 $options[$group->id] = strip_tags(format_string($group->name));
159 $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
160 $select = new single_select($popupurl, 'group', $options, $groupid, array());
161 $select->label = $strgroup;
162 $select->formid = 'selectgroup';
163 echo $OUTPUT->render($select);
165 /// Print table
166 $printed = false;
167 $hoverevents = array();
168 foreach ($members as $gpgid=>$groupdata) {
169 if ($groupingid and $groupingid != $gpgid) {
170 continue; // do not show
172 $table = new html_table();
173 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
174 $table->size = array('20%', '70%', '10%');
175 $table->align = array('left', 'left', 'center');
176 $table->width = '90%';
177 $table->data = array();
178 foreach ($groupdata as $gpid=>$users) {
179 if ($groupid and $groupid != $gpid) {
180 continue;
182 $line = array();
183 $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
184 $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
185 $options = new stdClass;
186 $options->noclean = true;
187 $options->overflowdiv = true;
188 $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options));
189 if (empty($jsdescription)) {
190 $line[] = $name;
191 } else {
192 $line[] = html_writer::tag('span', $name, array('id'=>'group_'.$gpid));
193 $hoverevents['group_'.$gpid] = $jsdescription;
195 $fullnames = array();
196 foreach ($users as $user) {
197 $fullnames[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user, true).'</a>';
199 $line[] = implode(', ', $fullnames);
200 $line[] = count($users);
201 $table->data[] = $line;
203 if ($groupid and empty($table->data)) {
204 continue;
206 if ($gpgid < 0) {
207 echo $OUTPUT->heading($strnotingrouping, 3);
208 } else {
209 echo $OUTPUT->heading(format_string($groupings[$gpgid]->name), 3);
210 $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
211 $options = new stdClass;
212 $options->noclean = true;
213 $options->overflowdiv = true;
214 echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter');
216 echo html_writer::table($table);
217 $printed = true;
220 if (count($hoverevents)>0) {
221 $PAGE->requires->string_for_js('description', 'moodle');
222 $PAGE->requires->js_init_call('M.core_group.init_hover_events', array($hoverevents));
225 echo $OUTPUT->footer();