prevent NULL contents before field change (old servers can have this wrongly defined...
[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
20if (!$course = get_record('course', '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 {
46 if (!$groupings = get_records('groupings', 'courseid', $courseid, 'name')) {
47 $groupings = array();
48 }
49 $members = array();
50 foreach ($groupings as $grouping) {
51 $members[$grouping->id] = array();
52 }
53 $members[-1] = array(); //groups not in a grouping
54}
55
56// Get all groups
57if (!$groups = get_records('groups', 'courseid', $courseid, 'name')) {
58 $groups = array();
59}
acf000b0 60
f16fa0a3 61if (empty($CFG->enablegroupings)) {
62 $groupwhere = $groupid ? "AND g.id = $groupid" : "";
63 $sql = "SELECT g.id AS groupid, NULL AS groupingid, u.id AS userid, u.firstname, u.lastname, u.idnumber, u.username
64 FROM {$CFG->prefix}groups g
65 LEFT JOIN {$CFG->prefix}groups_members gm ON g.id = gm.groupid
66 LEFT JOIN {$CFG->prefix}user u ON gm.userid = u.id
67 WHERE g.courseid = {$course->id} $groupwhere
68 ORDER BY g.name, u.lastname, u.firstname";
69} else {
70 $groupingwhere = $groupingid ? "AND gg.groupingid = $groupingid" : "";
71 $groupwhere = $groupid ? "AND g.id = $groupid" : "";
72 $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, u.firstname, u.lastname, u.idnumber, u.username
73 FROM {$CFG->prefix}groups g
74 LEFT JOIN {$CFG->prefix}groupings_groups gg ON g.id = gg.groupid
75 LEFT JOIN {$CFG->prefix}groups_members gm ON g.id = gm.groupid
76 LEFT JOIN {$CFG->prefix}user u ON gm.userid = u.id
77 WHERE g.courseid = {$course->id} $groupingwhere $groupwhere
78 ORDER BY g.name, u.lastname, u.firstname";
79}
80
81if ($rs = get_recordset_sql($sql)) {
82 while ($row = rs_fetch_next_record($rs)) {
83 $user = new object();
84 $user->id = $row->userid;
85 $user->firstname = $row->firstname;
86 $user->lastname = $row->lastname;
87 $user->username = $row->username;
88 $user->idnumber = $row->idnumber;
89 if (!$row->groupingid) {
90 $row->groupingid = -1;
91 }
92 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
93 $members[$row->groupingid][$row->groupid] = array();
94 }
95 $members[$row->groupingid][$row->groupid][] = $user;
96 }
97 rs_close($rs);
98}
acf000b0 99
acf000b0 100
101// Print the page and form
102$navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
103 array('name'=>$strgroups, 'link'=>'', 'type'=>'misc'));
104$navigation = build_navigation($navlinks);
105
106/// Print header
107print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
f16fa0a3 108// Add tabs
109$currenttab = 'overview';
110require('tabs.php');
acf000b0 111
f16fa0a3 112/// Print overview
113print_heading(format_string($course->shortname) .' '.$stroverview, 'center', 3);
acf000b0 114
f16fa0a3 115echo $strfiltergroups;
acf000b0 116
acf000b0 117if (!empty($CFG->enablegroupings)) {
f16fa0a3 118 $options = array();
119 $options[0] = get_string('all');
120 foreach ($groupings as $grouping) {
121 $options[$grouping->id] = strip_tags(format_string($grouping->name));
acf000b0 122 }
f16fa0a3 123 popup_form($rooturl.'&amp;group='.$groupid.'&amp;grouping=', $options, 'selectgrouping', $groupingid, '', '', '', false, 'self', $strgrouping);
acf000b0 124}
125
f16fa0a3 126$options = array();
127$options[0] = get_string('all');
128foreach ($groups as $group) {
129 $options[$group->id] = strip_tags(format_string($group->name));
acf000b0 130}
f16fa0a3 131popup_form($rooturl.'&amp;grouping='.$groupingid.'&amp;group=', $options, 'selectgroup', $groupid, '', '', '', false, 'self', $strgroup);
acf000b0 132
f16fa0a3 133
134/// Print table
135$printed = false;
136foreach ($members as $gpgid=>$groupdata) {
137 if ($groupingid and $groupingid != $gpgid) {
138 continue; // do not show
acf000b0 139 }
f16fa0a3 140 $table = new object();
141 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
142 $table->size = array('20%', '70%', '10%');
143 $table->align = array('left', 'left', 'center');
144 $table->width = '90%';
145 $table->data = array();
146 foreach ($groupdata as $gpid=>$users) {
147 if ($groupid and $groupid != $gpid) {
148 continue;
acf000b0 149 }
f16fa0a3 150 $line = array();
151 $name = format_string($groups[$gpid]->name);
3980c306 152 $jsdescription = addslashes_js(trim(format_text($groups[$gpid]->description)));
f16fa0a3 153 if (empty($jsdescription)) {
154 $line[] = $name;
155 } else {
156 $jsstrdescription = addslashes_js($strdescription);
157 $overlib = "return overlib('$jsdescription', BORDER, 0, FGCLASS, 'description', "
158 ."CAPTIONFONTCLASS, 'caption', CAPTION, '$jsstrdescription');";
159 $line[] = '<span onmouseover="'.s($overlib).'" onmouseout="return nd();">'.$name.'</span>';
160 }
161 $fullnames = array();
162 foreach ($users as $user) {
163 $fullnames[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user, true).'</a>';
164 }
165 $line[] = implode(', ', $fullnames);
166 $line[] = count($users);
167 $table->data[] = $line;
acf000b0 168 }
f16fa0a3 169 if ($groupid and empty($table->data)) {
170 continue;
acf000b0 171 }
f16fa0a3 172 if (!empty($CFG->enablegroupings)) {
173 if ($gpgid < 0) {
174 print_heading($strnotingrouping, '', 3);
175 } else {
176 print_heading(format_string($groupings[$gpgid]->name), '', 3);
3980c306 177 print_box(format_text($groupings[$gpgid]->description), 'generalbox boxwidthnarrow boxaligncenter');
acf000b0 178 }
179 }
f16fa0a3 180 print_table($table, false);
181 $printed = true;
acf000b0 182}
183
184print_footer($course);
185?>