-
Notifications
You must be signed in to change notification settings - Fork 994
/
qanda_helper.php
8258 lines (7388 loc) · 328 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 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.
*
* $Id: qanda.php 10590 2011-07-26 15:38:45Z shnoulle $
*/
// 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['insertarray'] variable which is built at the commencement of the survey.
* See index.php, function "buildsurveysession()"
* One $ia array exists for every question in the survey. The $_SESSION['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 || 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*
*/
/**
* setNoAnswerMode
*/
function setNoAnswerMode($thissurvey)
{
if(getGlobalSetting('shownoanswer') > 0 && $thissurvey['shownoanswer'] != 'N')
{
define('SHOW_NO_ANSWER',1);
}
else
{
define('SHOW_NO_ANSWER',0);
};
}
// TMSW Conditions->Relevance: Not needed
function retrieveConditionInfo($ia)
{
//This function returns an array containing all related conditions
//for a question - the array contains the fields from the conditions table
$CI =& get_instance();
$dbprefix = $CI->db->dbprefix;
if ($ia[7] == "Y")
{ //DEVELOP CONDITIONS ARRAY FOR THIS QUESTION
$cquery = "SELECT {$dbprefix}conditions.qid, "
."{$dbprefix}conditions.scenario, "
."{$dbprefix}conditions.cqid, "
."{$dbprefix}conditions.cfieldname, "
."{$dbprefix}conditions.value, "
."{$dbprefix}questions.type, "
."{$dbprefix}questions.sid, "
."{$dbprefix}questions.gid, "
."{$dbprefix}conditions.method, "
."questionssrc.gid as srcgid "
."FROM {$dbprefix}conditions, "
."{$dbprefix}questions ,"
."{$dbprefix}questions as questionssrc "
."WHERE {$dbprefix}conditions.cqid={$dbprefix}questions.qid "
."AND {$dbprefix}conditions.qid=questionssrc.qid "
."AND {$dbprefix}conditions.qid=$ia[0] "
."AND {$dbprefix}questions.language='".$_SESSION['s_lang']."' "
."AND {$dbprefix}conditions.cfieldname NOT LIKE '{%' "
."ORDER BY {$dbprefix}conditions.scenario, "
."{$dbprefix}conditions.cqid, "
."{$dbprefix}conditions.cfieldname";
$cresult = db_execute_assoc($cquery) or safe_die ("OOPS<br />$cquery<br />".$connect->ErrorMsg()); //Checked
$cquerytoken = "SELECT {$dbprefix}conditions.qid, "
."{$dbprefix}conditions.scenario, "
."{$dbprefix}conditions.cqid, "
."{$dbprefix}conditions.cfieldname, "
."{$dbprefix}conditions.value, "
."'' as type, "
."0 as sid, "
."0 as gid, "
."{$dbprefix}conditions.method,"
."questionssrc.gid as srcgid "
."FROM {$dbprefix}conditions, {$dbprefix}questions as questionssrc "
."WHERE {$dbprefix}conditions.qid=questionssrc.qid "
."AND {$dbprefix}conditions.qid=$ia[0] "
."AND {$dbprefix}conditions.cfieldname LIKE '{%' "
."ORDER BY {$dbprefix}conditions.scenario, "
."{$dbprefix}conditions.cqid, "
."{$dbprefix}conditions.cfieldname";
$cresulttoken = db_execute_assoc($cquerytoken) or safe_die ("OOPS<br />$cquerytoken<br />".$connect->ErrorMsg()); //Checked
foreach ($cresulttoken->result_array() as $tempcrow)
{
$aAllConditions[] = $tempcrow;
}
foreach($cresult->result_array() as $tempcrow)
{
$aAllConditions[] = $tempcrow;
}
// while ($crow = $cresult->FetchRow())
foreach ($aAllConditions as $crow)
{
if (preg_match("/^\+(.*)$/",$crow['cfieldname'],$cfieldnamematch))
{ // this condition uses a single checkbox as source
$crow['type'] = "+".$crow['type'];
$crow['cfieldname'] = $cfieldnamematch[1];
}
$conditions[] = array ($crow['qid'],
$crow['cqid'],
$crow['cfieldname'],
$crow['value'],
$crow['type'],
$crow['sid']."X".$crow['gid']."X".$crow['cqid'],
$crow['method'],
$crow['scenario'],
$crow['srcgid']);
}
foreach ($conditions as $condkey => $condarr)
{ // We need to sort the merged array by Ascending scenario, cqid, then cfieldname
// otherwise condition evaluations of Tokens in Scenarii won't work in "live" Javascript eval
$scenariolist[$condkey] = $condarr[7];
$cqidlist[$condkey] = $condarr[1];
$cfieldnamelist[$condkey] = $condarr[2];
}
array_multisort($scenariolist,SORT_ASC,$cqidlist,SORT_ASC,$cfieldnamelist,SORT_ASC,$conditions);
return $conditions;
}
else
{
return null;
}
}
// TMSW Conditions->Relevance: This function not needed - JS name identified within EM.
// returns the Javascript IdName of a question used in conditions
// $cd = Array (
// 0 => Unused
// 1 => qid of the question
// 2 => fieldname of the question
// 3 => value used in comparison (only usd for type M and P egals 'Y', optionnal for other types)
// 4 => type of the question
// 5 => SGQ code corresponding to the fieldname
// if $currentgid is not null (Group by group survey), the fieldname depends on the groupId
function retrieveJSidname($cd,$currentgid=null)
{
global $dropdownthreshold;
$CI =& get_instance();
$dbprefix = $CI->db->dbprefix;
if (preg_match("/^\+{0,1}[0-9]+X([0-9]+)X(.*)$/",$cd[2]) == 0)
{ // This is not a true fieldname (for instance a {TOKEN:ATTR..}
// placeholder
return "NoJSidname";
}
//preg_match("/^[0-9]+X([0-9]+)X([0-9]+)$/",$cd[2],$matchFields);
//^^^^^does not seem to work, explode below should
$matchFields = explode('X', $cd[2], 3);
$questiongid=$matchFields[1];
$questionFieldpart=$matchFields[2];
if ($cd[4] == "L")
{
$cccquery="SELECT code FROM {$dbprefix}answers WHERE qid={$cd[1]} AND language='".$_SESSION['s_lang']."'";
$cccresult=db_execute_assoc($cccquery); // Checked
$cccount=$cccresult->num_rows();
}
if ($cd[4] == "R")
{
if (!isset($currentgid) || $questiongid == $currentgid)
{ // if question is on same page then field is fvalue_XXXX
//$idname="fvalue_".$cd[1].substr($cd[2], strlen($cd[2])-1,1); // broken when ranking more than 9 items
$idname="fvalue_".$questionFieldpart;
}
else
{ // If question is on another page then field if javaXXXX
$idname="java$cd[2]";
}
}
elseif ($cd[4] == "5" ||
$cd[4] == "A" ||
$cd[4] == "B" ||
$cd[4] == "C" ||
$cd[4] == "E" ||
$cd[4] == "F" ||
$cd[4] == "H" ||
$cd[4] == "G" ||
$cd[4] == "Y" ||
$cd[4] == "1" ||
($cd[4] == "L" && $cccount <= $dropdownthreshold))
{
$idname="java$cd[2]";
}
elseif ($cd[4] == "M" ||
$cd[4] == "P")
{
$idname="java$cd[5]$cd[3]";
}
elseif ($cd[4] == "+M" ||
$cd[4] == "+P")
{
$idname="java$cd[2]";
}
elseif ($cd[4] == "D" ||
$cd[4] == "N" ||
$cd[4] == "S" ||
$cd[4] == "T" ||
$cd[4] == "U" ||
$cd[4] == "Q" ||
$cd[4] == "K" )
{
if (!isset($currentgid) || $questiongid == $currentgid)
{ // if question is on same page then field is answerXXXX
$idname="answer$cd[2]";
}
else
{ // If question is on another page then field if javaXXXX
$idname="java$cd[2]";
}
}
else
{
$idname="java".$cd[2];
}
return $idname;
}
// TMSW Conditions->Relevance: EM will manage mandatories - but must check all question specific issues (like ranking)
function create_mandatorylist($ia)
{
//Checks current question and returns required mandatory arrays if required
if ($ia[6] == 'Y')
{
switch($ia[4])
{
case 'R':
$thismandatory = setman_ranking($ia);
break;
case 'M':
$thismandatory = setman_questionandcode($ia);
break;
case 'J':
case 'P':
case 'Q':
case 'K':
case 'A':
case 'B':
case 'C':
case 'E':
case 'F':
case 'H':
$thismandatory = setman_questionandcode($ia);
break;
case ':':
case ';':
$thismandatory = setman_multiflex($ia);
break;
case '1':
$thismandatory = setman_questionandcode_multiscale($ia);
break;
case 'X':
//Do nothing - boilerplate questions CANNOT be mandatory
break;
default:
$thismandatory = setman_normal($ia);
}
if ($ia[7] != 'Y' && isset($thismandatory)) //Question is not conditional - addto mandatory arrays
{
$mandatory=$thismandatory;
}
if ($ia[7] == 'Y' && isset($thismandatory)) //Question IS conditional - add to conmandatory arrays
{
$conmandatory=$thismandatory;
}
}
if (isset($mandatory))
{
return array($mandatory, null);
}
elseif (isset($conmandatory))
{
return array(null, $conmandatory);
}
else
{
return array(null, null);
}
}
// TMSW Conditions->Relevance: EM will manage mandatories - but must check all question specific issues (like ranking)
function setman_normal($ia)
{
$mandatorys[]=$ia[1];
$mandatoryfns[]=$ia[1];
return array($mandatorys, $mandatoryfns);
}
// TMSW Conditions->Relevance: EM will manage mandatories - but must check all question specific issues (like ranking)
function setman_ranking($ia)
{
$CI =& get_instance();
$dbprefix = $CI->db->dbprefix;
$ansquery = "SELECT * FROM {$dbprefix}answers WHERE qid={$ia[0]} AND language='".$_SESSION['s_lang']."' ORDER BY sortorder, answer";
$ansresult = db_execute_assoc($ansquery); //Checked
$anscount = $ansresult->num_rows();
$aQuestionAttributes=getQuestionAttributeValues($ia[0],$ia[4]);
if (trim($aQuestionAttributes['max_answers'])!='') {
$max_answers = $aQuestionAttributes['max_answers'];
}
else
{
$max_answers = $anscount;
}
for ($i=1; $i<=$max_answers; $i++)
{
$mandatorys[]=$ia[1].$i;
$mandatoryfns[]=$ia[1];
}
return array($mandatorys, $mandatoryfns);
}
// TMSW Conditions->Relevance: EM will manage mandatories - but must check all question specific issues (like ranking)
function setman_questionandcode($ia)
{
$CI =& get_instance();
$dbprefix = $CI->db->dbprefix;
$qquery = "SELECT other FROM {$dbprefix}questions WHERE qid=".$ia[0]." AND language='".$_SESSION['s_lang']."' and parent_qid=0";
$qresult = db_execute_assoc($qquery); //Checked
$qrow = $qresult->row_array();
$other = $qrow['other'];
$subquestionquery = "SELECT title FROM {$dbprefix}questions WHERE parent_qid={$ia[0]} AND language='".$_SESSION['s_lang']."' ORDER BY question_order";
$sqresult = db_execute_assoc($subquestionquery); //Checked
foreach ($sqresult->row_array() as $subquestionrow)
{
$mandatorys[]=$ia[1].$subquestionrow['title'];
$mandatoryfns[]=$ia[1];
}
if ($other == "Y" and ($ia[4]=="!" or $ia[4]=="L" or $ia[4]=="M" or $ia[4]=="P"))
{
$mandatorys[]=$ia[1]."other";
$mandatoryfns[]=$ia[1];
}
return array($mandatorys, $mandatoryfns);
}
// TMSW Conditions->Relevance: EM will manage mandatories - but must check all question specific issues (like ranking)
/**
* The point of these functions (setman) is to return an array containing two arrays.
* The first ($mandatorys) is an array containing question, so they can all be checked
* The second ($mandatoryfns) is an arry containing the fieldnames of every question
* What's the difference? The difference arises from multiple option questions, and came
* about when trying to distinguish between answering just one option (which satisfies
* the mandatory requirement, and answering them all). The "mandatorys" input contains the
* actual specific response items that could be filled in.. ie: in a multiple option
* question, there will be a unique one for every possible answer. The "mandatoryfns" array
* contains the generic question fieldname for the question as a whole (it will be repeated
* for multiple option qeustions, but won't contain unique items.
*
* @param mixed $ia
* @return array See explanation above
*/
function setman_multiflex($ia)
{
$CI =& get_instance();
$dbprefix = $CI->db->dbprefix;
$mandatorys=array();
$mandatoryfns=array();
$ansquery = "SELECT * FROM {$dbprefix}questions WHERE parent_qid={$ia[0]} AND language='".$_SESSION['s_lang']."' and scale_id=0 ORDER BY question_order, title";
$ansresult = db_execute_assoc($ansquery);
$ans2query = "SELECT * FROM {$dbprefix}questions WHERE parent_qid={$ia[0]} AND language='".$_SESSION['s_lang']."' and scale_id=1 ORDER BY question_order, title";
$ans2result = db_execute_assoc($ans2query);
foreach ($ans2result->result_array()as $ans2row)
{
$lset[]=$ans2row;
}
$aQuestionAttributes=getQuestionAttributeValues($ia[0],$ia[4]);
foreach ($ansresult->result_array() as $ansrow)
{
//Don't add to mandatory list if the row is filtered out with the array_filter option
if (trim($aQuestionAttributes['array_filter'])!='')
{
//This particular one may not be mandatory if it's hidden
$selected = getArrayFiltersForQuestion($ia[0]);
if (!in_array($ansrow['title'],$selected))
{
//This one's hidden, so don't add it to the mandatory list
}
else
{
//This one's not hidden. so add it to the mandatory list
foreach($lset as $ls)
{
$mandatorys[]=$ia[1].$ansrow['title']."_".$ls['title'];
$mandatoryfns[]=$ia[1];
}
}
}
elseif (trim($aQuestionAttributes['array_filter_exclude'])!='')
{
//This particular one may not be mandatory if it's hidden
$selected = getArrayFilterExcludesForQuestion($ia[0]);
if ($selected!=false && in_array($ansrow['title'],$selected))
{
//This one's hidden, so don't add it to the mandatory list
}
else
{
//This one's not hidden. so add it to the mandatory list
foreach($lset as $ls)
{
$mandatorys[]=$ia[1].$ansrow['title']."_".$ls['title'];
$mandatoryfns[]=$ia[1];
}
}
} else { //There is no array_filter option, so we should definitely add to the mandatory list here!
foreach($lset as $ls)
{
$mandatorys[]=$ia[1].$ansrow['title']."_".$ls['title'];
$mandatoryfns[]=$ia[1];
}
}
}
return array($mandatorys, $mandatoryfns);
}
// TMSW Conditions->Relevance: EM will manage mandatories - but must check all question specific issues (like ranking)
function setman_questionandcode_multiscale($ia)
{
$CI =& get_instance();
$dbprefix = $CI->db->dbprefix;
$qquery = "SELECT other FROM {$dbprefix}questions WHERE qid=".$ia[0]." AND language='".$_SESSION['s_lang']."'";
$qresult = db_execute_assoc($qquery); //Checked
foreach ($qresult->result_array() as $qrow)
{
$other = $qrow['other'];
}
// Get Subquestions
$subquery="SELECT * "
."FROM {$dbprefix}questions "
."WHERE parent_qid={$ia[0]} "
."AND language='".$_SESSION['s_lang']."' "
."ORDER BY question_order";
$subresult = db_execute_assoc($subquery); //Checked
// Get Answer Scale 1
$ans1query="SELECT qid "
."FROM {$dbprefix}answers "
."WHERE qid={$ia[0]} "
."AND scale_id=0 "
."AND language='".$_SESSION['s_lang']."' "
."ORDER BY sortorder";
$ans1result = db_execute_assoc($ans1query); //Checked
$ans1count = $ans1result->num_rows();
// Get Answer Scale 2
$ans2query="SELECT qid "
."FROM {$dbprefix}answers "
."WHERE qid={$ia[0]} "
."AND scale_id=1 "
."AND language='".$_SESSION['s_lang']."' "
."ORDER BY sortorder";
$ans2result = db_execute_assoc($ans2query); //Checked
$ans2count = $ans2result->num_rows();
foreach ($subresult->result_array() as $subrow)
{
// first answer set
if ($ans1count > 0)
{
$mandatorys[]=$ia[1].$subrow['title']."#0";
$mandatoryfns[]=$ia[1];
}
else
{
$mandatorys[]=$ia[1].$subrow['title'];
$mandatoryfns[]=$ia[1];
}
// second answer set
if ($ans2count > 0)
{
$mandatorys[]=$ia[1].$subrow['title']."#1";
$mandatoryfns[]=$ia[1];
}
else
{
$mandatorys[]=$ia[1].$subrow['title'];
$mandatoryfns[]=$ia[1];
}
}
if ($other == "Y" and ($ia[4]=="!" or $ia[4]=="L" or $ia[4]=="M" or $ia[4]=="P" or $ia[4]=="1"))
{
$mandatorys[]=$ia[1]."other";
$mandatoryfns[]=$ia[1];
}
return array($mandatorys, $mandatoryfns);
}
/**
* 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 or survey.php
*
* @param mixed $ia
* @param mixed $notanswered
* @param mixed $notvalidated
* @param mixed $filenotvalidated
* @return mixed
*/
// TMSW Conditions->Relevance: Refactor this - EM does most of this already without need for DB query per question
function retrieveAnswers($ia, $notanswered=null, $notvalidated=null, $filenotvalidated=null)
{
//globalise required config variables
global $thissurvey, $gl; //These are set by index.php
$CI =& get_instance();
$dbprefix = $CI->db->dbprefix;
$clang = $CI->limesurvey_lang;
//DISPLAY
$display = $ia[7];
//QUESTION NAME
$name = $ia[0];
$qtitle=$ia[3];
//Replace INSERTANS statements with previously provided answers;
// $CI->load->library("Dtexts");
$qtitle=dTexts__run($qtitle,$ia[0]);
//GET HELP
$hquery="SELECT help FROM {$dbprefix}questions WHERE qid=$ia[0] AND language='".$_SESSION['s_lang']."'";
$hresult=db_execute_assoc($hquery) or safe_die($connect->ErrorMsg()); //Checked
$help="";
$hrow=$hresult->row_array(); $help=reset($hrow);
//A bit of housekeeping to stop PHP Notices
$answer = "";
if (!isset($_SESSION[$ia[1]])) {$_SESSION[$ia[1]] = "";}
$aQuestionAttributes=getQuestionAttributeValues($ia[0],$ia[4]);
//echo "<pre>";print_r($aQuestionAttributes);echo "</pre>";
//Create the question/answer html
// 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.
$number = isset($ia[9]) ? $ia[9] : '';
$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' => ''
);
switch ($ia[4])
{
case 'X': //BOILERPLATE QUESTION
$values = do_boilerplate($ia);
break;
case '5': //5 POINT CHOICE radio-buttons
$values = do_5pointchoice($ia);
break;
case 'D': //DATE
$values = do_date($ia);
break;
case 'L': //LIST drop-down/radio-button list
$values = do_list_radio($ia);
if ($aQuestionAttributes['hide_tip']==0)
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. $clang->gT('Choose one of the following answers').'</span>';
$question_text['help'] = $clang->gT('Choose one of the following answers');
}
break;
case '!': //List - dropdown
$values=do_list_dropdown($ia);
if ($aQuestionAttributes['hide_tip']==0)
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. $clang->gT('Choose one of the following answers').'</span>';
$question_text['help'] = $clang->gT('Choose one of the following answers');
}
break;
case 'O': //LIST WITH COMMENT drop-down/radio-button list + textarea
$values=do_listwithcomment($ia);
if (count($values[1]) > 1 && $aQuestionAttributes['hide_tip']==0)
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. $clang->gT('Choose one of the following answers').'</span>';
$question_text['help'] = $clang->gT('Choose one of the following answers');
}
break;
case 'R': //RANKING STYLE
$values=do_ranking($ia);
if (count($values[1]) > 1 && $aQuestionAttributes['hide_tip']==0)
{
$question_text['help'] = $clang->gT("Drag items in the list on the left to the list on the right in the order that you would like to rank them.");
if (trim($aQuestionAttributes['min_answers'])!='')
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. sprintf($clang->ngT("Rank at least %d item.","Rank at least %d items.",$aQuestionAttributes['min_answers']),$aQuestionAttributes['min_answers'])."</span>";
$question_text['help'] .=' '.sprintf($clang->ngT("Rank at least %d item.","Rank at least %d items.",$aQuestionAttributes['min_answers']),$aQuestionAttributes['min_answers']);
}
}
break;
case 'M': //Multiple choice checkbox
$values=do_multiplechoice($ia);
if (count($values[1]) > 1 && $aQuestionAttributes['hide_tip']==0)
{
$maxansw=trim($aQuestionAttributes['max_answers']);
$minansw=trim($aQuestionAttributes['min_answers']);
if (!($maxansw || $minansw))
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. $clang->gT('Check any that apply').'</span>';
$question_text['help'] = $clang->gT('Check any that apply');
}
else
{
if ($maxansw && $minansw)
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. sprintf($clang->gT("Check between %d and %d answers"), $minansw['value'], $maxansw['value'])."</span>";
$question_text['help'] = sprintf($clang->gT("Check between %d and %d answers"), $minansw['value'], $maxansw['value']);
} elseif ($maxansw)
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. sprintf($clang->gT("Check at most %d answers"), $maxansw['value'])."</span>";
$question_text['help'] = sprintf($clang->gT("Check at most %d answers"), $maxansw['value']);
} else
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. sprintf($clang->ngT("Check at least %d answer","Check at least %d answers",$minansw['value']),$minansw['value'])."</span>";
$question_text['help'] = sprintf($clang->ngT("Check at least %d answer","Check at least %d answers",$minansw['value']),$minansw['value']);
}
}
}
break;
case 'I': //Language Question
$values=do_language($ia);
if (count($values[1]) > 1)
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. $clang->gT('Choose your language').'</span>';
$question_text['help'] = $clang->gT('Choose your language');
}
break;
case 'P': //Multiple choice with comments checkbox + text
$values=do_multiplechoice_withcomments($ia);
if (count($values[1]) > 1 && $aQuestionAttributes['hide_tip']==0)
{
$maxansw=trim($aQuestionAttributes["max_answers"]);
$minansw=trim($aQuestionAttributes["min_answers"]);
if (!($maxansw || $minansw))
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. $clang->gT('Check any that apply').'</span>';
$question_text['help'] = $clang->gT('Check any that apply');
}
else
{
if ($maxansw && $minansw)
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. sprintf($clang->gT("Check between %d and %d answers"), $minansw['value'], $maxansw['value'])."</span>";
$question_text['help'] = sprintf($clang->gT("Check between %d and %d answers"), $minansw['value'], $maxansw['value']);
} elseif ($maxansw)
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. sprintf($clang->gT("Check at most %d answers"), $maxansw['value'])."</span>";
$question_text['help'] = sprintf($clang->gT("Check at most %d answers"), $maxansw['value']);
} else
{
$qtitle .= "<br />\n<span class=\"questionhelp\">"
. sprintf($clang->gT("Check at least %d answers"), $minansw['value'])."</span>";
$question_text['help'] = sprintf($clang->gT("Check at least %d answers"), $minansw['value']);
}
}
}
break;
case '|': //File Upload
$values=do_file_upload($ia);
if ($aQuestionAttributes['min_num_of_files'] != 0)
{
if (trim($aQuestionAttributes['min_num_of_files']) != 0)
{
$qtitle .= "<br />\n<span class = \"questionhelp\">"
.sprintf($clang->gT("At least %d files must be uploaded for this question"), $aQuestionAttributes['min_num_of_files'])."<span>";
$question_text['help'] .= ' '.sprintf($clang->gT("At least %d files must be uploaded for this question"), $aQuestionAttributes['min_num_of_files']);
}
}
break;
case 'Q': //MULTIPLE SHORT TEXT
$values=do_multipleshorttext($ia);
break;
case 'K': //MULTIPLE NUMERICAL QUESTION
$values=do_multiplenumeric($ia);
break;
case 'N': //NUMERICAL QUESTION TYPE
$values=do_numerical($ia);
break;
case 'S': //SHORT FREE TEXT
$values=do_shortfreetext($ia);
break;
case 'T': //LONG FREE TEXT
$values=do_longfreetext($ia);
break;
case 'U': //HUGE FREE TEXT
$values=do_hugefreetext($ia);
break;
case 'Y': //YES/NO radio-buttons
$values=do_yesno($ia);
break;
case 'G': //GENDER drop-down list
$values=do_gender($ia);
break;
case 'A': //ARRAY (5 POINT CHOICE) radio-buttons
$values=do_array_5point($ia);
break;
case 'B': //ARRAY (10 POINT CHOICE) radio-buttons
$values=do_array_10point($ia);
break;
case 'C': //ARRAY (YES/UNCERTAIN/NO) radio-buttons
$values=do_array_yesnouncertain($ia);
break;
case 'E': //ARRAY (Increase/Same/Decrease) radio-buttons
$values=do_array_increasesamedecrease($ia);
break;
case 'F': //ARRAY (Flexible) - Row Format
$values=do_array($ia);
break;
case 'H': //ARRAY (Flexible) - Column Format
$values=do_arraycolumns($ia);
break;
case ':': //ARRAY (Multi Flexi) 1 to 10
$values=do_array_multiflexi($ia);
break;
case ';': //ARRAY (Multi Flexi) Text
$values=do_array_multitext($ia); //It's like the "5th element" movie, come to life
break;
case '1': //Array (Flexible Labels) dual scale
$values=do_array_dual($ia);
break;
case '*': // Equation
$values=do_equation($ia);
break;
} //End Switch
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;
}
$answer .= "\n\t<input type='hidden' name='display$ia[1]' id='display$ia[0]' value='";
$answer .= 'on'; //If this is single format, then it must be showing. Needed for checking conditional mandatories
$answer .= "' />\n"; //for conditional mandatory questions
if ($ia[6] == 'Y')
{
$qtitle = '<span class="asterisk">'.$clang->gT('*').'</span>'.$qtitle;
$question_text['mandatory'] = $clang->gT('*');
}
//If this question is mandatory but wasn't answered in the last page
//add a message HIGHLIGHTING the question
$qtitle .= mandatory_message($ia);
$question_text['man_message'] = mandatory_message($ia);
$qtitle .= validation_message($ia);
$question_text['valid_message'] = validation_message($ia);
$qtitle .= $ia[4] == "|" ? file_validation_message($ia) : "";
$question_text['file_valid_message'] = $ia[4] == "|" ? file_validation_message($ia) : "";
if(!empty($question_text['man_message']) || !empty($question_text['valid_message']) || !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 = isset($thissurvey['template']) ? $thissurvey['template'] : NULL;
if(is_file('templates/'.validate_templatedir($sTemplate).'/question_start.pstpl'))
{
$qtitle_custom = '';
$replace=array();
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(sGetTemplatePath($thissurvey['template']).'/question_start.pstpl' , true));
};
$qtitle_custom = str_replace( $find , $replace , 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.
{
$matches = 0;
$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.
{
$matches = 0;
$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;
};
// $qtitle = $qtitle_custom;
$question_text['all'] = $qtitle_custom;
}
else
{
$question_text['all'] = $qtitle;
};
// END: legacy question_start.pstpl code
//===================================================================
// echo '<pre>[qanda.php] line '.__LINE__.": $question_text =\n".htmlspecialchars(print_r($question_text,true)).'</pre>';
$qtitle = $question_text;
// =====================================================
$qanda=array($qtitle, $answer, $help, $display, $name, $ia[2], $gl[0], $ia[1] );
//New Return
return array($qanda, $inputnames);
}
// TMSW Conditions->Relevance: EM will manage mandatories - but must check all question specific issues (like ranking)
function mandatory_message($ia)
{
//This function checks to see if this question is mandatory and
//is being re-displayed because it wasn't answered. It returns
global $notanswered;
$CI =& get_instance();
$dbprefix = $CI->db->dbprefix;
$clang = $CI->limesurvey_lang;
$qtitle="";
if (isset($notanswered) && is_array($notanswered)) //ADD WARNINGS TO QUESTIONS IF THEY WERE MANDATORY BUT NOT ANSWERED
{
global $mandatorypopup, $popup;
if (in_array($ia[1], $notanswered))
{
$qtitle .= "<strong><br /><span class='errormandatory'>".$clang->gT('This question is mandatory').'.';
switch($ia[4])
{
case 'A':
case 'B':
case 'C':
case 'Q':
case 'K':
case 'F':
case 'J':
case 'H':
case ':':
$qtitle .= "<br />\n".$clang->gT('Please complete all parts').'.';
break;
case '1':
$qtitle .= "<br />\n".$clang->gT('Please check the items').'.';
break;
case 'R':
$qtitle .= "<br />\n".$clang->gT('Please rank all items').'.';
break;
case 'M':
case 'P':
$qtitle .= ' '.$clang->gT('Please check at least one item.').'.';
$qquery = "SELECT other FROM {$dbprefix}questions WHERE qid=".$ia[0];
$qresult = db_execute_assoc($qquery); //Checked
$qrow = $qresult->row_array();
if ($qrow['other']=='Y')
{
$aQuestionAttributes=getQuestionAttributeValues($ia[0],$ia[4]);
if (trim($aQuestionAttributes['other_replace_text'][$_SESSION['s_lang']])!='')
{
$othertext=$aQuestionAttributes['other_replace_text'][$_SESSION['s_lang']];
}
else
{
$othertext=$clang->gT('Other:');
}
$qtitle .= "<br />\n".sprintf($clang->gT("If you choose '%s' you must provide a description."), $othertext);
}
break;
} // end switch
$qtitle .= "</span></strong><br />\n";
}
}
return $qtitle;
}
// TMSW Conditions->Relevance: EM will manage all validation
function validation_message($ia)
{
//This function checks to see if this question requires validation and
//that validation has not been met.
global $notvalidated;
$CI =& get_instance();
$dbprefix = $CI->db->dbprefix;
$clang = $CI->limesurvey_lang;
$qtitle="";
if (isset($notvalidated) && is_array($notvalidated)) //ADD WARNINGS TO QUESTIONS IF THEY ARE NOT VALID
{
global $validationpopup, $popup;
if (in_array($ia[1], $notvalidated))
{
$help='';
$helpselect="SELECT help\n"
."FROM {$dbprefix}questions\n"
."WHERE qid={$ia[0]} AND language='".$_SESSION['s_lang']."'";
$helpresult=db_execute_assoc($helpselect) or safe_die($helpselect.'<br />'.$connect->ErrorMsg()); //Checked
foreach ($helpresult->result_array() as $helprow)
{
$help=' <span class="questionhelp">'.$helprow['help'].'</span>';
}
$qtitle .= '<br /><span class="errormandatory">'.$clang->gT('This question must be answered correctly').'.'.$help.'</span><br />
';
}
}
return $qtitle;
}
function file_validation_message($ia)
{
global $filenotvalidated;