Excel reports now use the new libraray ... code by Russell Jungwirth - thanks!
[moodle.git] / mod / survey / download.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3 require ("../../config.php");
4
5// Check that all the parameters have been provided.
6
7 require_variable($id); // Course Module ID
8 optional_variable($type, "xls");
9
10 if (! $cm = get_record("course_modules", "id", $id)) {
11 error("Course Module ID was incorrect");
12 }
13
14 if (! $course = get_record("course", "id", $cm->course)) {
15 error("Course is misconfigured");
16 }
17
18 require_login($course->id);
19
20 if (!isteacher($course->id)) {
21 error("Sorry, only teachers can see this.");
22 }
23
24 if (! $survey = get_record("survey", "id", $cm->instance)) {
25 error("Survey ID was incorrect");
26 }
27
5eb45fa0 28 add_to_log($course->id, "survey", "download", "download.php?id=$cm->id&type=$type", "$survey->id");
f9903ed0 29
30
31// Get all the questions and their proper order
32
e323955d 33 $questions = get_records_list("survey_questions", "id", $survey->questions);
f9903ed0 34 $order = explode(",", $survey->questions);
35
36 foreach ($order as $key => $qid) { // Do we have virtual scales?
37 $question = $questions[$qid];
38 if ($question->type < 0) {
39 $virtualscales = true;
40 break;
41 }
42 }
43
44 $fullorderlist = "";
45 foreach ($order as $key => $qid) { // build up list of actual questions
46 $question = $questions[$qid];
47
48 if (!(empty($fullorderlist))) {
49 $fullorderlist .= ",";
50 }
51
52 if ($question->multi) {
53 $addlist = $question->multi;
54 } else {
55 $addlist = $qid;
56 }
57
58 if ($virtualscales && ($question->type < 0)) { // only use them
59 $fullorderlist .= $addlist;
60
61 } else if (!$virtualscales && ($question->type >= 0)){ // ignore them
62 $fullorderlist .= $addlist;
63 }
64 }
65
e323955d 66 $fullquestions = get_records_list("survey_questions", "id", $fullorderlist);
f9903ed0 67
68// Question type of multi-questions overrides the type of single questions
69 foreach ($order as $key => $qid) {
70 $question = $questions[$qid];
71
72 if ($question->multi) {
73 $subs = explode(",", $question->multi);
74 while (list ($skey, $sqid) = each ($subs)) {
75 $fullquestions["$sqid"]->type = $question->type;
76 }
77 }
78 }
79
80 $order = explode(",", $fullorderlist);
81 $questions = $fullquestions;
82
f762c448 83// Translate all the question texts
84
85 foreach ($questions as $key => $question) {
86 $questions[$key]->text = get_string($question->text, "survey");
87 }
88
f9903ed0 89
90// Get and collate all the results in one big array
91
92 if (! $aaa = get_records("survey_answers", "survey", "$survey->id", "time ASC")) {
93 error("There are no answers for this survey yet.");
94 }
95
96 foreach ($aaa as $a) {
ebc3bd2b 97 if (!$results["$a->userid"]) { // init new array
98 $results["$a->userid"]["time"] = $a->time;
f9903ed0 99 foreach ($order as $key => $qid) {
ebc3bd2b 100 $results["$a->userid"]["$qid"]["answer1"] = "";
101 $results["$a->userid"]["$qid"]["answer2"] = "";
f9903ed0 102 }
103 }
ebc3bd2b 104 $results["$a->userid"]["$a->question"]["answer1"] = $a->answer1;
105 $results["$a->userid"]["$a->question"]["answer2"] = $a->answer2;
f9903ed0 106 }
107
108
109// Output the file as a valid Excel spreadsheet if required
110
111 if ($type == "xls") {
b0e3a925 112 require_once( "$CFG->libdir/psxlsgen.php" );
f9903ed0 113
114
115 $myxls = new PhpSimpleXlsGen();
116 $myxls->totalcol = count($order) + 100;
117 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
118 $myxls->ChangePos(0,0);
119 foreach ($header as $item) {
120 $myxls->InsertText($item);
121 }
122 foreach ($order as $key => $qid) {
123 $question = $questions["$qid"];
ee990b19 124 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 125 $myxls->InsertText("$question->text");
126 }
127 if ($question->type == "2" || $question->type == "3") {
128 $myxls->InsertText("$question->text (preferred)");
129 }
130 }
131
132 $i = 0;
133 foreach ($results as $user => $rest) {
134 $i++;
135 $myxls->ChangePos($i,0);
136 if (! $u = get_record("user", "id", $user)) {
137 error("Error finding student # $user");
138 }
ebc3bd2b 139 if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
f9903ed0 140 $notes = $n->notes;
141 } else {
142 $notes = "No notes made";
143 }
144 $myxls->InsertText($survey->id);
145 $myxls->InsertText($survey->name);
146 $myxls->InsertText($user);
147 $myxls->InsertText($u->firstname);
148 $myxls->InsertText($u->lastname);
149 $myxls->InsertText($u->email);
150 $myxls->InsertText($u->idnumber);
7a302afc 151 $myxls->InsertText( userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
f9903ed0 152 $myxls->InsertText($notes);
153
154 foreach ($order as $key => $qid) {
155 $question = $questions["$qid"];
ee990b19 156 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 157 $myxls->InsertText( $results["$user"]["$qid"]["answer1"] );
158 }
159 if ($question->type == "2" || $question->type == "3") {
160 $myxls->InsertText( $results["$user"]["$qid"]["answer2"] );
161 }
162 }
163 }
814ada43 164 $myxls->SendFileName("$survey->name");
f9903ed0 165
166 exit;
167 }
168
169// Otherwise, return the text file.
170
171// Print header to force download
172
173 header("Content-Type: application/download\n");
814ada43 174 header("Content-Disposition: attachment; filename=\"$survey->name.txt\"");
f9903ed0 175
176// Print names of all the fields
177
178 echo "surveyid surveyname userid firstname lastname email idnumber time ";
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") {
f9903ed0 182 echo "$question->text ";
183 }
184 if ($question->type == "2" || $question->type == "3") {
185 echo "$question->text (preferred) ";
186 }
187 }
188 echo "\n";
189
190// Print all the lines of data.
191
192 foreach ($results as $user => $rest) {
e323955d 193 if (! $u = get_record("user", "id", $user)) {
f9903ed0 194 error("Error finding student # $user");
195 }
7a302afc 196 echo $survey->id."\t";
197 echo $survey->name."\t";
198 echo $user."\t";
199 echo $u->firstname."\t";
200 echo $u->lastname."\t";
201 echo $u->email."\t";
202 echo $u->idnumber."\t";
203 echo userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
f9903ed0 204
205 foreach ($order as $key => $qid) {
206 $question = $questions["$qid"];
ee990b19 207 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 208 echo $results["$user"]["$qid"]["answer1"]." ";
209 }
210 if ($question->type == "2" || $question->type == "3") {
211 echo $results["$user"]["$qid"]["answer2"]." ";
212 }
213 }
214 echo "\n";
215 }
216 exit;
217
218
219?>
220