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 | |
f9903ed0 |
108 | // Output the file as a valid Excel spreadsheet if required |
109 | |
110 | if ($type == "xls") { |
329cbe68 |
111 | require_once("$CFG->libdir/excel/Worksheet.php"); |
112 | require_once("$CFG->libdir/excel/Workbook.php"); |
113 | |
114 | header("Content-type: application/vnd.ms-excel"); |
115 | header("Content-Disposition: attachment; filename=".$survey->name.".xls"); |
116 | header("Expires: 0"); |
117 | header("Cache-Control: must-revalidate, post-check=0,pre-check=0"); |
118 | header("Pragma: public"); |
f9903ed0 |
119 | |
329cbe68 |
120 | $workbook = new Workbook("-"); |
121 | // Creating the first worksheet |
1bdcef10 |
122 | $myxls =& $workbook->add_worksheet(substr($survey->name, 0, 31)); |
f9903ed0 |
123 | |
f9903ed0 |
124 | $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes"); |
329cbe68 |
125 | $col=0; |
f9903ed0 |
126 | foreach ($header as $item) { |
329cbe68 |
127 | $myxls->write_string(0,$col++,$item); |
f9903ed0 |
128 | } |
129 | foreach ($order as $key => $qid) { |
130 | $question = $questions["$qid"]; |
ee990b19 |
131 | if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { |
329cbe68 |
132 | $myxls->write_string(0,$col++,"$question->text"); |
f9903ed0 |
133 | } |
134 | if ($question->type == "2" || $question->type == "3") { |
329cbe68 |
135 | $myxls->write_string(0,$col++,"$question->text (preferred)"); |
f9903ed0 |
136 | } |
137 | } |
138 | |
329cbe68 |
139 | // $date = $workbook->addformat(); |
140 | // $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below |
141 | |
142 | $row = 0; |
f9903ed0 |
143 | foreach ($results as $user => $rest) { |
329cbe68 |
144 | $col = 0; |
145 | $row++; |
f9903ed0 |
146 | if (! $u = get_record("user", "id", $user)) { |
147 | error("Error finding student # $user"); |
148 | } |
ebc3bd2b |
149 | if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) { |
f9903ed0 |
150 | $notes = $n->notes; |
151 | } else { |
152 | $notes = "No notes made"; |
153 | } |
329cbe68 |
154 | $myxls->write_string($row,$col++,$survey->id); |
155 | $myxls->write_string($row,$col++,$survey->name); |
156 | $myxls->write_string($row,$col++,$user); |
157 | $myxls->write_string($row,$col++,$u->firstname); |
158 | $myxls->write_string($row,$col++,$u->lastname); |
159 | $myxls->write_string($row,$col++,$u->email); |
160 | $myxls->write_string($row,$col++,$u->idnumber); |
161 | $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") ); |
162 | // $myxls->write_number($row,$col++,$results["$user"]["time"],$date); |
163 | $myxls->write_string($row,$col++,$notes); |
f9903ed0 |
164 | |
165 | foreach ($order as $key => $qid) { |
166 | $question = $questions["$qid"]; |
ee990b19 |
167 | if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { |
329cbe68 |
168 | $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] ); |
f9903ed0 |
169 | } |
170 | if ($question->type == "2" || $question->type == "3") { |
329cbe68 |
171 | $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] ); |
f9903ed0 |
172 | } |
173 | } |
174 | } |
329cbe68 |
175 | $workbook->close(); |
f9903ed0 |
176 | |
177 | exit; |
178 | } |
179 | |
180 | // Otherwise, return the text file. |
181 | |
182 | // Print header to force download |
183 | |
184 | header("Content-Type: application/download\n"); |
814ada43 |
185 | header("Content-Disposition: attachment; filename=\"$survey->name.txt\""); |
f9903ed0 |
186 | |
187 | // Print names of all the fields |
188 | |
189 | echo "surveyid surveyname userid firstname lastname email idnumber time "; |
190 | foreach ($order as $key => $qid) { |
191 | $question = $questions["$qid"]; |
ee990b19 |
192 | if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { |
f9903ed0 |
193 | echo "$question->text "; |
194 | } |
195 | if ($question->type == "2" || $question->type == "3") { |
196 | echo "$question->text (preferred) "; |
197 | } |
198 | } |
199 | echo "\n"; |
200 | |
201 | // Print all the lines of data. |
202 | |
203 | foreach ($results as $user => $rest) { |
e323955d |
204 | if (! $u = get_record("user", "id", $user)) { |
f9903ed0 |
205 | error("Error finding student # $user"); |
206 | } |
7a302afc |
207 | echo $survey->id."\t"; |
208 | echo $survey->name."\t"; |
209 | echo $user."\t"; |
210 | echo $u->firstname."\t"; |
211 | echo $u->lastname."\t"; |
212 | echo $u->email."\t"; |
213 | echo $u->idnumber."\t"; |
214 | echo userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p")."\t"; |
f9903ed0 |
215 | |
216 | foreach ($order as $key => $qid) { |
217 | $question = $questions["$qid"]; |
ee990b19 |
218 | if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") { |
f9903ed0 |
219 | echo $results["$user"]["$qid"]["answer1"]." "; |
220 | } |
221 | if ($question->type == "2" || $question->type == "3") { |
222 | echo $results["$user"]["$qid"]["answer2"]." "; |
223 | } |
224 | } |
225 | echo "\n"; |
226 | } |
227 | exit; |
228 | |
229 | |
230 | ?> |
231 | |