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