Header and footer need to come from current_theme Always
[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) {
ae2f3b4c 112 if (!($answers[$user->id]->optionid==0 && isadmin($user->id)) &&
113 (!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) &&
114 !($choice->showunanswered==0 && $answers[$user->id]->optionid==0) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
115
116 $myxls->write_string($row,0,$user->lastname);
117 $myxls->write_string($row,1,$user->firstname);
118 $studentid=(($user->idnumber != "") ? $user->idnumber : " ");
119 $myxls->write_string($row,2,$studentid);
120 $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
121 if (isset($useroption)) {
122 $myxls->write_string($row,3,$useroption);
123 }
124 $row++;
64739659 125 }
126 $pos=4;
127 }
128
129 $workbook->close();
130 exit;
131 }
132 // print text file
133 if ($download == "txt") {
134 $filename = $course->shortname."_".$choice->name.".txt";
135 header("Content-Type: application/download\n");
136 header("Content-Disposition: attachment; filename=\"".$filename."\"");
137
138 /// Print names of all the fields
139
140 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
141 echo get_string("choice","choice"). "\n";
142
143 /// generate the data for the body of the spreadsheet
144 $i=0;
145 $row=1;
146 if ($users) foreach ($users as $user) {
ae2f3b4c 147 if (!($answers[$user->id]->optionid==0 && isadmin($user->id)) &&
148 (!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) &&
149 !($choice->showunanswered==0 && $answers[$user->id]->optionid==0) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
150
64739659 151 echo $user->lastname;
152 echo "\t".$user->firstname;
153 $studentid=(($user->idnumber != "") ? $user->idnumber : " ");
154 echo "\t". $studentid."\t";
155 echo choice_get_option_text($choice, $answers[$user->id]->optionid). "\n";
156 }
157 $row++;
158 }
159 exit;
160}
161
162 print_header_simple(format_string($choice->name).": $strresponses", "",
163 "<a href=\"index.php?id=$course->id\">$strchoices</a> ->
164 <a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "");
f9903ed0 165
bfe97ca6 166 switch ($format) {
167 case CHOICE_PUBLISH_NAMES:
168
169 $tablewidth = (int) (100.0 / count($useranswer));
170
171 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\">";
172 echo "<tr>";
173 foreach ($useranswer as $optionid => $userlist) {
174 if ($optionid) {
175 echo "<th class=\"col$optionid\" width=\"$tablewidth%\">";
176 } else if ($choice->showunanswered) {
177 echo "<th class=\"col$optionid\" width=\"$tablewidth%\">";
178 } else {
179 continue;
180 }
181 echo format_string(choice_get_option_text($choice, $optionid));
182 echo "</th>";
183 }
184 echo "</tr><tr>";
185
186 foreach ($useranswer as $optionid => $userlist) {
187 if ($optionid) {
188 echo "<td class=\"col$optionid\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
189 } else if ($choice->showunanswered) {
190 echo "<td class=\"col$optionid\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
191 } else {
192 continue;
193 }
194
195 echo "<table width=\"100%\">";
196 foreach ($userlist as $user) {
af40722c 197 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.
198 echo "<tr><td width=\"10\" nowrap=\"nowrap\">";
199 print_user_picture($user->id, $course->id, $user->picture);
200 echo "</td><td width=\"100%\" nowrap=\"nowrap\">";
201 echo "<p>".fullname($user, true)."</p>";
202 echo "</td></tr>";
203 }
bfe97ca6 204 }
205 echo "</table>";
206
207 echo "</td>";
208 }
209 echo "</tr></table>";
210 break;
211
212
213 case CHOICE_PUBLISH_ANONYMOUS:
214 $tablewidth = (int) (100.0 / count($useranswer));
215
216 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\">";
217 echo "<tr>";
218 foreach ($useranswer as $optionid => $userlist) {
219 if ($optionid) {
220 echo "<th width=\"$tablewidth%\">";
221 } else if ($choice->showunanswered) {
222 echo "<th width=\"$tablewidth%\">";
223 } else {
224 continue;
225 }
226 echo choice_get_option_text($choice, $optionid);
227 echo "</th>";
228 }
229 echo "</tr>";
230
231 $maxcolumn = 0;
232 foreach ($useranswer as $optionid => $userlist) {
233 if (!$optionid and !$choice->showunanswered) {
234 continue;
235 }
236 $column[$optionid] = count($userlist);
237 if ($column[$optionid] > $maxcolumn) {
238 $maxcolumn = $column[$optionid];
239 }
240 }
241
242 echo "<tr>";
243 foreach ($useranswer as $optionid => $userlist) {
244 if (!$optionid and !$choice->showunanswered) {
245 continue;
246 }
247 $height = 0;
248 if ($maxcolumn) {
249 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
250 }
251 echo "<td valign=\"bottom\" align=\"center\">";
252 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
253 echo "</td>";
254 }
255 echo "</tr>";
256
257 echo "<tr>";
258 foreach ($useranswer as $optionid => $userlist) {
259 if (!$optionid and !$choice->showunanswered) {
260 continue;
261 }
262 echo "<td align=\"center\">".$column[$optionid]."</td>";
263 }
264 echo "</tr></table>";
265
266 break;
f9903ed0 267 }
64739659 268
269 echo "<br />\n";
270 echo "<table border=\"0\" align=\"center\"><tr>\n";
271 echo "<td>";
272 unset($options);
273 $options["id"] = "$cm->id";
274 $options["download"] = "xls";
275 print_single_button("report.php", $options, get_string("downloadexcel"));
276 echo "</td><td>";
277 $options["download"] = "txt";
278 print_single_button("report.php", $options, get_string("downloadtext"));
279
280 echo "</td></tr></table>";
bfe97ca6 281print_footer($course);
f9903ed0 282
ec81373f 283
f9903ed0 284?>