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 | |
33 | $questions = get_records_sql("SELECT * FROM survey_questions WHERE id in ($survey->questions)"); |
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 | |
66 | $fullquestions = get_records_sql("SELECT * FROM survey_questions WHERE id in ($fullorderlist)"); |
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 | |
83 | |
84 | // Get and collate all the results in one big array |
85 | |
86 | if (! $aaa = get_records("survey_answers", "survey", "$survey->id", "time ASC")) { |
87 | error("There are no answers for this survey yet."); |
88 | } |
89 | |
90 | foreach ($aaa as $a) { |
91 | if (!$results["$a->user"]) { // init new array |
92 | $results["$a->user"]["time"] = $a->time; |
93 | foreach ($order as $key => $qid) { |
94 | $results["$a->user"]["$qid"]["answer1"] = ""; |
95 | $results["$a->user"]["$qid"]["answer2"] = ""; |
96 | } |
97 | } |
98 | $results["$a->user"]["$a->question"]["answer1"] = $a->answer1; |
99 | $results["$a->user"]["$a->question"]["answer2"] = $a->answer2; |
100 | } |
101 | |
102 | |
103 | // Output the file as a valid Excel spreadsheet if required |
104 | |
105 | if ($type == "xls") { |
106 | include( "$CFG->libdir/psxlsgen.php" ); |
107 | |
108 | |
109 | $myxls = new PhpSimpleXlsGen(); |
110 | $myxls->totalcol = count($order) + 100; |
111 | $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes"); |
112 | $myxls->ChangePos(0,0); |
113 | foreach ($header as $item) { |
114 | $myxls->InsertText($item); |
115 | } |
116 | foreach ($order as $key => $qid) { |
117 | $question = $questions["$qid"]; |
ee990b19 |
118 | if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { |
f9903ed0 |
119 | $myxls->InsertText("$question->text"); |
120 | } |
121 | if ($question->type == "2" || $question->type == "3") { |
122 | $myxls->InsertText("$question->text (preferred)"); |
123 | } |
124 | } |
125 | |
126 | $i = 0; |
127 | foreach ($results as $user => $rest) { |
128 | $i++; |
129 | $myxls->ChangePos($i,0); |
130 | if (! $u = get_record("user", "id", $user)) { |
131 | error("Error finding student # $user"); |
132 | } |
133 | if ($n = get_record_sql("SELECT * FROM survey_analysis WHERE survey='$survey->id' AND user='$user'")) { |
134 | $notes = $n->notes; |
135 | } else { |
136 | $notes = "No notes made"; |
137 | } |
138 | $myxls->InsertText($survey->id); |
139 | $myxls->InsertText($survey->name); |
140 | $myxls->InsertText($user); |
141 | $myxls->InsertText($u->firstname); |
142 | $myxls->InsertText($u->lastname); |
143 | $myxls->InsertText($u->email); |
144 | $myxls->InsertText($u->idnumber); |
873960de |
145 | $myxls->InsertText( userdate($results["$user"]["time"], "d-M-Y h:i:s A") ); |
f9903ed0 |
146 | $myxls->InsertText($notes); |
147 | |
148 | foreach ($order as $key => $qid) { |
149 | $question = $questions["$qid"]; |
ee990b19 |
150 | if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { |
f9903ed0 |
151 | $myxls->InsertText( $results["$user"]["$qid"]["answer1"] ); |
152 | } |
153 | if ($question->type == "2" || $question->type == "3") { |
154 | $myxls->InsertText( $results["$user"]["$qid"]["answer2"] ); |
155 | } |
156 | } |
157 | } |
158 | $myxls->SendFile("surveyreport"); |
159 | |
160 | exit; |
161 | } |
162 | |
163 | // Otherwise, return the text file. |
164 | |
165 | // Print header to force download |
166 | |
167 | header("Content-Type: application/download\n"); |
168 | header("Content-Disposition: attachment; filename=\"$survey->name results.txt\""); |
169 | |
170 | // Print names of all the fields |
171 | |
172 | echo "surveyid surveyname userid firstname lastname email idnumber time "; |
173 | foreach ($order as $key => $qid) { |
174 | $question = $questions["$qid"]; |
ee990b19 |
175 | if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { |
f9903ed0 |
176 | echo "$question->text "; |
177 | } |
178 | if ($question->type == "2" || $question->type == "3") { |
179 | echo "$question->text (preferred) "; |
180 | } |
181 | } |
182 | echo "\n"; |
183 | |
184 | // Print all the lines of data. |
185 | |
186 | foreach ($results as $user => $rest) { |
187 | if (! $u = get_record_sql("SELECT firstname,lastname,email,idnumber FROM user WHERE id = '$user'")) { |
188 | error("Error finding student # $user"); |
189 | } |
190 | echo $survey->id." "; |
191 | echo $survey->name." "; |
192 | echo $user." "; |
193 | echo $u->firstname." "; |
194 | echo $u->lastname." "; |
195 | echo $u->email." "; |
196 | echo $u->idnumber." "; |
873960de |
197 | echo userdate($results["$user"]["time"], "d-M-Y h:i:s A")." "; |
f9903ed0 |
198 | |
199 | foreach ($order as $key => $qid) { |
200 | $question = $questions["$qid"]; |
ee990b19 |
201 | if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { |
f9903ed0 |
202 | echo $results["$user"]["$qid"]["answer1"]." "; |
203 | } |
204 | if ($question->type == "2" || $question->type == "3") { |
205 | echo $results["$user"]["$qid"]["answer2"]." "; |
206 | } |
207 | } |
208 | echo "\n"; |
209 | } |
210 | exit; |
211 | |
212 | |
213 | ?> |
214 | |