-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectCSVExport.php
More file actions
executable file
·222 lines (194 loc) · 9.52 KB
/
DirectCSVExport.php
File metadata and controls
executable file
·222 lines (194 loc) · 9.52 KB
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
<?php
/**
* DirectCSVExport plugin for LimeSurvey
* - Provides a URL that in combination with a secret key will allow the download of the complete data from a survey including token/participant data
* - The CSV is formatted in a way ideal for analytics.
* - Ideal for running scripted imports to systems like Microsoft Power BI without the need for direct database access
*
* @author Adam Zammit <adam@acspri.org.au>
* @copyright 2020 ACSPRI <https://www.acspri.org.au>
* @license GPL v3
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
class DirectCSVExport extends PluginBase {
protected $storage = 'DbStorage';
static protected $description = 'Direct CSV Export';
static protected $name = 'DirectCSVExport';
public function init() {
$this->subscribe('beforeHasPermission');
$this->subscribe('beforeSurveySettings');
$this->subscribe('newSurveySettings');
$this->subscribe('newDirectRequest'); //for call to API
$this->subscribe('newUnsecureRequest','newDirectRequest'); //for call to API
}
protected $settings = array(
'bUse' => array (
'type' => 'select',
'options' => array (
0 => 'No',
1 => 'Yes'
),
'default' => 1,
'label' => 'Use for every survey on this installation by default?',
'help' => 'Overwritable in each Survey setting'
),
);
/**
* Ensure plugin has permission to access token records even when
* user is not logged in through the browser
*
*/
public function beforeHasPermission()
{
$oEvent = $this->getEvent();
$sSurveyId = Yii::app()->request->getQuery('surveyId');
$sAPIKey = Yii::app()->request->getQuery('APIKey');
$surveyidExists = false;
if (!empty($sSurveyId)) {
$iSurveyId = intval($sSurveyId);
$surveyidExists = Survey::model()->findByPk($iSurveyId);
}
if ($surveyidExists && !empty($sAPIKey) && !(($this->get('bUse','Survey',$iSurveyId)==0)||(($this->get('bUse','Survey',$iSurveyId)==2) && ($this->get('bUse',null,null,$this->settings['bUse'])==0)))) {
if ($sAPIKey == $this->get ( 'sAPIKey', 'Survey', $iSurveyId ) ) {//APIKey matches
if ($oEvent->get('iEntityID') == $iSurveyId && $oEvent->get('sEntityName') == 'survey' && $oEvent->get('sPermission') == 'tokens' && $oEvent->get('sCRUD') == 'read') {
$oEvent->set('bPermission', true);
}
}
}
}
public function newDirectRequest()
{
$oEvent = $this->getEvent();
if ($oEvent->get('target') != $this->getName())
return;
if ((Yii::app()->request->getQuery('surveyId') == NULL) || (Yii::app()->request->getQuery('APIKey') == NULL))
return;
$sSurveyId = Yii::app()->request->getQuery('surveyId');
$sAPIKey = Yii::app()->request->getQuery('APIKey');
$surveyidExists = false;
if (!empty($sSurveyId)) {
$iSurveyId = intval($sSurveyId);
$surveyidExists = Survey::model()->findByPk($iSurveyId);
}
if ($surveyidExists === false) {
die("Survey does not exist");
}
if (!empty($sAPIKey) && !(($this->get('bUse','Survey',$iSurveyId)==0)||(($this->get('bUse','Survey',$iSurveyId)==2) && ($this->get('bUse',null,null,$this->settings['bUse'])==0)))) {
//if enabled for this survey
if ($sAPIKey == $this->get ( 'sAPIKey', 'Survey', $iSurveyId ) || $sAPIKey == $this->get ( 'sAPIKeyNT', 'Survey', $iSurveyId )) {//APIKey matches
Yii::import('application.helpers.admin.export.FormattingOptions', true);
Yii::import('application.helpers.admin.exportresults_helper', true);
Yii::import('application.helpers.viewHelper', true);
$survey = Survey::model()->findByPk($iSurveyId);
if (!tableExists($survey->responsesTableName)) {
die('No Data, survey table does not exist.');
}
if (!($maxId = SurveyDynamic::model($iSurveyId)->getMaxId())) {
die('No Data, could not get max id.');
}
$oFormattingOptions = new FormattingOptions();
$oFormattingOptions->responseMinRecord = 1;
$oFormattingOptions->responseMaxRecord = $maxId;
$aFields = array_keys(createFieldMap($survey, 'full', true, false, $survey->language));
$aTokenFields = [];
if ($survey->hasTokensTable && $sAPIKey == $this->get ( 'sAPIKey', 'Survey', $iSurveyId )) {
$aTokenFields = array('tid','participant_id','firstname','lastname','email','emailstatus','language','blacklisted','sent','remindersent','remindercount','completed','usesleft','validfrom','validuntil','mpid');
foreach($survey->tokenAttributes as $key => $value) {
$aTokenFields[] = $key;
}
}
$oFormattingOptions->selectedColumns = array_merge($aFields,$aTokenFields);
$oFormattingOptions->responseCompletionState = 'all';
$oFormattingOptions->headingFormat = 'full';
$oFormattingOptions->answerFormat = 'long';
$oFormattingOptions->csvFieldSeparator = ',';
$oFormattingOptions->output = 'display';
$oExport = new ExportSurveyResultsService();
$sTempFile = $oExport->exportResponses($iSurveyId, $survey->language, 'csv', $oFormattingOptions, '');
} else {
die("Unavailable");
}
} else {
die("Unavailable");
}
}
/**
* Survey level settings - require the setting of an API key before displaying the API URL
*/
public function beforeSurveySettings()
{
$oEvent = $this->event;
$apiKey = $this->get ( 'sAPIKey', 'Survey', $oEvent->get ( 'survey' ) );
$apiKeyNT = $this->get ( 'sAPIKeyNT', 'Survey', $oEvent->get ( 'survey' ) );
$message = "Set an API key and save these settings before you can access the API";
$messageNT = "Set a no tokens data API key and save these settings before you can access the API";
if (!empty($apiKey)) {
$message = Yii::app()->createAbsoluteUrl('plugins/direct', array('plugin' => "DirectCSVExport", 'surveyId' => $oEvent->get('survey'), "APIKey" =>$apiKey ));
}
if (!empty($apiKeyNT)) {
$messageNT = Yii::app()->createAbsoluteUrl('plugins/direct', array('plugin' => "DirectCSVExport", 'surveyId' => $oEvent->get('survey'), "APIKey" =>$apiKeyNT ));
}
$aSets = array (
'bUse' => array (
'type' => 'select',
'label' => 'Enable the Direct CSV Export API for this survey?',
'options' => array (
0 => 'No',
1 => 'Yes',
2 => 'Use site settings (default)'
),
'default' => 2,
'help' => 'Leave default to use global setting',
'current' => $this->get ( 'bUse', 'Survey', $oEvent->get ( 'survey' ) )
),
'sAPIKey' => array (
'type' => 'text',
'label' => 'The API Key for accessing data WITH token data if available',
'help' => 'Treat this string like a password',
'current' => $this->get('sAPIKey', 'Survey', $oEvent->get('survey'),$this->get('sAPIKey',null,null,str_replace(array('~', '_'), array('a', 'z'), Yii::app()->securityManager->generateRandomString(64)))),
),
'sAPIKeyNT' => array (
'type' => 'text',
'label' => 'The API Key for accessing data without token data',
'help' => 'Treat this string like a password',
'current' => $this->get('sAPIKeyNT', 'Survey', $oEvent->get('survey'),$this->get('sAPIKeyNT',null,null,str_replace(array('~', '_'), array('a', 'z'), Yii::app()->securityManager->generateRandomString(64)))),
),
'sInfo' => array (
'type' => 'info',
'label' => 'The URL to access this API WITH token data if available',
'help' => $message
),
'sInfoNT' => array (
'type' => 'info',
'label' => 'The URL to access this API without token data',
'help' => $messageNT
),
);
$aSettings = array(
'name' => get_class ( $this ),
'settings' => $aSets,
);
$oEvent->set("surveysettings.{$this->id}", $aSettings);
}
/**
* Save the settings
*/
public function newSurveySettings()
{
$event = $this->event;
foreach ($event->get('settings') as $name => $value)
{
$default=$event->get($name,null,null,isset($this->settings[$name]['default'])?$this->settings[$name]['default']:NULL);
$this->set($name, $value, 'Survey', $event->get('survey'),$default);
}
}
}