Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / survey / download.php
blobbe32352dc7e28098bf8ec242f9520441cfcdf6d5
1 <?php // $Id$
3 require ("../../config.php");
5 // Check that all the parameters have been provided.
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);
11 if (! $cm = get_record("course_modules", "id", $id)) {
12 error("Course Module ID was incorrect");
15 if (! $course = get_record("course", "id", $cm->course)) {
16 error("Course is misconfigured");
19 require_login($course->id, false);
20 require_capability('mod/survey:download', get_context_instance(CONTEXT_MODULE, $cm->id)) ;
22 if (! $survey = get_record("survey", "id", $cm->instance)) {
23 error("Survey ID was incorrect");
26 add_to_log($course->id, "survey", "download", "download.php?id=$cm->id&amp;type=$type", "$survey->id", $cm->id);
28 /// Check to see if groups are being used in this survey
30 $groupmode = groupmode($course, $cm); // Groups are being used
32 if ($groupmode and $group) {
33 $users = get_group_users($group);
34 } else {
35 $users = get_course_users($course->id);
36 $group = false;
39 // Get all the questions and their proper order
41 $questions = get_records_list("survey_questions", "id", $survey->questions);
42 $order = explode(",", $survey->questions);
44 $virtualscales = false;
45 foreach ($order as $key => $qid) { // Do we have virtual scales?
46 $question = $questions[$qid];
47 if ($question->type < 0) {
48 $virtualscales = true;
49 break;
53 $fullorderlist = "";
54 foreach ($order as $key => $qid) { // build up list of actual questions
55 $question = $questions[$qid];
57 if (!(empty($fullorderlist))) {
58 $fullorderlist .= ",";
61 if ($question->multi) {
62 $addlist = $question->multi;
63 } else {
64 $addlist = $qid;
67 if ($virtualscales && ($question->type < 0)) { // only use them
68 $fullorderlist .= $addlist;
70 } else if (!$virtualscales && ($question->type >= 0)){ // ignore them
71 $fullorderlist .= $addlist;
75 $fullquestions = get_records_list("survey_questions", "id", $fullorderlist);
77 // Question type of multi-questions overrides the type of single questions
78 foreach ($order as $key => $qid) {
79 $question = $questions[$qid];
81 if ($question->multi) {
82 $subs = explode(",", $question->multi);
83 while (list ($skey, $sqid) = each ($subs)) {
84 $fullquestions["$sqid"]->type = $question->type;
89 $order = explode(",", $fullorderlist);
90 $questions = $fullquestions;
92 // Translate all the question texts
94 foreach ($questions as $key => $question) {
95 $questions[$key]->text = get_string($question->text, "survey");
99 // Get and collate all the results in one big array
101 if (! $aaa = get_records("survey_answers", "survey", "$survey->id", "time ASC")) {
102 error("There are no answers for this survey yet.");
105 foreach ($aaa as $a) {
106 if (!$group or isset($users[$a->userid])) {
107 if (empty($results["$a->userid"])) { // init new array
108 $results["$a->userid"]["time"] = $a->time;
109 foreach ($order as $key => $qid) {
110 $results["$a->userid"]["$qid"]["answer1"] = "";
111 $results["$a->userid"]["$qid"]["answer2"] = "";
114 $results["$a->userid"]["$a->question"]["answer1"] = $a->answer1;
115 $results["$a->userid"]["$a->question"]["answer2"] = $a->answer2;
119 // Output the file as a valid ODS spreadsheet if required
121 if ($type == "ods") {
122 require_once("$CFG->libdir/odslib.class.php");
124 /// Calculate file name
125 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.ods';
126 /// Creating a workbook
127 $workbook = new MoodleODSWorkbook("-");
128 /// Sending HTTP headers
129 $workbook->send($downloadfilename);
130 /// Creating the first worksheet
131 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
133 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
134 $col=0;
135 foreach ($header as $item) {
136 $myxls->write_string(0,$col++,$item);
138 foreach ($order as $key => $qid) {
139 $question = $questions["$qid"];
140 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
141 $myxls->write_string(0,$col++,"$question->text");
143 if ($question->type == "2" || $question->type == "3") {
144 $myxls->write_string(0,$col++,"$question->text (preferred)");
148 // $date = $workbook->addformat();
149 // $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
151 $row = 0;
152 foreach ($results as $user => $rest) {
153 $col = 0;
154 $row++;
155 if (! $u = get_record("user", "id", $user)) {
156 error("Error finding student # $user");
158 if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
159 $notes = $n->notes;
160 } else {
161 $notes = "No notes made";
163 $myxls->write_string($row,$col++,$survey->id);
164 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
165 $myxls->write_string($row,$col++,$user);
166 $myxls->write_string($row,$col++,$u->firstname);
167 $myxls->write_string($row,$col++,$u->lastname);
168 $myxls->write_string($row,$col++,$u->email);
169 $myxls->write_string($row,$col++,$u->idnumber);
170 $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
171 // $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
172 $myxls->write_string($row,$col++,$notes);
174 foreach ($order as $key => $qid) {
175 $question = $questions["$qid"];
176 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
177 $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
179 if ($question->type == "2" || $question->type == "3") {
180 $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
184 $workbook->close();
186 exit;
189 // Output the file as a valid Excel spreadsheet if required
191 if ($type == "xls") {
192 require_once("$CFG->libdir/excellib.class.php");
194 /// Calculate file name
195 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.xls';
196 /// Creating a workbook
197 $workbook = new MoodleExcelWorkbook("-");
198 /// Sending HTTP headers
199 $workbook->send($downloadfilename);
200 /// Creating the first worksheet
201 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
203 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
204 $col=0;
205 foreach ($header as $item) {
206 $myxls->write_string(0,$col++,$item);
208 foreach ($order as $key => $qid) {
209 $question = $questions["$qid"];
210 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
211 $myxls->write_string(0,$col++,"$question->text");
213 if ($question->type == "2" || $question->type == "3") {
214 $myxls->write_string(0,$col++,"$question->text (preferred)");
218 // $date = $workbook->addformat();
219 // $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
221 $row = 0;
222 foreach ($results as $user => $rest) {
223 $col = 0;
224 $row++;
225 if (! $u = get_record("user", "id", $user)) {
226 error("Error finding student # $user");
228 if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
229 $notes = $n->notes;
230 } else {
231 $notes = "No notes made";
233 $myxls->write_string($row,$col++,$survey->id);
234 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
235 $myxls->write_string($row,$col++,$user);
236 $myxls->write_string($row,$col++,$u->firstname);
237 $myxls->write_string($row,$col++,$u->lastname);
238 $myxls->write_string($row,$col++,$u->email);
239 $myxls->write_string($row,$col++,$u->idnumber);
240 $myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
241 // $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
242 $myxls->write_string($row,$col++,$notes);
244 foreach ($order as $key => $qid) {
245 $question = $questions["$qid"];
246 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
247 $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
249 if ($question->type == "2" || $question->type == "3") {
250 $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
254 $workbook->close();
256 exit;
259 // Otherwise, return the text file.
261 // Print header to force download
263 header("Content-Type: application/download\n");
265 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true)));
266 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
268 // Print names of all the fields
270 echo "surveyid surveyname userid firstname lastname email idnumber time ";
271 foreach ($order as $key => $qid) {
272 $question = $questions["$qid"];
273 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
274 echo "$question->text ";
276 if ($question->type == "2" || $question->type == "3") {
277 echo "$question->text (preferred) ";
280 echo "\n";
282 // Print all the lines of data.
284 foreach ($results as $user => $rest) {
285 if (! $u = get_record("user", "id", $user)) {
286 error("Error finding student # $user");
288 echo $survey->id."\t";
289 echo strip_tags(format_string($survey->name,true))."\t";
290 echo $user."\t";
291 echo $u->firstname."\t";
292 echo $u->lastname."\t";
293 echo $u->email."\t";
294 echo $u->idnumber."\t";
295 echo userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
297 foreach ($order as $key => $qid) {
298 $question = $questions["$qid"];
299 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
300 echo $results["$user"]["$qid"]["answer1"]." ";
302 if ($question->type == "2" || $question->type == "3") {
303 echo $results["$user"]["$qid"]["answer2"]." ";
306 echo "\n";
308 exit;