problem with "not answererd yet" column on anon responses page. - diplaying admins...
[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
f9903ed0 34
b6ee2d82 35/// Check to see if groups are being used in this choice
36 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
37 $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
38 } else {
39 $currentgroup = false;
40 }
41
42 if ($currentgroup) {
bd3ba1dd 43 $users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname') + get_admins();
b6ee2d82 44 } else {
bd3ba1dd 45 $users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname') + get_admins();
b6ee2d82 46 }
f9903ed0 47
b6ee2d82 48 if (!$users) {
49 print_heading(get_string("nousersyet"));
50 print_footer($course);
51 exit;
f9903ed0 52 }
53
bfe97ca6 54 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
6fd87e3b 55 foreach ($allresponses as $aa) {
ebc3bd2b 56 $answers[$aa->userid] = $aa;
f9903ed0 57 }
f9903ed0 58 } else {
59 $answers = array () ;
60 }
f9903ed0 61
62 $timenow = time();
63
bfe97ca6 64 foreach ($choice->option as $optionid => $text) {
65 $useranswer[$optionid] = array();
66062dd3 66 }
da664183 67 foreach ($users as $user) {
bfe97ca6 68 if (!empty($user->id) and !empty($answers[$user->id])) {
dcde9f02 69 $answer = $answers[$user->id];
bfe97ca6 70 $useranswer[(int)$answer->optionid][] = $user;
dcde9f02 71 } else {
bfe97ca6 72 $useranswer[0][] = $user;
dcde9f02 73 }
da664183 74 }
bfe97ca6 75 foreach ($choice->option as $optionid => $text) {
76 if (!$choice->option[$optionid]) {
77 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
cd3fccff 78 }
79 }
da664183 80 ksort($useranswer);
64739659 81
82 //print spreadsheet if one is asked for:
83 if ($download == "xls") {
84
85 require_once("$CFG->libdir/excel/Worksheet.php");
86 require_once("$CFG->libdir/excel/Workbook.php");
87
88 // HTTP headers
89 $filename = $course->shortname."_".$choice->name.".xls";
90
91 header("Content-type: application/vnd.ms-excel");
92 header("Content-Disposition: attachment; filename=$filename" );
93 header("Expires: 0");
94 header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
95 header("Pragma: public");
96
97 // Creating a workbook
98 $workbook = new Workbook("-");
99 // Creating the first worksheet
100 $myxls =& $workbook->add_worksheet('Responses');
101
102 $myxls->write_string(0,0,get_string("lastname"));
103 $myxls->write_string(0,1,get_string("firstname"));
104 $myxls->write_string(0,2,get_string("idnumber"));
105 $myxls->write_string(0,3,get_string("choice","choice"));
106
107
108 /// generate the data for the body of the spreadsheet
109 $i=0;
110 $row=1;
111 if ($users) foreach ($users as $user) {
112 if (!($answers[$user->id]->optionid==0 && isadmin($user->id)) && !($answers[$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.
113 $myxls->write_string($row,0,$user->lastname);
114 $myxls->write_string($row,1,$user->firstname);
115 $studentid=(($user->idnumber != "") ? $user->idnumber : " ");
116 $myxls->write_string($row,2,$studentid);
117 $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
118 if (isset($useroption)) {
119 $myxls->write_string($row,3,$useroption);
120 }
121 $row++;
122 }
123 $pos=4;
124 }
125
126 $workbook->close();
127 exit;
128 }
129 // print text file
130 if ($download == "txt") {
131 $filename = $course->shortname."_".$choice->name.".txt";
132 header("Content-Type: application/download\n");
133 header("Content-Disposition: attachment; filename=\"".$filename."\"");
134
135 /// Print names of all the fields
136
137 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
138 echo get_string("choice","choice"). "\n";
139
140 /// generate the data for the body of the spreadsheet
141 $i=0;
142 $row=1;
143 if ($users) foreach ($users as $user) {
144 if (!($answers[$user->id]->optionid==0 && isadmin($user->id)) && !($answers[$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.
145 echo $user->lastname;
146 echo "\t".$user->firstname;
147 $studentid=(($user->idnumber != "") ? $user->idnumber : " ");
148 echo "\t". $studentid."\t";
149 echo choice_get_option_text($choice, $answers[$user->id]->optionid). "\n";
150 }
151 $row++;
152 }
153 exit;
154}
155
156 print_header_simple(format_string($choice->name).": $strresponses", "",
157 "<a href=\"index.php?id=$course->id\">$strchoices</a> ->
158 <a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "");
f9903ed0 159
bfe97ca6 160 switch ($format) {
161 case CHOICE_PUBLISH_NAMES:
162
163 $tablewidth = (int) (100.0 / count($useranswer));
164
165 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\">";
166 echo "<tr>";
167 foreach ($useranswer as $optionid => $userlist) {
168 if ($optionid) {
169 echo "<th class=\"col$optionid\" width=\"$tablewidth%\">";
170 } else if ($choice->showunanswered) {
171 echo "<th class=\"col$optionid\" width=\"$tablewidth%\">";
172 } else {
173 continue;
174 }
175 echo format_string(choice_get_option_text($choice, $optionid));
176 echo "</th>";
177 }
178 echo "</tr><tr>";
179
180 foreach ($useranswer as $optionid => $userlist) {
181 if ($optionid) {
182 echo "<td class=\"col$optionid\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
183 } else if ($choice->showunanswered) {
184 echo "<td class=\"col$optionid\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
185 } else {
186 continue;
187 }
188
189 echo "<table width=\"100%\">";
190 foreach ($userlist as $user) {
af40722c 191 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.
192 echo "<tr><td width=\"10\" nowrap=\"nowrap\">";
193 print_user_picture($user->id, $course->id, $user->picture);
194 echo "</td><td width=\"100%\" nowrap=\"nowrap\">";
195 echo "<p>".fullname($user, true)."</p>";
196 echo "</td></tr>";
197 }
bfe97ca6 198 }
199 echo "</table>";
200
201 echo "</td>";
202 }
203 echo "</tr></table>";
204 break;
205
206
207 case CHOICE_PUBLISH_ANONYMOUS:
208 $tablewidth = (int) (100.0 / count($useranswer));
209
210 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\">";
211 echo "<tr>";
212 foreach ($useranswer as $optionid => $userlist) {
213 if ($optionid) {
214 echo "<th width=\"$tablewidth%\">";
215 } else if ($choice->showunanswered) {
216 echo "<th width=\"$tablewidth%\">";
217 } else {
218 continue;
219 }
220 echo choice_get_option_text($choice, $optionid);
221 echo "</th>";
222 }
223 echo "</tr>";
224
225 $maxcolumn = 0;
226 foreach ($useranswer as $optionid => $userlist) {
227 if (!$optionid and !$choice->showunanswered) {
228 continue;
229 }
230 $column[$optionid] = count($userlist);
231 if ($column[$optionid] > $maxcolumn) {
232 $maxcolumn = $column[$optionid];
233 }
234 }
235
236 echo "<tr>";
237 foreach ($useranswer as $optionid => $userlist) {
238 if (!$optionid and !$choice->showunanswered) {
239 continue;
240 }
241 $height = 0;
242 if ($maxcolumn) {
243 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
244 }
245 echo "<td valign=\"bottom\" align=\"center\">";
246 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
247 echo "</td>";
248 }
249 echo "</tr>";
250
251 echo "<tr>";
252 foreach ($useranswer as $optionid => $userlist) {
253 if (!$optionid and !$choice->showunanswered) {
254 continue;
255 }
256 echo "<td align=\"center\">".$column[$optionid]."</td>";
257 }
258 echo "</tr></table>";
259
260 break;
f9903ed0 261 }
64739659 262
263 echo "<br />\n";
264 echo "<table border=\"0\" align=\"center\"><tr>\n";
265 echo "<td>";
266 unset($options);
267 $options["id"] = "$cm->id";
268 $options["download"] = "xls";
269 print_single_button("report.php", $options, get_string("downloadexcel"));
270 echo "</td><td>";
271 $options["download"] = "txt";
272 print_single_button("report.php", $options, get_string("downloadtext"));
273
274 echo "</td></tr></table>";
bfe97ca6 275print_footer($course);
f9903ed0 276
ec81373f 277
f9903ed0 278?>