Fix for bug 2821. I 'm not sure if it makes any difference, but it cannot hurt.
[moodle.git] / mod / choice / report.php
CommitLineData
1c61c8d6 1<?php // $Id$
f9903ed0 2
b0e3a925 3 require_once("../../config.php");
4 require_once("lib.php");
f9903ed0 5
6 require_variable($id); // course module
7
bfe97ca6 8 $format = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT);
9
f9903ed0 10 if (! $cm = get_record("course_modules", "id", $id)) {
11 error("Course Module ID was incorrect");
12 }
13
14 if (! $course = get_record("course", "id", $cm->course)) {
15 error("Course module is misconfigured");
16 }
17
ec81373f 18 require_login($course->id, false, $cm);
f9903ed0 19
20 if (!isteacher($course->id)) {
21 error("Only teachers can look at this page");
22 }
23
cd3fccff 24 if (!$choice = choice_get_choice($cm->instance)) {
f9903ed0 25 error("Course module is incorrect");
26 }
27
c4016bc1 28 $strchoice = get_string("modulename", "choice");
29 $strchoices = get_string("modulenameplural", "choice");
30 $strresponses = get_string("responses", "choice");
31
408f7c41 32 add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
f9903ed0 33
c61d50b8 34 print_header_simple(format_string($choice->name).": $strresponses", "",
1ff45e74 35 "<a href=\"index.php?id=$course->id\">$strchoices</a> ->
c61d50b8 36 <a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "");
f9903ed0 37
b6ee2d82 38/// Check to see if groups are being used in this choice
39 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
40 $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
41 } else {
42 $currentgroup = false;
43 }
44
45 if ($currentgroup) {
bd3ba1dd 46 $users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname') + get_admins();
b6ee2d82 47 } else {
bd3ba1dd 48 $users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname') + get_admins();
b6ee2d82 49 }
f9903ed0 50
b6ee2d82 51 if (!$users) {
52 print_heading(get_string("nousersyet"));
53 print_footer($course);
54 exit;
f9903ed0 55 }
56
bfe97ca6 57 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
6fd87e3b 58 foreach ($allresponses as $aa) {
ebc3bd2b 59 $answers[$aa->userid] = $aa;
f9903ed0 60 }
f9903ed0 61 } else {
62 $answers = array () ;
63 }
f9903ed0 64
65 $timenow = time();
66
bfe97ca6 67 foreach ($choice->option as $optionid => $text) {
68 $useranswer[$optionid] = array();
66062dd3 69 }
da664183 70 foreach ($users as $user) {
bfe97ca6 71 if (!empty($user->id) and !empty($answers[$user->id])) {
dcde9f02 72 $answer = $answers[$user->id];
bfe97ca6 73 $useranswer[(int)$answer->optionid][] = $user;
dcde9f02 74 } else {
bfe97ca6 75 $useranswer[0][] = $user;
dcde9f02 76 }
da664183 77 }
bfe97ca6 78 foreach ($choice->option as $optionid => $text) {
79 if (!$choice->option[$optionid]) {
80 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
cd3fccff 81 }
82 }
da664183 83 ksort($useranswer);
f9903ed0 84
bfe97ca6 85 switch ($format) {
86 case CHOICE_PUBLISH_NAMES:
87
88 $tablewidth = (int) (100.0 / count($useranswer));
89
90 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\">";
91 echo "<tr>";
92 foreach ($useranswer as $optionid => $userlist) {
93 if ($optionid) {
94 echo "<th class=\"col$optionid\" width=\"$tablewidth%\">";
95 } else if ($choice->showunanswered) {
96 echo "<th class=\"col$optionid\" width=\"$tablewidth%\">";
97 } else {
98 continue;
99 }
100 echo format_string(choice_get_option_text($choice, $optionid));
101 echo "</th>";
102 }
103 echo "</tr><tr>";
104
105 foreach ($useranswer as $optionid => $userlist) {
106 if ($optionid) {
107 echo "<td class=\"col$optionid\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
108 } else if ($choice->showunanswered) {
109 echo "<td class=\"col$optionid\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
110 } else {
111 continue;
112 }
113
114 echo "<table width=\"100%\">";
115 foreach ($userlist as $user) {
af40722c 116 if (!($optionid==0 && isadmin($user->id)) && !($optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) { //make sure admins and hidden teachers are not shown in not answered yet column.
117 echo "<tr><td width=\"10\" nowrap=\"nowrap\">";
118 print_user_picture($user->id, $course->id, $user->picture);
119 echo "</td><td width=\"100%\" nowrap=\"nowrap\">";
120 echo "<p>".fullname($user, true)."</p>";
121 echo "</td></tr>";
122 }
bfe97ca6 123 }
124 echo "</table>";
125
126 echo "</td>";
127 }
128 echo "</tr></table>";
129 break;
130
131
132 case CHOICE_PUBLISH_ANONYMOUS:
133 $tablewidth = (int) (100.0 / count($useranswer));
134
135 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\">";
136 echo "<tr>";
137 foreach ($useranswer as $optionid => $userlist) {
138 if ($optionid) {
139 echo "<th width=\"$tablewidth%\">";
140 } else if ($choice->showunanswered) {
141 echo "<th width=\"$tablewidth%\">";
142 } else {
143 continue;
144 }
145 echo choice_get_option_text($choice, $optionid);
146 echo "</th>";
147 }
148 echo "</tr>";
149
150 $maxcolumn = 0;
151 foreach ($useranswer as $optionid => $userlist) {
152 if (!$optionid and !$choice->showunanswered) {
153 continue;
154 }
155 $column[$optionid] = count($userlist);
156 if ($column[$optionid] > $maxcolumn) {
157 $maxcolumn = $column[$optionid];
158 }
159 }
160
161 echo "<tr>";
162 foreach ($useranswer as $optionid => $userlist) {
163 if (!$optionid and !$choice->showunanswered) {
164 continue;
165 }
166 $height = 0;
167 if ($maxcolumn) {
168 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
169 }
170 echo "<td valign=\"bottom\" align=\"center\">";
171 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
172 echo "</td>";
173 }
174 echo "</tr>";
175
176 echo "<tr>";
177 foreach ($useranswer as $optionid => $userlist) {
178 if (!$optionid and !$choice->showunanswered) {
179 continue;
180 }
181 echo "<td align=\"center\">".$column[$optionid]."</td>";
182 }
183 echo "</tr></table>";
184
185 break;
f9903ed0 186 }
f9903ed0 187
bfe97ca6 188print_footer($course);
f9903ed0 189
ec81373f 190
f9903ed0 191?>