Integration 3 - groups in core, modules, calendar... (Part of MDL-7380, new groups)
[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);
81e956b9 20 require_capability('mod/survey:download', get_context_instance(CONTEXT_MODULE, $cm->id)) ;
f9903ed0 21
22 if (! $survey = get_record("survey", "id", $cm->instance)) {
23 error("Survey ID was incorrect");
24 }
25
839f2456 26 add_to_log($course->id, "survey", "download", "download.php?id=$cm->id&amp;type=$type", "$survey->id", $cm->id);
f9903ed0 27
a9ccbf60 28/// Check to see if groups are being used in this survey
29
30 $groupmode = groupmode($course, $cm); // Groups are being used
31
32 if ($groupmode and $group) {
a12f686b 33 $users = get_group_users($group);
a9ccbf60 34 } else {
35 $users = get_course_users($course->id);
36 $group = false;
37 }
f9903ed0 38
39// Get all the questions and their proper order
40
e323955d 41 $questions = get_records_list("survey_questions", "id", $survey->questions);
f9903ed0 42 $order = explode(",", $survey->questions);
43
8f414ab8 44 $virtualscales = false;
f9903ed0 45 foreach ($order as $key => $qid) { // Do we have virtual scales?
46 $question = $questions[$qid];
ec81373f 47 if ($question->type < 0) {
f9903ed0 48 $virtualscales = true;
49 break;
50 }
51 }
52
53 $fullorderlist = "";
54 foreach ($order as $key => $qid) { // build up list of actual questions
55 $question = $questions[$qid];
56
57 if (!(empty($fullorderlist))) {
58 $fullorderlist .= ",";
59 }
60
61 if ($question->multi) {
62 $addlist = $question->multi;
63 } else {
64 $addlist = $qid;
65 }
ec81373f 66
f9903ed0 67 if ($virtualscales && ($question->type < 0)) { // only use them
68 $fullorderlist .= $addlist;
69
70 } else if (!$virtualscales && ($question->type >= 0)){ // ignore them
71 $fullorderlist .= $addlist;
72 }
73 }
74
e323955d 75 $fullquestions = get_records_list("survey_questions", "id", $fullorderlist);
f9903ed0 76
77// Question type of multi-questions overrides the type of single questions
78 foreach ($order as $key => $qid) {
79 $question = $questions[$qid];
80
81 if ($question->multi) {
82 $subs = explode(",", $question->multi);
83 while (list ($skey, $sqid) = each ($subs)) {
84 $fullquestions["$sqid"]->type = $question->type;
85 }
86 }
87 }
88
89 $order = explode(",", $fullorderlist);
90 $questions = $fullquestions;
91
f762c448 92// Translate all the question texts
93
94 foreach ($questions as $key => $question) {
95 $questions[$key]->text = get_string($question->text, "survey");
96 }
97
f9903ed0 98
99// Get and collate all the results in one big array
100
101 if (! $aaa = get_records("survey_answers", "survey", "$survey->id", "time ASC")) {
102 error("There are no answers for this survey yet.");
103 }
ec81373f 104
f9903ed0 105 foreach ($aaa as $a) {
a9ccbf60 106 if (!$group or isset($users[$a->userid])) {
2d5b322c 107 if (empty($results["$a->userid"])) { // init new array
a9ccbf60 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"] = "";
112 }
f9903ed0 113 }
a9ccbf60 114 $results["$a->userid"]["$a->question"]["answer1"] = $a->answer1;
115 $results["$a->userid"]["$a->question"]["answer2"] = $a->answer2;
f9903ed0 116 }
f9903ed0 117 }
118
f9903ed0 119// Output the file as a valid Excel spreadsheet if required
120
121 if ($type == "xls") {
9661a2d3 122 require_once("$CFG->libdir/excellib.class.php");
123
124 /// Calculate file name
125 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.xls';
126 /// Creating a workbook
127 $workbook = new MoodleExcelWorkbook("-");
128 /// Sending HTTP headers
129 $workbook->send($downloadfilename);
130 /// Creating the first worksheet
cd5d2d4b 131 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
f9903ed0 132
f9903ed0 133 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
329cbe68 134 $col=0;
f9903ed0 135 foreach ($header as $item) {
329cbe68 136 $myxls->write_string(0,$col++,$item);
f9903ed0 137 }
138 foreach ($order as $key => $qid) {
139 $question = $questions["$qid"];
ee990b19 140 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
329cbe68 141 $myxls->write_string(0,$col++,"$question->text");
f9903ed0 142 }
143 if ($question->type == "2" || $question->type == "3") {
329cbe68 144 $myxls->write_string(0,$col++,"$question->text (preferred)");
f9903ed0 145 }
146 }
147
329cbe68 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
150
151 $row = 0;
f9903ed0 152 foreach ($results as $user => $rest) {
329cbe68 153 $col = 0;
154 $row++;
f9903ed0 155 if (! $u = get_record("user", "id", $user)) {
156 error("Error finding student # $user");
157 }
ebc3bd2b 158 if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
f9903ed0 159 $notes = $n->notes;
160 } else {
161 $notes = "No notes made";
162 }
329cbe68 163 $myxls->write_string($row,$col++,$survey->id);
cd5d2d4b 164 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
329cbe68 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);
ec81373f 173
f9903ed0 174 foreach ($order as $key => $qid) {
175 $question = $questions["$qid"];
ee990b19 176 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
329cbe68 177 $myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
f9903ed0 178 }
179 if ($question->type == "2" || $question->type == "3") {
329cbe68 180 $myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
f9903ed0 181 }
182 }
183 }
329cbe68 184 $workbook->close();
f9903ed0 185
186 exit;
187 }
188
189// Otherwise, return the text file.
190
191// Print header to force download
192
ec81373f 193 header("Content-Type: application/download\n");
deba41af 194
cd5d2d4b 195 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true)));
deba41af 196 header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
f9903ed0 197
198// Print names of all the fields
199
200 echo "surveyid surveyname userid firstname lastname email idnumber time ";
201 foreach ($order as $key => $qid) {
202 $question = $questions["$qid"];
ee990b19 203 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 204 echo "$question->text ";
205 }
206 if ($question->type == "2" || $question->type == "3") {
207 echo "$question->text (preferred) ";
208 }
209 }
210 echo "\n";
211
212// Print all the lines of data.
213
214 foreach ($results as $user => $rest) {
e323955d 215 if (! $u = get_record("user", "id", $user)) {
f9903ed0 216 error("Error finding student # $user");
217 }
7a302afc 218 echo $survey->id."\t";
cd5d2d4b 219 echo strip_tags(format_string($survey->name,true))."\t";
7a302afc 220 echo $user."\t";
221 echo $u->firstname."\t";
222 echo $u->lastname."\t";
223 echo $u->email."\t";
224 echo $u->idnumber."\t";
225 echo userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
f9903ed0 226
227 foreach ($order as $key => $qid) {
228 $question = $questions["$qid"];
ee990b19 229 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
f9903ed0 230 echo $results["$user"]["$qid"]["answer1"]." ";
231 }
232 if ($question->type == "2" || $question->type == "3") {
233 echo $results["$user"]["$qid"]["answer2"]." ";
234 }
235 }
236 echo "\n";
237 }
238 exit;
239
240
241?>
242