/
printablesurvey.php
1964 lines (1752 loc) · 96.4 KB
/
printablesurvey.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$
*/
//Ensure script is not run directly, avoid path disclosure
include_once("login_check.php");
//gett all settings for the printable survey
include_once(dirname(__FILE__)."/../config-defaults.php");
//we need this one for PDF export
include_once(dirname(__FILE__)."/classes/tcpdf/extensiontcpdf.php");
// TEMP function for debugging
function try_debug($line)
{
global $debug;
if($debug > 0)
{
return '<!-- printablesurvey.php: '.$line.' -->';
}
}
$surveyid = $_GET['sid'];
//echo '<pre>'.print_r($_SESSION,true).'</pre>';
// PRESENT SURVEY DATAENTRY SCREEN
if(isset($_POST['printableexport']))
{
$pdf = new PDF ($pdforientation,'mm','A4');
$pdf->SetFont($pdfdefaultfont,'',$pdffontsize);
$pdf->AddPage();
}
// Set the language of the survey, either from GET parameter of session var
if (isset($_GET['lang']))
{
$_GET['lang'] = preg_replace("/[^a-zA-Z0-9-]/", "", $_GET['lang']);
if ($_GET['lang']) $surveyprintlang = $_GET['lang'];
} else
{
$surveyprintlang=GetbaseLanguageFromSurveyid($surveyid);
}
// Setting the selected language for printout
$clang = new limesurvey_lang($surveyprintlang);
$desquery = "SELECT * FROM ".db_table_name('surveys')." inner join ".db_table_name('surveys_languagesettings')." on (surveyls_survey_id=sid) WHERE sid={$surveyid} and surveyls_language=".$connect->qstr($surveyprintlang); //Getting data for this survey
$desrow = $connect->GetRow($desquery);
if ($desrow==false || count($desrow)==0)
{
safe_die('Invalid survey ID');
}
//echo '<pre>'.print_r($desrow,true).'</pre>';
$template = $desrow['template'];
$welcome = $desrow['surveyls_welcometext'];
$end = $desrow['surveyls_endtext'];
$surveyname = $desrow['surveyls_title'];
$surveydesc = $desrow['surveyls_description'];
$surveyactive = $desrow['active'];
$surveytable = db_table_name("survey_".$desrow['sid']);
$surveyexpirydate = $desrow['expires'];
$surveystartdate = $desrow['startdate'];
$surveyfaxto = $desrow['faxto'];
$dateformattype = $desrow['surveyls_dateformat'];
if(isset($_POST['printableexport'])){$pdf->titleintopdf($surveyname,$surveydesc);}
$dformat=getDateFormatData($dateformattype);
$dformat=$dformat['phpdate'];
$expirytimestamp = strtotime($surveyexpirydate);
$expirytimeofday_h = date('H',$expirytimestamp);
$expirytimeofday_m = date('i',$expirytimestamp);
$surveyexpirydate = date($dformat,$expirytimestamp);
if(!empty($expirytimeofday_h) || !empty($expirytimeofday_m))
{
$surveyexpirydate .= ' – '.$expirytimeofday_h.':'.$expirytimeofday_m;
};
//define('PRINT_TEMPLATE' , '/templates/print/' , true);
if(is_file($usertemplaterootdir.'/'.$template.'/print_survey.pstpl'))
{
define('PRINT_TEMPLATE_DIR' , $usertemplaterootdir.'/'.$template.'/' , true);
define('PRINT_TEMPLATE_URL' , $usertemplaterooturl.'/'.$template.'/' , true);
}
else
{
define('PRINT_TEMPLATE_DIR' , $standardtemplaterootdir.'/default/' , true);
define('PRINT_TEMPLATE_URL' , $standardtemplaterooturl.'/default/' , true);
}
$fieldmap=createFieldMap($surveyid);
$degquery = "SELECT * FROM ".db_table_name("groups")." WHERE sid='{$surveyid}' AND language='{$surveyprintlang}' ORDER BY ".db_table_name("groups").".group_order";
$degresult = db_execute_assoc($degquery);
if (!isset($surveyfaxto) || !$surveyfaxto and isset($surveyfaxnumber))
{
$surveyfaxto=$surveyfaxnumber; //Use system fax number if none is set in survey.
}
$pdf_form='';
if(isset($usepdfexport) && $usepdfexport == 1 && !in_array($surveyprintlang,$notsupportlanguages))
{
$pdf_form = '
<form action="'.$scriptname.'?action=showprintablesurvey&sid='.$surveyid.'&lang='.$surveyprintlang.'" method="post">
<input type="submit" value="'.$clang->gT('PDF Export').'"/>
<input type="hidden" name="checksessionbypost" value="'.htmlspecialchars($_SESSION['checksessionpost']).'"/>
<input type="hidden" name="printableexport" value="true"/>
</form>
';
}
$headelements = getPrintableHeader();
//if $showsgqacode is enabled at config.php show table name for reference
if(isset($showsgqacode) && $showsgqacode == true)
{
$surveyname = $surveyname."<br />[".$clang->gT('Database')." ".$clang->gT('table').": $surveytable]";
}
else
{
$surveyname = "";
}
$survey_output = array(
'SITENAME' => $sitename
,'SURVEYNAME' => $surveyname
,'SURVEYDESCRIPTION' => $surveydesc
,'WELCOME' => $welcome
,'END' => $end
,'THEREAREXQUESTIONS' => 0
,'SUBMIT_TEXT' => $clang->gT("Submit Your Survey.")
,'SUBMIT_BY' => $surveyexpirydate
,'THANKS' => $clang->gT("Thank you for completing this survey.")
,'PDF_FORM' => $pdf_form
,'HEADELEMENTS' => $headelements
,'TEMPLATEURL' => PRINT_TEMPLATE_URL
,'FAXTO' => $surveyfaxto
,'PRIVACY' => ''
,'GROUPS' => ''
);
$survey_output['FAX_TO'] ='';
if(!empty($surveyfaxto) && $surveyfaxto != '000-00000000') //If no fax number exists, don't display faxing information!
{
$survey_output['FAX_TO'] = $clang->gT("Please fax your completed survey to:")." $surveyfaxto";
}
if ($surveystartdate!='')
{
$survey_output['SUBMIT_BY'] = sprintf($clang->gT("Please submit by %s"), $surveyexpirydate);
}
/**
* Output arrays:
* $survey_output = final vaiables for whole survey
* $survey_output['SITENAME'] =
* $survey_output['SURVEYNAME'] =
* $survey_output['SURVEY_DESCRIPTION'] =
* $survey_output['WELCOME'] =
* $survey_output['THEREAREXQUESTIONS'] =
* $survey_output['PDF_FORM'] =
* $survey_output['HEADELEMENTS'] =
* $survey_output['TEMPLATEURL'] =
* $survey_output['SUBMIT_TEXT'] =
* $survey_output['SUBMIT_BY'] =
* $survey_output['THANKS'] =
* $survey_output['FAX_TO'] =
* $survey_output['SURVEY'] = contains an array of all the group arrays
*
* $groups[] = an array of all the groups output
* $group['GROUPNAME'] =
* $group['GROUPDESCRIPTION'] =
* $group['QUESTIONS'] = templated formatted content if $question is appended to this at the end of processing each question.
* $group['ODD_EVEN'] = class to differentiate alternate groups
* $group['SCENARIO'] =
*
* $questions[] = contains an array of all the questions within a group
* $question['QUESTION_CODE'] = content of the question code field
* $question['QUESTION_TEXT'] = content of the question field
* $question['QUESTION_SCENARIO'] = if there are conditions on a question, list the conditions.
* $question['QUESTION_MANDATORY'] = translated 'mandatory' identifier
* $question['QUESTION_CLASS'] = classes to be added to wrapping question div
* $question['QUESTION_TYPE_HELP'] = instructions on how to complete the question
* $question['QUESTION_MAN_MESSAGE'] = (not sure if this is used) mandatory error
* $question['QUESTION_VALID_MESSAGE'] = (not sure if this is used) validation error
* $question['ANSWER'] = contains formatted HTML answer
* $question['QUESTIONHELP'] = content of the question help field.
*
*/
function populate_template( $template , $input , $line = '')
{
global $rootdir, $debug;
/**
* A poor mans templating system.
*
* $template template filename (path is privided by config.php)
* $input a key => value array containg all the stuff to be put into the template
* $line for debugging purposes only.
*
* Returns a formatted string containing template with
* keywords replaced by variables.
*
* How:
*/
$full_path = PRINT_TEMPLATE_DIR.'print_'.$template.'.pstpl';
$full_constant = 'TEMPLATE'.$template.'.pstpl';
if(!defined($full_constant))
{
if(is_file($full_path))
{
define( $full_constant , file_get_contents($full_path));
$template_content = constant($full_constant);
$test_empty = trim($template_content);
if(empty($test_empty))
{
return "<!--\n\t$full_path\n\tThe template was empty so is useless.\n-->";
}
}
else
{
define($full_constant , '');
return "<!--\n\t$full_path is not a propper file or is missing.\n-->";
}
}
else
{
$template_content = constant($full_constant);
$test_empty = trim($template_content);
if(empty($test_empty))
{
return "<!--\n\t$full_path\n\tThe template was empty so is useless.\n-->";
}
}
if(is_array($input))
{
foreach($input as $key => $value)
{
$find[] = '{'.$key.'}';
$replace[] = $value;
}
return str_replace( $find , $replace , $template_content );
}
else
{
if($debug > 0)
{
if(!empty($line))
{
$line = 'LINE '.$line.': ';
}
return '<!-- '.$line.'There was nothing to put into the template -->'."\n";
}
}
}
function input_type_image( $type , $title = '' , $x = 40 , $y = 1 , $line = '' )
{
global $rooturl, $rootdir;
if($type == 'other' or $type == 'othercomment')
{
$x = 1;
}
$tail = substr($x , -1 , 1);
switch($tail)
{
case '%':
case 'm':
case 'x': $x_ = $x;
break;
default: $x_ = $x / 2;
}
if($y < 2)
{
$y_ = 2;
}
else
{
$y_ = $y * 2;
}
if(!empty($title))
{
$div_title = ' title="'.htmlspecialchars($title).'"';
}
else
{
$div_title = '';
}
switch($type)
{
case 'textarea':
case 'text': $style = ' style="width:'.$x_.'em; height:'.$y_.'em;"';
break;
default: $style = '';
}
switch($type)
{
case 'radio':
case 'checkbox':if(!defined('IMAGE_'.$type.'_SIZE'))
{
$image_dimensions = getimagesize(PRINT_TEMPLATE_DIR.'print_img_'.$type.'.png');
// define('IMAGE_'.$type.'_SIZE' , ' width="'.$image_dimensions[0].'" height="'.$image_dimensions[1].'"');
define('IMAGE_'.$type.'_SIZE' , ' width="14" height="14"');
}
$output = '<img src="'.PRINT_TEMPLATE_URL.'print_img_'.$type.'.png"'.constant('IMAGE_'.$type.'_SIZE').' alt="'.htmlspecialchars($title).'" class="input-'.$type.'" />';
break;
case 'rank':
case 'other':
case 'othercomment':
case 'text':
case 'textarea':$output = '<div class="input-'.$type.'"'.$style.$div_title.'>{NOTEMPTY}</div>';
break;
default: $output = '';
}
return $output;
}
function star_replace($input)
{
return preg_replace(
'/\*(.*)\*/U'
,'<strong>\1</strong>'
,$input
);
}
$total_questions = 0;
$mapquestionsNumbers=Array();
// =========================================================
// START doin the business:
$pdfoutput = '';
while ($degrow = $degresult->FetchRow())
{
// ---------------------------------------------------
// START doing groups
$deqquery = "SELECT * FROM ".db_table_name("questions")." WHERE sid=$surveyid AND gid={$degrow['gid']} AND language='{$surveyprintlang}' AND parent_qid=0 AND TYPE<>'I' ORDER BY question_order";
$deqresult = db_execute_assoc($deqquery);
$deqrows = array(); //Create an empty array in case FetchRow does not return any rows
while ($deqrow = $deqresult->FetchRow()) {$deqrows[] = $deqrow;} // Get table output into array
// Perform a case insensitive natural sort on group name then question title of a multidimensional array
usort($deqrows, 'GroupOrderThenQuestionOrder');
if ($degrow['description'])
{
$group_desc = $degrow['description'];
}
else
{
$group_desc = '';
}
$group = array(
'GROUPNAME' => $degrow['group_name']
,'GROUPDESCRIPTION' => $group_desc
,'QUESTIONS' => '' // templated formatted content if $question is appended to this at the end of processing each question.
);
if(isset($_POST['printableexport'])){$pdf->titleintopdf($degrow['group_name'],$degrow['description']);}
$gid = $degrow['gid'];
//Alternate bgcolor for different groups
if (!isset($group['ODD_EVEN']) || $group['ODD_EVEN'] == ' g-row-even')
{
$group['ODD_EVEN'] = ' g-row-odd';}
else
{
$group['ODD_EVEN'] = ' g-row-even';
}
//Loop through questions
foreach ($deqrows as $deqrow)
{
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// START doing questions
$qidattributes=getQuestionAttributes($deqrow['qid'],$deqrow['type']);
if ($qidattributes['hidden']==1)
{
continue;
}
//GET ANY CONDITIONS THAT APPLY TO THIS QUESTION
$printablesurveyoutput = '';
$explanation = ''; //reset conditions explanation
$s=0;
$scenarioquery="SELECT DISTINCT ".db_table_name("conditions").".scenario FROM ".db_table_name("conditions")." WHERE ".db_table_name("conditions").".qid={$deqrow['qid']} ORDER BY scenario";
$scenarioresult=db_execute_assoc($scenarioquery);
//Loop through distinct scenarios, thus grouping them together.
while ($scenariorow=$scenarioresult->FetchRow())
{
if($s == 0 && $scenarioresult->RecordCount() > 1)
{
$explanation .= '<p class="scenario">'.try_debug(__LINE__)." -------- Scenario {$scenariorow['scenario']} --------</p>\n\n";
}
if($s > 0)
{
$explanation .= '<p class="scenario">'.try_debug(__LINE__).' -------- '.$clang->gT("or")." Scenario {$scenariorow['scenario']} --------</p>\n\n";
}
$x=0;
$distinctquery="SELECT cqid, method, cfieldname, value
FROM ".db_table_name("conditions")."
WHERE ".db_table_name("conditions").".qid={$deqrow['qid']}
AND ".db_table_name("conditions").".scenario={$scenariorow['scenario']}
group by cqid, method, cfieldname, value
ORDER BY cqid";
$distinctresult=db_execute_assoc($distinctquery);
//Loop through each condition for a particular scenario.
while ($distinctrow=$distinctresult->FetchRow())
{
$subquery='select title, question from '.db_table_name("questions")." where qid={$distinctrow['cqid']} AND parent_qid=0 AND language='{$surveyprintlang}'";
$subresult=$connect->GetRow($subquery);
if($x > 0)
{
$explanation .= ' <em>'.$clang->gT('and').'</em> ';
}
if(trim($distinctrow['method'])=='')
{
$distinctrow['method']='==';
}
if($distinctrow['cqid']){ // cqid != 0 ==> previous answer match
if($distinctrow['method']=='==')
{
$explanation .= $clang->gT("Answer was")." ";
}
elseif($distinctrow['method']=='!=')
{
$explanation .= $clang->gT("Answer was NOT")." ";
}
elseif($distinctrow['method']=='<')
{
$explanation .= $clang->gT("Answer was less than")." ";
}
elseif($distinctrow['method']=='<=')
{
$explanation .= $clang->gT("Answer was less than or equal to")." ";
}
elseif($distinctrow['method']=='>=')
{
$explanation .= $clang->gT("Answer was greater than or equal to")." ";
}
elseif($distinctrow['method']=='>')
{
$explanation .= $clang->gT("Answer was greater than")." ";
}
elseif($distinctrow['method']=='RX')
{
$explanation .= $clang->gT("Answer matched (regexp)")." ";
}
else
{
$explanation .= $clang->gT("Answer was")." ";
}
}
if(!$distinctrow['cqid']){ // cqid == 0 ==> token attribute match
$tokenData = GetTokenFieldsAndNames($surveyid);
preg_match('/^{TOKEN:([^}]*)}$/',$distinctrow['cfieldname'],$extractedTokenAttr);
$explanation .= "Your ".$tokenData[strtolower($extractedTokenAttr[1])]." ";
if($distinctrow['method']=='==')
{
$explanation .= $clang->gT("is")." ";
}
elseif($distinctrow['method']=='!=')
{
$explanation .= $clang->gT("is NOT")." ";
}
elseif($distinctrow['method']=='<')
{
$explanation .= $clang->gT("is less than")." ";
}
elseif($distinctrow['method']=='<=')
{
$explanation .= $clang->gT("is less than or equal to")." ";
}
elseif($distinctrow['method']=='>=')
{
$explanation .= $clang->gT("is greater than or equal to")." ";
}
elseif($distinctrow['method']=='>')
{
$explanation .= $clang->gT("is greater than")." ";
}
elseif($distinctrow['method']=='RX')
{
$explanation .= $clang->gT("is matched (regexp)")." ";
}
else
{
$explanation .= $clang->gT("is")." ";
}
$answer_section = $distinctrow['value'];
}
$conquery="SELECT cid, cqid, q.title,\n"
."q.question, value, q.type, cfieldname\n"
."FROM ".db_table_name("conditions")." c, ".db_table_name("questions")." q\n"
."WHERE c.cqid=q.qid\n"
."AND c.cqid={$distinctrow['cqid']}\n"
."AND c.qid={$deqrow['qid']} \n"
."AND c.scenario={$scenariorow['scenario']} \n"
."AND language='{$surveyprintlang}'";
$conresult=db_execute_assoc($conquery) or safe_die("$conquery<br />".htmlspecialchars($connect->ErrorMsg()));
$conditions=array();
while ($conrow=$conresult->FetchRow())
{
$postans="";
$value=$conrow['value'];
switch($conrow['type'])
{
case "Y":
switch ($conrow['value'])
{
case "Y": $conditions[]=$clang->gT("Yes"); break;
case "N": $conditions[]=$clang->gT("No"); break;
}
break;
case "G":
switch($conrow['value'])
{
case "M": $conditions[]=$clang->gT("Male"); break;
case "F": $conditions[]=$clang->gT("Female"); break;
} // switch
break;
case "A":
case "B":
case ":":
case ";":
$conditions[]=$conrow['value'];
break;
case "C":
switch($conrow['value'])
{
case "Y": $conditions[]=$clang->gT("Yes"); break;
case "U": $conditions[]=$clang->gT("Uncertain"); break;
case "N": $conditions[]=$clang->gT("No"); break;
} // switch
break;
case "E":
switch($conrow['value'])
{
case "I": $conditions[]=$clang->gT("Increase"); break;
case "D": $conditions[]=$clang->gT("Decrease"); break;
case "S": $conditions[]=$clang->gT("Same"); break;
}
case "1":
$labelIndex=preg_match("/^[^#]+#([01]{1})$/",$conrow['cfieldname']);
if ($labelIndex == 0)
{ // TIBO
$fquery = "SELECT * FROM ".db_table_name("answers")."\n"
. "WHERE qid='{$conrow['cqid']}'\n"
. "AND code='{$conrow['value']}' and scale_id=0 AND language='{$surveyprintlang}'";
$fresult=db_execute_assoc($fquery) or safe_die("$fquery<br />".htmlspecialchars($connect->ErrorMsg()));
while($frow=$fresult->FetchRow())
{
$postans=$frow['answer'];
$conditions[]=$frow['answer'];
} // while
}
elseif ($labelIndex == 1)
{
$fquery = "SELECT * FROM ".db_table_name("answers")."\n"
. "WHERE qid='{$conrow['cqid']}'\n"
. "AND code='{$conrow['value']}' and scale_id=1 AND language='{$surveyprintlang}'";
$fresult=db_execute_assoc($fquery) or safe_die("$fquery<br />".htmlspecialchars($connect->ErrorMsg()));
while($frow=$fresult->FetchRow())
{
$postans=$frow['answer'];
$conditions[]=$frow['answer'];
} // while
}
break;
case "L":
case "!":
case "O":
case "R":
$ansquery="SELECT answer FROM ".db_table_name("answers")." WHERE qid='{$conrow['cqid']}' AND code='{$conrow['value']}' AND language='{$surveyprintlang}'";
$ansresult=db_execute_assoc($ansquery);
while ($ansrow=$ansresult->FetchRow())
{
$conditions[]=$ansrow['answer'];
}
$conditions = array_unique($conditions);
break;
case "M":
case "P":
$ansquery="SELECT question FROM ".db_table_name("questions")." WHERE parent_qid='{$conrow['cqid']}' AND title='{$conrow['value']}' AND language='{$surveyprintlang}'";
$ansresult=db_execute_assoc($ansquery);
while ($ansrow=$ansresult->FetchRow())
{
$conditions[]=$ansrow['question'];
}
$conditions = array_unique($conditions);
break;
case "F":
case "H":
default:
$value=substr($conrow['cfieldname'], strpos($conrow['cfieldname'], "X".$conrow['cqid'])+strlen("X".$conrow['cqid']), strlen($conrow['cfieldname']));
$fquery = "SELECT * FROM ".db_table_name("answers")."\n"
. "WHERE qid='{$conrow['cqid']}'\n"
. "AND code='{$conrow['value']}' AND language='{$surveyprintlang}'";
$fresult=db_execute_assoc($fquery) or safe_die("$fquery<br />".htmlspecialchars($connect->ErrorMsg()));
while($frow=$fresult->FetchRow())
{
$postans=$frow['answer'];
$conditions[]=$frow['answer'];
} // while
break;
} // switch
// Now let's complete the answer text with the answer_section
$answer_section="";
switch($conrow['type'])
{
case "A":
case "B":
case "C":
case "E":
case "F":
case "H":
case "K":
$thiscquestion=$fieldmap[$conrow['cfieldname']];
$ansquery="SELECT question FROM ".db_table_name("questions")." WHERE parent_qid='{$conrow['cqid']}' AND title='{$thiscquestion['aid']}' AND language='{$surveyprintlang}'";
//$ansquery="SELECT question FROM ".db_table_name("questions")." WHERE qid='{$conrow['cqid']}' AND language='{$surveyprintlang}'";
$ansresult=db_execute_assoc($ansquery);
while ($ansrow=$ansresult->FetchRow())
{
$answer_section=" (".$ansrow['question'].")";
}
break;
case "1": // dual: (Label 1), (Label 2)
$labelIndex=substr($conrow['cfieldname'],-1);
$thiscquestion=$fieldmap[$conrow['cfieldname']];
$ansquery="SELECT question FROM ".db_table_name("questions")." WHERE parent_qid='{$conrow['cqid']}' AND title='{$thiscquestion['aid']}' AND language='{$surveyprintlang}'";
//$ansquery="SELECT question FROM ".db_table_name("questions")." WHERE qid='{$conrow['cqid']}' AND language='{$surveyprintlang}'";
$ansresult=db_execute_assoc($ansquery);
$cqidattributes = getQuestionAttributes($conrow['cqid'], $conrow['type']);
if ($labelIndex == 0)
{
if (trim($cqidattributes['dualscale_headerA']) != '') {
$header = $clang->gT($cqidattributes['dualscale_headerA']);
} else {
$header = '1';
}
}
elseif ($labelIndex == 1)
{
if (trim($cqidattributes['dualscale_headerB']) != '') {
$header = $clang->gT($cqidattributes['dualscale_headerB']);
} else {
$header = '2';
}
}
while ($ansrow=$ansresult->FetchRow())
{
$answer_section=" (".$ansrow['question']." ".sprintf($clang->gT("Label %s"),$header).")";
}
break;
case ":":
case ";": //multi flexi: ( answer [label] )
$thiscquestion=$fieldmap[$conrow['cfieldname']];
$ansquery="SELECT question FROM ".db_table_name("questions")." WHERE parent_qid='{$conrow['cqid']}' AND title='{$thiscquestion['aid']}' AND language='{$surveyprintlang}'";
$ansresult=db_execute_assoc($ansquery);
while ($ansrow=$ansresult->FetchRow())
{
$fquery = "SELECT * FROM ".db_table_name("answers")."\n"
. "WHERE qid='{$conrow['cqid']}'\n"
. "AND code='{$conrow['value']}' AND language='{$surveyprintlang}'";
$fresult=db_execute_assoc($fquery) or safe_die("$fquery<br />".htmlspecialchars($connect->ErrorMsg()));
while($frow=$fresult->FetchRow())
{
//$conditions[]=$frow['title'];
$answer_section=" (".$ansrow['question']."[".$frow['answer']."])";
} // while
}
break;
case "R": // (Rank 1), (Rank 2)... TIBO
$thiscquestion=$fieldmap[$conrow['cfieldname']];
$rankid=$thiscquestion['aid'];
$answer_section=" (".$clang->gT("RANK")." $rankid)";
break;
default: // nothing to add
break;
}
}
if (count($conditions) > 1)
{
$explanation .= "'".implode("' ".$clang->gT("or")." '", $conditions)."'";
}
elseif (count($conditions) == 1)
{
$explanation .= "'".$conditions[0]."'";
}
unset($conditions);
// Following line commented out because answer_section was lost, but is required for some question types
//$explanation .= " ".$clang->gT("to question")." '".$mapquestionsNumbers[$distinctrow['cqid']]."' $answer_section ";
if($distinctrow['cqid']){
$explanation .= " ".$clang->gT("at question")." '".$mapquestionsNumbers[$distinctrow['cqid']]." [".$subresult['title']."]' (".strip_tags($subresult['question'])."$answer_section)" ;
}
else{
$explanation .= " ".$distinctrow['value'] ;
}
//$distinctrow
$x++;
}
$s++;
}
if ($explanation)
{
$explanation = "<b>".$clang->gT('Only answer this question if the following conditions are met:')."</b>"
."<br/> ° ".$explanation;//"[".sprintf($clang->gT("Only answer this question %s"), $explanation)."]";
}
else
{
$explanation = '';
}
++$total_questions;
//TIBO map question qid to their q number
$mapquestionsNumbers[$deqrow['qid']]=$total_questions;
//END OF GETTING CONDITIONS
$qid = $deqrow['qid'];
$fieldname = "$surveyid"."X"."$gid"."X"."$qid";
if(isset($showsgqacode) && $showsgqacode == true)
{
$deqrow['question'] = $deqrow['question']."<br />".$clang->gT("ID:")." $fieldname <br />".
$clang->gT("Question code:")." ".$deqrow['title'];
}
$question = array(
'QUESTION_NUMBER' => $total_questions // content of the question code field
,'QUESTION_CODE' => $deqrow['title']
,'QUESTION_TEXT' => preg_replace('/(?:<br ?\/?>|<\/(?:p|h[1-6])>)$/is' , '' , $deqrow['question']) // content of the question field
,'QUESTION_SCENARIO' => $explanation // if there are conditions on a question, list the conditions.
,'QUESTION_MANDATORY' => '' // translated 'mandatory' identifier
,'QUESTION_ID' => $deqrow['qid'] // id to be added to wrapping question div
,'QUESTION_CLASS' => question_class( $deqrow['type']) // classes to be added to wrapping question div
,'QUESTION_TYPE_HELP' => '' // instructions on how to complete the question
,'QUESTION_MAN_MESSAGE' => '' // (not sure if this is used) mandatory error
,'QUESTION_VALID_MESSAGE' => '' // (not sure if this is used) validation error
,'QUESTION_FILE_VALID_MESSAGE' => ''// (not sure if this is used) file validation error
,'QUESTIONHELP' => '' // content of the question help field.
,'ANSWER' => '' // contains formatted HTML answer
);
if ($deqrow['mandatory'] == 'Y')
{
$question['QUESTION_MANDATORY'] = $clang->gT('*');
$question['QUESTION_CLASS'] .= ' mandatory';
$pdfoutput .= $clang->gT("*");
}
$pdfoutput ='';
//DIFFERENT TYPES OF DATA FIELD HERE
if(isset($_POST['printableexport'])){$pdf->intopdf($deqrow['title']." ".$deqrow['question']);}
if ($deqrow['help'])
{
$hh = $deqrow['help'];
$question['QUESTIONHELP'] = $hh;
if(isset($_POST['printableexport'])){$pdf->helptextintopdf($hh);}
}
if ($qidattributes['page_break']!=0)
{
$question['QUESTION_CLASS'] .=' breakbefore ';
}
if (isset($qidattributes['maximum_chars']) && $qidattributes['maximum_chars']!='') {
$question['QUESTION_CLASS'] ="max-chars-{$qidattributes['maximum_chars']} ".$question['QUESTION_CLASS'];
}
switch($deqrow['type'])
{
// ==================================================================
case "5": //5 POINT CHOICE
$question['QUESTION_TYPE_HELP'] = $clang->gT('Please choose *only one* of the following:');
if(isset($_POST['printableexport'])){$pdf->intopdf($clang->gT("Please choose *only one* of the following:"),"U");}
$pdfoutput ='';
$question['ANSWER'] .= "\n\t<ul>\n";
for ($i=1; $i<=5; $i++)
{
$pdfoutput .=" o ".$i." ";
// $printablesurveyoutput .="\t\t\t<input type='checkbox' name='$fieldname' value='$i' readonly='readonly' />$i \n";
$question['ANSWER'] .="\t\t<li>\n\t\t\t".input_type_image('radio',$i)."\n\t\t\t$i\n\t\t</li>\n";
}
if(isset($_POST['printableexport'])){$pdf->intopdf($pdfoutput);}
$question['ANSWER'] .="\t</ul>\n";
break;
// ==================================================================
case "D": //DATE
$question['QUESTION_TYPE_HELP'] = $clang->gT('Please enter a date:');
$question['ANSWER'] .= "\t".input_type_image('text',$question['QUESTION_TYPE_HELP'],30,1);
if(isset($_POST['printableexport'])){$pdf->intopdf($clang->gT("Please enter a date:")." ___________");}
break;
// ==================================================================
case "G": //GENDER
$question['QUESTION_TYPE_HELP'] = $clang->gT("Please choose *only one* of the following:");
$question['ANSWER'] .= "\n\t<ul>\n";
$question['ANSWER'] .= "\t\t<li>\n\t\t\t".input_type_image('radio',$clang->gT("Female"))."\n\t\t\t".$clang->gT("Female")."\n\t\t</li>\n";
$question['ANSWER'] .= "\t\t<li>\n\t\t\t".input_type_image('radio',$clang->gT("Male"))."\n\t\t\t".$clang->gT("Male")."\n\t\t</li>\n";
$question['ANSWER'] .= "\t</ul>\n";
if(isset($_POST['printableexport'])){$pdf->intopdf($clang->gT("Please choose *only one* of the following:"));}
if(isset($_POST['printableexport'])){$pdf->intopdf(" o ".$clang->gT("Female")." | o ".$clang->gT("Male"));}
break;
// ==================================================================
case "L": //LIST drop-down/radio-button list
// ==================================================================
case "!": //List - dropdown
if (isset($qidattributes['display_columns']) && trim($qidattributes['display_columns'])!='')
{
$dcols=$qidattributes['display_columns'];
}
else
{
$dcols=0;
}
if (isset($qidattributes['category_separator']) && trim($qidattributes['category_separator'])!='') {
$optCategorySeparator = $qidattributes['category_separator'];
}
else
{
unset($optCategorySeparator);
}
$question['QUESTION_TYPE_HELP'] = $clang->gT("Please choose *only one* of the following:");
$question['QUESTION_TYPE_HELP'] .= array_filter_help($qidattributes, $surveyprintlang, $surveyid);
if(isset($_POST['printableexport'])){$pdf->intopdf($clang->gT("Please choose *only one* of the following:"));}
$deaquery = "SELECT * FROM ".db_table_name("answers")." WHERE qid={$deqrow['qid']} AND language='{$surveyprintlang}' ORDER BY sortorder, answer";
$dearesult = db_execute_assoc($deaquery);
$deacount=$dearesult->RecordCount();
if ($deqrow['other'] == "Y") {$deacount++;}
$wrapper = setup_columns(0, $deacount);
$question['ANSWER'] = $wrapper['whole-start'];
$rowcounter = 0;
$colcounter = 1;
while ($dearow = $dearesult->FetchRow())
{
if (isset($optCategorySeparator))
{
list ($category, $answer) = explode($optCategorySeparator,$dearow['answer']);
if ($category != '')
{
$dearow['answer'] = "($category) $answer";
}
else
{
$dearow['answer'] = $answer;
}
}
$question['ANSWER'] .= "\t".$wrapper['item-start']."\t\t".input_type_image('radio' , $dearow['answer'])."\n\t\t\t".$dearow['answer']."\n".$wrapper['item-end'];
if(isset($_POST['printableexport'])){$pdf->intopdf(" o ".$dearow['answer']);}
++$rowcounter;
if ($rowcounter == $wrapper['maxrows'] && $colcounter < $wrapper['cols'])
{
if($colcounter == $wrapper['cols'] - 1)
{
$question['ANSWER'] .= $wrapper['col-devide-last'];
}
else
{
$question['ANSWER'] .= $wrapper['col-devide'];
}
$rowcounter = 0;
++$colcounter;
}
}
if ($deqrow['other'] == 'Y')
{
if(trim($qidattributes["other_replace_text"])=='')
{$qidattributes["other_replace_text"]="Other";}
// $printablesurveyoutput .="\t".$wrapper['item-start']."\t\t".input_type_image('radio' , $clang->gT("Other"))."\n\t\t\t".$clang->gT("Other")."\n\t\t\t<input type='text' size='30' readonly='readonly' />\n".$wrapper['item-end'];
$question['ANSWER'] .= $wrapper['item-start-other'].input_type_image('radio',$clang->gT($qidattributes["other_replace_text"])).' '.$clang->gT($qidattributes["other_replace_text"])."\n\t\t\t".input_type_image('other')."\n".$wrapper['item-end'];
if(isset($_POST['printableexport'])){$pdf->intopdf(" o ".$clang->gT($qidattributes["other_replace_text"]).": ________");}
}
$question['ANSWER'] .= $wrapper['whole-end'];
//Let's break the presentation into columns.
break;
// ==================================================================
case "O": //LIST WITH COMMENT
$question['QUESTION_TYPE_HELP'] = $clang->gT("Please choose *only one* of the following:");
if(isset($_POST['printableexport'])){$pdf->intopdf($clang->gT("Please choose *only one* of the following:"),"U");}
$deaquery = "SELECT * FROM ".db_table_name("answers")." WHERE qid={$deqrow['qid']} AND language='{$surveyprintlang}' ORDER BY sortorder, answer ";
$dearesult = db_execute_assoc($deaquery);
$question['ANSWER'] = "\t<ul>\n";
while ($dearow = $dearesult->FetchRow())
{
$question['ANSWER'] .= "\t\t<li>\n\t\t\t".input_type_image('radio',$dearow['answer'])."\n\t\t\t".$dearow['answer']."\n\t\t</li>\n";
if(isset($_POST['printableexport'])){$pdf->intopdf($dearow['answer']);}
}
$question['ANSWER'] .= "\t</ul>\n";
$question['ANSWER'] .= "\t<p class=\"comment\">\n\t\t".$clang->gT("Make a comment on your choice here:")."\n";
if(isset($_POST['printableexport'])){$pdf->intopdf("Make a comment on your choice here:");}
$question['ANSWER'] .= "\t\t".input_type_image('textarea',$clang->gT("Make a comment on your choice here:"),50,8)."\n\t</p>\n";
for($i=0;$i<9;$i++)
{
if(isset($_POST['printableexport'])){$pdf->intopdf("____________________");}
}
break;
// ==================================================================
case "R": //RANKING Type Question
$reaquery = "SELECT * FROM ".db_table_name("answers")." WHERE qid={$deqrow['qid']} AND language='{$surveyprintlang}' ORDER BY sortorder, answer";
$rearesult = db_execute_assoc($reaquery) or safe_die ("Couldn't get ranked answers<br />".$connect->ErrorMsg());
$reacount = $rearesult->RecordCount();
$question['QUESTION_TYPE_HELP'] = $clang->gT("Please number each box in order of preference from 1 to")." $reacount";
if(isset($_POST['printableexport'])){$pdf->intopdf($clang->gT("Please number each box in order of preference from 1 to ").$reacount,"U");}
$question['ANSWER'] = "\n<ul>\n";
while ($rearow = $rearesult->FetchRow())
{
$question['ANSWER'] .="\t<li>\n\t".input_type_image('rank','',4,1)."\n\t\t ".$rearow['answer'].addsgqacode(" (".$fieldname.$rearow['code'].")")."\n\t</li>\n";
if(isset($_POST['printableexport'])){$pdf->intopdf("__ ".$rearow['answer']);}
}
$question['ANSWER'] .= "\n</ul>\n";
break;
// ==================================================================
case "M": //Multiple choice (Quite tricky really!)
if (trim($qidattributes['display_columns'])!='')
{
$dcols=$qidattributes['display_columns'];
}
else
{
$dcols=0;
}
if (trim($qidattributes['max_answers'])=='') {
$question['QUESTION_TYPE_HELP'] = $clang->gT("Please choose *all* that apply:");
if(isset($_POST['printableexport'])){$pdf->intopdf($clang->gT("Please choose *all* that apply:"),"U");}
}