MDL-14980 missing strsurveysaved when saving
[moodle.git] / mod / survey / download.php
CommitLineData
d1290cec 1<?php // $Id$
f9903ed0 2
3 require ("../../config.php");
4
5// Check that all the parameters have been provided.
6
2d5b322c 7 $id = required_param('id', PARAM_INT); // Course Module ID
8 $type = optional_param('type', 'xls', PARAM_ALPHA);
9 $group = optional_param('group', 0, PARAM_INT);
f9903ed0 10
11 if (! $cm = get_record("course_modules", "id", $id)) {
5a2a5331 12 print_error("Course Module ID was incorrect");
f9903ed0 13 }
14
15 if (! $course = get_record("course", "id", $cm->course)) {
5a2a5331 16 print_error("Course is misconfigured");
f9903ed0 17 }
18
bb5b7224 19 require_login($course->id, false, $cm);
81e956b9 20 require_capability('mod/survey:download', get_context_instance(CONTEXT_MODULE, $cm->id)) ;
f9903ed0 21
22 if (! $survey = get_record("survey", "id", $cm->instance)) {
5a2a5331 23 print_error("Survey ID was incorrect");
f9903ed0 24 }
25
839f2456 26 add_to_log($course->id, "survey", "download", "download.php?id=$cm->id&amp;type=$type", "$survey->id", $cm->id);
f9903ed0 27
a9ccbf60 28/// Check to see if groups are being used in this survey
29
07aa2057 30 $groupmode = groups_get_activity_groupmode($cm); // Groups are being used
a9ccbf60 31
32 if ($groupmode and $group) {
07aa2057 33 $users = groups_get_members($group);
a9ccbf60 34 } else {
35 $users = get_course_users($course->id);
36 $group = false;
37 }
f9903ed0 38
39// Get all the questions and their proper order
40
e323955d 41 $questions = get_records_list("survey_questions", "id", $survey->questions);
f9903ed0 42 $order = explode(",", $survey->questions);
43
8f414ab8 44 $virtualscales = false;
f9903ed0 45 foreach ($order as $key => $qid) { // Do we have virtual scales?
46 $question = $questions[$qid];
ec81373f 47 if ($question->type < 0) {
f9903ed0 48 $virtualscales = true;
49 break;
50 }
51 }
52
53 $fullorderlist = "";
54 foreach ($order as $key => $qid) { // build up list of actual questions
55 $question = $questions[$qid];
56
57 if (!(empty($fullorderlist))) {
58 $fullorderlist .= ",";
59 }
60
61 if ($question->multi) {
62 $addlist = $question->multi;
63 } else {
64 $addlist = $qid;
65 }
ec81373f 66
f9903ed0 67 if ($virtualscales && ($question->type < 0)) { // only use them
68 $fullorderlist .= $addlist;
69
70 } else if (!$virtualscales && ($question->type >= 0)){ // ignore them
71 $fullorderlist .= $addlist;
72 }
73 }
74
e323955d 75 $fullquestions = get_records_list("survey_questions", "id", $fullorderlist);
f9903ed0 76
77// Question type of multi-questions overrides the type of single questions
78 foreach ($order as $key => $qid) {
79 $question = $questions[$qid];
80
81 if ($question->multi) {
82 $subs = explode(",", $question->multi);
83 while (list ($skey, $sqid) = each ($subs)) {
84 $fullquestions["$sqid"]->type = $question->type;
85 }
86 }
87 }
88
89 $order = explode(",", $fullorderlist);
90 $questions = $fullquestions;
91
f762c448 92// Translate all the question texts
93
94 foreach ($questions as $key => $question) {
95 $questions[$key]->text = get_string($question->text, "survey");
96 }
97
f9903ed0 98
99// Get and collate all the results in one big array
100
101 if (! $aaa = get_records("survey_answers", "survey", "$survey->id", "time ASC")) {
5a2a5331 102 print_error("There are no answers for this survey yet.");
f9903ed0 103 }
ec81373f 104
f9903ed0 105 foreach ($aaa as $a) {
a9ccbf60 106 if (!$group or isset($users[$a->userid])) {
2d5b322c 107 if (empty($results["$a->userid"])) { // init new array
a9ccbf60 108 $results["$a->userid"]["time"] = $a->time;
109 foreach ($order as $key => $qid) {
110 $results["$a->userid"]["$qid"]["answer1"] = "";
111 $results["$a->userid"]["$qid"]["answer2"] = "";
112 }
f9903ed0 113 }
a9ccbf60 114 $results["$a->userid"]["$a->question"]["answer1"] = $a->answer1;
115 $results["$a->userid"]["$a->question"]["answer2"] = $a->answer2;
f9903ed0 116 }
f9903ed0 117 }
118
d81b7ffb 119// Output the file as a valid ODS spreadsheet if required
120
121 if ($type == "ods") {
122 require_once("$CFG->libdir/odslib.class.php");
123
124 /// Calculate file name
125 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.ods';
126 /// Creating a workbook
127 $workbook = new MoodleODSWorkbook("-");
128 /// Sending HTTP headers
129 $workbook->send($downloadfilename);
130 /// Creating the first worksheet
131 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
132
133 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
134 $col=0;
135 foreach ($header as $item) {
136 $myxls->write_string(0,$col++,$item);
137 }
138 foreach ($order as $key => $qid) {
139 $question = $questions["$qid"];
140 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
141 $myxls->write_string(0,$col++,"$question->text");
142 }
143 if ($question->type == "2" || $question->type == "3") {
144 $myxls->write_string(0,$col++,"$question->text (preferred)");
145 }
146 }
147
148// $date = $workbook->addformat();
149// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
150
151 $row = 0;
152 foreach ($results as $user => $rest) {
153 $col = 0;
154 $row++;
155 if (! $u = get_record("user", "id", $user)) {
5a2a5331 156 print_error("Error finding student # $user");
d81b7ffb 157 }
158 if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
159 $notes = $n->notes;
160 } else {
161 $notes = "No notes made";
162 }
163 $myxls->write_string($row,$col++,$survey->id);
164 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
165 $myxls->write_string($row,$col++,$user);
166 $myxls->write_string($row,$col++,$u->firstname);
167 $myxls->write_string($row,$col++,$u->lastname);
168 $myxls->write_string($row,$col++,$u->email);
169 $myxls->write_string($row,$col++,$u->idnumber);
170 $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
171// $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
172 $myxls->write_string($row,$col++,$notes);
173
174 foreach ($order as $key => $qid) {
175 $question = $questions["$qid"];
176 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
177 $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
178 }
179 if ($question->type == "2" || $question->type == "3") {
180 $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
181 }
182 }
183 }
184 $workbook->close();
185
186 exit;
187 }
188
f9903ed0 189// Output the file as a valid Excel spreadsheet if required
190
191 if ($type == "xls") {
9661a2d3 192 require_once("$CFG->libdir/excellib.class.php");
193
194 /// Calculate file name
195 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.xls';
196 /// Creating a workbook
197 $workbook = new MoodleExcelWorkbook("-");
198 /// Sending HTTP headers
199 $workbook->send($downloadfilename);
200 /// Creating the first worksheet
cd5d2d4b 201 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
f9903ed0 202
f9903ed0 203 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
329cbe68 204 $col=0;
f9903ed0 205 foreach ($header as $item) {
329cbe68 206 $myxls->write_string(0,$col++,$item);
f9903ed0 207 }
208 foreach ($order as $key => $qid) {
209 $question = $questions["$qid"];
ee990b19 210 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
329cbe68 211 $myxls->write_string(0,$col++,"$question->text");
f9903ed0 212 }
213 if ($question->type == "2" || $question->type == "3") {
329cbe68 214 $myxls->write_string(0,$col++,"$question->text (preferred)");
f9903ed0 215 }
216 }
217
329cbe68 218// $date = $workbook->addformat();
219// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
220
221 $row = 0;
f9903ed0 222 foreach ($results as $user => $rest) {
329cbe68 223 $col = 0;
224 $row++;
f9903ed0 225 if (! $u = get_record("user", "id", $user)) {
5a2a5331 226 print_error("Error finding student # $user");
f9903ed0 227 }
ebc3bd2b 228 if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
f9903ed0 229 $notes = $n->notes;
230 } else {
231 $notes = "No notes made";
232 }
329cbe68 233 $myxls->write_string($row,$col++,$survey->id);
cd5d2d4b 234 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
329cbe68 235 $myxls->write_string($row,$col++,$user);
236 $myxls->write_string($row,$col++,$u->firstname);
237 $myxls->write_string($row,$col++,$u->lastname);
238 $myxls->write_string($row,$col++,$u->email);
239 $myxls->write_string($row,$col++,$u->idnumber);
240 $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
241// $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
242 $myxls->write_string($row,$col++,$notes);
ec81373f 243
f9903ed0 244 foreach ($order as $key => $qid) {
245 $question = $questions["$qid"];
ee990b19 246 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
329cbe68 247 $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
f9903ed0 248 }
249 if ($question->type == "2" || $question->type == "3") {
329cbe68 250 $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
f9903ed0 251 }
252 }
253 }
329cbe68 254 $workbook->close();
f9903ed0 255
256 exit;
257 }
258
259// Otherwise, return the text file.
260
261// Print header to force download
262
ec81373f 263 header("Content-Type: application/download\n");
deba41af 264
cd5d2d4b 265 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true)));
deba41af 266 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
f9903ed0 267
268// Print names of all the fields
269
270 echo "surveyid surveyname userid firstname lastname email idnumber time ";
271 foreach ($order as $key => $qid) {
272 $question = $questions["$qid"];
ee990b19 273 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 274 echo "$question->text ";
275 }
276 if ($question->type == "2" || $question->type == "3") {
277 echo "$question->text (preferred) ";
278 }
279 }
280 echo "\n";
281
282// Print all the lines of data.
283
284 foreach ($results as $user => $rest) {
e323955d 285 if (! $u = get_record("user", "id", $user)) {
5a2a5331 286 print_error("Error finding student # $user");
f9903ed0 287 }
7a302afc 288 echo $survey->id."\t";
cd5d2d4b 289 echo strip_tags(format_string($survey->name,true))."\t";
7a302afc 290 echo $user."\t";
291 echo $u->firstname."\t";
292 echo $u->lastname."\t";
293 echo $u->email."\t";
294 echo $u->idnumber."\t";
295 echo userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
f9903ed0 296
297 foreach ($order as $key => $qid) {
298 $question = $questions["$qid"];
ee990b19 299 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 300 echo $results["$user"]["$qid"]["answer1"]." ";
301 }
302 if ($question->type == "2" || $question->type == "3") {
303 echo $results["$user"]["$qid"]["answer2"]." ";
304 }
305 }
306 echo "\n";
307 }
308 exit;
309
310
311?>