polished.
[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
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
31344133 36 $action = optional_param('action', '');
37
38 switch($action) {
39 case 'delete': /// Some attempts need to be deleted
40 // the following needs to be improved to delete all associated data as well
41
42 $attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array();
43 if(!is_array($attemptids) || empty($attemptids)) {
44 break;
45 }
46
47 foreach($attemptids as $num => $attemptid) {
48 if(empty($attemptid)) {
49 unset($attemptids[$num]);
50 }
51 }
52
53 foreach($attemptids as $attemptid) {
54 if ($todelete = get_record('choice_answers', 'id', $attemptid)) {
55 delete_records('choice_answers', 'id', $attemptid);
56 }
57 }
58 break;
59 }
d921818d 60 if ($download <> "xls" and $download <> "txt" ) {
61 print_header_simple(format_string($choice->name).": $strresponses", "",
62 "<a href=\"index.php?id=$course->id\">$strchoices</a> ->
63 <a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "", '', true,
64 update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
65
31344133 66
d921818d 67 /// Check to see if groups are being used in this choice
68 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
69 $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
70 } else {
71 $currentgroup = false;
72 }
b6ee2d82 73 }
b6ee2d82 74 if ($currentgroup) {
d921818d 75 $users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber');
b6ee2d82 76 } else {
d921818d 77 $users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber') + get_admins();
b6ee2d82 78 }
f9903ed0 79
b6ee2d82 80 if (!$users) {
39ee02a3 81 print_heading(get_string("nousersyet"));
f9903ed0 82 }
83
bfe97ca6 84 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
6fd87e3b 85 foreach ($allresponses as $aa) {
ebc3bd2b 86 $answers[$aa->userid] = $aa;
f9903ed0 87 }
f9903ed0 88 } else {
89 $answers = array () ;
90 }
f9903ed0 91
92 $timenow = time();
93
bfe97ca6 94 foreach ($choice->option as $optionid => $text) {
95 $useranswer[$optionid] = array();
66062dd3 96 }
da664183 97 foreach ($users as $user) {
bfe97ca6 98 if (!empty($user->id) and !empty($answers[$user->id])) {
dcde9f02 99 $answer = $answers[$user->id];
bfe97ca6 100 $useranswer[(int)$answer->optionid][] = $user;
dcde9f02 101 } else {
bfe97ca6 102 $useranswer[0][] = $user;
dcde9f02 103 }
da664183 104 }
bfe97ca6 105 foreach ($choice->option as $optionid => $text) {
106 if (!$choice->option[$optionid]) {
107 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
cd3fccff 108 }
109 }
da664183 110 ksort($useranswer);
64739659 111
112 //print spreadsheet if one is asked for:
113 if ($download == "xls") {
b0898566 114 require_once("$CFG->libdir/excellib.class.php");
64739659 115
31344133 116 /// Calculate file name
b0898566 117 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
118 /// Creating a workbook
119 $workbook = new MoodleExcelWorkbook("-");
120 /// Send HTTP headers
121 $workbook->send($filename);
122 /// Creating the first worksheet
31344133 123 $myxls =& $workbook->add_worksheet($strresponses);
64739659 124
b0898566 125 /// Print names of all the fields
64739659 126 $myxls->write_string(0,0,get_string("lastname"));
127 $myxls->write_string(0,1,get_string("firstname"));
128 $myxls->write_string(0,2,get_string("idnumber"));
129 $myxls->write_string(0,3,get_string("choice","choice"));
130
64739659 131 /// generate the data for the body of the spreadsheet
b0898566 132 $i=0;
133 $row=1;
134 if ($users) {
135 foreach ($users as $user) {
136 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && isadmin($user->id)) &&
137 (!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) &&
138 !($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.
139
140 $myxls->write_string($row,0,$user->lastname);
141 $myxls->write_string($row,1,$user->firstname);
142 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
143 $myxls->write_string($row,2,$studentid);
144 $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
145 if (isset($useroption)) {
146 $myxls->write_string($row,3,format_string($useroption,true));
147 }
148 $row++;
149 }
150 $pos=4;
151 }
152 }
153
154 /// Close the workbook
155 $workbook->close();
156
157 exit;
64739659 158 }
159 // print text file
160 if ($download == "txt") {
09972dd3 161 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
31344133 162
163 header("Content-Type: application/download\n");
164 header("Content-Disposition: attachment; filename=\"$filename\"");
165 header("Expires: 0");
166 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
167 header("Pragma: public");
168
64739659 169 /// Print names of all the fields
170
171 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
172 echo get_string("choice","choice"). "\n";
173
174 /// generate the data for the body of the spreadsheet
175 $i=0;
176 $row=1;
177 if ($users) foreach ($users as $user) {
ae2f3b4c 178 if (!($answers[$user->id]->optionid==0 && isadmin($user->id)) &&
179 (!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) &&
180 !($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.
181
64739659 182 echo $user->lastname;
183 echo "\t".$user->firstname;
184 $studentid=(($user->idnumber != "") ? $user->idnumber : " ");
185 echo "\t". $studentid."\t";
09972dd3 186 echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n";
64739659 187 }
188 $row++;
189 }
190 exit;
191}
192
f9903ed0 193
bfe97ca6 194 switch ($format) {
195 case CHOICE_PUBLISH_NAMES:
31344133 196 echo '<div id="tablecontainer">';
197 echo '<form id="attemptsform" method="post" action="report.php" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes(get_string('deleteattemptcheck','quiz')).'\' : true);">';
198 echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
199 echo '<input type="hidden" name="mode" value="overview" />';
bfe97ca6 200 $tablewidth = (int) (100.0 / count($useranswer));
201
51e041f4 202 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results names\">";
bfe97ca6 203 echo "<tr>";
51e041f4 204 $count = 0;
bfe97ca6 205 foreach ($useranswer as $optionid => $userlist) {
206 if ($optionid) {
51e041f4 207 echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
bfe97ca6 208 } else if ($choice->showunanswered) {
51e041f4 209 echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
bfe97ca6 210 } else {
211 continue;
212 }
213 echo format_string(choice_get_option_text($choice, $optionid));
214 echo "</th>";
51e041f4 215 $count++;
bfe97ca6 216 }
217 echo "</tr><tr>";
218
51e041f4 219 $count = 0;
bfe97ca6 220 foreach ($useranswer as $optionid => $userlist) {
221 if ($optionid) {
51e041f4 222 echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
bfe97ca6 223 } else if ($choice->showunanswered) {
51e041f4 224 echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
bfe97ca6 225 } else {
226 continue;
227 }
228
51e041f4 229 echo "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";
bfe97ca6 230 foreach ($userlist as $user) {
af40722c 231 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.
31344133 232 echo "<tr><td width=\"5\" nowrap=\"nowrap\">";
233 echo '<input type="checkbox" name="attemptid[]" value="'. $answers[$user->id]->id. '" />';
234 echo "</td><td width=\"10\" nowrap=\"nowrap\" class=\"picture\">";
235
af40722c 236 print_user_picture($user->id, $course->id, $user->picture);
51e041f4 237 echo "</td><td width=\"100%\" nowrap=\"nowrap\" class=\"fullname\">";
af40722c 238 echo "<p>".fullname($user, true)."</p>";
239 echo "</td></tr>";
240 }
bfe97ca6 241 }
242 echo "</table>";
243
244 echo "</td>";
51e041f4 245 $count++;
bfe97ca6 246 }
31344133 247 echo "</tr><tr><td>";
248
249 /// Print "Select all" etc.
250 // if (!empty($attempts)) {
251 echo '<table id="commands">';
252 echo '<tr><td>';
253 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
254 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
255 echo '&nbsp;&nbsp;';
256 $options = array('delete' => get_string('delete'));
257 echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
258 echo '<noscript id="noscriptmenuaction" style="display: inline;">';
259 echo '<input type="submit" value="'.get_string('go').'" /></noscript>';
260 echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
261 echo '</td></tr></table>';
262 // }
263 echo "</td></tr></table></div>";
264 /// Close form
265 echo '</form>';
bfe97ca6 266 break;
267
268
269 case CHOICE_PUBLISH_ANONYMOUS:
270 $tablewidth = (int) (100.0 / count($useranswer));
271
51e041f4 272 echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results anonymous\">";
bfe97ca6 273 echo "<tr>";
51e041f4 274 $count = 0;
bfe97ca6 275 foreach ($useranswer as $optionid => $userlist) {
276 if ($optionid) {
51e041f4 277 echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
bfe97ca6 278 } else if ($choice->showunanswered) {
51e041f4 279 echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
bfe97ca6 280 } else {
281 continue;
282 }
283 echo choice_get_option_text($choice, $optionid);
284 echo "</th>";
51e041f4 285 $count++;
bfe97ca6 286 }
287 echo "</tr>";
288
289 $maxcolumn = 0;
290 foreach ($useranswer as $optionid => $userlist) {
291 if (!$optionid and !$choice->showunanswered) {
292 continue;
293 }
294 $column[$optionid] = count($userlist);
295 if ($column[$optionid] > $maxcolumn) {
296 $maxcolumn = $column[$optionid];
297 }
298 }
299
300 echo "<tr>";
51e041f4 301 $count = 0;
bfe97ca6 302 foreach ($useranswer as $optionid => $userlist) {
303 if (!$optionid and !$choice->showunanswered) {
304 continue;
305 }
306 $height = 0;
307 if ($maxcolumn) {
308 $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
309 }
51e041f4 310 echo "<td valign=\"bottom\" align=\"center\" class=\"col$count data\">";
bfe97ca6 311 echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
312 echo "</td>";
51e041f4 313 $count++;
bfe97ca6 314 }
315 echo "</tr>";
316
317 echo "<tr>";
51e041f4 318 $count = 0;
bfe97ca6 319 foreach ($useranswer as $optionid => $userlist) {
320 if (!$optionid and !$choice->showunanswered) {
321 continue;
322 }
51e041f4 323 echo "<td align=\"center\" class=\"col$count count\">".$column[$optionid]."</td>";
324 $count++;
bfe97ca6 325 }
326 echo "</tr></table>";
327
328 break;
f9903ed0 329 }
64739659 330
331 echo "<br />\n";
332 echo "<table border=\"0\" align=\"center\"><tr>\n";
333 echo "<td>";
334 unset($options);
335 $options["id"] = "$cm->id";
336 $options["download"] = "xls";
337 print_single_button("report.php", $options, get_string("downloadexcel"));
338 echo "</td><td>";
339 $options["download"] = "txt";
340 print_single_button("report.php", $options, get_string("downloadtext"));
341
342 echo "</td></tr></table>";
bfe97ca6 343print_footer($course);
f9903ed0 344
ec81373f 345
f9903ed0 346?>