More uses for current_charset()
[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)) {
12 error("Course Module ID was incorrect");
13 }
14
15 if (! $course = get_record("course", "id", $cm->course)) {
16 error("Course is misconfigured");
17 }
18
ec81373f 19 require_login($course->id, false);
f9903ed0 20
21 if (!isteacher($course->id)) {
22 error("Sorry, only teachers can see this.");
23 }
24
25 if (! $survey = get_record("survey", "id", $cm->instance)) {
26 error("Survey ID was incorrect");
27 }
28
839f2456 29 add_to_log($course->id, "survey", "download", "download.php?id=$cm->id&amp;type=$type", "$survey->id", $cm->id);
f9903ed0 30
a9ccbf60 31/// Check to see if groups are being used in this survey
32
33 $groupmode = groupmode($course, $cm); // Groups are being used
34
35 if ($groupmode and $group) {
a12f686b 36 $users = get_group_users($group);
a9ccbf60 37 } else {
38 $users = get_course_users($course->id);
39 $group = false;
40 }
f9903ed0 41
42// Get all the questions and their proper order
43
e323955d 44 $questions = get_records_list("survey_questions", "id", $survey->questions);
f9903ed0 45 $order = explode(",", $survey->questions);
46
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
55 $fullorderlist = "";
56 foreach ($order as $key => $qid) { // build up list of actual questions
57 $question = $questions[$qid];
58
59 if (!(empty($fullorderlist))) {
60 $fullorderlist .= ",";
61 }
62
63 if ($question->multi) {
64 $addlist = $question->multi;
65 } else {
66 $addlist = $qid;
67 }
ec81373f 68
f9903ed0 69 if ($virtualscales && ($question->type < 0)) { // only use them
70 $fullorderlist .= $addlist;
71
72 } else if (!$virtualscales && ($question->type >= 0)){ // ignore them
73 $fullorderlist .= $addlist;
74 }
75 }
76
e323955d 77 $fullquestions = get_records_list("survey_questions", "id", $fullorderlist);
f9903ed0 78
79// Question type of multi-questions overrides the type of single questions
80 foreach ($order as $key => $qid) {
81 $question = $questions[$qid];
82
83 if ($question->multi) {
84 $subs = explode(",", $question->multi);
85 while (list ($skey, $sqid) = each ($subs)) {
86 $fullquestions["$sqid"]->type = $question->type;
87 }
88 }
89 }
90
91 $order = explode(",", $fullorderlist);
92 $questions = $fullquestions;
93
f762c448 94// Translate all the question texts
95
96 foreach ($questions as $key => $question) {
97 $questions[$key]->text = get_string($question->text, "survey");
98 }
99
f9903ed0 100
101// Get and collate all the results in one big array
102
103 if (! $aaa = get_records("survey_answers", "survey", "$survey->id", "time ASC")) {
104 error("There are no answers for this survey yet.");
105 }
ec81373f 106
f9903ed0 107 foreach ($aaa as $a) {
a9ccbf60 108 if (!$group or isset($users[$a->userid])) {
2d5b322c 109 if (empty($results["$a->userid"])) { // init new array
a9ccbf60 110 $results["$a->userid"]["time"] = $a->time;
111 foreach ($order as $key => $qid) {
112 $results["$a->userid"]["$qid"]["answer1"] = "";
113 $results["$a->userid"]["$qid"]["answer2"] = "";
114 }
f9903ed0 115 }
a9ccbf60 116 $results["$a->userid"]["$a->question"]["answer1"] = $a->answer1;
117 $results["$a->userid"]["$a->question"]["answer2"] = $a->answer2;
f9903ed0 118 }
f9903ed0 119 }
120
f9903ed0 121// Output the file as a valid Excel spreadsheet if required
122
123 if ($type == "xls") {
329cbe68 124 require_once("$CFG->libdir/excel/Worksheet.php");
125 require_once("$CFG->libdir/excel/Workbook.php");
126
127 header("Content-type: application/vnd.ms-excel");
cd5d2d4b 128 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true)));
deba41af 129 header("Content-Disposition: attachment; filename=$downloadfilename.xls");
329cbe68 130 header("Expires: 0");
131 header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
132 header("Pragma: public");
f9903ed0 133
329cbe68 134 $workbook = new Workbook("-");
135 // Creating the first worksheet
cd5d2d4b 136 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
f9903ed0 137
f9903ed0 138 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
329cbe68 139 $col=0;
f9903ed0 140 foreach ($header as $item) {
329cbe68 141 $myxls->write_string(0,$col++,$item);
f9903ed0 142 }
143 foreach ($order as $key => $qid) {
144 $question = $questions["$qid"];
ee990b19 145 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
329cbe68 146 $myxls->write_string(0,$col++,"$question->text");
f9903ed0 147 }
148 if ($question->type == "2" || $question->type == "3") {
329cbe68 149 $myxls->write_string(0,$col++,"$question->text (preferred)");
f9903ed0 150 }
151 }
152
329cbe68 153// $date = $workbook->addformat();
154// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
155
156 $row = 0;
f9903ed0 157 foreach ($results as $user => $rest) {
329cbe68 158 $col = 0;
159 $row++;
f9903ed0 160 if (! $u = get_record("user", "id", $user)) {
161 error("Error finding student # $user");
162 }
ebc3bd2b 163 if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
f9903ed0 164 $notes = $n->notes;
165 } else {
166 $notes = "No notes made";
167 }
329cbe68 168 $myxls->write_string($row,$col++,$survey->id);
cd5d2d4b 169 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
329cbe68 170 $myxls->write_string($row,$col++,$user);
171 $myxls->write_string($row,$col++,$u->firstname);
172 $myxls->write_string($row,$col++,$u->lastname);
173 $myxls->write_string($row,$col++,$u->email);
174 $myxls->write_string($row,$col++,$u->idnumber);
175 $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
176// $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
177 $myxls->write_string($row,$col++,$notes);
ec81373f 178
f9903ed0 179 foreach ($order as $key => $qid) {
180 $question = $questions["$qid"];
ee990b19 181 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
329cbe68 182 $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
f9903ed0 183 }
184 if ($question->type == "2" || $question->type == "3") {
329cbe68 185 $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
f9903ed0 186 }
187 }
188 }
329cbe68 189 $workbook->close();
f9903ed0 190
191 exit;
192 }
193
194// Otherwise, return the text file.
195
196// Print header to force download
197
ec81373f 198 header("Content-Type: application/download\n");
deba41af 199
cd5d2d4b 200 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true)));
deba41af 201 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
f9903ed0 202
203// Print names of all the fields
204
205 echo "surveyid surveyname userid firstname lastname email idnumber time ";
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") {
f9903ed0 209 echo "$question->text ";
210 }
211 if ($question->type == "2" || $question->type == "3") {
212 echo "$question->text (preferred) ";
213 }
214 }
215 echo "\n";
216
217// Print all the lines of data.
218
219 foreach ($results as $user => $rest) {
e323955d 220 if (! $u = get_record("user", "id", $user)) {
f9903ed0 221 error("Error finding student # $user");
222 }
7a302afc 223 echo $survey->id."\t";
cd5d2d4b 224 echo strip_tags(format_string($survey->name,true))."\t";
7a302afc 225 echo $user."\t";
226 echo $u->firstname."\t";
227 echo $u->lastname."\t";
228 echo $u->email."\t";
229 echo $u->idnumber."\t";
230 echo userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
f9903ed0 231
232 foreach ($order as $key => $qid) {
233 $question = $questions["$qid"];
ee990b19 234 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 235 echo $results["$user"]["$qid"]["answer1"]." ";
236 }
237 if ($question->type == "2" || $question->type == "3") {
238 echo $results["$user"]["$qid"]["answer2"]." ";
239 }
240 }
241 echo "\n";
242 }
243 exit;
244
245
246?>
247