lesson MDL-19812 Updated print_header and build_navigation to OUTPUT and PAGE equivalents
[moodle.git] / group / overview.php
CommitLineData
acf000b0 1<?php // $Id$
2/**
3 * Print an overview of groupings & group membership
4 *
5 * @author Matt Clarkson mattc@catalyst.net.nz
6 * @version 0.0.1
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package groups
9 */
f16fa0a3 10
acf000b0 11require_once('../config.php');
12
f16fa0a3 13$courseid = required_param('id', PARAM_INT);
14$groupid = optional_param('group', 0, PARAM_INT);
15$groupingid = optional_param('grouping', 0, PARAM_INT);
acf000b0 16
17$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
f16fa0a3 18$rooturl = $CFG->wwwroot.'/group/overview.php?id='.$courseid;
acf000b0 19
dfdaabd6 20if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
5a2a5331 21 print_error('invalidcourse');
acf000b0 22}
23
24// Make sure that the user has permissions to manage groups.
25require_login($course);
26
27$context = get_context_instance(CONTEXT_COURSE, $courseid);
28require_capability('moodle/course:managegroups', $context);
29
f16fa0a3 30$strgroups = get_string('groups');
31$strparticipants = get_string('participants');
32$stroverview = get_string('overview', 'group');
33$strgrouping = get_string('grouping', 'group');
34$strgroup = get_string('group', 'group');
35$strnotingrouping = get_string('notingrouping', 'group');
36$strfiltergroups = get_string('filtergroups', 'group');
37$strnogroups = get_string('nogroups', 'group');
38$strdescription = get_string('description');
39
40// Get all groupings
41if (empty($CFG->enablegroupings)) {
42 $groupings = array();
43 $members = array(-1 => array()); //groups not in a grouping
44 $groupingid = 0;
45} else {
5e2f308b 46 $groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name');
f16fa0a3 47 $members = array();
48 foreach ($groupings as $grouping) {
49 $members[$grouping->id] = array();
50 }
51 $members[-1] = array(); //groups not in a grouping
52}
53
54// Get all groups
5e2f308b 55$groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
acf000b0 56
dfdaabd6 57$params = array('courseid'=>$courseid);
f33e1ed4 58if ($groupid) {
dfdaabd6 59 $groupwhere = "AND g.id = :groupid";
60 $params['groupid'] = $groupid;
f33e1ed4 61} else {
62 $groupwhere = "";
63}
64
f16fa0a3 65if (empty($CFG->enablegroupings)) {
f16fa0a3 66 $sql = "SELECT g.id AS groupid, NULL AS groupingid, u.id AS userid, u.firstname, u.lastname, u.idnumber, u.username
f33e1ed4 67 FROM {groups} g
68 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
69 LEFT JOIN {user} u ON gm.userid = u.id
dfdaabd6 70 WHERE g.courseid = :courseid $groupwhere
f16fa0a3 71 ORDER BY g.name, u.lastname, u.firstname";
72} else {
f33e1ed4 73 if ($groupingid) {
dfdaabd6 74 $groupingwhere = "AND gg.groupingid = :groupingid";
75 $params['groupingid'] = $groupingid;
f33e1ed4 76 } else {
77 $groupingwhere = "";
78 }
f16fa0a3 79 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, u.firstname, u.lastname, u.idnumber, u.username
f33e1ed4 80 FROM {groups} g
81 LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
82 LEFT JOIN {groups_members} gm ON g.id = gm.groupid
83 LEFT JOIN {user} u ON gm.userid = u.id
dfdaabd6 84 WHERE g.courseid = :courseid $groupwhere $groupingwhere
f16fa0a3 85 ORDER BY g.name, u.lastname, u.firstname";
86}
87
f33e1ed4 88if ($rs = $DB->get_recordset_sql($sql, $params)) {
89 foreach ($rs as $row) {
f16fa0a3 90 $user = new object();
91 $user->id = $row->userid;
92 $user->firstname = $row->firstname;
93 $user->lastname = $row->lastname;
94 $user->username = $row->username;
95 $user->idnumber = $row->idnumber;
96 if (!$row->groupingid) {
97 $row->groupingid = -1;
98 }
99 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
100 $members[$row->groupingid][$row->groupid] = array();
101 }
0c0e87f8 102 if(isset($user->id)){
103 $members[$row->groupingid][$row->groupid][] = $user;
104 }
f16fa0a3 105 }
dfdaabd6 106 $rs->close();
f16fa0a3 107}
acf000b0 108
acf000b0 109
110// Print the page and form
111$navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
112 array('name'=>$strgroups, 'link'=>'', 'type'=>'misc'));
113$navigation = build_navigation($navlinks);
114
115/// Print header
116print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
f16fa0a3 117// Add tabs
118$currenttab = 'overview';
119require('tabs.php');
acf000b0 120
f16fa0a3 121/// Print overview
04a7ba52 122echo $OUTPUT->heading(format_string($course->shortname) .' '.$stroverview, 3);
acf000b0 123
f16fa0a3 124echo $strfiltergroups;
acf000b0 125
acf000b0 126if (!empty($CFG->enablegroupings)) {
f16fa0a3 127 $options = array();
128 $options[0] = get_string('all');
129 foreach ($groupings as $grouping) {
130 $options[$grouping->id] = strip_tags(format_string($grouping->name));
acf000b0 131 }
8076ad5a 132 $popupurl = $rooturl.'&group='.$groupid;
7b1f2c82 133 $select = html_select::make_popup_form($popupurl, 'grouping', $options, 'selectgrouping', $groupingid);
8076ad5a 134 $select->set_label($strgrouping);
135 echo $OUTPUT->select($select);
acf000b0 136}
137
f16fa0a3 138$options = array();
139$options[0] = get_string('all');
140foreach ($groups as $group) {
141 $options[$group->id] = strip_tags(format_string($group->name));
acf000b0 142}
8076ad5a 143$popupurl = $rooturl.'&grouping='.$groupingid;
7b1f2c82 144$select = html_select::make_popup_form($popupurl, 'group', $options, 'selectgroup', $groupid);
8076ad5a 145$select->set_label($strgroup);
146echo $OUTPUT->select($select);
f16fa0a3 147
148/// Print table
149$printed = false;
150foreach ($members as $gpgid=>$groupdata) {
151 if ($groupingid and $groupingid != $gpgid) {
152 continue; // do not show
acf000b0 153 }
768cefa0 154 $table = new html_table();
f16fa0a3 155 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
156 $table->size = array('20%', '70%', '10%');
157 $table->align = array('left', 'left', 'center');
158 $table->width = '90%';
159 $table->data = array();
160 foreach ($groupdata as $gpid=>$users) {
161 if ($groupid and $groupid != $gpid) {
162 continue;
acf000b0 163 }
f16fa0a3 164 $line = array();
165 $name = format_string($groups[$gpid]->name);
3980c306 166 $jsdescription = addslashes_js(trim(format_text($groups[$gpid]->description)));
f16fa0a3 167 if (empty($jsdescription)) {
168 $line[] = $name;
169 } else {
170 $jsstrdescription = addslashes_js($strdescription);
171 $overlib = "return overlib('$jsdescription', BORDER, 0, FGCLASS, 'description', "
172 ."CAPTIONFONTCLASS, 'caption', CAPTION, '$jsstrdescription');";
173 $line[] = '<span onmouseover="'.s($overlib).'" onmouseout="return nd();">'.$name.'</span>';
174 }
175 $fullnames = array();
176 foreach ($users as $user) {
177 $fullnames[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user, true).'</a>';
178 }
179 $line[] = implode(', ', $fullnames);
180 $line[] = count($users);
181 $table->data[] = $line;
acf000b0 182 }
f16fa0a3 183 if ($groupid and empty($table->data)) {
184 continue;
acf000b0 185 }
f16fa0a3 186 if (!empty($CFG->enablegroupings)) {
187 if ($gpgid < 0) {
9cb990ec 188 echo $OUTPUT->heading($strnotingrouping, 3);
f16fa0a3 189 } else {
9cb990ec 190 echo $OUTPUT->heading(format_string($groupings[$gpgid]->name), 3);
3ced3884 191 echo $OUTPUT->box(format_text($groupings[$gpgid]->description), 'generalbox boxwidthnarrow boxaligncenter');
acf000b0 192 }
193 }
768cefa0 194 echo $OUTPUT->table($table);
f16fa0a3 195 $printed = true;
acf000b0 196}
197
653468d4 198echo $OUTPUT->footer();
acf000b0 199?>