-
Notifications
You must be signed in to change notification settings - Fork 988
/
limereplacementfields.php
499 lines (424 loc) · 21.6 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?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 = (int) App()->request->getQuery('surveyid');
$gid = (int) App()->request->getQuery('gid');
$qid = (int) App()->request->getQuery('qid');
$newType = (bool) App()->request->getQuery('newtype', 0);
$survey = Survey::model()->findByPk($surveyid);
$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);
}
if ($newType) {
$this->getNewTypeResponse($fieldtype, $surveyid, $gid, $qid );
return;
}
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);
$surveyformat = $survey->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 = [];
$data['countfields'] = count($replacementFields);
$data['replacements'] = [];
asort($replacementFields);
$data['replacements'][gt('General replacements')] = $replacementFields;
if (isset($childQuestions)) {
$data['replacements'][gT('Questions')] = $childQuestions;
}
if (isset($surveyformat)) {
$data['replacements'][gT('Surveyformat')] = $surveyformat;
}
$this->getController()->renderPartial('/admin/super/_renderJson', ['data' => $data]);
return;
}
/**
* @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
* @param string $surveyformat
*/
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;
}
/**
* Collect the general replacements
*
* @param string $fieldtype The field to collect replacements for
* @param integer $surveyid The transferred surveyid
*
* @return array
*/
private function _getReplacementFields($fieldtype, $surveyid)
{
$oSurvey = Survey::model()->findByPk($surveyid);
$replFields = array();
// The only time no survey id is necessary is in the global settings
if ($fieldtype === 'globalSurveySettings') {
$replFields['TOKEN:FIRSTNAME'] = gT("First name from participant");
$replFields['TOKEN:LASTNAME'] = gT("Last name from participant");
$replFields['TOKEN:EMAIL'] = gT("Email from the participant");
$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);
}
if (!$surveyid) {
return array($replFields, false);
}
if (strpos($fieldtype, 'survey-desc') !== false
|| strpos($fieldtype, 'survey-welc') !== false
|| strpos($fieldtype, 'survey-endtext') !== false
|| strpos($fieldtype, 'edittitle') !== false// for translation
|| strpos($fieldtype, 'editdescription') !== false// for translation
|| strpos($fieldtype, 'editwelcome') !== false// for translation
|| strpos($fieldtype, 'editend') !== false
) { // for translation
$replFields['TOKEN:FIRSTNAME'] = gT("First name from participant");
$replFields['TOKEN:LASTNAME'] = gT("Last name from participant");
$replFields['TOKEN:EMAIL'] = gT("Email from the participant");
$attributes = getTokenFieldsAndNames($surveyid, true);
foreach ($attributes as $attributefield => $attributedescription) {
$replFields['TOKEN:' . strtoupper($attributefield)] = sprintf(gT("Participant 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);
} elseif (strpos($fieldtype, 'email_admin_notification') !== false
|| strpos($fieldtype, 'email_admin_detailed_notification') !== false
) {
$replFields['VIEWRESPONSEURL'] = gT("View response URL");
$replFields['EDITRESPONSEURL'] = gT("Edit response URL");
$replFields['STATISTICSURL'] = gT("Statistics URL");
$replFields['TOKEN'] = gT("Access code for this participant");
$replFields['TOKEN:FIRSTNAME'] = gT("First name from participant");
$replFields['TOKEN:LASTNAME'] = gT("Last name from participant");
$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("Participant 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);
} elseif (strpos($fieldtype, 'email-admin-resp') !== false) {
$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("Access code for this participant");
$replFields['TOKEN:FIRSTNAME'] = gT("First name from participant");
$replFields['TOKEN:LASTNAME'] = gT("Last name from participant");
$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("Participant 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);
} elseif (strpos($fieldtype, 'email_invitation') !== false
|| strpos($fieldtype, 'email_reminder') !== false
) {
// these 2 fields are supported by email-inv and email-rem
// but not email-reg for the moment
$replFields['EMAIL'] = gT("Email from the participant");
$replFields['TOKEN'] = gT("Access 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['FIRSTNAME'] = gT("First name from participant");
$replFields['LASTNAME'] = gT("Last name from participant");
$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("Participant 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);
// $replFields['SID']= gT("Survey ID");
} elseif (strpos($fieldtype, 'email_registration') !== false) {
$replFields['FIRSTNAME'] = gT("First name from participant");
$replFields['LASTNAME'] = gT("Last name from participant");
$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("Participant 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);
} elseif (strpos($fieldtype, 'email_confirmation') !== false) {
$replFields['TOKEN'] = gT("Access code for this participant");
$replFields['FIRSTNAME'] = gT("First name from participant");
$replFields['LASTNAME'] = gT("Last name from participant");
$replFields['EMAIL'] = gT("Email from participant");
$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("Participant 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 (!empty($oSurvey)) {
if (!$oSurvey->isAnonymized) {
return array($replFields, true);
}
}
return array($replFields, false);
} elseif (strpos($fieldtype, 'group-desc') !== false
|| strpos($fieldtype, 'question-text') !== false
|| strpos($fieldtype, 'question-help') !== false
|| strpos($fieldtype, 'editgroup') !== false // for translation
|| strpos($fieldtype, 'editgroup_desc') !== false // for translation
|| strpos($fieldtype, 'editquestion') !== false // for translation
|| strpos($fieldtype, 'editquestion_help') !== false // for translation
) {
$replFields['TOKEN:FIRSTNAME'] = gT("First name from participant");
$replFields['TOKEN:LASTNAME'] = gT("Last name from participant");
$replFields['TOKEN:EMAIL'] = gT("Email from the participant");
$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("Participant attribute: %s"), $attributedescription['description']);
}
$replFields['EXPIRY'] = gT("Survey expiration date");
return array($replFields, true);
} elseif (strpos($fieldtype, 'editanswer') !== false) {
$replFields['TOKEN:FIRSTNAME'] = gT("First name from participant");
$replFields['TOKEN:LASTNAME'] = gT("Last name from participant");
$replFields['TOKEN:EMAIL'] = gT("Email from the participant");
$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("Participant attribute: %s"), $attributedescription['description']);
}
$replFields['EXPIRY'] = gT("Survey expiration date");
return array($replFields, true);
} elseif (strpos($fieldtype, 'assessment-text') !== false) {
$replFields['TOTAL'] = gT("Overall assessment score");
$replFields['PERC'] = gT("Assessment group score");
return array($replFields, false);
} else {
return [[], false];
}
}
public function getNewTypeResponse($fieldtype, $surveyid = null, $gid = null, $qid = null)
{
$returnArray = [];
$generalArray = $this->_getReplacementFields($fieldtype, $surveyid);
foreach ($generalArray[0] as $key => $value) {
$returnArray[gT('General')][$key] = [
"type" => 'general',
"value" => $value
];
}
if ($qid != null || $gid != null || $generalArray[1]) {
$returnArray[gT('Questions')] = $this->collectQuestionReplacements($surveyid, $gid, $qid);
}
$this->getController()->renderPartial('/admin/super/_renderJson', ['data' => $returnArray]);
return;
}
private function collectQuestionReplacements($surveyid, $gid = null, $qid = null) {
$oSurvey = Survey::model()->findByPk($surveyid);
$oCurrentQuestion = Question::model()->findByPk($qid);
$aResult = [];
$oCriteria = new CDbCriteria();
$oCriteria->compare('t.sid', $surveyid);
$oCriteria->compare('parent_qid', 0);
if ($gid != null && $qid == null) {
$oGroup = QuestionGroup::model()->findByPk($gid);
$oCriteria->with = ['group'];
$oCriteria->compare('group_order', '<='.$oGroup->group_order,);
}
if ($qid != null) {
$oCriteria->with = ['group'];
$oCriteria->compare('group_order', '<='.$oCurrentQuestion->group->group_order);
if ($oCurrentQuestion->parent_qid != 0) {
$oCriteria->compare('question_order', '<'.$oCurrentQuestion->parent->question_order);
} else {
$oCriteria->compare('question_order', '<'.$oCurrentQuestion->question_order );
}
}
$aQuestions = Question::model()->findAll($oCriteria);
uasort(
$aQuestions,
function ($a,$b) {
if ($a->gid != $b->gid) {
return $a->group->group_order < $b->group->group_order ? -1 : 1;
}
return $a->question_order < $b->question_order ? -1 : 1;
});
foreach ($aQuestions as $oQuestion) {
if ($oCurrentQuestion != null && $oCurrentQuestion->qid == $oQuestion->qid) {
continue;
}
if (safecount($oQuestion->subquestions) != 0) {
$aSubquestions = $oQuestion->subquestions;
uasort($aSubquestions, function ($a,$b) { return $a->question_order < $b->question_order ? -1 : 1; });
foreach ($aSubquestions as $oSubQuestion) {
$aResult[$oQuestion->title.'_'.$oSubQuestion->title] = [
'type' => 'question',
'value' => ' -('.$oQuestion->title.')| '.$oSubQuestion->questionL10ns[$oSurvey->language]->question
];
}
} else {
$aResult[$oQuestion->title] = [
'type' => 'question',
'value' => $oQuestion->questionL10ns[$oSurvey->language]->question,
];
}
}
return $aResult;
}
}