-
Notifications
You must be signed in to change notification settings - Fork 985
/
PrintanswersController.php
256 lines (219 loc) · 9.79 KB
/
PrintanswersController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2011 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id$
*/
/**
* printanswers
*
* @package LimeSurvey
* @copyright 2011
* @version $Id$
* @access public
*/
class PrintanswersController extends LSYii_Controller {
/**
* printanswers::view()
* View answers at the end of a survey in one place. To export as pdf, set 'usepdfexport' = 1 in lsconfig.php and $printableexport='pdf'.
* @param mixed $surveyid
* @param bool $printableexport
* @return
*/
function actionView($surveyid,$printableexport=FALSE)
{
global $siteadminname, $siteadminemail;
Yii::app()->loadHelper("frontend");
if(Yii::app()->getConfig('usepdfexport'))
{
Yii::import('application.libraries.admin.pdf');
}
$surveyid = (int)($surveyid);
Yii::app()->loadHelper('database');
if (isset($_SESSION['survey_'.$surveyid]['sid']))
{
$surveyid = $_SESSION['survey_'.$surveyid]['sid'];
}
else
{
die('Invalid survey/session');
}
//Debut session time out
if (!isset($_SESSION['survey_'.$surveyid]['finished']) || !isset($_SESSION['survey_'.$surveyid]['srid']))
// Argh ... a session time out! RUN!
//display "sorry but your session has expired"
{
//require_once($rootdir.'/classes/core/language.php');
$baselang = Survey::model()->findByPk($surveyid)->language;
Yii::import('application.libraries.Limesurvey_lang', true);
$clang = new Limesurvey_lang($baselang);
//A nice exit
sendCacheHeaders();
doHeader();
echo templatereplace(file_get_contents(getTemplatePath(validateTemplateDir("default"))."/startpage.pstpl"),array(),array());
echo "<center><br />\n"
."\t<font color='RED'><strong>".$clang->gT("ERROR")."</strong></font><br />\n"
."\t".$clang->gT("We are sorry but your session has expired.")."<br />".$clang->gT("Either you have been inactive for too long, you have cookies disabled for your browser, or there were problems with your connection.")."<br />\n"
."\t".sprintf($clang->gT("Please contact %s ( %s ) for further assistance."),$siteadminname,$siteadminemail)."\n"
."</center><br />\n";
echo templatereplace(file_get_contents(getTemplatePath(validateTemplateDir("default"))."/endpage.pstpl"),array(),array());
doFooter();
exit;
}
//Fin session time out
$id = $_SESSION['survey_'.$surveyid]['srid']; //I want to see the answers with this id
$clang = $_SESSION['survey_'.$surveyid]['s_lang'];
//Ensure script is not run directly, avoid path disclosure
//if (!isset($rootdir) || isset($_REQUEST['$rootdir'])) {die( "browse - Cannot run this script directly");}
// Set the language for dispay
//require_once($rootdir.'/classes/core/language.php'); // has been secured
if (isset($_SESSION['survey_'.$surveyid]['s_lang']))
{
$clang = SetSurveyLanguage( $surveyid, $_SESSION['survey_'.$surveyid]['s_lang']);
$language = $_SESSION['survey_'.$surveyid]['s_lang'];
}
else
{
$language = Survey::model()->findByPk($surveyid)->language;
$clang = SetSurveyLanguage( $surveyid, $language);
}
// Get the survey inforamtion
$thissurvey = getSurveyInfo($surveyid,$language);
//SET THE TEMPLATE DIRECTORY
if (!isset($thissurvey['templatedir']) || !$thissurvey['templatedir'])
{
$thistpl = validateTemplateDir("default");
}
else
{
$thistpl = validateTemplateDir($thissurvey['templatedir']);
}
if ($thissurvey['printanswers'] == 'N')
{
die(); //Die quietly if print answers is not permitted
}
//CHECK IF SURVEY IS ACTIVATED AND EXISTS
$surveytable = "{{survey_{$surveyid}}}";
$surveyname = $thissurvey['surveyls_title'];
$anonymized = $thissurvey['anonymized'];
//OK. IF WE GOT THIS FAR, THEN THE SURVEY EXISTS AND IT IS ACTIVE, SO LETS GET TO WORK.
//SHOW HEADER
$printoutput = '';
if(Yii::app()->getConfig('usepdfexport') == 1)
{
$printoutput .= "<form action='".Yii::app()->getController()->createUrl('printanswers/view/surveyid/'.$surveyid.'/printableexport/pdf')."' method='post'>\n<center><input type='submit' value='".$clang->gT("PDF export")."'id=\"exportbutton\"/><input type='hidden' name='printableexport' /></center></form>";
}
if($printableexport == 'pdf')
{
require (Yii::app()->getConfig('rootdir').'/application/config/tcpdf.php');
Yii::import('application.libraries.admin.pdf', true);
$pdf = new pdf();
$pdf->_config = $tcpdf;
//$pdf->SetFont($pdfdefaultfont,'',$pdffontsize);
$pdf->AddPage();
//$pdf->titleintopdf($clang->gT("Survey name (ID)",'unescaped').": {$surveyname} ({$surveyid})");
$pdf->SetTitle($clang->gT("Survey name (ID)",'unescaped').": {$surveyname} ({$surveyid})");
}
$printoutput .= "\t<div class='printouttitle'><strong>".$clang->gT("Survey name (ID):")."</strong> $surveyname ($surveyid)</div><p> \n";
LimeExpressionManager::StartProcessingPage(true); // means that all variables are on the same page
// Since all data are loaded, and don't need JavaScript, pretend all from Group 1
LimeExpressionManager::StartProcessingGroup(1,($thissurvey['anonymized']!="N"),$surveyid);
$aFullResponseTable = getFullResponseTable($surveyid,$id,$language,true);
//Get the fieldmap @TODO: do we need to filter out some fields?
unset ($aFullResponseTable['id']);
unset ($aFullResponseTable['token']);
unset ($aFullResponseTable['lastpage']);
unset ($aFullResponseTable['startlanguage']);
unset ($aFullResponseTable['datestamp']);
unset ($aFullResponseTable['startdate']);
$printoutput .= "<table class='printouttable' >\n";
if($printableexport == 'pdf')
{
$pdf->intopdf($clang->gT("Question",'unescaped').": ".$clang->gT("Your answer",'unescaped'));
}
$oldgid = 0;
$oldqid = 0;
foreach ($aFullResponseTable as $sFieldname=>$fname)
{
if (substr($sFieldname,0,4) == 'gid_')
{
if($printableexport)
{
$pdf->intopdf(flattenText($fname[0],false,true));
$pdf->ln(2);
}
else
{
$printoutput .= "\t<tr class='printanswersgroup'><td colspan='2'>{$fname[0]}</td></tr>\n";
}
}
elseif (substr($sFieldname,0,4)=='qid_')
{
if($printableexport == 'pdf')
{
$pdf->intopdf(flattenText($fname[0].$fname[1],false,true).": ".$fname[2]);
$pdf->ln(2);
}
else
{
$printoutput .= "\t<tr class='printanswersquestionhead'><td colspan='2'>{$fname[0]}</td></tr>\n";
}
}
elseif ($sFieldname=='submitdate')
{
if($anonymized != 'Y')
{
if($printableexport == 'pdf')
{
$pdf->intopdf(flattenText($fname[0].$fname[1],false,true).": ".$fname[2]);
$pdf->ln(2);
}
else
{
$printoutput .= "\t<tr class='printanswersquestion'><td>{$fname[0]} {$fname[1]} {$sFieldname}</td><td class='printanswersanswertext'>{$fname[2]}</td></tr>";
}
}
}
else
{
if($printableexport == 'pdf')
{
$pdf->intopdf(flattenText($fname[0].$fname[1],false,true).": ".$fname[2]);
$pdf->ln(2);
}
else
{
$printoutput .= "\t<tr class='printanswersquestion'><td>{$fname[0]} {$fname[1]}</td><td class='printanswersanswertext'>{$fname[2]}</td></tr>";
}
}
}
$printoutput .= "</table>\n";
if($printableexport == 'pdf')
{
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
$sExportFileName = sanitize_filename($surveyname);
$pdf->Output($sExportFileName."-".$surveyid.".pdf","D");
}
//Display the page with user answers
if(!$printableexport)
{
sendCacheHeaders();
doHeader();
echo templatereplace(file_get_contents(getTemplatePath($thistpl).'/startpage.pstpl'));
echo templatereplace(file_get_contents(getTemplatePath($thistpl).'/printanswers.pstpl'),array('ANSWERTABLE'=>$printoutput));
echo templatereplace(file_get_contents(getTemplatePath($thistpl).'/endpage.pstpl'));
echo "</body></html>";
}
LimeExpressionManager::FinishProcessingGroup();
LimeExpressionManager::FinishProcessingPage();
}
}