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