-
Notifications
You must be signed in to change notification settings - Fork 987
/
limereplacementfields.php
345 lines (312 loc) · 15.1 KB
/
limereplacementfields.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/*
* LimeSurvey
* Copyright (C) 2013 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.
*
*/
class limereplacementfields extends Survey_Common_Action
{
public function index()
{
$surveyid = intval(App()->request->getQuery('surveyid'));
$survey = Survey::model()->findByPk($surveyid);
$gid = intval(App()->request->getQuery('gid'));
$qid = intval(App()->request->getQuery('qid'));
$fieldtype = sanitize_xss_string(App()->request->getQuery('fieldtype'));
$action = sanitize_xss_string(App()->request->getQuery('action'));
if (!Yii::app()->session['loginID']) {
throw new CHttpException(401);
}
list($replacementFields, $isInsertAnswerEnabled) = $this->_getReplacementFields($fieldtype, $surveyid);
if ($isInsertAnswerEnabled === true) {
//2: Get all other questions that occur before this question that are pre-determined answer types
$fieldmap = createFieldMap($survey,'full',false,false,$survey->language);
$oSurvey = Survey::model()->findByPk($surveyid);
$surveyformat = $oSurvey->format; // S, G, A
//Go through each question until we reach the current one
//error_log(print_r($qrows,true));
$questionlist = $this->_getQuestionList($action, $gid, $qid, $fieldmap, $fieldtype, $surveyformat);
$childQuestions = $this->_getChildQuestions($questionlist);
}
$data['countfields'] = count($replacementFields);
asort($replacementFields);
$data['replFields'] = $replacementFields;
if (isset($childQuestions)) {
$data['cquestions'] = $childQuestions;
}
if (isset($surveyformat)) {
$data['surveyformat'] = $surveyformat;
}
$this->getController()->render('/admin/limeReplacementFields_view', $data);
}
/**
* @param integer $gid
* @param integer $qid
* @param string $surveyformat
* @return array
*/
private function _getQuestionList($action, $gid, $qid, array $fieldmap, $questionType, $surveyformat)
{
$previousQuestion = null;
$isPreviousPageQuestion = true;
$questionList = array();
foreach ($fieldmap as $question) {
if (empty($question['qid'])) {
continue;
}
if (is_null($qid) || $this->_shouldAddQuestion($action, $gid, $qid, $question, $previousQuestion)) {
$isPreviousPageQuestion = $this->_addQuestionToList($action, $gid, $question, $questionType, $surveyformat, $isPreviousPageQuestion, $questionList);
$previousQuestion = $question;
} else {
break;
}
}
return $questionList;
}
/**
* @param integer $gid
* @param integer $qid
*/
private function _shouldAddQuestion($action, $gid, $qid, array $question, $previousQuestion)
{
switch ($action)
{
case 'addgroup':
return true;
case 'editgroup':
case 'editgroup_desc':
case 'translategroup':
if (empty($gid)) {
safeDie("No GID provided.");
}
if ($question['gid'] == $gid) {
return false;
}
return true;
case 'addquestion':
if (empty($gid)) {
safeDie("No GID provided. Please save the question and try again.");
}
if (!is_null($previousQuestion) && $previousQuestion['gid'] == $gid && $question['gid'] != $gid ) {
return false;
}
return true;
case 'editanswer':
case 'copyquestion':
case 'editquestion':
case 'translatequestion':
case 'translateanswer':
if (empty($gid)) {
safeDie("No GID provided.");
}
if (empty($qid)) {
safeDie("No QID provided.");
}
if ($question['gid'] == $gid && $question['qid'] == $qid) {
return false;
}
return true;
case 'editemailtemplates':
// this is the case for email-conf
return true;
default:
safeDie("No Action provided.");
}
}
/**
* @param integer $gid
*/
private function _addQuestionToList($action, $gid, array $field, $questionType, $surveyformat, $isPreviousPageQuestion, &$questionList)
{
if ($action == 'tokens' && $questionType == 'email-conf' || $surveyformat == "S") {
$isPreviousPageQuestion = true;
}
elseif ($surveyformat == "G")
{
if ($isPreviousPageQuestion === true) { // Last question was on a previous page
if ($field["gid"] == $gid) { // This question is on same page
$isPreviousPageQuestion = false;
}
}
}
elseif ($surveyformat == "A")
{
$isPreviousPageQuestion = false;
}
$questionList[] = array_merge($field, Array("previouspage" => $isPreviousPageQuestion));
return $isPreviousPageQuestion;
}
private function _getChildQuestions(array $questions)
{
$cquestions = array();
foreach ($questions as $row)
{
$question = $row['question'];
if (isset($row['subquestion'])) {
$question = "[{$row['subquestion']}] " . $question;
}
if (isset($row['subquestion1'])) {
$question = "[{$row['subquestion1']}] " . $question;
}
if (isset($row['subquestion2'])) {
$question = "[{$row['subquestion2']}] " . $question;
}
$shortquestion = $row['title'] . ": " . flattenText($question);
$cquestions[] = array($shortquestion, $row['qid'], $row['type'], $row['fieldname'], $row['previouspage']);
}
return $cquestions;
}
/**
* @param integer $surveyid
*/
private function _getReplacementFields($fieldtype, $surveyid)
{
$replFields = array();
if(!$surveyid)
return array($replFields, false);
switch ($fieldtype)
{
case 'survey-desc':
case 'survey-welc':
case 'survey-endtext':
case 'edittitle': // for translation
case 'editdescription': // for translation
case 'editwelcome': // for translation
case 'editend': // for translation
$replFields['TOKEN:FIRSTNAME']= gT("First name from token");
$replFields['TOKEN:LASTNAME']= gT("Last name from token");
$replFields['TOKEN:EMAIL']= gT("Email from the token");
$attributes = getTokenFieldsAndNames($surveyid, true);
foreach ($attributes as $attributefield => $attributedescription)
{
$replFields['TOKEN:' . strtoupper($attributefield)]= sprintf(gT("Token attribute: %s"), $attributedescription['description']);
}
$replFields['EXPIRY']= gT("Survey expiration date");
$replFields['ADMINNAME']= gT("Name of the survey administrator");
$replFields['ADMINEMAIL']= gT("Email address of the survey administrator");
return array($replFields, false);
case 'email-admin_notification':
case 'email-admin_detailed_notification':
$replFields['RELOADURL']= gT("Reload URL");
$replFields['VIEWRESPONSEURL']= gT("View response URL");
$replFields['EDITRESPONSEURL']= gT("Edit response URL");
$replFields['STATISTICSURL']= gT("Statistics URL");
$replFields['TOKEN']= gT("Token code for this participant");
$replFields['TOKEN:FIRSTNAME']= gT("First name from token");
$replFields['TOKEN:LASTNAME']= gT("Last name from token");
$replFields['SURVEYNAME']= gT("Survey title");
$replFields['SID']= gT("Survey ID");
$replFields['SURVEYDESCRIPTION']= gT("Survey description");
$attributes = getTokenFieldsAndNames($surveyid, true);
foreach ($attributes as $attributefield => $attributedescription) {
$replFields[strtoupper($attributefield)] = sprintf(gT("Token attribute: %s"), $attributedescription['description']);
}
$replFields['ADMINNAME']= gT("Name of the survey administrator");
$replFields['ADMINEMAIL']= gT("Email address of the survey administrator");
return array($replFields, false);
case 'email-admin-resp':
$replFields['RELOADURL']= gT("Reload URL");
$replFields['VIEWRESPONSEURL']= gT("View response URL");
$replFields['EDITRESPONSEURL']= gT("Edit response URL");
$replFields['STATISTICSURL']= gT("Statistics URL");
$replFields['ANSWERTABLE']= gT("Answers from this response");
$replFields['TOKEN']= gT("Token code for this participant");
$replFields['TOKEN:FIRSTNAME']= gT("First name from token");
$replFields['TOKEN:LASTNAME']= gT("Last name from token");
$replFields['SURVEYNAME']= gT("Survey title");
$replFields['SID']= gT("Survey ID");
$replFields['SURVEYDESCRIPTION']= gT("Survey description");
$attributes = getTokenFieldsAndNames($surveyid, true);
foreach ($attributes as $attributefield => $attributedescription)
{
$replFields[strtoupper($attributefield)] = sprintf(gT("Token attribute: %s"), $attributedescription['description']);
}
$replFields['ADMINNAME']= gT("Name of the survey administrator");
$replFields['ADMINEMAIL']= gT("Email address of the survey administrator");
return array($replFields, false);
case 'email-invitation':
case 'email-reminder':
// these 2 fields are supported by email-inv and email-rem
// but not email-reg for the moment
$replFields['EMAIL']= gT("Email from the token");
$replFields['TOKEN']= gT("Token code for this participant");
$replFields['OPTOUTURL']= gT("URL for a respondent to opt-out of this survey");
$replFields['OPTINURL']= gT("URL for a respondent to opt-in to this survey");
// $replFields['SID']= gT("Survey ID");
case 'email-registration':
$replFields['FIRSTNAME']= gT("First name from token");
$replFields['LASTNAME']= gT("Last name from token");
$replFields['SURVEYNAME']= gT("Survey title");
$replFields['SID']= gT("Survey ID");
$replFields['SURVEYDESCRIPTION']= gT("Survey description");
$attributes = getTokenFieldsAndNames($surveyid, true);
foreach ($attributes as $attributefield => $attributedescription)
{
$replFields[strtoupper($attributefield)] = sprintf(gT("Token attribute: %s"), $attributedescription['description']);
}
$replFields['ADMINNAME']= gT("Name of the survey administrator");
$replFields['ADMINEMAIL']= gT("Email address of the survey administrator");
$replFields['SURVEYURL']= gT("URL of the survey");
$replFields['EXPIRY']= gT("Survey expiration date");
return array($replFields, false);
case 'email-confirmation':
$replFields['TOKEN']= gT("Token code for this participant");
$replFields['FIRSTNAME']= gT("First name from token");
$replFields['LASTNAME']= gT("Last name from token");
$replFields['EMAIL']= gT("Email from token");
$replFields['SURVEYNAME']= gT("Survey title");
$replFields['SID']= gT("Survey ID");
$replFields['SURVEYDESCRIPTION']= gT("Survey description");
$attributes = getTokenFieldsAndNames($surveyid, true);
foreach ($attributes as $attributefield => $attributedescription)
{
$replFields[strtoupper($attributefield)]= sprintf(gT("Token attribute: %s"), $attributedescription['description']);
}
$replFields['ADMINNAME']= gT("Name of the survey administrator");
$replFields['ADMINEMAIL']= gT("Email address of the survey administrator");
$replFields['SURVEYURL']= gT("URL of the survey");
$replFields['EXPIRY']= gT("Survey expiration date");
// email-conf can accept insertans fields for non anonymous surveys
if (isset($surveyid)) {
$oSurvey = Survey::model()->findByPk($surveyid);
if ($oSurvey->anonymized == "N") {
return array($replFields, true);
}
}
return array($replFields, false);
case 'group-desc':
case 'question-text':
case 'question-help':
case 'editgroup': // for translation
case 'editgroup_desc': // for translation
case 'editquestion': // for translation
case 'editquestion_help': // for translation
$replFields['TOKEN:FIRSTNAME']= gT("First name from token");
$replFields['TOKEN:LASTNAME']= gT("Last name from token");
$replFields['TOKEN:EMAIL']= gT("Email from the token");
$replFields['SID']= gT("This question's survey ID number");
$replFields['GID']= gT("This question's group ID number");
$replFields['QID']= gT("This question's question ID number");
$replFields['SGQ']= gT("This question's SGQA code");
$attributes = getTokenFieldsAndNames($surveyid, true);
foreach ($attributes as $attributefield => $attributedescription)
{
$replFields['TOKEN:' . strtoupper($attributefield)]= sprintf(gT("Token attribute: %s"), $attributedescription['description']);
}
$replFields['EXPIRY']= gT("Survey expiration date");
case 'editanswer':
return array($replFields, true);
case 'assessment-text':
$replFields['TOTAL']= gT("Overall assessment score");
$replFields['PERC']= gT("Assessment group score");
return array($replFields, false);
}
}
}