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