survey MDL-23324 refactored some code that had been left behind during prior refactoring
[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 *
22 * @package mod-survey
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
a5cb6242 43$context = get_context_instance(CONTEXT_MODULE, $cm->id);
3c268f25 44
a6855934 45$PAGE->set_url('/mod/survey/download.php', array('id'=>$id, 'type'=>$type, 'group'=>$group));
f9903ed0 46
a5cb6242 47require_login($course->id, false, $cm);
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}
84$nestedorder = array();
85$preparray = array();
86foreach ($orderedquestions as $qid=>$question) {
87 $orderedquestions[$qid]->text = get_string($question->text, "survey");
88 if (!empty($question->multi)) {
89 $actualqid = explode(",", $questions[$qid]->multi);
90 foreach ($actualqid as $subqid) {
91 if (!empty($orderedquestions[$subqid]->type)) {
92 $orderedquestions[$subqid]->type = $questions[$qid]->type;
f9903ed0 93 }
94 }
a5cb6242 95 } else {
96 $actualqid = array($qid);
f9903ed0 97 }
a5cb6242 98 if ($virtualscales && $questions[$qid]->type < 0) {
99 $nestedorder[$qid] = $actualqid;
100 } else if (!$virtualscales && $question->type >= 0) {
101 $nestedorder[$qid] = $actualqid;
f762c448 102 }
a5cb6242 103}
104$reversednestedorder = array();
105foreach ($nestedorder as $qid=>$subqidarray) {
106 foreach ($subqidarray as $subqui) {
107 $reversednestedorder[$subqui] = $qid;
108 }
109}
f9903ed0 110
111// Get and collate all the results in one big array
a5cb6242 112if (! $surveyanswers = $DB->get_records("survey_answers", array("survey"=>$survey->id), "time ASC")) {
113 print_error('cannotfindanswer', 'survey');
114}
f9903ed0 115
a5cb6242 116$results = array();
ec81373f 117
a5cb6242 118foreach ($surveyanswers as $surveyanswer) {
119 if (!$group || isset($users[$surveyanswer->userid])) {
120 $questionid = $reversednestedorder[$surveyanswer->question];
121 if (!array_key_exists($surveyanswer->userid, $results)) {
122 $results[$surveyanswer->userid] = array('time'=>$surveyanswer->time);
f9903ed0 123 }
a5cb6242 124 $results[$surveyanswer->userid][$questionid]['answer1'] = $surveyanswer->answer1;
125 $results[$surveyanswer->userid][$questionid]['answer2'] = $surveyanswer->answer2;
f9903ed0 126 }
a5cb6242 127}
f9903ed0 128
d81b7ffb 129// Output the file as a valid ODS spreadsheet if required
130
a5cb6242 131if ($type == "ods") {
132 require_once("$CFG->libdir/odslib.class.php");
133
134/// Calculate file name
135 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.ods';
136/// Creating a workbook
137 $workbook = new MoodleODSWorkbook("-");
138/// Sending HTTP headers
139 $workbook->send($downloadfilename);
140/// Creating the first worksheet
141 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
142
143 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
144 $col=0;
145 foreach ($header as $item) {
146 $myxls->write_string(0,$col++,$item);
147 }
148 foreach ($order as $key => $qid) {
149 $question = $orderedquestions[$qid];
150 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
151 $myxls->write_string(0,$col++,"$question->text");
d81b7ffb 152 }
a5cb6242 153 if ($question->type == "2" || $question->type == "3") {
154 $myxls->write_string(0,$col++,"$question->text (preferred)");
d81b7ffb 155 }
a5cb6242 156 }
d81b7ffb 157
158// $date = $workbook->addformat();
159// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
160
a5cb6242 161 $row = 0;
162 foreach ($results as $user => $rest) {
163 $col = 0;
164 $row++;
165 if (! $u = $DB->get_record("user", array("id"=>$user))) {
166 print_error('invaliduserid');
d81b7ffb 167 }
a5cb6242 168 if ($n = $DB->get_record("survey_analysis", array("survey"=>$survey->id, "userid"=>$user))) {
169 $notes = $n->notes;
170 } else {
171 $notes = "No notes made";
f9903ed0 172 }
a5cb6242 173 $myxls->write_string($row,$col++,$survey->id);
174 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
175 $myxls->write_string($row,$col++,$user);
176 $myxls->write_string($row,$col++,$u->firstname);
177 $myxls->write_string($row,$col++,$u->lastname);
178 $myxls->write_string($row,$col++,$u->email);
179 $myxls->write_string($row,$col++,$u->idnumber);
180 $myxls->write_string($row,$col++, userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p") );
181// $myxls->write_number($row,$col++,$results[$user]["time"],$date);
182 $myxls->write_string($row,$col++,$notes);
183
f9903ed0 184 foreach ($order as $key => $qid) {
a5cb6242 185 $question = $orderedquestions[$qid];
ee990b19 186 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
a5cb6242 187 $myxls->write_string($row,$col++, $results[$user][$qid]["answer1"] );
f9903ed0 188 }
189 if ($question->type == "2" || $question->type == "3") {
a5cb6242 190 $myxls->write_string($row, $col++, $results[$user][$qid]["answer2"] );
f9903ed0 191 }
192 }
f9903ed0 193 }
a5cb6242 194 $workbook->close();
f9903ed0 195
a5cb6242 196 exit;
197}
f9903ed0 198
a5cb6242 199// Output the file as a valid Excel spreadsheet if required
f9903ed0 200
a5cb6242 201if ($type == "xls") {
202 require_once("$CFG->libdir/excellib.class.php");
203
204/// Calculate file name
205 $downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true))).'.xls';
206/// Creating a workbook
207 $workbook = new MoodleExcelWorkbook("-");
208/// Sending HTTP headers
209 $workbook->send($downloadfilename);
210/// Creating the first worksheet
211 $myxls =& $workbook->add_worksheet(substr(strip_tags(format_string($survey->name,true)), 0, 31));
212
213 $header = array("surveyid","surveyname","userid","firstname","lastname","email","idnumber","time", "notes");
214 $col=0;
215 foreach ($header as $item) {
216 $myxls->write_string(0,$col++,$item);
217 }
f9903ed0 218 foreach ($order as $key => $qid) {
a5cb6242 219 $question = $orderedquestions[$qid];
ee990b19 220 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
a5cb6242 221 $myxls->write_string(0,$col++,"$question->text");
f9903ed0 222 }
223 if ($question->type == "2" || $question->type == "3") {
a5cb6242 224 $myxls->write_string(0,$col++,"$question->text (preferred)");
f9903ed0 225 }
226 }
f9903ed0 227
a5cb6242 228// $date = $workbook->addformat();
229// $date->set_num_format('mmmm-d-yyyy h:mm:ss AM/PM'); // ?? adjust the settings to reflect the PHP format below
f9903ed0 230
a5cb6242 231 $row = 0;
f9903ed0 232 foreach ($results as $user => $rest) {
a5cb6242 233 $col = 0;
234 $row++;
deb3a60d 235 if (! $u = $DB->get_record("user", array("id"=>$user))) {
83b10e57 236 print_error('invaliduserid');
f9903ed0 237 }
a5cb6242 238 if ($n = $DB->get_record("survey_analysis", array("survey"=>$survey->id, "userid"=>$user))) {
239 $notes = $n->notes;
240 } else {
241 $notes = "No notes made";
242 }
243 $myxls->write_string($row,$col++,$survey->id);
244 $myxls->write_string($row,$col++,strip_tags(format_text($survey->name,true)));
245 $myxls->write_string($row,$col++,$user);
246 $myxls->write_string($row,$col++,$u->firstname);
247 $myxls->write_string($row,$col++,$u->lastname);
248 $myxls->write_string($row,$col++,$u->email);
249 $myxls->write_string($row,$col++,$u->idnumber);
250 $myxls->write_string($row,$col++, userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p") );
251// $myxls->write_number($row,$col++,$results[$user]["time"],$date);
252 $myxls->write_string($row,$col++,$notes);
f9903ed0 253
254 foreach ($order as $key => $qid) {
a5cb6242 255 $question = $orderedquestions[$qid];
ee990b19 256 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
a5cb6242 257 $myxls->write_string($row,$col++, $results[$user][$qid]["answer1"] );
f9903ed0 258 }
259 if ($question->type == "2" || $question->type == "3") {
a5cb6242 260 $myxls->write_string($row, $col++, $results[$user][$qid]["answer2"] );
f9903ed0 261 }
262 }
f9903ed0 263 }
a5cb6242 264 $workbook->close();
265
f9903ed0 266 exit;
a5cb6242 267}
f9903ed0 268
a5cb6242 269// Otherwise, return the text file.
270
271// Print header to force download
272
273header("Content-Type: application/download\n");
274
275$downloadfilename = clean_filename("$course->shortname ".strip_tags(format_string($survey->name,true)));
276header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
277
278// Print names of all the fields
279
280echo "surveyid surveyname userid firstname lastname email idnumber time ";
281foreach ($orderedquestions as $key => $question) {
282 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
283 echo "$question->text ";
284 }
285 if ($question->type == "2" || $question->type == "3") {
286 echo "$question->text (preferred) ";
287 }
288}
289echo "\n";
290
291// Print all the lines of data.
292foreach ($results as $user => $rest) {
293 if (! $u = $DB->get_record("user", array("id"=>$user))) {
294 print_error('invaliduserid');
295 }
296 echo $survey->id."\t";
297 echo strip_tags(format_string($survey->name,true))."\t";
298 echo $user."\t";
299 echo $u->firstname."\t";
300 echo $u->lastname."\t";
301 echo $u->email."\t";
302 echo $u->idnumber."\t";
303 echo userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
304
305 foreach ($order as $key => $qid) {
306 $question = $orderedquestions[$qid];
307 if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
308 echo $results[$user][$qid]["answer1"]." ";
309 }
310 if ($question->type == "2" || $question->type == "3") {
311 echo $results[$user][$qid]["answer2"]." ";
312 }
313 }
314 echo "\n";
315}
f9903ed0 316
a5cb6242 317exit;