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