-
Notifications
You must be signed in to change notification settings - Fork 996
/
qanda_helper.php
4960 lines (4405 loc) · 220 KB
/
qanda_helper.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* 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.
*/
// Security Checked: POST, GET, SESSION, REQUEST, returnGlobal, DB
//if (!isset($homedir) || isset($_REQUEST['$homedir'])) {die("Cannot run this script directly");}
/*
* Let's explain what this strange $ia var means
*
* The $ia string comes from the $_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['insertarray'] variable which is built at the commencement of the survey.
* See index.php, function "buildsurveysession()"
* One $ia array zexists for every question in the survey. The $_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['insertarray']
* string is an array of $ia arrays.
*
* $ia[0] => question id
* $ia[1] => fieldname
* $ia[2] => title
* $ia[3] => question text
* $ia[4] => type -- text, radio, select, array, etc
* $ia[5] => group id
* $ia[6] => mandatory Y || S || N
* $ia[7] => conditions exist for this question
* $ia[8] => other questions have conditions which rely on this question (including array_filter and array_filter_exclude attributes)
* $ia[9] => incremental question count (used by {QUESTION_NUMBER})
*
* $conditions element structure
* $condition[n][0] => qid = question id
* $condition[n][1] => cqid = question id of the target question, or 0 for TokenAttr leftOperand
* $condition[n][2] => field name of element [1] (Except for type M or P)
* $condition[n][3] => value to be evaluated on answers labeled.
* $condition[n][4] => type of question
* $condition[n][5] => SGQ code of element [1] (sub-part of [2])
* $condition[n][6] => method used to evaluate
* $condition[n][7] => scenario *NEW BY R.L.J. van den Burg*
*/
// ==================================================================
// setting constants for 'checked' and 'selected' inputs
define('CHECKED', ' checked="checked"');
define('SELECTED', ' selected="selected"');
/**
* setNoAnswerMode
*/
function setNoAnswerMode($thissurvey)
{
if (App()->getConfig('shownoanswer') == 2) {
if ($thissurvey['shownoanswer'] == 'N') {
define('SHOW_NO_ANSWER', 0);
} else {
define('SHOW_NO_ANSWER', 1);
}
} elseif (App()->getConfig('shownoanswer') == 1) {
define('SHOW_NO_ANSWER', 1);
} elseif (App()->getConfig('shownoanswer') == 0) {
define('SHOW_NO_ANSWER', 0);
} else {
define('SHOW_NO_ANSWER', 1);
}
}
/**
* This function returns an array containing the "question/answer" html display
* and a list of the question/answer fieldnames associated. It is called from
* question.php, group.php, survey.php or preview.php
*
* @param array $ia Details of $ia can be found at top of this file
* @return array Array like [array $qanda, array $inputnames] where
* $qanda has elements [
* $qtitle (question_text) : array [
* all : string; complete HTML?; all has been added for backwards compatibility with templates that use question_start.pstpl (now redundant)
* 'text' => $qtitle, question?? $ia[3]?
* 'code' => $ia[2] or title??
* 'number' => $number
* 'help' => ''
* 'mandatory' => ''
* man_message : string; message when mandatory is not answered
* 'valid_message' => ''
* file_valid_message : string; only relevant for file upload
* 'class' => ''
* 'man_class' => ''
* 'input_error_class' => '' // provides a class.
* 'essentials' => ''
* ]
* $answer ?
* 'help' : string
* $display : ?
* $qid : integer
* $ia[2] = title;
* $ia[5] = group id : int
* $ia[1] = fieldname : string
* ]
* and $inputnames is ? used for hiddenfieldnames and upload file?
*
*/
function retrieveAnswers($ia)
{
//globalise required config variables
global $thissurvey; //These are set by index.php
// TODO: This can be cached in some special cases.
// 1. If move back is disabled
// 2. No tokens
// 3. Always first time it's shown to one user (and no tokens).
// 4. No expressions with tokens or time or other dynamic features.
if (EmCacheHelper::cacheQanda($ia, $_SESSION['survey_' . $thissurvey['sid']])) {
$cacheKey = 'retrieveAnswers_' . sha1(implode('_', $ia));
$value = EmCacheHelper::get($cacheKey);
if ($value !== false) {
return $value;
}
}
$display = $ia[7]; //DISPLAY
$qid = $ia[0]; // Question ID
$qtitle = $ia[3];
$inputnames = [];
$answer = ""; //Create the question/answer html
$number = $ia[9] ?? ''; // Previously in limesurvey, it was virtually impossible to control how the start of questions were formatted. // this is an attempt to allow users (or rather system admins) some control over how the starting text is formatted.
$aQuestionAttributes = QuestionAttribute::model()->getQuestionAttributes($ia[0]);
$question_text = array(
'all' => '' // All has been added for backwards compatibility with templates that use question_start.pstpl (now redundant)
,'text' => $qtitle
,'code' => $ia[2]
,'number' => $number
,'help' => ''
,'mandatory' => ''
,'man_message' => ''
,'valid_message' => ''
,'file_valid_message' => ''
,'class' => ''
,'man_class' => ''
,'input_error_class' => '' // provides a class.
,'essentials' => ''
);
$oQuestion = Question::model()->findByPk($ia[0]);
$oQuestionTemplate = QuestionTemplate::getNewInstance($oQuestion);
$oQuestionTemplate->registerAssets(); // Register the custom assets of the question template, if needed
$oRenderer = $oQuestion->getRenderererObject($ia);
$values = $oRenderer->render();
if (isset($values)) {
//Break apart $values array returned from switch
//$answer is the html code to be printed
//$inputnames is an array containing the names of each input field
list($answer, $inputnames) = $values;
}
$question_text['mandatory'] = $ia[6];
//If this question is mandatory but wasn't answered in the last page
//add a message HIGHLIGHTING the question
$mandatory_msg = (($_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['step'] != $_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['maxstep']) || ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['step'] == $_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['prevstep'])) ? mandatory_message($ia) : '';
$qtitle .= $mandatory_msg;
$question_text['man_message'] = $mandatory_msg;
//show or hide tip
$_vshow = false;
if (isset($aQuestionAttributes['hide_tip'])) {
$_vshow = $aQuestionAttributes['hide_tip'] == 0; //hide_tip=0 means: show the tip
}
list($validation_msg, $isValid) = validation_message($ia, $_vshow);
$qtitle .= $validation_msg;
$question_text['valid_message'] = $validation_msg;
if (($_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['step'] != $_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['maxstep']) || ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['step'] == $_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['prevstep'])) {
$file_validation_msg = file_validation_message($ia);
} else {
$file_validation_msg = '';
$isValid = true; // don't want to show any validation messages.
}
$qtitle .= $ia[4] == "|" ? $file_validation_msg : "";
$question_text['file_valid_message'] = $ia[4] == "|" ? $file_validation_msg : "";
if (!empty($question_text['man_message']) || !$isValid || !empty($question_text['file_valid_message'])) {
$question_text['input_error_class'] = ' input-error'; // provides a class to style question wrapper differently if there is some kind of user input error;
}
// =====================================================
// START: legacy question_start.pstpl code
// The following section adds to the templating system by allowing
// templaters to control where the various parts of the question text
// are put.
$sTemplate = $thissurvey['template'] ?? null;
if (is_file('templates/' . $sTemplate . '/question_start.pstpl')) {
$replace = [];
$find = [];
foreach ($question_text as $key => $value) {
$find[] = '{QUESTION_' . strtoupper($key) . '}'; // Match key words from template
$replace[] = $value; // substitue text
};
if (!defined('QUESTION_START')) {
define('QUESTION_START', file_get_contents(getTemplatePath($thissurvey['template']) . '/question_start.pstpl'));
};
$qtitle_custom = str_replace($find, $replace, (string) QUESTION_START);
$c = 1;
// START: <EMBED> work-around step 1
$qtitle_custom = preg_replace('/(<embed[^>]+>)(<\/embed>)/i', '\1NOT_EMPTY\2', $qtitle_custom);
// END <EMBED> work-around step 1
while ($c > 0) {
// This recursively strips any empty tags to minimise rendering bugs.
$oldtitle = $qtitle_custom;
$qtitle_custom = preg_replace('/<([^ >]+)[^>]*>[\r\n\t ]*<\/\1>[\r\n\t ]*/isU', '', $qtitle_custom, -1); // I removed the $count param because it is PHP 5.1 only.
$c = ($qtitle_custom != $oldtitle) ? 1 : 0;
};
// START <EMBED> work-around step 2
$qtitle_custom = preg_replace('/(<embed[^>]+>)NOT_EMPTY(<\/embed>)/i', '\1\2', $qtitle_custom);
// END <EMBED> work-around step 2
while ($c > 0) {
// This recursively strips any empty tags to minimise rendering bugs.
$oldtitle = $qtitle_custom;
$qtitle_custom = preg_replace('/(<br(?: ?\/)?>(?: |\r\n|\n\r|\r|\n| )*)+$/i', '', $qtitle_custom, -1); // I removed the $count param because it is PHP 5.1 only.
$c = ($qtitle_custom != $oldtitle) ? 1 : 0;
};
$question_text['all'] = $qtitle_custom;
} else {
$question_text['all'] = $qtitle;
};
// END: legacy question_start.pstpl code
//===================================================================
$qtitle = $question_text;
// =====================================================
$qanda = array($qtitle, $answer, 'help', $display, $qid, $ia[2], $ia[5], $ia[1]);
if (EmCacheHelper::cacheQanda($ia, $_SESSION['survey_' . $thissurvey['sid']])) {
EmCacheHelper::set($cacheKey, [$qanda, $inputnames]);
}
//New Return
return array($qanda, $inputnames);
}
function mandatory_message($ia)
{
$qinfo = LimeExpressionManager::GetQuestionStatus($ia[0]);
$qinfoValue = ($qinfo['mandViolation']) ? $qinfo['mandTip'] : "";
return $qinfoValue;
}
/**
*
* @param array $ia
* @param boolean $show - true if should initially be visible
* @return array
*/
function validation_message($ia, $show)
{
$qinfo = LimeExpressionManager::GetQuestionStatus($ia[0]);
$class = (!$show) ? ' hide-tip' : '';
$id = "vmsg_" . $ia[0];
$message = $qinfo['validTip'];
if ($message != "") {
$tip = doRender('/survey/questions/question_help/help', array('message' => $message, 'classes' => $class, 'id' => $id), true);
} else {
$tip = "";
}
$isValid = $qinfo['valid'];
return array($tip, $isValid);
}
// TMSW Validation -> EM
function file_validation_message($ia)
{
global $filenotvalidated;
$qtitle = "";
if (isset($filenotvalidated) && is_array($filenotvalidated) && $ia[4] == "|") {
foreach ($filenotvalidated as $k => $v) {
if ($ia[1] == $k || strpos($k, "_") && $ia[1] == substr(0, strpos($k, "_") - 1)) {
$message = gT($filenotvalidated[$k]);
$qtitle .= doRender('/survey/questions/question_help/error', array('message' => $message, 'classes' => ''), true);
}
}
}
return $qtitle;
}
// TMSW Validation -> EM
function mandatory_popup($ia, $notanswered = null)
{
global $mandatorypopup, $popup;
//This sets the mandatory popup message to show if required
//Called from question.php, group.php or survey.php
if ($notanswered === null) {
unset($notanswered);
}
if (isset($notanswered) && is_array($notanswered)) {
//ADD WARNINGS TO QUESTIONS IF THEY WERE MANDATORY BUT NOT ANSWERED
//POPUP WARNING
// If there is no "hard" mandatory violation (both current and previous violations belong to Soft Mandatory questions),
// we show the soft mandatory message.
if ($ia[6] == 'S' && (!isset($mandatorypopup) || $mandatorypopup == 'S')) {
$popup = gT("One or more mandatory questions have not been answered. If possible, please complete them before continuing to the next page.");
$mandatorypopup = "S";
} elseif (!isset($mandatorypopup) && ($ia[4] == 'T' || $ia[4] == 'S' || $ia[4] == 'U')) {
// If
$popup = gT("You cannot proceed until you enter some text for one or more questions.");
$mandatorypopup = "Y";
} else {
$popup = gT("One or more mandatory questions have not been answered. You cannot proceed until these have been completed.");
$mandatorypopup = "Y";
}
return array($mandatorypopup, $popup);
} else {
return false;
}
}
// TMSW Validation -> EM
function validation_popup($ia, $notvalidated = null)
{
global $validationpopup, $vpopup;
//This sets the validation popup message to show if required
//Called from question.php, group.php or survey.php
if ($notvalidated === null) {
unset($notvalidated);
}
if (isset($notvalidated) && is_array($notvalidated)) {
//ADD WARNINGS TO QUESTIONS IF THEY ARE NOT VALID
//POPUP WARNING
if (!isset($validationpopup)) {
$vpopup = gT("One or more questions have not been answered in a valid manner. You cannot proceed until these answers are valid.");
$validationpopup = "Y";
}
return array($validationpopup, $vpopup);
} else {
return false;
}
}
// TMSW Validation -> EM
/**
* @param boolean $filenotvalidated
*/
function file_validation_popup($ia, $filenotvalidated = null)
{
global $filevalidationpopup, $fpopup;
if ($filenotvalidated === null) {
unset($filenotvalidated);
}
if (isset($filenotvalidated) && is_array($filenotvalidated)) {
if (!isset($filevalidationpopup)) {
$fpopup = gT("One or more file have either exceeded the filesize/are not in the right format or the minimum number of required files have not been uploaded. You cannot proceed until these have been completed");
$filevalidationpopup = "Y";
}
return array($filevalidationpopup, $fpopup);
} else {
return false;
}
}
/**
* @param string $disable
* @return string
* @todo : check if really deprecated (date : 20240902)
*/
function return_timer_script($aQuestionAttributes, $ia, $disable = null)
{
global $thissurvey;
global $gid;
$time_limit = intval($aQuestionAttributes['time_limit']);
if($time_limit <= 0) {
return;
}
Yii::app()->getClientScript()->registerScriptFile(Yii::app()->getConfig("generalscripts") . 'coookies.js', CClientScript::POS_BEGIN);
Yii::app()->getClientScript()->registerPackage('timer-addition');
$langTimer = array(
'hours' => gT("hours"),
'mins' => gT("mins"),
'seconds' => gT("seconds"),
);
/* Registering script : don't go to EM : no need usage of ls_json_encode */
App()->getClientScript()->registerScript("LSVarLangTimer", "LSvar.lang.timer=" . json_encode($langTimer) . ";", CClientScript::POS_BEGIN);
/**
* The following lines cover for previewing questions, because no $_SESSION['survey_'.Yii::app()->getConfig('surveyID')]['fieldarray'] exists.
* This just stops error messages occuring
*/
if (!isset($_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['fieldarray'])) {
$_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['fieldarray'] = [];
}
/* End */
//Used to count how many timer questions in a page, and ensure scripts only load once
$thissurvey['timercount'] = (isset($thissurvey['timercount'])) ? $thissurvey['timercount']++ : 1;
$disable_next = trim((string) $aQuestionAttributes['time_limit_disable_next']) != '' ? $aQuestionAttributes['time_limit_disable_next'] : 0;
$disable_prev = trim((string) $aQuestionAttributes['time_limit_disable_prev']) != '' ? $aQuestionAttributes['time_limit_disable_prev'] : 0;
$time_limit_action = trim((string) $aQuestionAttributes['time_limit_action']) != '' ? $aQuestionAttributes['time_limit_action'] : 1;
$time_limit_message = trim((string) $aQuestionAttributes['time_limit_message'][$_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['s_lang']]) != '' ? htmlspecialchars((string) $aQuestionAttributes['time_limit_message'][$_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['s_lang']], ENT_QUOTES) : gT("Your time to answer this question has expired");
$time_limit_warning = trim((string) $aQuestionAttributes['time_limit_warning']) != '' ? intval($aQuestionAttributes['time_limit_warning']) : 0;
$time_limit_warning_2 = trim((string) $aQuestionAttributes['time_limit_warning_2']) != '' ? intval($aQuestionAttributes['time_limit_warning_2']) : 0;
$time_limit_countdown_message = trim((string) $aQuestionAttributes['time_limit_countdown_message'][$_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['s_lang']]) != '' ? htmlspecialchars((string) $aQuestionAttributes['time_limit_countdown_message'][$_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['s_lang']], ENT_QUOTES) : gT("Time remaining");
$time_limit_warning_message = trim((string) $aQuestionAttributes['time_limit_warning_message'][$_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['s_lang']]) != '' ? htmlspecialchars((string) $aQuestionAttributes['time_limit_warning_message'][$_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['s_lang']], ENT_QUOTES) : gT("Your time to answer this question has nearly expired. You have {TIME} remaining.");
//Render timer
$timer_html = Yii::app()->twigRenderer->renderQuestion('/survey/questions/question_timer/timer', array('iQid' => $ia[0], 'sWarnId' => ''), true);
$time_limit_warning_message = str_replace("{TIME}", $timer_html, $time_limit_warning_message);
$time_limit_warning_display_time = trim((string) $aQuestionAttributes['time_limit_warning_display_time']) != '' ? intval($aQuestionAttributes['time_limit_warning_display_time']) + 1 : 0;
$time_limit_warning_2_message = trim((string) $aQuestionAttributes['time_limit_warning_2_message'][$_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['s_lang']]) != '' ? htmlspecialchars((string) $aQuestionAttributes['time_limit_warning_2_message'][$_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['s_lang']], ENT_QUOTES) : gT("Your time to answer this question has nearly expired. You have {TIME} remaining.");
//Render timer 2
$timer_html = Yii::app()->twigRenderer->renderQuestion('/survey/questions/question_timer/timer', array('iQid' => $ia[0], 'sWarnId' => '_Warning_2'), true);
$time_limit_message_delay = trim((string) $aQuestionAttributes['time_limit_message_delay']) != '' ? intval($aQuestionAttributes['time_limit_message_delay']) * 1000 : 1000;
$time_limit_warning_2_message = str_replace("{TIME}", $timer_html, $time_limit_warning_2_message);
$time_limit_warning_2_display_time = trim((string) $aQuestionAttributes['time_limit_warning_2_display_time']) != '' ? intval($aQuestionAttributes['time_limit_warning_2_display_time']) + 1 : 0;
$time_limit_message_style = trim((string) $aQuestionAttributes['time_limit_message_style']) != '' ? $aQuestionAttributes['time_limit_message_style'] : "";
$time_limit_message_class = "d-none ls-timer-content ls-timer-message ls-no-js-hidden";
$time_limit_warning_style = trim((string) $aQuestionAttributes['time_limit_warning_style']) != '' ? $aQuestionAttributes['time_limit_warning_style'] : "";
$time_limit_warning_class = "d-none ls-timer-content ls-timer-warning ls-no-js-hidden";
$time_limit_warning_2_style = trim((string) $aQuestionAttributes['time_limit_warning_2_style']) != '' ? $aQuestionAttributes['time_limit_warning_2_style'] : "";
$time_limit_warning_2_class = "d-none ls-timer-content ls-timer-warning2 ls-no-js-hidden";
$time_limit_timer_style = trim((string) $aQuestionAttributes['time_limit_timer_style']) != '' ? $aQuestionAttributes['time_limit_timer_style'] : "position: relative;";
$time_limit_timer_class = "ls-timer-content ls-timer-countdown ls-no-js-hidden";
$timersessionname = "timer_question_" . $ia[0];
if (isset($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$timersessionname])) {
$time_limit = $_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$timersessionname];
}
$output = Yii::app()->twigRenderer->renderQuestion('/survey/questions/question_timer/timer_header', array('timersessionname' => $timersessionname, 'time_limit' => $time_limit), true);
if ($thissurvey['timercount'] < 2) {
$iAction = '';
if (isset($thissurvey['format']) && $thissurvey['format'] == "G") {
$qcount = 0;
foreach ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['fieldarray'] as $ib) {
if ($ib[5] == $gid) {
$qcount++;
}
}
// Override all other options and just allow freezing, survey is presented in group by group mode
// Why don't allow submit in Group by group mode, this surely broke 'mandatory' question, but this remove a great system for user (Denis 140224)
if ($qcount > 1) {
$iAction = '3';
}
}
/* If this is a preview, don't allow the page to submit/reload */
$thisaction = returnglobal('action');
if ($thisaction == "previewquestion" || $thisaction == "previewgroup") {
$iAction = '3';
}
$output .= Yii::app()->twigRenderer->renderQuestion('/survey/questions/question_timer/timer_javascript', array(
'timersessionname' => $timersessionname,
'time_limit' => $time_limit,
'iAction' => $iAction,
'disable_next' => $disable_next,
'disable_prev' => $disable_prev,
'time_limit_countdown_message' => $time_limit_countdown_message,
'time_limit_message_delay' => $time_limit_message_delay
), true);
}
$output .= Yii::app()->twigRenderer->renderQuestion(
'/survey/questions/question_timer/timer_content',
array(
'iQid' => $ia[0],
'time_limit_message_style' => $time_limit_message_style,
'time_limit_message_class' => $time_limit_message_class,
'time_limit_message' => $time_limit_message,
'time_limit_warning_style' => $time_limit_warning_style,
'time_limit_warning_class' => $time_limit_warning_class,
'time_limit_warning_message' => $time_limit_warning_message,
'time_limit_warning_2_style' => $time_limit_warning_2_style,
'time_limit_warning_2_class' => $time_limit_warning_2_class,
'time_limit_warning_2_message' => $time_limit_warning_2_message,
'time_limit_timer_style' => $time_limit_timer_style,
'time_limit_timer_class' => $time_limit_timer_class,
),
true
);
$output .= Yii::app()->twigRenderer->renderQuestion(
'/survey/questions/question_timer/timer_footer',
array(
'iQid' => $ia[0],
'iSid' => Yii::app()->getConfig('surveyID'),
'time_limit' => $time_limit,
'time_limit_action' => $time_limit_action,
'time_limit_warning' => $time_limit_warning,
'time_limit_warning_2' => $time_limit_warning_2,
'time_limit_warning_display_time' => $time_limit_warning_display_time,
'time_limit_warning_2_display_time' => $time_limit_warning_2_display_time,
'disable' => $disable,
),
true
);
return $output;
}
/**
* Return class of a specific row (hidden by relevance)
* @param int $surveyId actual survey ID
* @param string $baseName the base name of the question
* @param string $name The name of the question/row to test
* @param array $aQuestionAttributes the question attributes
* @return string
*/
function currentRelevecanceClass($surveyId, $baseName, $name, $aQuestionAttributes)
{
$relevanceStatus = !isset($_SESSION["survey_{$surveyId}"]['relevanceStatus'][$name]) || $_SESSION["survey_{$surveyId}"]['relevanceStatus'][$name];
if ($relevanceStatus) {
return "";
}
$sExcludeAllOther = isset($aQuestionAttributes['exclude_all_others']) ? trim((string) $aQuestionAttributes['exclude_all_others']) : '';
/* EM don't set difference between relevance in session, if exclude_all_others is set , just ls-disabled */
if ($sExcludeAllOther) {
foreach (explode(';', $sExcludeAllOther) as $sExclude) {
$sExclude = $baseName . $sExclude;
if (
(!isset($_SESSION["survey_{$surveyId}"]['relevanceStatus'][$sExclude]) || $_SESSION["survey_{$surveyId}"]['relevanceStatus'][$sExclude])
&& (isset($_SESSION["survey_{$surveyId}"][$sExclude]) && $_SESSION["survey_{$surveyId}"][$sExclude] == "Y")
) {
return "ls-irrelevant ls-disabled";
}
}
}
$filterStyle = !empty($aQuestionAttributes['array_filter_style']); // Currently null/0/false=> hidden , 1 : disabled
if ($filterStyle) {
return "ls-irrelevant ls-disabled";
}
return "ls-irrelevant ls-hidden";
}
/**
* @param string $rowname
*/
function return_display_style($ia, $aQuestionAttributes, $thissurvey, $rowname)
{
/* Disabled actually : no inline style */
return "";
}
/**
* @param string $rowname
* @param string $valuename
*/
function return_array_filter_strings($ia, $aQuestionAttributes, $thissurvey, $ansrow, $rowname, $trbc, $valuename, $method = "tbody", $class = null)
{
$htmltbody2 = "\n\n\t<$method id='javatbd$rowname'";
$htmltbody2 .= ($class !== null) ? " class='$class'" : "";
$surveyid = $thissurvey['sid'];
if (isset($_SESSION["survey_{$surveyid}"]['relevanceStatus'][$rowname]) && !$_SESSION["survey_{$surveyid}"]['relevanceStatus'][$rowname]) {
// If using exclude_all_others, then need to know whether irrelevant rows should be hidden or disabled
if (isset($aQuestionAttributes['exclude_all_others'])) {
$disableit = false;
foreach (explode(';', trim((string) $aQuestionAttributes['exclude_all_others'])) as $eo) {
$eorow = $ia[1] . $eo;
if (
(!isset($_SESSION["survey_{$surveyid}"]['relevanceStatus'][$eorow]) || $_SESSION["survey_{$surveyid}"]['relevanceStatus'][$eorow])
&& (isset($_SESSION[$eorow]) && $_SESSION[$eorow] == "Y")
) {
$disableit = true;
}
}
if ($disableit) {
$htmltbody2 .= " disabled='disabled'";
} else {
if (!isset($aQuestionAttributes['array_filter_style']) || $aQuestionAttributes['array_filter_style'] == '0') {
$htmltbody2 .= " style='display: none'";
} else {
$htmltbody2 .= " disabled='disabled'";
}
}
} else {
if (!isset($aQuestionAttributes['array_filter_style']) || $aQuestionAttributes['array_filter_style'] == '0') {
$htmltbody2 .= " style='display: none'";
} else {
$htmltbody2 .= " disabled='disabled'";
}
}
}
$htmltbody2 .= ">\n";
return array($htmltbody2, "");
}
/**
* @param string $sUseKeyPad
* @return string
*/
function testKeypad($sUseKeyPad)
{
if ($sUseKeyPad == 'Y') {
includeKeypad();
$kpclass = "text-keypad";
} else {
$kpclass = "";
}
return $kpclass;
}
// ==================================================================
// QUESTION METHODS =================================================
// ---------------------------------------------------------------
function do_language($ia)
{
$checkconditionFunction = "checkconditions";
$answerlangs = Survey::model()->findByPk(Yii::app()->getConfig('surveyID'))->additionalLanguages;
$answerlangs[] = Survey::model()->findByPk(Yii::app()->getConfig('surveyID'))->language;
$sLang = $_SESSION['survey_' . Yii::app()->getConfig('surveyID')]['s_lang'];
$coreClass = "ls-answers answer-item dropdow-item language-item";
$inputnames = [];
if (!in_array($sLang, $answerlangs)) {
$sLang = Survey::model()->findByPk(Yii::app()->getConfig('surveyID'))->language;
}
$inputnames[] = $ia[1];
$languageData = array(
'name' => $ia[1],
'basename' => $ia[1],
'checkconditionFunction' => $checkconditionFunction . '(this.value, this.name, this.type)',
'answerlangs' => $answerlangs,
'sLang' => $sLang,
'coreClass' => $coreClass,
);
$answer = doRender('/survey/questions/answer/language/answer', $languageData, true);
return array($answer, $inputnames);
}
// ---------------------------------------------------------------
// TMSW TODO - Can remove DB query by passing in answer list from EM
function do_list_dropdown($ia)
{
//// Init variables
$inputnames = [];
// General variables
$checkconditionFunction = "checkconditions";
// Question attribute variables
$aQuestionAttributes = QuestionAttribute::model()->getQuestionAttributes($ia[0]);
$iSurveyId = Yii::app()->getConfig('surveyID'); // survey ID
$sSurveyLang = $_SESSION['survey_' . $iSurveyId]['s_lang']; // survey language
$othertext = (trim((string) $aQuestionAttributes['other_replace_text'][$sSurveyLang]) != '') ? $aQuestionAttributes['other_replace_text'][$sSurveyLang] : gT('Other:'); // text for 'other'
$optCategorySeparator = (trim((string) $aQuestionAttributes['category_separator']) != '') ? $aQuestionAttributes['category_separator'] : '';
$coreClass = "ls-answers answer-item dropdown-item";
if ($optCategorySeparator == '') {
unset($optCategorySeparator);
}
//// Retrieving datas
// Getting question
$oQuestion = Question::model()->findByPk(array('qid' => $ia[0], 'language' => $sSurveyLang));
$other = $oQuestion->other;
// Getting answers
$ansresult = $oQuestion->getOrderedAnswers($aQuestionAttributes['random_order'], $aQuestionAttributes['alphasort']);
$dropdownSize = null;
if (isset($aQuestionAttributes['dropdown_size']) && $aQuestionAttributes['dropdown_size'] > 0) {
$_height = sanitize_int($aQuestionAttributes['dropdown_size']);
$_maxHeight = count($ansresult);
if ((!is_null($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]]) || $_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] === '') && ($ia[6] != 'Y' && $ia[6] != 'S') && SHOW_NO_ANSWER == 1) {
++$_maxHeight; // for No Answer
}
if (isset($other) && $other == 'Y') {
++$_maxHeight; // for Other
}
if (is_null($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]])) {
++$_maxHeight; // for 'Please choose:'
}
if ($_height > $_maxHeight) {
$_height = $_maxHeight;
}
$dropdownSize = $_height;
}
$prefixStyle = 0;
if (isset($aQuestionAttributes['dropdown_prefix'])) {
$prefixStyle = sanitize_int($aQuestionAttributes['dropdown_prefix']);
}
$_rowNum = 0;
$_prefix = '';
$value = $_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]];
$sOptions = '';
// If no answer previously selected
if (is_null($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]]) || $_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] === '') {
$sOptions .= doRender('/survey/questions/answer/list_dropdown/rows/option', array(
'name' => $ia[1],
'value' => '',
'opt_select' => ($dropdownSize) ? SELECTED : "", /* needed width size, not for single first one */
'answer' => gT('Please choose...')
), true);
}
if (!isset($optCategorySeparator)) {
foreach ($ansresult as $ansrow) {
$opt_select = '';
if ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] == $ansrow['code']) {
$opt_select = SELECTED;
}
if ($prefixStyle == 1) {
$_prefix = ++$_rowNum . ') ';
}
// ==> rows
$sOptions .= doRender('/survey/questions/answer/list_dropdown/rows/option', array(
'name' => $ia[1],
'value' => $ansrow['code'],
'opt_select' => $opt_select,
'answer' => $_prefix . $ansrow->answerl10ns[$sSurveyLang]->answer,
), true);
}
} else {
$defaultopts = [];
$optgroups = [];
foreach ($ansresult as $ansrow) {
// Let's sort answers in an array indexed by subcategories
@list($categorytext, $answertext) = explode($optCategorySeparator, (string) $ansrow->answerl10ns[$sSurveyLang]->answer);
// The blank category is left at the end outside optgroups
if ($categorytext == '') {
$defaultopts[] = array('code' => $ansrow['code'], 'answer' => $answertext);
} else {
$optgroups[$categorytext][] = array('code' => $ansrow['code'], 'answer' => $answertext);
}
}
foreach ($optgroups as $categoryname => $optionlistarray) {
$sOptGroupOptions = '';
foreach ($optionlistarray as $optionarray) {
if ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] == $optionarray['code']) {
$opt_select = SELECTED;
} else {
$opt_select = '';
}
// ==> rows
$sOptGroupOptions .= doRender('/survey/questions/answer/list_dropdown/rows/option', array(
'name' => $ia[1],
'value' => $optionarray['code'],
'opt_select' => $opt_select,
'answer' => $optionarray['answer']
), true);
}
$sOptions .= doRender('/survey/questions/answer/list_dropdown/rows/optgroup', array(
'categoryname' => $categoryname,
'sOptGroupOptions' => $sOptGroupOptions,
), true);
}
foreach ($defaultopts as $optionarray) {
if ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] == $optionarray['code']) {
$opt_select = SELECTED;
} else {
$opt_select = '';
}
// ==> rows
$sOptions .= doRender('/survey/questions/answer/list_dropdown/rows/option', array(
'name' => $ia[1],
'value' => $optionarray['code'],
'opt_select' => $opt_select,
'answer' => $optionarray['answer']
), true);
}
}
if (isset($other) && $other == 'Y') {
if ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] == '-oth-') {
$opt_select = SELECTED;
} else {
$opt_select = '';
}
if ($prefixStyle == 1) {
$_prefix = ++$_rowNum . ') ';
}
$sOptions .= doRender('/survey/questions/answer/list_dropdown/rows/option', array(
'name' => $ia[1],
'classes' => 'other-item',
'value' => '-oth-',
'opt_select' => $opt_select,
'answer' => $_prefix . $othertext
), true);
}
if (!(is_null($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]]) || $_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] === "") && ($ia[6] != 'Y' && $ia[6] != 'S') && SHOW_NO_ANSWER == 1) {
if ($prefixStyle == 1) {
$_prefix = ++$_rowNum . ') ';
}
$optionData = array(
'name' => $ia[1],
'classes' => 'noanswer-item',
'value' => '',
'opt_select' => '', // Never selected
'answer' => $_prefix . gT('No answer')
);
// ==> rows
$sOptions .= doRender('/survey/questions/answer/list_dropdown/rows/option', $optionData, true);
}
$sOther = '';
if (isset($other) && $other == 'Y') {
$aData = [];
$aData['name'] = $ia[1];
$aData['checkconditionFunction'] = $checkconditionFunction;
$aData['display'] = ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] != '-oth-') ? 'display: none;' : '';
$aData['label'] = $othertext;
$thisfieldname = "$ia[1]other";
$aData['value'] = (isset($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$thisfieldname])) ? htmlspecialchars((string) $_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$thisfieldname], ENT_QUOTES) : '';
// ==> other
$sOther .= doRender('/survey/questions/answer/list_dropdown/rows/othertext', $aData, true);
$inputnames[] = $ia[1] . 'other';
}
// ==> answer
$answer = doRender('/survey/questions/answer/list_dropdown/answer', array(
'sOptions' => $sOptions,
'sOther' => $sOther,
'name' => $ia[1],
'basename' => $ia[1],
'dropdownSize' => $dropdownSize,
'checkconditionFunction' => $checkconditionFunction,
'value' => $value,
'coreClass' => $coreClass
), true);
$inputnames[] = $ia[1];
//Time Limit Code
if (trim((string) $aQuestionAttributes['time_limit']) != '') {
$answer .= return_timer_script($aQuestionAttributes, $ia);
}
//End Time Limit Code
return array($answer, $inputnames);
}
// ---------------------------------------------------------------
// TMSW TODO - Can remove DB query by passing in answer list from EM
function do_list_radio($ia)
{
//// Init variables
// General variables
global $thissurvey;
$kpclass = testKeypad($thissurvey['nokeyboard']); // Virtual keyboard (probably obsolete today)
$checkconditionFunction = "checkconditions"; // name of the function to check condition TODO : check is used more than once
$iSurveyId = Yii::app()->getConfig('surveyID'); // survey ID
$sSurveyLang = $_SESSION['survey_' . $iSurveyId]['s_lang']; // survey language
$inputnames = [];
$coreClass = "ls-answers answers-list radio-list";
// Question attribute variables
$aQuestionAttributes = QuestionAttribute::model()->getQuestionAttributes($ia[0]);
$othertext = (trim((string) $aQuestionAttributes['other_replace_text'][$sSurveyLang]) != '') ? $aQuestionAttributes['other_replace_text'][$sSurveyLang] : gT('Other:'); // text for 'other'
$iNbCols = $aQuestionAttributes['display_columns']; // number of columns
$sTimer = (trim((string) $aQuestionAttributes['time_limit']) != '') ? return_timer_script($aQuestionAttributes, $ia) : ''; //Time Limit
//// Retrieving datas
// Getting question
$oQuestion = Question::model()->findByPk(array('qid' => $ia[0], 'language' => $sSurveyLang));
$other = $oQuestion->other;
// Getting answers
$ansresult = $oQuestion->getOrderedAnswers($aQuestionAttributes['random_order'], $aQuestionAttributes['alphasort']);
$anscount = count($ansresult);
$anscount = ($other == 'Y') ? $anscount + 1 : $anscount; //COUNT OTHER AS AN ANSWER FOR MANDATORY CHECKING!
$anscount = (($ia[6] != 'Y' && $ia[6] != 'S') && SHOW_NO_ANSWER == 1) ? $anscount + 1 : $anscount; //Count up if "No answer" is showing
//// Columns containing answer rows, set by user in question attribute
/// TODO : move to a dedicated function
// setting variables
$iRowCount = 0;
$isOpen = false; // Is a column opened
if ($iNbCols > 1) {
// Add a class on the wrapper
$coreClass .= " multiple-list nbcol-{$iNbCols}";
// First we calculate the width of each column
// Max number of column is 12 http://getbootstrap.com/css/#grid
$iColumnWidth = round(12 / $iNbCols);
$iColumnWidth = ($iColumnWidth >= 1) ? $iColumnWidth : 1;
$iColumnWidth = ($iColumnWidth <= 12) ? $iColumnWidth : 12;
// Then, we calculate how many answer rows in each column
$iMaxRowsByColumn = ceil($anscount / $iNbCols);
} else {
$iColumnWidth = 12;
$iMaxRowsByColumn = $anscount + 3; // No max : anscount + no answer + other + 1 by security
}
// Get array_filter stuff
$i = 0;
$sRows = '';
foreach ($ansresult as $key => $ansrow) {
$i++; // general count of loop, to check if the item is the last one for column process. Never reset.
$iRowCount++; // counter of number of row by column. Is reset to zero each time a column is full.
$myfname = $ia[1] . $ansrow['code'];
$checkedState = '';
if ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] == $ansrow['code']) {
$checkedState = 'CHECKED';
}
//list($htmltbody2, $hiddenfield)=return_array_filter_strings($ia, $aQuestionAttributes, $thissurvey, $ansrow, $myfname, '', $myfname, "div","form-group answer-item radio-item");
/* Check for array_filter */
$sDisplayStyle = return_display_style($ia, $aQuestionAttributes, $thissurvey, $myfname);
////
// Open Column
// The column is opened if user set more than one column in question attribute
// and if this is the first answer row, or if the column has been closed and the row count reset before.
if ($iRowCount == 1) {
$sRows .= doRender('/survey/questions/answer/listradio/columns/column_header', array('iColumnWidth' => $iColumnWidth), true);
$isOpen = true; // If a column is not closed, it will be closed at the end of the process
}
////
// Insert row
// Display the answer row
$sRows .= doRender('/survey/questions/answer/listradio/rows/answer_row', array(
'sDisplayStyle' => $sDisplayStyle,
'name' => $ia[1],
'code' => $ansrow['code'],
'answer' => $ansrow->answerl10ns[$sSurveyLang]->answer,
'checkedState' => $checkedState,
'myfname' => $myfname,
'i' => $i
), true);
////
// Close column
// The column is closed if the user set more than one column in question attribute
// and if the max answer rows by column is reached.
// If max answer rows by column is not reached while there is no more answer,
// the column will remain opened, and it will be closed by 'other' answer row if set or at the end of the process
if ($iRowCount == $iMaxRowsByColumn) {
$last = ($i == $anscount) ? true : false; // If this loop count equal to the number of answers, then this answer is the last one.
$sRows .= doRender('/survey/questions/answer/listradio/columns/column_footer', array('last' => $last), true);
$iRowCount = 0;
$isOpen = false;
}
}
if (isset($other) && $other == 'Y') {
$iRowCount++;
$i++;
$sSeparator = getRadixPointData($thissurvey['surveyls_numberformat']);
$sSeparator = $sSeparator['separator'];
if ($_SESSION['survey_' . Yii::app()->getConfig('surveyID')][$ia[1]] == '-oth-') {
$checkedState = CHECKED;
} else {
$checkedState = '';
}