MDL-7574 Applying Matthew Davidson's patch.
[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
69e3c416 6 $id = required_param('id', PARAM_INT); //moduleid
09972dd3 7 $format = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT);
8 $download = optional_param('download', '', PARAM_ALPHA);
69e3c416 9 $action = optional_param('action', '', PARAM_ALPHA);
bfe97ca6 10
f9d5371b 11 if (! $cm = get_coursemodule_from_id('choice', $id)) {
f9903ed0 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);
bbbf2d40 20
dabfd0ed 21 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
bbbf2d40 22
0468976c 23 require_capability('mod/choice:readresponses', $context);
bbbf2d40 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);
348630d8 34
0468976c 35 if ($action == 'delete' && has_capability('mod/choice:deleteresponses',$context)) {
348630d8 36 $attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array(); //get array of repsonses to delete.
90ca2ce8 37 choice_delete_responses($attemptids); //delete responses.
dabfd0ed 38 redirect("report.php?id=$cm->id");
348630d8 39 }
40
3b27b0fe 41 if (!$download) {
a044c05d 42
01e38388 43 $navlinks = array();
3b27b0fe 44 $navlinks[] = array('name' => $strchoices, 'link' => "index.php?id=$course->id", 'type' => 'activity');
45 $navlinks[] = array('name' => format_string($choice->name), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
46 $navlinks[] = array('name' => $strresponses, 'link' => '', 'type' => 'title');
161fbddf 47
3b27b0fe 48 $navigation = build_navigation($navlinks);
161fbddf 49
50 print_header_simple(format_string($choice->name).": $strresponses", "", $navigation, "", '', true,
d921818d 51 update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
3b27b0fe 52 /// Check to see if groups are being used in this choice
53 $groupmode = groupmode($course, $cm);
54 setup_and_print_groups($course, $groupmode, 'report.php?id='.$id);
55 } else {
56 $groupmode = groupmode($course, $cm);
57 get_and_set_current_group($course, $groupmode);
b6ee2d82 58 }
f9903ed0 59
33a893aa 60 $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC');
348630d8 61
b6ee2d82 62 if (!$users) {
39ee02a3 63 print_heading(get_string("nousersyet"));
f9903ed0 64 }
65
bfe97ca6 66 if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
6fd87e3b 67 foreach ($allresponses as $aa) {
ebc3bd2b 68 $answers[$aa->userid] = $aa;
f9903ed0 69 }
f9903ed0 70 } else {
71 $answers = array () ;
72 }
f9903ed0 73
74 $timenow = time();
75
bfe97ca6 76 foreach ($choice->option as $optionid => $text) {
77 $useranswer[$optionid] = array();
66062dd3 78 }
da664183 79 foreach ($users as $user) {
bfe97ca6 80 if (!empty($user->id) and !empty($answers[$user->id])) {
dcde9f02 81 $answer = $answers[$user->id];
bfe97ca6 82 $useranswer[(int)$answer->optionid][] = $user;
dcde9f02 83 } else {
bfe97ca6 84 $useranswer[0][] = $user;
dcde9f02 85 }
da664183 86 }
bfe97ca6 87 foreach ($choice->option as $optionid => $text) {
88 if (!$choice->option[$optionid]) {
89 unset($useranswer[$optionid]); // Throw away any data that doesn't apply
cd3fccff 90 }
91 }
da664183 92 ksort($useranswer);
d81b7ffb 93
94
95 if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
96 require_once("$CFG->libdir/odslib.class.php");
97
98 /// Calculate file name
99 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
100 /// Creating a workbook
101 $workbook = new MoodleODSWorkbook("-");
102 /// Send HTTP headers
103 $workbook->send($filename);
104 /// Creating the first worksheet
105 $myxls =& $workbook->add_worksheet($strresponses);
106
107 /// Print names of all the fields
108 $myxls->write_string(0,0,get_string("lastname"));
109 $myxls->write_string(0,1,get_string("firstname"));
110 $myxls->write_string(0,2,get_string("idnumber"));
111 $myxls->write_string(0,3,get_string("group"));
112 $myxls->write_string(0,4,get_string("choice","choice"));
113
114
115 /// generate the data for the body of the spreadsheet
116 $i=0;
117 $row=1;
118 if ($users) {
119 foreach ($users as $user) {
120 // this needs fixing
121
122 if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) {
123
124 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==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.
125
126 $myxls->write_string($row,0,$user->lastname);
127 $myxls->write_string($row,1,$user->firstname);
128 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
129 $myxls->write_string($row,2,$studentid);
130 $ug2 = '';
2c386f82 131 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
d81b7ffb 132 foreach ($usergrps as $ug) {
133 $ug2 = $ug2. $ug->name;
134 }
135 }
136 $myxls->write_string($row,3,$ug2);
137
138 $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
139 if (isset($useroption)) {
140 $myxls->write_string($row,4,format_string($useroption,true));
141 }
142 $row++;
143 }
144 $pos=4;
145 }
146 }
147
148 /// Close the workbook
149 $workbook->close();
150
151 exit;
152 }
153 }
154
155
64739659 156 //print spreadsheet if one is asked for:
0468976c 157 if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
b0898566 158 require_once("$CFG->libdir/excellib.class.php");
64739659 159
31344133 160 /// Calculate file name
b0898566 161 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
162 /// Creating a workbook
163 $workbook = new MoodleExcelWorkbook("-");
164 /// Send HTTP headers
165 $workbook->send($filename);
166 /// Creating the first worksheet
31344133 167 $myxls =& $workbook->add_worksheet($strresponses);
64739659 168
b0898566 169 /// Print names of all the fields
64739659 170 $myxls->write_string(0,0,get_string("lastname"));
171 $myxls->write_string(0,1,get_string("firstname"));
172 $myxls->write_string(0,2,get_string("idnumber"));
348630d8 173 $myxls->write_string(0,3,get_string("group"));
174 $myxls->write_string(0,4,get_string("choice","choice"));
175
64739659 176
64739659 177 /// generate the data for the body of the spreadsheet
b0898566 178 $i=0;
179 $row=1;
180 if ($users) {
f9a59efa 181 foreach ($users as $user) {
182 // this needs fixing
39b3e7de 183
184 if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) {
185
186 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==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.
187
188 $myxls->write_string($row,0,$user->lastname);
189 $myxls->write_string($row,1,$user->firstname);
190 $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
191 $myxls->write_string($row,2,$studentid);
192 $ug2 = '';
2c386f82 193 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
39b3e7de 194 foreach ($usergrps as $ug) {
195 $ug2 = $ug2. $ug->name;
196 }
dabfd0ed 197 }
39b3e7de 198 $myxls->write_string($row,3,$ug2);
348630d8 199
39b3e7de 200 $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
201 if (isset($useroption)) {
202 $myxls->write_string($row,4,format_string($useroption,true));
203 }
204 $row++;
205 }
206 $pos=4;
b0898566 207 }
b0898566 208 }
b0898566 209
210 /// Close the workbook
39b3e7de 211 $workbook->close();
b0898566 212
39b3e7de 213 exit;
214 }
215 }
bbbf2d40 216 // print text file
0468976c 217 if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
09972dd3 218 $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
0468976c 219
220 header("Content-Type: application/download\n");
221 header("Content-Disposition: attachment; filename=\"$filename\"");
222 header("Expires: 0");
223 header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
224 header("Pragma: public");
225
226 /// Print names of all the fields
64739659 227
228 echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
348630d8 229 echo get_string("group"). "\t";
64739659 230 echo get_string("choice","choice"). "\n";
0468976c 231
232 /// generate the data for the body of the spreadsheet
233 $i=0;
234 $row=1;
235 if ($users) foreach ($users as $user) {
39b3e7de 236 if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==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.
0468976c 237
238 echo $user->lastname;
239 echo "\t".$user->firstname;
240 $studentid = " ";
241 if (!empty($user->idnumber)) {
242 $studentid = $user->idnumber;
243 }
244 echo "\t". $studentid."\t";
245 $ug2 = '';
2c386f82 246 if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
0468976c 247 foreach ($usergrps as $ug) {
248 $ug2 = $ug2. $ug->name;
249 }
250 }
251 echo $ug2. "\t";
252 echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n";
253 }
254 $row++;
255 }
256 exit;
257 }
258
348630d8 259 choice_show_results($choice, $course, $cm, $format); //show table with students responses.
260
261 //now give links for downloading spreadsheets.
64739659 262 echo "<br />\n";
95aa949b 263 echo "<table class=\"downloadreport\"><tr>\n";
64739659 264 echo "<td>";
d81b7ffb 265 $options = array();
64739659 266 $options["id"] = "$cm->id";
d81b7ffb 267 $options["download"] = "ods";
268 print_single_button("report.php", $options, get_string("downloadods"));
269 echo "</td><td>";
64739659 270 $options["download"] = "xls";
271 print_single_button("report.php", $options, get_string("downloadexcel"));
272 echo "</td><td>";
273 $options["download"] = "txt";
274 print_single_button("report.php", $options, get_string("downloadtext"));
275
276 echo "</td></tr></table>";
39b3e7de 277 print_footer($course);
ec81373f 278
f9903ed0 279?>