MDL-11419 - groups: interface enhancements + new features:
[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 */
10
11require_once('../config.php');
12
13$courseid = required_param('id', PARAM_INT);
14$groupid = optional_param('groupid', 0, PARAM_INT);
15$groupingid = optional_param('groupingid', 0, PARAM_INT);
16
17$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
18
19if (!$course = get_record('course', 'id',$courseid)) {
20 error('invalidcourse');
21}
22
23// Make sure that the user has permissions to manage groups.
24require_login($course);
25
26$context = get_context_instance(CONTEXT_COURSE, $courseid);
27require_capability('moodle/course:managegroups', $context);
28
29
30
31$strgroups = get_string('groups');
32$strparticipants = get_string('participants');
33$stroverview = get_string('overview', 'group');
34$strgrouping = get_string('grouping', 'group');
35$strgroup = get_string('group', 'group');
36$strnotingrouping = get_string('notingrouping', 'group');
37$strfiltergroups = get_string('filtergroups', 'group');
38$strnogroups = get_string('nogroups', 'group');
39$strnogroupsassigned = get_string('nogroupsassigned', 'group');
40
41// Print the page and form
42$navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
43 array('name'=>$strgroups, 'link'=>'', 'type'=>'misc'));
44$navigation = build_navigation($navlinks);
45
46/// Print header
47print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
48
49
50if (!empty($CFG->enablegroupings)) {
51 // Add tabs
52 $currenttab = 'overview';
53 require('tabs.php');
54}
55
56$groupings= array();
57
58// Get groupings and child group id's
59if (!empty($CFG->enablegroupings)) {
60 $sql = "SELECT gs.id, gs.name, gg.groupid " .
61 "FROM {$CFG->prefix}groupings gs " .
62 "LEFT JOIN {$CFG->prefix}groupings_groups gg ON gs.id = gg.groupingid " .
63 "WHERE gs.courseid = {$course->id} " .
64 "ORDER BY gs.name, gs.id ";
65
66 $rs = get_recordset_sql($sql);
67 while ($row = rs_fetch_next_record($rs)) {
68 $groupings[] = $row;
69 }
70}
71
72// Get groups & group members
73$sql = "SELECT g.id AS groupid, g.name, u.id AS userid, u.firstname, u.lastname, u.idnumber, u.username " .
74 "FROM {$CFG->prefix}groups g " .
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} " .
78 "ORDER BY g.name, g.id ";
79
80$rs = get_recordset_sql($sql);
81
82$groupsmembers = array();
83
84// Build a hash of keyed on groupid and userid;
85while ($row = rs_fetch_next_record($rs)) {
86 $groupsmembers[$row->groupid]->name = $row->name;
87 $groupsmembers[$row->groupid]->groupid = $row->groupid;
88 $groupsmembers[$row->groupid]->users[$row->userid] = $row;
89 $groupsmembers[$row->groupid]->printed = false;
90}
91
92if (empty($groupsmembers)) {
93 print_box($strnogroups);
94} else {
95
96 /// Print overview filter form
97
98 echo '<form method="get" action="overview.php">';
99 echo "<input type=\"hidden\" name=\"id\" value=\"{$course->id}\" />";
100 echo "<label for=\"groupingselect\">$strfiltergroups $strgrouping </label>";
101 echo '<select id="groupingselect" name="groupingid" onchange="this.parentNode.submit();">';
102 echo ' <option value=""></option>';
103 $lastgroupingid = false;
104 foreach ($groupings as $grouping) {
105 if ($lastgroupingid === false || $lastgroupingid != $grouping->id) {
106 $selected = $grouping->id == $groupingid ? 'selected="selected"':'';
107 echo "<option value=\"{$grouping->id}\" $selected>".format_string($grouping->name)."</option>\n";
108 }
109 $lastgroupingid = $grouping->id;
110 }
111 echo '</select>';
112
113 echo "<label for=\"groupselect\"> $strgroup </label>";
114 echo '<select id="groupselect" name="groupid" onchange="this.parentNode.submit();">';
115 echo ' <option value=""></option>';
116 $lastgroupid = false;
117
118 foreach ($groupsmembers as $group) {
119 if ($lastgroupid === false || $lastgroupid != $group->groupid) {
120 $selected = $group->groupid == $groupid ? 'selected="selected"':'';
121 echo "<option value=\"{$group->groupid}\" $selected>".format_string($group->name)."</option>\n";
122 }
123 $lastgroupid = $group->groupid ;
124 }
125 echo '</select>';
126
127 echo '</form>';
128
129
130 /// Print overview
131 print_heading(format_string($course->shortname) .' '.$stroverview, 'center', 3);
132
133
134
135 echo '<div id="grouping-groups-overview"><ul>';
136
137 if (!empty($CFG->enablegroupings) && isset($groupings)) {
138 $lastgroupingid = false;
139 foreach ($groupings as $grouping) {
140 if (!empty($groupingid) && $groupingid != $grouping->id) {
141 continue;
142 }
143 if (!empty($groupid) && $groupid != $grouping->groupid) {
144 continue;
145 }
146 if ($lastgroupingid === false || $lastgroupingid != $grouping->id) {
147 if($lastgroupingid !== false) {
148 echo '</ul></li>';
149 }
150
151 echo "<li>$strgrouping: {$grouping->name}<ul>\n";
152 $lastgroupingid = $grouping->id;
153 }
154 if (isset($groupsmembers[$grouping->groupid])) {
155 echo "<li>{$strgroup}: ".format_string($groupsmembers[$grouping->groupid]->name)."<ul>\n";
156 foreach ($groupsmembers[$grouping->groupid]->users as $user) {
157 echo "<li><a href=\"{$CFG->wwwroot}/user/view.php?id={$user->userid}\">".fullname($user)."</a></li>\n";
158 }
159 echo "</ul></li>";
160 }
161 else {
162 echo "<li>$strnogroupsassigned</li>";
163 }
164 if (isset($groupsmembers[$grouping->groupid])) {
165 $groupsmembers[$grouping->groupid]->printed = true;
166 }
167 }
168 }
169 if ($lastgroupingid !== false) {
170 echo '</ul></li>';
171 }
172 echo '</ul>';
173
174 // Print Groups not in a grouping
175
176
177 if (empty($groupingid)) {
178
179 $labelprinted = false;
180 foreach($groupsmembers as $groupmembers) {
181 if ($groupmembers->printed) {
182 continue;
183 }
184 if (!empty($groupid) && $groupid != $groupmembers->groupid) {
185 continue;
186 }
187 if ($labelprinted === false) {
188 echo "<ul><li>$strnotingrouping<ul>";
189 $labelprinted = true;
190 }
191
192 echo '<li>'.format_string($groupmembers->name).'<ul>';
193
194 foreach ($groupmembers->users as $user) {
195 echo "<li><a href=\"{$CFG->wwwroot}/user/view.php?id={$user->userid}\">".fullname($user)."</a></li>\n";
196 }
197 echo "</ul></li>";
198 }
199 if ($labelprinted !== false) {
200 echo '</ul></li></ul>';
201 }
202 }
203 echo '</div>';
204}
205
206print_footer($course);
207?>