-
Notifications
You must be signed in to change notification settings - Fork 988
/
PrintanswersController.php
231 lines (216 loc) · 12.2 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
<?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.
*
*/
/**
* printanswers
*
* @package LimeSurvey
* @copyright 2011
* @access public
*/
class PrintanswersController extends LSYii_Controller {
/* @var string : Default layout when using render : leave at bare actually : just send content */
public $layout= 'survey';
/* @var string the template name to be used when using layout */
public $sTemplate= 'default';
/* @var string[] Replacement data when use templatereplace function in layout, @see templatereplace $replacements */
public $aReplacementData= array();
/* @var array Global data when use templatereplace function in layout, @see templatereplace $redata */
public $aGlobalData= array();
/* @var boolean did we need survey.pstpl when using layout */
public $bStartSurvey= false;
/**
* 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)
{
Yii::app()->loadHelper("frontend");
Yii::import('application.libraries.admin.pdf');
$iSurveyID = (int)$surveyid;
$sExportType = $printableexport;
Yii::app()->loadHelper('database');
if (isset($_SESSION['survey_'.$iSurveyID]['sid']))
{
$iSurveyID = $_SESSION['survey_'.$iSurveyID]['sid'];
}
else
{
//die('Invalid survey/session');
}
// Get the survey inforamtion
// Set the language for dispay
if (isset($_SESSION['survey_'.$iSurveyID]['s_lang']))
{
$sLanguage = $_SESSION['survey_'.$iSurveyID]['s_lang'];
}
elseif(Survey::model()->findByPk($iSurveyID))// survey exist
{
$sLanguage = Survey::model()->findByPk($iSurveyID)->language;
}
else
{
$iSurveyID=0;
$sLanguage = Yii::app()->getConfig("defaultlang");
}
SetSurveyLanguage($iSurveyID, $sLanguage);
$aSurveyInfo = getSurveyInfo($iSurveyID,$sLanguage);
$oTemplate = Template::model()->getInstance(null, $iSurveyID);
if($oTemplate->cssFramework == 'bootstrap')
{
App()->bootstrap->register();
}
//Survey is not finished or don't exist
if (!isset($_SESSION['survey_'.$iSurveyID]['finished']) || !isset($_SESSION['survey_'.$iSurveyID]['srid']))
//display "sorry but your session has expired"
{
$oTemplate = Template::model()->getInstance('', $iSurveyID);
$this->sTemplate=$oTemplate->sTemplateName;
$error=$this->renderPartial("/survey/system/errorWarning",array(
'aErrors'=>array(
gT("We are sorry but your session has expired."),
),
),true);
$message=$this->renderPartial("/survey/system/message",array(
'aMessage'=>array(
gT("Either you have been inactive for too long, you have cookies disabled for your browser, or there were problems with your connection."),
),
),true);
/* Set the data for templatereplace */
$this->aGlobalData['thissurvey']=getSurveyInfo($iSurveyID);
$this->aReplacementData=$aReplacementData['MESSAGEID']='session-timeout';
$aReplacementData['MESSAGE']=$message;
$aReplacementData['URL']='';
$this->aReplacementData=$aReplacementData['ERROR']=$error; // Adding this to replacement data : allow to update title (for example) : @see https://bugs.limesurvey.org/view.php?id=9106 (but need more)
$content=templatereplace(file_get_contents($oTemplate->viewPath."message.pstpl"),$aReplacementData,$this->aGlobalData);
$this->render("/survey/system/display",array('content'=>$content));
App()->end();
}
//Fin session time out
$sSRID = $_SESSION['survey_'.$iSurveyID]['srid']; //I want to see the answers with this id
//Ensure script is not run directly, avoid path disclosure
//if (!isset($rootdir) || isset($_REQUEST['$rootdir'])) {die( "browse - Cannot run this script directly");}
//Ensure Participants printAnswer setting is set to true or that the logged user have read permissions over the responses.
if ($aSurveyInfo['printanswers'] == 'N' && !Permission::model()->hasSurveyPermission($iSurveyID,'responses','read'))
{
throw new CHttpException(401, 'You are not allowed to print answers.');
}
//CHECK IF SURVEY IS ACTIVATED AND EXISTS
$sSurveyName = $aSurveyInfo['surveyls_title'];
$sAnonymized = $aSurveyInfo['anonymized'];
//OK. IF WE GOT THIS FAR, THEN THE SURVEY EXISTS AND IT IS ACTIVE, SO LETS GET TO WORK.
//SHOW HEADER
if ($sExportType != 'pdf')
{
$sOutput = CHtml::form(array("printanswers/view/surveyid/{$iSurveyID}/printableexport/pdf"), 'post')
."<div class='text-center'><input class='btn btn-default' type='submit' value='".gT("PDF export")."'id=\"exportbutton\"/><input type='hidden' name='printableexport' /></div></form>";
$sOutput .= "\t<div class='h3 printouttitle'>".gT("Survey name (ID):")." $sSurveyName ($iSurveyID)</div>";
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,($aSurveyInfo['anonymized']!="N"),$iSurveyID);
$printanswershonorsconditions = Yii::app()->getConfig('printanswershonorsconditions');
$aFullResponseTable = getFullResponseTable($iSurveyID,$sSRID,$sLanguage,$printanswershonorsconditions);
//Get the fieldmap @TODO: do we need to filter out some fields?
if($aSurveyInfo['datestamp']!="Y" || $sAnonymized == 'Y'){
unset ($aFullResponseTable['submitdate']);
}else{
unset ($aFullResponseTable['id']);
}
unset ($aFullResponseTable['token']);
unset ($aFullResponseTable['lastpage']);
unset ($aFullResponseTable['startlanguage']);
unset ($aFullResponseTable['datestamp']);
unset ($aFullResponseTable['startdate']);
$sOutput .= "<table class='printouttable table table-bordered table-striped table-condensed' >\n";
foreach ($aFullResponseTable as $sFieldname=>$fname)
{
if (substr($sFieldname,0,4) == 'gid_')
{
$sOutput .= "\t<tr class='printanswersgroup info'><th colspan='2'>{$fname[0]}</th></tr>\n";
$sOutput .= "\t<tr class='printanswersgroupdesc info'><td colspan='2'>{$fname[1]}</td></tr>\n";
}
elseif ($sFieldname=='submitdate')
{
if($sAnonymized != 'Y')
{
$sOutput .= "\t<tr class='printanswersquestion'><th>{$fname[0]} {$fname[1]}</th><td class='printanswersanswertext'>{$fname[2]}</td></tr>";
}
}
elseif (substr($sFieldname,0,4) != 'qid_') // Question text is already in subquestion text, skipping it
{
$sOutput .= "\t<tr class='printanswersquestion'><th>{$fname[0]} {$fname[1]}</th><td class='printanswersanswertext'>".flattenText($fname[2])."</td></tr>";
}
}
$sOutput .= "</table>\n";
$this->aGlobalData['thissurvey']=$aSurveyInfo;
$sOutput=templatereplace($sOutput, array() , $sData, '', $aSurveyInfo['anonymized']=="Y",NULL, array(), true);// Do a static replacement
$content=templatereplace(file_get_contents($oTemplate->viewPath.'/printanswers.pstpl'),array('ANSWERTABLE'=>$sOutput),$this->aGlobalData);
$this->render("/survey/system/display",array('content'=>$sOutput));
App()->end();
}
if($sExportType == 'pdf')
{
// Get images for TCPDF from template directory
define('K_PATH_IMAGES', getTemplatePath($aSurveyInfo['template']).DIRECTORY_SEPARATOR);
Yii::import('application.libraries.admin.pdf', true);
Yii::import('application.helpers.pdfHelper');
$aPdfLanguageSettings=pdfHelper::getPdfLanguageSettings(App()->language);
$oPDF = new pdf();
$sDefaultHeaderString = $sSurveyName." (".gT("ID",'unescaped').":".$iSurveyID.")";
$oPDF->initAnswerPDF($aSurveyInfo, $aPdfLanguageSettings, Yii::app()->getConfig('sitename'), $sSurveyName, $sDefaultHeaderString);
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,($aSurveyInfo['anonymized']!="N"),$iSurveyID);
$printanswershonorsconditions = Yii::app()->getConfig('printanswershonorsconditions');
$aFullResponseTable = getFullResponseTable($iSurveyID,$sSRID,$sLanguage,$printanswershonorsconditions);
//Get the fieldmap @TODO: do we need to filter out some fields?
if($aSurveyInfo['datestamp']!="Y" || $sAnonymized == 'Y'){
unset ($aFullResponseTable['submitdate']);
}else{
unset ($aFullResponseTable['id']);
}
unset ($aFullResponseTable['token']);
unset ($aFullResponseTable['lastpage']);
unset ($aFullResponseTable['startlanguage']);
unset ($aFullResponseTable['datestamp']);
unset ($aFullResponseTable['startdate']);
foreach ($aFullResponseTable as $sFieldname=>$fname)
{
if (substr($sFieldname,0,4) == 'gid_')
{
$oPDF->addGidAnswer($fname[0], $fname[1]);
}
elseif ($sFieldname=='submitdate')
{
if($sAnonymized != 'Y')
{
$oPDF->addAnswer($fname[0]." ".$fname[1], $fname[2]);
}
}
elseif (substr($sFieldname,0,4) != 'qid_') // Question text is already in subquestion text, skipping it
{
$oPDF->addAnswer($fname[0]." ".$fname[1], $fname[2]);
}
}
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
$sExportFileName = sanitize_filename($sSurveyName);
$oPDF->Output($sExportFileName."-".$iSurveyID.".pdf","D");
}
LimeExpressionManager::FinishProcessingGroup();
LimeExpressionManager::FinishProcessingPage();
}
}