MDL-15109 glossary dml conversion
[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
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
24 if (! $survey = get_record("survey", "id", $cm->instance)) {
5a2a5331 25 print_error("Survey ID was incorrect");
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
87 $order = explode(",", $fullorderlist);
88 $questions = $fullquestions;
89
f762c448 90// Translate all the question texts
91
92 foreach ($questions as $key => $question) {
93 $questions[$key]->text = get_string($question->text, "survey");
94 }
95
f9903ed0 96
97// Get and collate all the results in one big array
98
99 if (! $aaa = get_records("survey_answers", "survey", "$survey->id", "time ASC")) {
5a2a5331 100 print_error("There are no answers for this survey yet.");
f9903ed0 101 }
ec81373f 102
f9903ed0 103 foreach ($aaa as $a) {
a9ccbf60 104 if (!$group or isset($users[$a->userid])) {
2d5b322c 105 if (empty($results["$a->userid"])) { // init new array
a9ccbf60 106 $results["$a->userid"]["time"] = $a->time;
107 foreach ($order as $key => $qid) {
108 $results["$a->userid"]["$qid"]["answer1"] = "";
109 $results["$a->userid"]["$qid"]["answer2"] = "";
110 }
f9903ed0 111 }
a9ccbf60 112 $results["$a->userid"]["$a->question"]["answer1"] = $a->answer1;
113 $results["$a->userid"]["$a->question"]["answer2"] = $a->answer2;
f9903ed0 114 }
f9903ed0 115 }
116
d81b7ffb 117// Output the file as a valid ODS spreadsheet if required
118
119 if ($type == "ods") {
120 require_once("$CFG->libdir/odslib.class.php");
121
122 /// Calculate file name
123 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.ods';
124 /// Creating a workbook
125 $workbook = new MoodleODSWorkbook("-");
126 /// Sending HTTP headers
127 $workbook->send($downloadfilename);
128 /// Creating the first worksheet
129 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
130
131 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
132 $col=0;
133 foreach ($header as $item) {
134 $myxls->write_string(0,$col++,$item);
135 }
136 foreach ($order as $key => $qid) {
137 $question = $questions["$qid"];
138 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
139 $myxls->write_string(0,$col++,"$question->text");
140 }
141 if ($question->type == "2" || $question->type == "3") {
142 $myxls->write_string(0,$col++,"$question->text (preferred)");
143 }
144 }
145
146// $date = $workbook->addformat();
147// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
148
149 $row = 0;
150 foreach ($results as $user => $rest) {
151 $col = 0;
152 $row++;
153 if (! $u = get_record("user", "id", $user)) {
5a2a5331 154 print_error("Error finding student # $user");
d81b7ffb 155 }
156 if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
157 $notes = $n->notes;
158 } else {
159 $notes = "No notes made";
160 }
161 $myxls->write_string($row,$col++,$survey->id);
162 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
163 $myxls->write_string($row,$col++,$user);
164 $myxls->write_string($row,$col++,$u->firstname);
165 $myxls->write_string($row,$col++,$u->lastname);
166 $myxls->write_string($row,$col++,$u->email);
167 $myxls->write_string($row,$col++,$u->idnumber);
168 $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
169// $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
170 $myxls->write_string($row,$col++,$notes);
171
172 foreach ($order as $key => $qid) {
173 $question = $questions["$qid"];
174 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
175 $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
176 }
177 if ($question->type == "2" || $question->type == "3") {
178 $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
179 }
180 }
181 }
182 $workbook->close();
183
184 exit;
185 }
186
f9903ed0 187// Output the file as a valid Excel spreadsheet if required
188
189 if ($type == "xls") {
9661a2d3 190 require_once("$CFG->libdir/excellib.class.php");
191
192 /// Calculate file name
193 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.xls';
194 /// Creating a workbook
195 $workbook = new MoodleExcelWorkbook("-");
196 /// Sending HTTP headers
197 $workbook->send($downloadfilename);
198 /// Creating the first worksheet
cd5d2d4b 199 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
f9903ed0 200
f9903ed0 201 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
329cbe68 202 $col=0;
f9903ed0 203 foreach ($header as $item) {
329cbe68 204 $myxls->write_string(0,$col++,$item);
f9903ed0 205 }
206 foreach ($order as $key => $qid) {
207 $question = $questions["$qid"];
ee990b19 208 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
329cbe68 209 $myxls->write_string(0,$col++,"$question->text");
f9903ed0 210 }
211 if ($question->type == "2" || $question->type == "3") {
329cbe68 212 $myxls->write_string(0,$col++,"$question->text (preferred)");
f9903ed0 213 }
214 }
215
329cbe68 216// $date = $workbook->addformat();
217// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
218
219 $row = 0;
f9903ed0 220 foreach ($results as $user => $rest) {
329cbe68 221 $col = 0;
222 $row++;
f9903ed0 223 if (! $u = get_record("user", "id", $user)) {
5a2a5331 224 print_error("Error finding student # $user");
f9903ed0 225 }
ebc3bd2b 226 if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
f9903ed0 227 $notes = $n->notes;
228 } else {
229 $notes = "No notes made";
230 }
329cbe68 231 $myxls->write_string($row,$col++,$survey->id);
cd5d2d4b 232 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
329cbe68 233 $myxls->write_string($row,$col++,$user);
234 $myxls->write_string($row,$col++,$u->firstname);
235 $myxls->write_string($row,$col++,$u->lastname);
236 $myxls->write_string($row,$col++,$u->email);
237 $myxls->write_string($row,$col++,$u->idnumber);
238 $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
239// $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
240 $myxls->write_string($row,$col++,$notes);
ec81373f 241
f9903ed0 242 foreach ($order as $key => $qid) {
243 $question = $questions["$qid"];
ee990b19 244 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
329cbe68 245 $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
f9903ed0 246 }
247 if ($question->type == "2" || $question->type == "3") {
329cbe68 248 $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
f9903ed0 249 }
250 }
251 }
329cbe68 252 $workbook->close();
f9903ed0 253
254 exit;
255 }
256
257// Otherwise, return the text file.
258
259// Print header to force download
260
ec81373f 261 header("Content-Type: application/download\n");
deba41af 262
cd5d2d4b 263 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true)));
deba41af 264 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
f9903ed0 265
266// Print names of all the fields
267
268 echo "surveyid surveyname userid firstname lastname email idnumber time ";
269 foreach ($order as $key => $qid) {
270 $question = $questions["$qid"];
ee990b19 271 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 272 echo "$question->text ";
273 }
274 if ($question->type == "2" || $question->type == "3") {
275 echo "$question->text (preferred) ";
276 }
277 }
278 echo "\n";
279
280// Print all the lines of data.
281
282 foreach ($results as $user => $rest) {
e323955d 283 if (! $u = get_record("user", "id", $user)) {
5a2a5331 284 print_error("Error finding student # $user");
f9903ed0 285 }
7a302afc 286 echo $survey->id."\t";
cd5d2d4b 287 echo strip_tags(format_string($survey->name,true))."\t";
7a302afc 288 echo $user."\t";
289 echo $u->firstname."\t";
290 echo $u->lastname."\t";
291 echo $u->email."\t";
292 echo $u->idnumber."\t";
293 echo userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
f9903ed0 294
295 foreach ($order as $key => $qid) {
296 $question = $questions["$qid"];
ee990b19 297 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 298 echo $results["$user"]["$qid"]["answer1"]." ";
299 }
300 if ($question->type == "2" || $question->type == "3") {
301 echo $results["$user"]["$qid"]["answer2"]." ";
302 }
303 }
304 echo "\n";
305 }
306 exit;
307
308
309?>