weekly release 1.9.12+
[moodle.git] / group / overview.php
blobec567aaf6f46131247a7c8e114f083b330ad3da6
1 <?php // $Id$
2 /**
3 * Print an overview of groupings & group membership
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 */
11 require_once('../config.php');
13 $courseid = required_param('id', PARAM_INT);
14 $groupid = optional_param('group', 0, PARAM_INT);
15 $groupingid = optional_param('grouping', 0, PARAM_INT);
17 $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
18 $rooturl = $CFG->wwwroot.'/group/overview.php?id='.$courseid;
20 if (!$course = get_record('course', 'id',$courseid)) {
21 error('invalidcourse');
24 // Make sure that the user has permissions to manage groups.
25 require_login($course);
27 $context = get_context_instance(CONTEXT_COURSE, $courseid);
28 require_capability('moodle/course:managegroups', $context);
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');
40 // Get all groupings
41 if (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();
49 $members = array();
50 foreach ($groupings as $grouping) {
51 $members[$grouping->id] = array();
53 $members[-1] = array(); //groups not in a grouping
56 // Get all groups
57 if (!$groups = get_records('groups', 'courseid', $courseid, 'name')) {
58 $groups = array();
61 if (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";
81 if ($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;
92 if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
93 $members[$row->groupingid][$row->groupid] = array();
95 if(isset($user->id)){
96 $members[$row->groupingid][$row->groupid][] = $user;
99 rs_close($rs);
103 // Print the page and form
104 $navlinks = array(array('name'=>$strparticipants, 'link'=>$CFG->wwwroot.'/user/index.php?id='.$courseid, 'type'=>'misc'),
105 array('name'=>$strgroups, 'link'=>'', 'type'=>'misc'));
106 $navigation = build_navigation($navlinks);
108 /// Print header
109 print_header_simple($strgroups, ': '.$strgroups, $navigation, '', '', true, '', navmenu($course));
110 // Add tabs
111 $currenttab = 'overview';
112 require('tabs.php');
114 /// Print overview
115 print_heading(format_string($course->shortname) .' '.$stroverview, 'center', 3);
117 echo $strfiltergroups;
119 if (!empty($CFG->enablegroupings)) {
120 $options = array();
121 $options[0] = get_string('all');
122 foreach ($groupings as $grouping) {
123 $options[$grouping->id] = strip_tags(format_string($grouping->name));
125 popup_form($rooturl.'&amp;group='.$groupid.'&amp;grouping=', $options, 'selectgrouping', $groupingid, '', '', '', false, 'self', $strgrouping);
128 $options = array();
129 $options[0] = get_string('all');
130 foreach ($groups as $group) {
131 $options[$group->id] = strip_tags(format_string($group->name));
133 popup_form($rooturl.'&amp;grouping='.$groupingid.'&amp;group=', $options, 'selectgroup', $groupid, '', '', '', false, 'self', $strgroup);
136 /// Print table
137 $printed = false;
138 foreach ($members as $gpgid=>$groupdata) {
139 if ($groupingid and $groupingid != $gpgid) {
140 continue; // do not show
142 $table = new object();
143 $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
144 $table->size = array('20%', '70%', '10%');
145 $table->align = array('left', 'left', 'center');
146 $table->width = '90%';
147 $table->data = array();
148 foreach ($groupdata as $gpid=>$users) {
149 if ($groupid and $groupid != $gpid) {
150 continue;
152 $line = array();
153 $name = format_string($groups[$gpid]->name);
154 $jsdescription = addslashes_js(trim(format_text($groups[$gpid]->description)));
155 if (empty($jsdescription)) {
156 $line[] = $name;
157 } else {
158 $jsstrdescription = addslashes_js($strdescription);
159 $overlib = "return overlib('$jsdescription', BORDER, 0, FGCLASS, 'description', "
160 ."CAPTIONFONTCLASS, 'caption', CAPTION, '$jsstrdescription');";
161 $line[] = '<span onmouseover="'.s($overlib).'" onmouseout="return nd();">'.$name.'</span>';
163 $fullnames = array();
164 foreach ($users as $user) {
165 $fullnames[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user, true).'</a>';
167 $line[] = implode(', ', $fullnames);
168 $line[] = count($users);
169 $table->data[] = $line;
171 if ($groupid and empty($table->data)) {
172 continue;
174 if (!empty($CFG->enablegroupings)) {
175 if ($gpgid < 0) {
176 print_heading($strnotingrouping, '', 3);
177 } else {
178 print_heading(format_string($groupings[$gpgid]->name), '', 3);
179 print_box(format_text($groupings[$gpgid]->description), 'generalbox boxwidthnarrow boxaligncenter');
182 print_table($table, false);
183 $printed = true;
186 print_footer($course);