-
Notifications
You must be signed in to change notification settings - Fork 988
/
conditionshandling.php
2103 lines (1899 loc) · 98.9 KB
/
conditionshandling.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$
*/
//
// TODO
//
// Optionnally mark ADDED, UPDATED lines with different colors just as the EDITTED one
//
// add warning if updating scenario with a scenario number which already exists (merging)
//
// Try a wrapping with scrollbar for conditions display: http://www.htmlcodetutorial.com/help/ftopic2394.html
// or http://www.kelvinluck.com/assets/jquery/jScrollPane/jScrollPane.html
//
// Try to use an intelligent dropdown: http://www.fairwaytech.com/flexbox/Demos.aspx
include_once("login_check.php");
//BEGIN Sanitizing POSTed data
if (!isset($surveyid)) {$surveyid=returnglobal('sid');}
if (!isset($qid)) {$qid=returnglobal('qid');}
if (!isset($gid)) {$gid=returnglobal('gid');}
if (!isset($p_scenario)) {$p_scenario=returnglobal('scenario');}
if (!isset($p_cqid))
{
$p_cqid=returnglobal('cqid');
if ($p_cqid == '') $p_cqid=0; // we are not using another question as source of condition
}
if (!isset($p_cid)) {$p_cid=returnglobal('cid');}
if (!isset($p_subaction)) {$p_subaction=returnglobal('subaction');}
if (!isset($p_cquestions)) {$p_cquestions=returnglobal('cquestions');}
if (!isset($p_csrctoken)) {$p_csrctoken=returnglobal('csrctoken');}
if (!isset($p_prevquestionsgqa)) {$p_prevquestionsgqa=returnglobal('prevQuestionSGQA');}
if (!isset($p_canswers))
{
if (isset($_POST['canswers']) && is_array($_POST['canswers']))
{
foreach ($_POST['canswers'] as $key => $val)
{
$p_canswers[$key]= preg_replace("/[^_.a-zA-Z0-9]@/", "", $val);
}
}
}
if (isset($_POST['method']))
{
if (!in_array($_POST['method'], array('<','<=','>','>=','==','!=','RX')))
{
$p_method = "==";
}
else
{
$p_method = trim ($_POST['method']);
}
}
if (isset($_POST['newscenarionum']))
{
$p_newscenarionum = sanitize_int($_POST['newscenarionum']);
}
//END Sanitizing POSTed data
include_once("login_check.php");
include_once("database.php");
// Caution (lemeur): database.php uses auto_unescape on all entries in $_POST
// Take care to not use auto_unescape on $_POST variables after this
//MAKE SURE THAT THERE IS A SID
if (!isset($surveyid) || !$surveyid)
{
$conditionsoutput = "<div class='header'>".$clang->gT("Conditions manager")."</div>\n"
."<div class='messagebox'>\n"
."\t<div class='warningheader'>".$clang->gT("Error")."</div><br />"
.$clang->gT("You have not selected a survey")."<br /><br />"
."<input type='submit' value='".$clang->gT("Main admin screen")."' onclick=\"window.open('$scriptname', '_top')\" /><br />\n"
."</div>\n";
return;
}
//MAKE SURE THAT THERE IS A QID
if (!isset($qid) || !$qid)
{
$conditionsoutput = "<div class='header'>".$clang->gT("Conditions manager")."</div>\n"
."<div class='messagebox'>\n"
."\t<div class='warningheader'>".$clang->gT("Error")."</div><br />"
.$clang->gT("You have not selected a question")."<br /><br />"
."<input type='submit' value='".$clang->gT("Main admin screen")."' onclick=\"window.open('$scriptname', '_top')\" /><br />\n"
."</div>\n";
return;
}
// If we made it this far, then lets develop the menu items
// add the conditions container table
$extraGetParams ="";
if (isset($qid) && isset($gid))
{
$extraGetParams="&gid=$gid&qid=$qid";
}
$conditionsoutput_header = "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr><td>\n";
$conditionsoutput_menubar = ""; // will be defined later when we have enough information about the questions
$conditionsoutput_action_error = ""; // defined during the actions
$conditionsoutput_main_content = ""; // everything after the menubar
$markcidarray=Array();
if (isset($_GET['markcid']))
{
$markcidarray=explode("-",$_GET['markcid']);
}
//BEGIN PROCESS ACTIONS
// ADD NEW ENTRY IF THIS IS AN ADD
if (isset($p_subaction) && $p_subaction == "insertcondition")
{
if ((!isset($p_canswers) &&
!isset($_POST['ConditionConst']) &&
!isset($_POST['prevQuestionSGQA']) &&
!isset($_POST['tokenAttr']) &&
!isset($_POST['ConditionRegexp'])) ||
(!isset($p_cquestions) && !isset($p_csrctoken)))
{
$conditionsoutput_action_error .= "<script type=\"text/javascript\">\n<!--\n alert(\"".$clang->gT("Your condition could not be added! It did not include the question and/or answer upon which the condition was based. Please ensure you have selected a question and an answer.","js")."\")\n //-->\n</script>\n";
}
else
{
if (isset($p_cquestions) && $p_cquestions != '')
{
$conditionCfieldname=$p_cquestions;
}
elseif(isset($p_csrctoken) && $p_csrctoken != '')
{
$conditionCfieldname=$p_csrctoken;
}
if (isset($p_canswers))
{
foreach ($p_canswers as $ca)
{
//First lets make sure there isn't already an exact replica of this condition
$query = "SELECT * FROM {$dbprefix}conditions\n"
."WHERE qid='$qid'\n"
."AND scenario='".$p_scenario."'\n"
."AND cqid='".$p_cqid."'\n"
."AND cfieldname='".$conditionCfieldname."'\n"
."AND method='".$p_method."'\n"
."AND value='".$ca."'";
$result = $connect->Execute($query) or safe_die("Couldn't check for existing condition<br />$query<br />".$connect->ErrorMsg());
$count_caseinsensitivedupes = $result->RecordCount();
if ($count_caseinsensitivedupes == 0)
{
$query = "INSERT INTO {$dbprefix}conditions (qid, scenario, cqid, cfieldname, method, value) VALUES "
. "('{$qid}', '{$p_scenario}', '{$p_cqid}', '{$conditionCfieldname}', '{$p_method}', '$ca')";
$result = $connect->Execute($query) or safe_die ("Couldn't insert new condition<br />$query<br />".$connect->ErrorMsg());
}
}
}
unset($posted_condition_value);
// Please note that auto_unescape is already applied in database.php included above
// so we only need to db_quote _POST variables
if (isset($_POST['ConditionConst']) && isset($_POST['editTargetTab']) && $_POST['editTargetTab']=="#CONST")
{
$posted_condition_value = db_quote($_POST['ConditionConst']);
}
elseif (isset($_POST['prevQuestionSGQA']) && isset($_POST['editTargetTab']) && $_POST['editTargetTab']=="#PREVQUESTIONS")
{
$posted_condition_value = db_quote($_POST['prevQuestionSGQA']);
}
elseif (isset($_POST['tokenAttr']) && isset($_POST['editTargetTab']) && $_POST['editTargetTab']=="#TOKENATTRS")
{
$posted_condition_value = db_quote($_POST['tokenAttr']);
}
elseif (isset($_POST['ConditionRegexp']) && isset($_POST['editTargetTab']) && $_POST['editTargetTab']=="#REGEXP")
{
$posted_condition_value = db_quote($_POST['ConditionRegexp']);
}
if (isset($posted_condition_value))
{
$query = "INSERT INTO {$dbprefix}conditions (qid, scenario, cqid, cfieldname, method, value) VALUES "
. "('{$qid}', '{$p_scenario}', '{$p_cqid}', '{$conditionCfieldname}', '{$p_method}', '".$posted_condition_value."')";
$result = $connect->Execute($query) or safe_die ("Couldn't insert new condition<br />$query<br />".$connect->ErrorMsg());
}
}
}
// UPDATE ENTRY IF THIS IS AN EDIT
if (isset($p_subaction) && $p_subaction == "updatecondition")
{
if ((!isset($p_canswers) &&
!isset($_POST['ConditionConst']) &&
!isset($_POST['prevQuestionSGQA']) &&
!isset($_POST['tokenAttr']) &&
!isset($_POST['ConditionRegexp'])) ||
(!isset($p_cquestions) && !isset($p_csrctoken)))
{
$conditionsoutput_action_error .= "<script type=\"text/javascript\">\n<!--\n alert(\"".$clang->gT("Your condition could not be added! It did not include the question and/or answer upon which the condition was based. Please ensure you have selected a question and an answer.","js")."\")\n //-->\n</script>\n";
}
else
{
if (isset($p_cquestions) && $p_cquestions != '')
{
$conditionCfieldname=$p_cquestions;
}
elseif(isset($p_csrctoken) && $p_csrctoken != '')
{
$conditionCfieldname=$p_csrctoken;
}
if (isset($p_canswers))
{
foreach ($p_canswers as $ca)
{ // This is an Edit, there will only be ONE VALUE
$query = "UPDATE {$dbprefix}conditions SET qid='{$qid}', scenario='{$p_scenario}', cqid='{$p_cqid}', cfieldname='{$conditionCfieldname}', method='{$p_method}', value='$ca' "
. " WHERE cid={$p_cid}";
$result = $connect->Execute($query) or safe_die ("Couldn't update condition<br />$query<br />".$connect->ErrorMsg());
}
}
unset($posted_condition_value);
// Please note that auto_unescape is already applied in database.php included above
// so we only need to db_quote _POST variables
if (isset($_POST['ConditionConst']) && isset($_POST['editTargetTab']) && $_POST['editTargetTab']=="#CONST")
{
$posted_condition_value = db_quote($_POST['ConditionConst']);
}
elseif (isset($_POST['prevQuestionSGQA']) && isset($_POST['editTargetTab']) && $_POST['editTargetTab']=="#PREVQUESTIONS")
{
$posted_condition_value = db_quote($_POST['prevQuestionSGQA']);
}
elseif (isset($_POST['tokenAttr']) && isset($_POST['editTargetTab']) && $_POST['editTargetTab']=="#TOKENATTRS")
{
$posted_condition_value = db_quote($_POST['tokenAttr']);
}
elseif (isset($_POST['ConditionRegexp']) && isset($_POST['editTargetTab']) && $_POST['editTargetTab']=="#REGEXP")
{
$posted_condition_value = db_quote($_POST['ConditionRegexp']);
}
if (isset($posted_condition_value))
{
$query = "UPDATE {$dbprefix}conditions SET qid='{$qid}', scenario='{$p_scenario}' , cqid='{$p_cqid}', cfieldname='{$conditionCfieldname}', method='{$p_method}', value='".$posted_condition_value."' "
. " WHERE cid={$p_cid}";
$result = $connect->Execute($query) or safe_die ("Couldn't insert new condition<br />$query<br />".$connect->ErrorMsg());
}
}
}
// DELETE ENTRY IF THIS IS DELETE
if (isset($p_subaction) && $p_subaction == "delete")
{
$query = "DELETE FROM {$dbprefix}conditions WHERE cid={$p_cid}";
$result = $connect->Execute($query) or safe_die ("Couldn't delete condition<br />$query<br />".$connect->ErrorMsg());
}
// DELETE ALL CONDITIONS IN THIS SCENARIO
if (isset($p_subaction) && $p_subaction == "deletescenario")
{
$query = "DELETE FROM {$dbprefix}conditions WHERE qid={$qid} AND scenario={$p_scenario}";
$result = $connect->Execute($query) or safe_die ("Couldn't delete scenario<br />$query<br />".$connect->ErrorMsg());
}
// UPDATE SCENARIO
if (isset($p_subaction) && $p_subaction == "updatescenario" && isset($p_newscenarionum))
{
$query = "UPDATE {$dbprefix}conditions SET scenario=$p_newscenarionum WHERE qid={$qid} AND scenario={$p_scenario}";
$result = $connect->Execute($query) or safe_die ("Couldn't delete scenario<br />$query<br />".$connect->ErrorMsg());
}
// DELETE ALL CONDITIONS FOR THIS QUESTION
if (isset($p_subaction) && $p_subaction == "deleteallconditions")
{
$query = "DELETE FROM {$dbprefix}conditions WHERE qid={$qid}";
$result = $connect->Execute($query) or safe_die ("Couldn't delete scenario<br />$query<br />".$connect->ErrorMsg());
}
// RENUMBER SCENARIOS
if (isset($p_subaction) && $p_subaction == "renumberscenarios")
{
$query = "SELECT DISTINCT scenario FROM {$dbprefix}conditions WHERE qid={$qid} ORDER BY scenario";
$result = $connect->Execute($query) or safe_die ("Couldn't select scenario<br />$query<br />".$connect->ErrorMsg());
$newindex=1;
while ($srow = $result->FetchRow())
{
$query2 = "UPDATE {$dbprefix}conditions set scenario=$newindex WHERE qid={$qid} AND scenario=".$srow['scenario'].";";
$result2 = $connect->Execute($query2) or safe_die ("Couldn't renumber scenario<br />$query<br />".$connect->ErrorMsg());
$newindex++;
}
}
// COPY CONDITIONS IF THIS IS COPY
if (isset($p_subaction) && $p_subaction == "copyconditions")
{
$qid=returnglobal('qid');
$copyconditionsfrom=returnglobal('copyconditionsfrom');
$copyconditionsto=returnglobal('copyconditionsto');
if (isset($copyconditionsto) && is_array($copyconditionsto) && isset($copyconditionsfrom) && is_array($copyconditionsfrom))
{
//Get the conditions we are going to copy
$query = "SELECT * FROM {$dbprefix}conditions\n"
."WHERE cid in ('";
$query .= implode("', '", $copyconditionsfrom);
$query .= "')";
$result = db_execute_assoc($query) or safe_die("Couldn't get conditions for copy<br />$query<br />".$connect->ErrorMsg());
while($row=$result->FetchRow())
{
$proformaconditions[]=array("scenario"=>$row['scenario'],
"cqid"=>$row['cqid'],
"cfieldname"=>$row['cfieldname'],
"method"=>$row['method'],
"value"=>$row['value']);
} // while
foreach ($copyconditionsto as $copyc)
{
list($newsid, $newgid, $newqid)=explode("X", $copyc);
foreach ($proformaconditions as $pfc)
{ //TIBO
//First lets make sure there isn't already an exact replica of this condition
$query = "SELECT * FROM {$dbprefix}conditions\n"
."WHERE qid='$newqid'\n"
."AND scenario='".$pfc['scenario']."'\n"
."AND cqid='".$pfc['cqid']."'\n"
."AND cfieldname='".$pfc['cfieldname']."'\n"
."AND method='".$pfc['method']."'\n"
."AND value='".$pfc['value']."'";
$result = $connect->Execute($query) or safe_die("Couldn't check for existing condition<br />$query<br />".$connect->ErrorMsg());
$count_caseinsensitivedupes = $result->RecordCount();
$countduplicates = 0;
if ($count_caseinsensitivedupes != 0)
{
while ($ccrow=$result->FetchRow())
{
if ($ccrow['value'] == $pfc['value']) $countduplicates++;
}
}
if ($countduplicates == 0) //If there is no match, add the condition.
{
$query = "INSERT INTO {$dbprefix}conditions ( qid,scenario,cqid,cfieldname,method,value) \n"
."VALUES ( '$newqid', '".$pfc['scenario']."', '".$pfc['cqid']."',"
."'".$pfc['cfieldname']."', '".$pfc['method']."',"
."'".$pfc['value']."')";
$result=$connect->Execute($query) or safe_die ("Couldn't insert query<br />$query<br />".$connect->ErrorMsg());
$conditionCopied=true;
}
else
{
$conditionDuplicated=true;
}
}
}
if (isset($conditionCopied) && $conditionCopied === true)
{
if (isset($conditionDuplicated) && $conditionDuplicated ==true)
{
$CopyConditionsMessage = "<div class='partialheader'>(".$clang->gT("Conditions successfully copied (some were skipped because they were duplicates)").")</div>";
}
else
{
$CopyConditionsMessage = "<div class='successheader'>(".$clang->gT("Conditions successfully copied").")</div>";
}
}
else
{
$CopyConditionsMessage = "<div class='warningheader'>(".$clang->gT("No conditions could be copied (due to duplicates)").")</div>";
}
}
else
{
$message = $clang->gT("Did not copy questions","js").": ";
if (!isset($copyconditionsfrom))
{
$message .= $clang->gT("No condition selected to copy from","js").". ";
}
if (!isset($copyconditionsto))
{
$message .= $clang->gT("No question selected to copy condition to","js").".";
}
$conditionsoutput_action_error .= "<script type=\"text/javascript\">\n<!--\nalert('$message');\n//-->\n</script>\n";
}
}
//END PROCESS ACTIONS
$cquestions=Array();
$canswers=Array();
//BEGIN: GATHER INFORMATION
// 1: Get information for this question
if (!isset($qid)) {$qid=returnglobal('qid');}
if (!isset($surveyid)) {$surveyid=returnglobal('sid');}
$thissurvey=getSurveyInfo($surveyid);
$query = "SELECT * "
."FROM {$dbprefix}questions, "
."{$dbprefix}groups "
."WHERE {$dbprefix}questions.gid={$dbprefix}groups.gid "
."AND qid=$qid "
."AND parent_qid=0 "
."AND {$dbprefix}questions.language='".GetBaseLanguageFromSurveyID($surveyid)."'" ;
$result = db_execute_assoc($query) or safe_die ("Couldn't get information for question $qid<br />$query<br />".$connect->ErrorMsg());
while ($rows=$result->FetchRow())
{
$questiongroupname=$rows['group_name'];
$questiontitle=$rows['title'];
$questiontext=$rows['question'];
$questiontype=$rows['type'];
}
// 2: Get all other questions that occur before this question that are pre-determined answer types
// To avoid natural sort order issues,
// first get all questions in natural sort order
// , and find out which number in that order this question is
$qquery = "SELECT * "
."FROM {$dbprefix}questions, "
."{$dbprefix}groups "
."WHERE {$dbprefix}questions.gid={$dbprefix}groups.gid "
."AND parent_qid=0 "
."AND {$dbprefix}questions.sid=$surveyid "
."AND {$dbprefix}questions.language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."AND {$dbprefix}groups.language='".GetBaseLanguageFromSurveyID($surveyid)."' " ;
$qresult = db_execute_assoc($qquery) or safe_die ("$qquery<br />".$connect->ErrorMsg());
$qrows = $qresult->GetRows();
// Perform a case insensitive natural sort on group name then question title (known as "code" in the form) of a multidimensional array
usort($qrows, 'GroupOrderThenQuestionOrder');
$position="before";
// Go through each question until we reach the current one
foreach ($qrows as $qrow)
{
if ($qrow["qid"] != $qid && $position=="before")
{
// remember all previous questions
// all question types are supported.
$questionlist[]=$qrow["qid"];
}
elseif ($qrow["qid"] == $qid)
{
break;
}
}
// Now, using the same array which is now properly sorted by group then question
// Create an array of all the questions that appear AFTER the current one
$position = "before";
foreach ($qrows as $qrow) //Go through each question until we reach the current one
{
if ($qrow["qid"] == $qid)
{
$position="after";
//break;
}
elseif ($qrow["qid"] != $qid && $position=="after")
{
$postquestionlist[]=$qrow['qid'];
}
}
$theserows=array();
$postrows=array();
if (isset($questionlist) && is_array($questionlist))
{
foreach ($questionlist as $ql)
{
$query = "SELECT {$dbprefix}questions.qid, "
."{$dbprefix}questions.sid, "
."{$dbprefix}questions.gid, "
."{$dbprefix}questions.question, "
."{$dbprefix}questions.type, "
."{$dbprefix}questions.title, "
."{$dbprefix}questions.other, "
."{$dbprefix}questions.mandatory "
."FROM {$dbprefix}questions, "
."{$dbprefix}groups "
."WHERE {$dbprefix}questions.gid={$dbprefix}groups.gid "
."AND parent_qid=0 "
."AND {$dbprefix}questions.qid=$ql "
."AND {$dbprefix}questions.language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."AND {$dbprefix}groups.language='".GetBaseLanguageFromSurveyID($surveyid)."'" ;
$result=db_execute_assoc($query) or die("Couldn't get question $qid");
$thiscount=$result->RecordCount();
// And store again these questions in this array...
while ($myrows=$result->FetchRow())
{ //key => value
$theserows[]=array("qid"=>$myrows['qid'],
"sid"=>$myrows['sid'],
"gid"=>$myrows['gid'],
"question"=>$myrows['question'],
"type"=>$myrows['type'],
"mandatory"=>$myrows['mandatory'],
"other"=>$myrows['other'],
"title"=>$myrows['title']);
}
}
}
if (isset($postquestionlist) && is_array($postquestionlist))
{
foreach ($postquestionlist as $pq)
{
$query = "SELECT q.qid, "
."q.sid, "
."q.gid, "
."q.question, "
."q.type, "
."q.title, "
."q.other, "
."q.mandatory "
."FROM {$dbprefix}questions q, "
."{$dbprefix}groups g "
."WHERE q.gid=g.gid AND "
."q.parent_qid=0 AND "
."q.qid=$pq AND "
."q.language='".GetBaseLanguageFromSurveyID($surveyid)."' AND "
."g.language='".GetBaseLanguageFromSurveyID($surveyid)."'";
$result = db_execute_assoc($query) or safe_die("Couldn't get postquestions $qid<br />$query<br />".$connect->ErrorMsg());
$postcount=$result->RecordCount();
while($myrows=$result->FetchRow())
{
$postrows[]=array("qid"=>$myrows['qid'],
"sid"=>$myrows['sid'],
"gid"=>$myrows['gid'],
"question"=>$myrows['question'],
"type"=>$myrows['type'],
"mandatory"=>$myrows['mandatory'],
"other"=>$myrows['other'],
"title"=>$myrows['title']);
} // while
}
$postquestionscount=count($postrows);
}
$questionscount=count($theserows);
if (isset($postquestionscount) && $postquestionscount > 0)
{ //Build the array used for the questionNav and copyTo select boxes
foreach ($postrows as $pr)
{
$pquestions[]=array("text"=>$pr['title'].": ".substr(strip_tags($pr['question']), 0, 80),
"fieldname"=>$pr['sid']."X".$pr['gid']."X".$pr['qid']);
}
}
// Previous question parsing ==> building cquestions[] and canswers[]
if ($questionscount > 0)
{
$X="X";
foreach($theserows as $rows)
{
$shortquestion=$rows['title'].": ".strip_tags($rows['question']);
if ($rows['type'] == "A" ||
$rows['type'] == "B" ||
$rows['type'] == "C" ||
$rows['type'] == "E" ||
$rows['type'] == "F" ||
$rows['type'] == "H" )
{
$aquery="SELECT * "
."FROM {$dbprefix}questions "
."WHERE parent_qid={$rows['qid']} "
."AND language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."ORDER BY question_order";
$aresult=db_execute_assoc($aquery) or safe_die ("Couldn't get answers to Array questions<br />$aquery<br />".$connect->ErrorMsg());
while ($arows = $aresult->FetchRow())
{
$shortanswer = "{$arows['title']}: [" . FlattenText($arows['question']) . "]";
$shortquestion=$rows['title'].":$shortanswer ".FlattenText($rows['question']);
$cquestions[]=array($shortquestion, $rows['qid'], $rows['type'], $rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']);
switch ($rows['type'])
{
case "A": //Array 5 buttons
for ($i=1; $i<=5; $i++)
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], $i, $i);
}
break;
case "B": //Array 10 buttons
for ($i=1; $i<=10; $i++)
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], $i, $i);
}
break;
case "C": //Array Y/N/NA
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], "Y", $clang->gT("Yes"));
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], "U", $clang->gT("Uncertain"));
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], "N", $clang->gT("No"));
break;
case "E": //Array >/=/<
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], "I", $clang->gT("Increase"));
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], "S", $clang->gT("Same"));
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], "D", $clang->gT("Decrease"));
break;
case "F": //Array Flexible Row
case "H": //Array Flexible Column
$fquery = "SELECT * "
."FROM {$dbprefix}answers "
."WHERE qid={$rows['qid']} "
."AND language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."AND scale_id=0 "
."ORDER BY sortorder, code ";
$fresult = db_execute_assoc($fquery);
while ($frow=$fresult->FetchRow())
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], $frow['code'], $frow['answer']);
}
break;
}
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], "", $clang->gT("No answer"));
}
} //while
}
elseif ($rows['type'] == ":" || $rows['type'] == ";")
{ // Multiflexi
//Get question attribute for $canswers
$qidattributes=getQuestionAttributes($rows['qid'], $rows['type']);
if (isset($qidattributes['multiflexible_max']) && trim($qidattributes['multiflexible_max'])!='') {
$maxvalue=$qidattributes['multiflexible_max'];
} else {
$maxvalue=10;
}
if (isset($qidattributes['multiflexible_min']) && trim($qidattributes['multiflexible_min'])!='') {
$minvalue=$qidattributes['multiflexible_min'];
} else {
$minvalue=1;
}
if (isset($qidattributes['multiflexible_step']) && trim($qidattributes['multiflexible_step'])!='') {
$stepvalue=$qidattributes['multiflexible_step'];
} else {
$stepvalue=1;
}
if (isset($qidattributes['multiflexible_checkbox']) && $qidattributes['multiflexible_checkbox']!=0) {
$minvalue=0;
$maxvalue=1;
$stepvalue=1;
}
// Get the Y-Axis
$fquery = "SELECT sq.*, q.other"
." FROM ".db_table_name('questions')." sq, ".db_table_name('questions')." q"
." WHERE sq.sid=$surveyid AND sq.parent_qid=q.qid "
. "AND q.language='".GetBaseLanguageFromSurveyID($surveyid)."'"
." AND sq.language='".GetBaseLanguageFromSurveyID($surveyid)."'"
." AND q.qid={$rows['qid']}
AND sq.scale_id=0
ORDER BY sq.question_order";
$y_axis_db = db_execute_assoc($fquery);
// Get the X-Axis
$aquery = "SELECT sq.*
FROM ".db_table_name('questions')." q, ".db_table_name('questions')." sq
WHERE q.sid=$surveyid
AND sq.parent_qid=q.qid
AND q.language='".GetBaseLanguageFromSurveyID($surveyid)."'
AND sq.language='".GetBaseLanguageFromSurveyID($surveyid)."'
AND q.qid=".$rows['qid']."
AND sq.scale_id=1
ORDER BY sq.question_order";
$x_axis_db=db_execute_assoc($aquery) or safe_die ("Couldn't get answers to Array questions<br />$aquery<br />".$connect->ErrorMsg());
while ($frow=$x_axis_db->FetchRow())
{
$x_axis[$frow['title']]=$frow['question'];
}
while ($arows = $y_axis_db->FetchRow())
{
foreach($x_axis as $key=>$val)
{
$shortquestion=$rows['title'].":{$arows['title']}:$key: [".strip_tags($arows['question']). "][" .strip_tags($val). "] " . FlattenText($rows['question']);
$cquestions[]=array($shortquestion, $rows['qid'], $rows['type'], $rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']."_".$key);
if ($rows['type'] == ":")
{
for($ii=$minvalue; $ii<=$maxvalue; $ii+=$stepvalue)
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], $ii, $ii);
}
}
}
}
unset($x_axis);
} //if A,B,C,E,F,H
elseif ($rows['type'] == "1") //Multi Scale
{
$aquery="SELECT * "
."FROM {$dbprefix}questions "
."WHERE parent_qid={$rows['qid']} "
."AND language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."ORDER BY question_order";
$aresult=db_execute_assoc($aquery) or safe_die ("Couldn't get answers to Array questions<br />$aquery<br />".$connect->ErrorMsg());
while ($arows = $aresult->FetchRow())
{
$attr = getQuestionAttributes($rows['qid']);
$label1 = isset($attr['dualscale_headerA']) ? $attr['dualscale_headerA'] : 'Label1';
$label2 = isset($attr['dualscale_headerB']) ? $attr['dualscale_headerB'] : 'Label2';
$shortanswer = "{$arows['title']}: [" . strip_tags($arows['question']) . "][$label1]";
$shortquestion=$rows['title'].":$shortanswer ".strip_tags($rows['question']);
$cquestions[]=array($shortquestion, $rows['qid'], $rows['type'], $rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']."#0");
$shortanswer = "{$arows['title']}: [" . strip_tags($arows['question']) . "][$label2]";
$shortquestion=$rows['title'].":$shortanswer ".strip_tags($rows['question']);
$cquestions[]=array($shortquestion, $rows['qid'], $rows['type'], $rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']."#1");
// first label
$lquery="SELECT * "
."FROM {$dbprefix}answers "
."WHERE qid={$rows['qid']} "
."AND scale_id=0 "
."AND language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."ORDER BY sortorder, answer";
$lresult=db_execute_assoc($lquery) or safe_die ("Couldn't get labels to Array <br />$lquery<br />".$connect->ErrorMsg());
while ($lrows = $lresult->FetchRow())
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']."#0", "{$lrows['code']}", "{$lrows['code']}");
}
// second label
$lquery="SELECT * "
."FROM {$dbprefix}answers "
."WHERE qid={$rows['qid']} "
."AND scale_id=1 "
."AND language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."ORDER BY sortorder, answer";
$lresult=db_execute_assoc($lquery) or safe_die ("Couldn't get labels to Array <br />$lquery<br />".$connect->ErrorMsg());
while ($lrows = $lresult->FetchRow())
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']."#1", "{$lrows['code']}", "{$lrows['code']}");
}
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']."#0", "", $clang->gT("No answer"));
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']."#1", "", $clang->gT("No answer"));
}
} //while
}
elseif ($rows['type'] == "K" ||$rows['type'] == "Q") //Multi shorttext/numerical
{
$aquery="SELECT * "
."FROM {$dbprefix}questions "
."WHERE parent_qid={$rows['qid']} "
."AND language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."ORDER BY question_order";
$aresult=db_execute_assoc($aquery) or safe_die ("Couldn't get answers to Array questions<br />$aquery<br />".$connect->ErrorMsg());
while ($arows = $aresult->FetchRow())
{
$shortanswer = "{$arows['title']}: [" . strip_tags($arows['question']) . "]";
$shortquestion=$rows['title'].":$shortanswer ".strip_tags($rows['question']);
$cquestions[]=array($shortquestion, $rows['qid'], $rows['type'], $rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']);
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], "", $clang->gT("No answer"));
}
} //while
}
elseif ($rows['type'] == "R") //Answer Ranking
{
$aquery="SELECT * "
."FROM {$dbprefix}answers "
."WHERE qid={$rows['qid']} "
."AND ".db_table_name('answers').".language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."AND scale_id=0 "
."ORDER BY sortorder, answer";
$aresult=db_execute_assoc($aquery) or safe_die ("Couldn't get answers to Ranking question<br />$aquery<br />".$connect->ErrorMsg());
$acount=$aresult->RecordCount();
while ($arow=$aresult->FetchRow())
{
$theanswer = addcslashes($arow['answer'], "'");
$quicky[]=array($arow['code'], $theanswer);
}
for ($i=1; $i<=$acount; $i++)
{
$cquestions[]=array("{$rows['title']}: [RANK $i] ".strip_tags($rows['question']), $rows['qid'], $rows['type'], $rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$i);
foreach ($quicky as $qck)
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$i, $qck[0], $qck[1]);
}
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$i, " ", $clang->gT("No answer"));
}
}
unset($quicky);
} // End if type R
elseif($rows['type'] == "M" || $rows['type'] == "P")
{
$shortanswer = " [".$clang->gT("Group of checkboxes")."]";
$shortquestion=$rows['title'].":$shortanswer ".strip_tags($rows['question']);
$cquestions[]=array($shortquestion, $rows['qid'], $rows['type'], $rows['sid'].$X.$rows['gid'].$X.$rows['qid']);
$aquery="SELECT * "
."FROM {$dbprefix}questions "
."WHERE parent_qid={$rows['qid']} "
."AND language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."ORDER BY question_order";
$aresult=db_execute_assoc($aquery) or safe_die ("Couldn't get answers to this question<br />$aquery<br />".$connect->ErrorMsg());
while ($arows=$aresult->FetchRow())
{
$theanswer = addcslashes($arows['question'], "'");
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], $arows['title'], $theanswer);
$shortanswer = "{$arows['title']}: [" . strip_tags($arows['question']) . "]";
$shortanswer .= "[".$clang->gT("Single checkbox")."]";
$shortquestion=$rows['title'].":$shortanswer ".strip_tags($rows['question']);
$cquestions[]=array($shortquestion, $rows['qid'], $rows['type'], "+".$rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title']);
$canswers[]=array("+".$rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], 'Y', 'checked');
$canswers[]=array("+".$rows['sid'].$X.$rows['gid'].$X.$rows['qid'].$arows['title'], '', 'not checked');
}
}
elseif($rows['type'] == "X") //Boilerplate question
{
//Just ignore this questiontype
}
else
{
$cquestions[]=array($shortquestion, $rows['qid'], $rows['type'], $rows['sid'].$X.$rows['gid'].$X.$rows['qid']);
switch ($rows['type'])
{
case "Y": // Y/N/NA
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], "Y", $clang->gT("Yes"));
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], "N", $clang->gT("No"));
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], " ", $clang->gT("No answer"));
}
break;
case "G": //Gender
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], "F", $clang->gT("Female"));
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], "M", $clang->gT("Male"));
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], " ", $clang->gT("No answer"));
}
break;
case "5": // 5 choice
for ($i=1; $i<=5; $i++)
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], $i, $i);
}
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], " ", $clang->gT("No answer"));
}
break;
case "N": // Simple Numerical questions
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], " ", $clang->gT("No answer"));
}
break;
default:
$aquery="SELECT * "
."FROM {$dbprefix}answers "
."WHERE qid={$rows['qid']} "
."AND language='".GetBaseLanguageFromSurveyID($surveyid)."' "
."AND scale_id=0 "
."ORDER BY sortorder, "
."answer";
// Ranking question? Replacing "Ranking" by "this"
$aresult=db_execute_assoc($aquery) or safe_die ("Couldn't get answers to this question<br />$aquery<br />".$connect->ErrorMsg());
while ($arows=$aresult->FetchRow())
{
$theanswer = addcslashes($arows['answer'], "'");
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], $arows['code'], $theanswer);
}
if ($rows['type'] == "D")
{
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], " ", $clang->gT("No answer"));
}
}
elseif ($rows['type'] != "M" &&
$rows['type'] != "P" &&
$rows['type'] != "J" &&
$rows['type'] != "I" )
{
// For dropdown questions
// optinnaly add the 'Other' answer
if ( ($rows['type'] == "L" ||
$rows['type'] == "!") &&
$rows['other'] == "Y")
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], "-oth-", $clang->gT("Other"));
}
// Only Show No-Answer if question is not mandatory
if ($rows['mandatory'] != 'Y')
{
$canswers[]=array($rows['sid'].$X.$rows['gid'].$X.$rows['qid'], " ", $clang->gT("No answer"));
}
}
break;
}//switch row type
} //else
} //foreach theserows
} //if questionscount > 0
//END Gather Information for this question
$conditionsoutput_main_content .= "\t<tr>\n"
."<td align='center'>\n";
// Now we have enough information, we can create the menubar and question Navigator
$conditionsoutput_menubar .= "\t<div class='menubar'>"
."<div class='menubar-title'>"
."<strong>".$clang->gT("Conditions designer").":</strong> "
."</div>\n";
$conditionsoutput_menubar .= "\t<div class='menubar-main'>\n"
."<div class='menubar-left'>\n"
."<a href=\"#\" onclick=\"window.open('$scriptname?sid=$surveyid$extraGetParams', '_top')\" title='".$clang->gTview("Return to survey administration")."'>"
."<img name='HomeButton' src='$imagefiles/home.png' alt='".$clang->gT("Return to survey administration")."' /></a>\n"
."<img src='$imagefiles/blank.gif' alt='' width='11' />\n"
."<img src='$imagefiles/seperator.gif' alt='' />\n"
."<a href=\"#\" onclick=\"window.open('$scriptname?action=conditions&sid=$surveyid&gid=$gid&qid=$qid', '_top')\" title='".$clang->gTview("Show conditions for this question")."' >"
."<img name='SummaryButton' src='$imagefiles/summary.png' alt='".$clang->gT("Show conditions for this question")."' /></a>\n"
."<img src='$imagefiles/seperator.gif' alt='' />\n"
."<a href=\"#\" onclick=\"window.open('$scriptname?action=conditions&sid=$surveyid&gid=$gid&qid=$qid&subaction=editconditionsform', '_top')\" title='".$clang->gTview("Add and edit conditions")."' >"
."<img name='ConditionAddButton' src='$imagefiles/conditions_add.png' alt='".$clang->gT("Add and edit conditions")."' /></a>\n"
."<a href=\"#\" onclick=\"window.open('$scriptname?action=conditions&sid=$surveyid&gid=$gid&qid=$qid&subaction=copyconditionsform', '_top')\" title='".$clang->gTview("Copy conditions")."' >"
."<img name='ConditionCopyButton' src='$imagefiles/conditions_copy.png' alt='".$clang->gT("Copy conditions")."' /></a>\n";
$quesitonNavOptions = "<optgroup class='activesurveyselect' label='".$clang->gT("Before","js")."'>";
foreach ($theserows as $row)
{
$question=$row['question'];
$question=strip_tags($question);
if (strlen($question)<35)
{
$questionselecter = $question;
}
else
{
//$questionselecter = substr($question, 0, 35)."..";
$questionselecter = htmlspecialchars(mb_strcut(html_entity_decode($question,ENT_QUOTES,'UTF-8'), 0, 35, 'UTF-8'))."...";