MDL-44182 mod_survey: unified @package use
[moodle.git] / mod / survey / download.php
CommitLineData
a5cb6242 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file is responsible for producing the downloadable versions of a survey
20 * module.
21 *
da9bddbe 22 * @package mod_survey
a5cb6242 23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once ("../../config.php");
f9903ed0 28
29// Check that all the parameters have been provided.
30
a5cb6242 31$id = required_param('id', PARAM_INT); // Course Module ID
32$type = optional_param('type', 'xls', PARAM_ALPHA);
33$group = optional_param('group', 0, PARAM_INT);
f9903ed0 34
a5cb6242 35if (! $cm = get_coursemodule_from_id('survey', $id)) {
36 print_error('invalidcoursemodule');
37}
f9903ed0 38
a5cb6242 39if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
40 print_error('coursemisconf');
41}
f9903ed0 42
1c969116 43$context = context_module::instance($cm->id);
3c268f25 44
a6855934 45$PAGE->set_url('/mod/survey/download.php', array('id'=>$id, 'type'=>$type, 'group'=>$group));
f9903ed0 46
cdbea7ee 47require_login($course, false, $cm);
a5cb6242 48require_capability('mod/survey:download', $context) ;
f9903ed0 49
a5cb6242 50if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
51 print_error('invalidsurveyid', 'survey');
52}
f9903ed0 53
a5cb6242 54add_to_log($course->id, "survey", "download", $PAGE->url->out(), "$survey->id", $cm->id);
a9ccbf60 55
a5cb6242 56/// Check to see if groups are being used in this survey
f9903ed0 57
a5cb6242 58$groupmode = groups_get_activity_groupmode($cm); // Groups are being used
f9903ed0 59
a5cb6242 60if ($groupmode and $group) {
61 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $group, null, false);
62} else {
63 $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false);
64 $group = false;
65}
f9903ed0 66
a5cb6242 67// The order of the questions
68$order = explode(",", $survey->questions);
f9903ed0 69
a5cb6242 70// Get the actual questions from the database
71$questions = $DB->get_records_list("survey_questions", "id", $order);
f9903ed0 72
a5cb6242 73// Get an ordered array of questions
74$orderedquestions = array();
ec81373f 75
a5cb6242 76$virtualscales = false;
77foreach ($order as $qid) {
78 $orderedquestions[$qid] = $questions[$qid];
79 // Check if this question is using virtual scales
80 if (!$virtualscales && $questions[$qid]->type < 0) {
81 $virtualscales = true;
f9903ed0 82 }
a5cb6242 83}
3ce4270a 84$nestedorder = array();//will contain the subquestions attached to the main questions
a5cb6242 85$preparray = array();
3ce4270a 86
a5cb6242 87foreach ($orderedquestions as $qid=>$question) {
3ce4270a 88 //$orderedquestions[$qid]->text = get_string($question->text, "survey");
a5cb6242 89 if (!empty($question->multi)) {
3ce4270a
AD
90 $actualqids = explode(",", $questions[$qid]->multi);
91 foreach ($actualqids as $subqid) {
a5cb6242 92 if (!empty($orderedquestions[$subqid]->type)) {
93 $orderedquestions[$subqid]->type = $questions[$qid]->type;
f9903ed0 94 }
95 }
a5cb6242 96 } else {
3ce4270a 97 $actualqids = array($qid);
f9903ed0 98 }
a5cb6242 99 if ($virtualscales && $questions[$qid]->type < 0) {
3ce4270a 100 $nestedorder[$qid] = $actualqids;
a5cb6242 101 } else if (!$virtualscales && $question->type >= 0) {
3ce4270a
AD
102 $nestedorder[$qid] = $actualqids;
103 } else {
104 //todo andrew this was added by me. Is it correct?
105 $nestedorder[$qid] = array();
f762c448 106 }
a5cb6242 107}
3ce4270a 108
a5cb6242 109$reversednestedorder = array();
110foreach ($nestedorder as $qid=>$subqidarray) {
111 foreach ($subqidarray as $subqui) {
112 $reversednestedorder[$subqui] = $qid;
113 }
114}
f9903ed0 115
3ce4270a
AD
116//need to get info on the sub-questions from the db and merge the arrays of questions
117$allquestions = array_merge($questions, $DB->get_records_list("survey_questions", "id", array_keys($reversednestedorder)));
118
119//array_merge() messes up the keys so reinstate them
120$questions = array();
121foreach($allquestions as $question) {
122 $questions[$question->id] = $question;
123
124 //while were iterating over the questions get the question text
125 $questions[$question->id]->text = get_string($questions[$question->id]->text, "survey");
126}
127unset($allquestions);
128
f9903ed0 129// Get and collate all the results in one big array
a5cb6242 130if (! $surveyanswers = $DB->get_records("survey_answers", array("survey"=>$survey->id), "time ASC")) {
131 print_error('cannotfindanswer', 'survey');
132}
f9903ed0 133
a5cb6242 134$results = array();
ec81373f 135
a5cb6242 136foreach ($surveyanswers as $surveyanswer) {
137 if (!$group || isset($users[$surveyanswer->userid])) {
3ce4270a
AD
138 //$questionid = $reversednestedorder[$surveyanswer->question];
139 $questionid = $surveyanswer->question;
a5cb6242 140 if (!array_key_exists($surveyanswer->userid, $results)) {
141 $results[$surveyanswer->userid] = array('time'=>$surveyanswer->time);
f9903ed0 142 }
a5cb6242 143 $results[$surveyanswer->userid][$questionid]['answer1'] = $surveyanswer->answer1;
144 $results[$surveyanswer->userid][$questionid]['answer2'] = $surveyanswer->answer2;
f9903ed0 145 }
a5cb6242 146}
f9903ed0 147
d81b7ffb 148// Output the file as a valid ODS spreadsheet if required
1c969116 149$coursecontext = context_course::instance($course->id);
8ebbb06a 150$courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
d81b7ffb 151
a5cb6242 152if ($type == "ods") {
153 require_once("$CFG->libdir/odslib.class.php");
154
155/// Calculate file name
8ebbb06a 156 $downloadfilename = clean_filename(strip_tags($courseshortname.' '.format_string($survey->name, true))).'.ods';
a5cb6242 157/// Creating a workbook
158 $workbook = new MoodleODSWorkbook("-");
159/// Sending HTTP headers
160 $workbook->send($downloadfilename);
161/// Creating the first worksheet
2f1e464a 162 $myxls = $workbook->add_worksheet(core_text::substr(strip_tags(format_string($survey->name,true)), 0, 31));
a5cb6242 163
164 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
165 $col=0;
166 foreach ($header as $item) {
167 $myxls->write_string(0,$col++,$item);
168 }
3ce4270a
AD
169
170 foreach ($nestedorder as $key => $nestedquestions) {
171 foreach ($nestedquestions as $key2 => $qid) {
172 $question = $questions[$qid];
173 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
174 $myxls->write_string(0,$col++,"$question->text");
175 }
176 if ($question->type == "2" || $question->type == "3") {
177 $myxls->write_string(0,$col++,"$question->text (preferred)");
178 }
d81b7ffb 179 }
a5cb6242 180 }
d81b7ffb 181
182// $date = $workbook->addformat();
183// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
184
a5cb6242 185 $row = 0;
186 foreach ($results as $user => $rest) {
187 $col = 0;
188 $row++;
189 if (! $u = $DB->get_record("user", array("id"=>$user))) {
190 print_error('invaliduserid');
d81b7ffb 191 }
a5cb6242 192 if ($n = $DB->get_record("survey_analysis", array("survey"=>$survey->id, "userid"=>$user))) {
193 $notes = $n->notes;
194 } else {
195 $notes = "No notes made";
f9903ed0 196 }
a5cb6242 197 $myxls->write_string($row,$col++,$survey->id);
198 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
199 $myxls->write_string($row,$col++,$user);
200 $myxls->write_string($row,$col++,$u->firstname);
201 $myxls->write_string($row,$col++,$u->lastname);
202 $myxls->write_string($row,$col++,$u->email);
203 $myxls->write_string($row,$col++,$u->idnumber);
204 $myxls->write_string($row,$col++, userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p") );
205// $myxls->write_number($row,$col++,$results[$user]["time"],$date);
206 $myxls->write_string($row,$col++,$notes);
207
3ce4270a
AD
208 foreach ($nestedorder as $key => $nestedquestions) {
209 foreach ($nestedquestions as $key2 => $qid) {
210 $question = $questions[$qid];
211 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
212 $myxls->write_string($row,$col++, $results[$user][$qid]["answer1"] );
213 }
214 if ($question->type == "2" || $question->type == "3") {
215 $myxls->write_string($row, $col++, $results[$user][$qid]["answer2"] );
216 }
f9903ed0 217 }
218 }
f9903ed0 219 }
a5cb6242 220 $workbook->close();
f9903ed0 221
a5cb6242 222 exit;
223}
f9903ed0 224
a5cb6242 225// Output the file as a valid Excel spreadsheet if required
f9903ed0 226
a5cb6242 227if ($type == "xls") {
228 require_once("$CFG->libdir/excellib.class.php");
229
230/// Calculate file name
8ebbb06a 231 $downloadfilename = clean_filename(strip_tags($courseshortname.' '.format_string($survey->name,true))).'.xls';
a5cb6242 232/// Creating a workbook
233 $workbook = new MoodleExcelWorkbook("-");
234/// Sending HTTP headers
235 $workbook->send($downloadfilename);
236/// Creating the first worksheet
2f1e464a 237 $myxls = $workbook->add_worksheet(core_text::substr(strip_tags(format_string($survey->name,true)), 0, 31));
a5cb6242 238
239 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
240 $col=0;
241 foreach ($header as $item) {
242 $myxls->write_string(0,$col++,$item);
243 }
3ce4270a
AD
244
245 foreach ($nestedorder as $key => $nestedquestions) {
246 foreach ($nestedquestions as $key2 => $qid) {
247 $question = $questions[$qid];
248
249 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
250 $myxls->write_string(0,$col++,"$question->text");
251 }
252 if ($question->type == "2" || $question->type == "3") {
253 $myxls->write_string(0,$col++,"$question->text (preferred)");
254 }
f9903ed0 255 }
256 }
f9903ed0 257
a5cb6242 258// $date = $workbook->addformat();
259// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
f9903ed0 260
a5cb6242 261 $row = 0;
f9903ed0 262 foreach ($results as $user => $rest) {
a5cb6242 263 $col = 0;
264 $row++;
deb3a60d 265 if (! $u = $DB->get_record("user", array("id"=>$user))) {
83b10e57 266 print_error('invaliduserid');
f9903ed0 267 }
a5cb6242 268 if ($n = $DB->get_record("survey_analysis", array("survey"=>$survey->id, "userid"=>$user))) {
269 $notes = $n->notes;
270 } else {
271 $notes = "No notes made";
272 }
273 $myxls->write_string($row,$col++,$survey->id);
274 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
275 $myxls->write_string($row,$col++,$user);
276 $myxls->write_string($row,$col++,$u->firstname);
277 $myxls->write_string($row,$col++,$u->lastname);
278 $myxls->write_string($row,$col++,$u->email);
279 $myxls->write_string($row,$col++,$u->idnumber);
280 $myxls->write_string($row,$col++, userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p") );
281// $myxls->write_number($row,$col++,$results[$user]["time"],$date);
282 $myxls->write_string($row,$col++,$notes);
f9903ed0 283
3ce4270a
AD
284 foreach ($nestedorder as $key => $nestedquestions) {
285 foreach ($nestedquestions as $key2 => $qid) {
286 $question = $questions[$qid];
287 if (($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1")
288 && array_key_exists($qid, $results[$user]) ){
a5cb6242 289 $myxls->write_string($row,$col++, $results[$user][$qid]["answer1"] );
f9903ed0 290 }
3ce4270a
AD
291 if (($question->type == "2" || $question->type == "3")
292 && array_key_exists($qid, $results[$user]) ){
a5cb6242 293 $myxls->write_string($row, $col++, $results[$user][$qid]["answer2"] );
f9903ed0 294 }
295 }
f9903ed0 296 }
3ce4270a 297 }
a5cb6242 298 $workbook->close();
299
f9903ed0 300 exit;
a5cb6242 301}
f9903ed0 302
a5cb6242 303// Otherwise, return the text file.
304
305// Print header to force download
306
307header("Content-Type: application/download\n");
308
8ebbb06a 309$downloadfilename = clean_filename(strip_tags($courseshortname.' '.format_string($survey->name,true)));
a5cb6242 310header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
311
312// Print names of all the fields
313
314echo "surveyid surveyname userid firstname lastname email idnumber time ";
3ce4270a
AD
315
316foreach ($nestedorder as $key => $nestedquestions) {
317 foreach ($nestedquestions as $key2 => $qid) {
318 $question = $questions[$qid];
a5cb6242 319 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
320 echo "$question->text ";
321 }
322 if ($question->type == "2" || $question->type == "3") {
323 echo "$question->text (preferred) ";
324 }
325}
3ce4270a 326}
a5cb6242 327echo "\n";
328
329// Print all the lines of data.
330foreach ($results as $user => $rest) {
331 if (! $u = $DB->get_record("user", array("id"=>$user))) {
332 print_error('invaliduserid');
333 }
334 echo $survey->id."\t";
335 echo strip_tags(format_string($survey->name,true))."\t";
336 echo $user."\t";
337 echo $u->firstname."\t";
338 echo $u->lastname."\t";
339 echo $u->email."\t";
340 echo $u->idnumber."\t";
341 echo userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
342
3ce4270a
AD
343 foreach ($nestedorder as $key => $nestedquestions) {
344 foreach ($nestedquestions as $key2 => $qid) {
345 $question = $questions[$qid];
ae8530e8 346
3ce4270a
AD
347 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
348 echo $results[$user][$qid]["answer1"]." ";
349 }
350 if ($question->type == "2" || $question->type == "3") {
351 echo $results[$user][$qid]["answer2"]." ";
352 }
a5cb6242 353 }
354 }
355 echo "\n";
356}
f9903ed0 357
ae8530e8 358exit;