forked from LimeSurvey/LimeSurvey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.php
1058 lines (917 loc) · 40.6 KB
/
group.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: group.php 12432 2012-02-10 15:24:45Z tmswhite $
*/
if (!isset($homedir) || isset($_REQUEST['$homedir'])) {die("Cannot run this script directly");}
require_once("save.php"); // for supporting functions only
// $LEMdebugLevel - customizable debugging for Lime Expression Manager
$LEMdebugLevel=0; // LEM_DEBUG_TIMING; // (LEM_DEBUG_TIMING + LEM_DEBUG_VALIDATION_SUMMARY + LEM_DEBUG_VALIDATION_DETAIL);
$LEMskipReprocessing=false; // true if used GetLastMoveResult to avoid generation of unneeded extra JavaScript
switch ($thissurvey['format'])
{
case "A": //All in one
$surveyMode='survey';
break;
default:
case "S": //One at a time
$surveyMode='question';
break;
case "G": //Group at a time
$surveyMode='group';
break;
}
$radix=getRadixPointData($thissurvey['surveyls_numberformat']);
$radix = $radix['seperator'];
global $deletenonvalues; // set in config-defaults.php
$surveyOptions = array(
'active'=>($thissurvey['active']=='Y'),
'allowsave'=>($thissurvey['allowsave']=='Y'),
'anonymized'=>($thissurvey['anonymized']!='N'),
'assessments'=>($thissurvey['assessments']=='Y'),
'datestamp'=>($thissurvey['datestamp']=='Y'),
'deletenonvalues'=>(isset($deletenonvalues)? $deletenonvalues: 1),
'hyperlinkSyntaxHighlighting'=>(($LEMdebugLevel & LEM_DEBUG_VALIDATION_SUMMARY) == LEM_DEBUG_VALIDATION_SUMMARY), // TODO set this to true if in admin mode but not if running a survey
'ipaddr'=>($thissurvey['ipaddr']=='Y'),
'radix'=>$radix,
'refurl'=>(($thissurvey['refurl'] == "Y") ? $_SESSION['refurl'] : NULL),
'rooturl'=>(isset($rooturl) ? $rooturl : ''),
'savetimings'=>($thissurvey['savetimings'] == "Y"),
'surveyls_dateformat'=>(isset($thissurvey['surveyls_dateformat']) ? $thissurvey['surveyls_dateformat'] : 1),
'startlanguage'=>(isset($clang->langcode) ? $clang->langcode : $thissurvey['language']),
'target'=>(isset($uploaddir) ? "{$uploaddir}/surveys/{$thissurvey['sid']}/files/" : "/temp/{$thissurvey['sid']}/files"),
'tempdir'=>(isset($tempdir) ? $tempdir : '/temp/'),
'timeadjust'=>(isset($timeadjust) ? $timeadjust : 0),
'token'=>(isset($clienttoken) ? $clienttoken : NULL),
);
//Security Checked: POST, GET, SESSION, REQUEST, returnglobal, DB
$previewgrp = false;
if ( $surveyMode=='group' && isset($_REQUEST['action']) && ($_REQUEST['action']=='previewgroup')){
$previewgrp = true;
}
if (isset($_REQUEST['newtest']))
if ($_REQUEST['newtest']=="Y")
setcookie("limesurvey_timers", "0");
$show_empty_group = false;
if ($previewgrp)
{
$_SESSION['prevstep'] = 1;
$_SESSION['maxstep'] = 0;
}
else
{
//RUN THIS IF THIS IS THE FIRST TIME , OR THE FIRST PAGE ########################################
if (!isset($_SESSION['step'])) // || !$_SESSION['step']) - don't do this for step0, else rebuild the session
{
$totalquestions = buildsurveysession();
LimeExpressionManager::StartSurvey($thissurvey['sid'], $surveyMode, $surveyOptions, false,$LEMdebugLevel);
$_SESSION['step'] = 0;
if ($surveyMode == 'survey') {
$move = "movenext"; // to force a call to NavigateForwards()
}
else if (isset($thissurvey['showwelcome']) && $thissurvey['showwelcome'] == 'N') {
//If explicitply set, hide the welcome screen
$_SESSION['step'] = 0;
$move = "movenext";
}
}
if (!isset($_SESSION['totalsteps'])) {$_SESSION['totalsteps']=0;}
if (!isset($_SESSION['maxstep'])) {$_SESSION['maxstep']=0;}
if (isset($_SESSION['LEMpostKey']) && isset($_POST['LEMpostKey']) && $_POST['LEMpostKey'] != $_SESSION['LEMpostKey'])
{
// then trying to resubmit (e.g. Next, Previous, Submit) from a cached copy of the page
// Does not try to save anything from the page to the database
$moveResult = LimeExpressionManager::GetLastMoveResult(true);
if (isset($_POST['thisstep']) && isset($moveResult['seq']) && $_POST['thisstep'] == $moveResult['seq'])
{
// then pressing F5 or otherwise refreshing the current page, which is OK
$LEMskipReprocessing=true;
$move = "movenext"; // so will re-display the survey
}
else
{
// trying to use browser back buttons, which may be disallowed if no 'previous' button is present
$LEMskipReprocessing=true;
$move = "movenext"; // so will re-display the survey
$invalidLastPage=true;
$vpopup="<script type=\"text/javascript\">\n
<!--\n $(document).ready(function(){
alert(\"".$clang->gT("Please use the LimeSurvey navigation buttons or index. It appears you attempted to use the browser back button to re-submit a page.", "js")."\");});\n //-->\n
</script>\n";
}
}
if (!(isset($_POST['saveall']) || isset($_POST['saveprompt']) || isset($_POST['loadall']) || isset($_GET['sid']) || $LEMskipReprocessing || (isset($move) && (preg_match('/^changelang_/',$move)))))
{
$_SESSION['prevstep']=$_SESSION['step'];
}
if (!isset($_SESSION['prevstep']))
{
$_SESSION['prevstep']=-1; // this only happens on re-load
}
if (isset($_SESSION['LEMtokenResume']))
{
LimeExpressionManager::StartSurvey($thissurvey['sid'], $surveyMode, $surveyOptions, false,$LEMdebugLevel);
$moveResult = LimeExpressionManager::JumpTo($_SESSION['step']+1,false,false); // if late in the survey, will re-validate contents, which may be overkill
unset($_SESSION['LEMtokenResume']);
unset($_SESSION['LEMreload']);
}
else if (!$LEMskipReprocessing)
{
//Move current step ###########################################################################
if (isset($move) && $move == 'moveprev' && ($thissurvey['allowprev']=='Y' || $thissurvey['allowjumps']=='Y'))
{
$moveResult = LimeExpressionManager::NavigateBackwards();
if ($moveResult['at_start']) {
$_SESSION['step']=0;
unset($moveResult); // so display welcome page again
}
}
if (isset($move) && $move == "movenext")
{
if (isset($_SESSION['LEMreload']))
{
LimeExpressionManager::StartSurvey($thissurvey['sid'], $surveyMode, $surveyOptions, false,$LEMdebugLevel);
$moveResult = LimeExpressionManager::JumpTo($_SESSION['step'],false,false); // if late in the survey, will re-validate contents, which may be overkill
unset($_SESSION['LEMreload']);
}
else {
$moveResult = LimeExpressionManager::NavigateForwards();
}
}
if (isset($move) && ($move == 'movesubmit'))
{
if ($surveyMode == 'survey')
{
$moveResult = LimeExpressionManager::NavigateForwards();
}
else
{
// may be submitting from the navigation bar, in which case need to process all intervening questions
// in order to update equations and ensure there are no intervening relevant mandatory or relevant invalid questions
$moveResult = LimeExpressionManager::JumpTo($_SESSION['totalsteps']+1,false);
}
}
if (isset($move) && (preg_match('/^changelang_/',$move))) {
// jump to current step using new language, processing POST values
$moveResult = LimeExpressionManager::JumpTo($_SESSION['step'],false,true,false,true); // do process the POST data
}
if (isset($move) && bIsNumericInt($move) && $thissurvey['allowjumps']=='Y')
{
$move = (int)$move;
if ($move > 0 && (($move <= $_SESSION['step']) || (isset($_SESSION['maxstep']) && $move <= $_SESSION['maxstep']))) {
$moveResult = LimeExpressionManager::JumpTo($move,false);
}
}
if (!isset($moveResult) && !($surveyMode != 'survey' && $_SESSION['step'] == 0)) {
// Just in case not set via any other means, but don't do this if it is the welcome page
$moveResult = LimeExpressionManager::GetLastMoveResult(true);
$LEMskipReprocessing=true;
}
}
if (isset($moveResult)) {
if ($moveResult['finished']==true) {
$move = 'movesubmit';
}
else
{
$_SESSION['step']= $moveResult['seq']+1; // step is index base 1
$stepInfo = LimeExpressionManager::GetStepIndexInfo($moveResult['seq']);
}
if ($move == "movesubmit" && $moveResult['finished'] == false) {
// then there are errors, so don't finalize the survey
$move = "movenext"; // so will re-display the survey
$invalidLastPage=true;
}
}
// We do not keep the participant session anymore when the same browser is used to answer a second time a survey (let's think of a library PC for instance).
// Previously we used to keep the session and redirect the user to the
// submit page.
if ($surveyMode != 'survey' && $_SESSION['step'] == 0) {
display_first_page();
exit;
}
//CHECK IF ALL MANDATORY QUESTIONS HAVE BEEN ANSWERED ############################################
//First, see if we are moving backwards or doing a Save so far, and its OK not to check:
if (
(isset($move) && ($move == "moveprev" || (is_int($move) && $_SESSION['prevstep'] == $_SESSION['maxstep']) || $_SESSION['prevstep'] == $_SESSION['step'])) ||
(isset($_POST['saveall']) && $_POST['saveall'] == $clang->gT("Save your responses so far")))
{
if ($allowmandbackwards==1) {
$backok="Y";
}
else
{
$backok="N";
}
}
else
{
$backok="N"; // NA, since not moving backwards
}
if ($thissurvey['active'] == "Y" && isset($_POST['saveall']))
{
// must do this here to process the POSTed values
//$moveResult = LimeExpressionManager::JumpTo($_SESSION['step'],false); // by jumping to current step, saves data so far
//showsaveform(); // generates a form and exits, awaiting input
if(!tableExists('tokens_'.$surveyid) || $thissurvey['anonymized']=='Y' || (tableExists('tokens_'.$surveyid) && $thissurvey['tokenanswerspersistence'] == 'N' ))
{
$moveResult = LimeExpressionManager::JumpTo($_SESSION['step'],false,true,false,false,true); // by jumping to current step, saves data so far
showsaveform();
}
else
{
$moveResult = LimeExpressionManager::JumpTo($_SESSION['step'],false,true,false,false,true); // by jumping to current step, saves data so far
}
}
if ($thissurvey['active'] == "Y" && isset($_POST['saveprompt']))
{
// The response from the save form
// CREATE SAVED CONTROL RECORD USING SAVE FORM INFORMATION
$flashmessage = savedcontrol();
if (isset($errormsg) && $errormsg != "")
{
showsaveform(); // reshow the form if there is an error
}
$moveResult = LimeExpressionManager::GetLastMoveResult(true);
$LEMskipReprocessing=true;
// TODO - does this work automatically for token answer persistence? Used to be savedsilent()
}
//Now, we check mandatory questions if necessary
//CHECK IF ALL CONDITIONAL MANDATORY QUESTIONS THAT APPLY HAVE BEEN ANSWERED
global $notanswered;
if (isset($moveResult) && !$moveResult['finished'])
{
$unansweredSQList = $moveResult['unansweredSQs'];
if (strlen($unansweredSQList) > 0 && $backok != "N") {
$notanswered = explode('|',$unansweredSQList);
}
else {
$notanswered = array();
}
//CHECK INPUT
$invalidSQList = $moveResult['invalidSQs'];
if (strlen($invalidSQList) > 0 && $backok != "N") {
$notvalidated = explode('|',$invalidSQList);
}
else {
$notvalidated = array();
}
}
// CHECK UPLOADED FILES
// TMSW - Move this into LEM::NavigateForwards?
$filenotvalidated = checkUploadedFileValidity($move, $backok);
//SEE IF THIS GROUP SHOULD DISPLAY
$show_empty_group = false;
if ($_SESSION['step']==0)
$show_empty_group = true;
//SUBMIT ###############################################################################
if ((isset($move) && $move == "movesubmit"))
{
setcookie ("limesurvey_timers", "", time() - 3600);// remove the timers cookies
if ($thissurvey['refurl'] == "Y")
{
if (!in_array("refurl", $_SESSION['insertarray'])) //Only add this if it doesn't already exist
{
$_SESSION['insertarray'][] = "refurl";
}
}
//COMMIT CHANGES TO DATABASE
if ($thissurvey['active'] != "Y") //If survey is not active, don't really commit
{
if ($thissurvey['assessments']== "Y")
{
$assessments = doAssessment($surveyid);
}
sendcacheheaders();
doHeader();
echo templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
//Check for assessments
if ($thissurvey['assessments']== "Y" && $assessments)
{
echo templatereplace(file_get_contents("$thistpl/assessment.pstpl"));
}
// fetch all filenames from $_SESSIONS['files'] and delete them all
// from the /upload/tmp/ directory
/*echo "<pre>";print_r($_SESSION);echo "</pre>";
for($i = 1; isset($_SESSION['files'][$i]); $i++)
{
unlink('upload/tmp/'.$_SESSION['files'][$i]['filename']);
}
*/
// can't kill session before end message, otherwise INSERTANS doesn't work.
$completed = templatereplace($thissurvey['surveyls_endtext']);
$completed .= "<br /><strong><font size='2' color='red'>".$clang->gT("Did Not Save")."</font></strong><br /><br />\n\n";
$completed .= $clang->gT("Your survey responses have not been recorded. This survey is not yet active.")."<br /><br />\n";
if ($thissurvey['printanswers'] == 'Y')
{
// ClearAll link is only relevant for survey with printanswers enabled
// in other cases the session is cleared at submit time
$completed .= "<a href='{$publicurl}/index.php?sid=$surveyid&move=clearall'>".$clang->gT("Clear Responses")."</a><br /><br />\n";
}
if($thissurvey['printanswers'] != 'Y')
{
killSession();
}
}
else //THE FOLLOWING DEALS WITH SUBMITTING ANSWERS AND COMPLETING AN ACTIVE SURVEY
{
if ($thissurvey['usecookie'] == "Y" && $tokensexist != 1) //don't use cookies if tokens are being used
{
$cookiename="PHPSID".returnglobal('sid')."STATUS";
setcookie("$cookiename", "COMPLETE", time() + 31536000); //Cookie will expire in 365 days
}
//Before doing the "templatereplace()" function, check the $thissurvey['url']
//field for limereplace stuff, and do transformations!
$thissurvey['surveyls_url']=passthruReplace($thissurvey['surveyls_url'], $thissurvey);
$thissurvey['surveyls_url']=templatereplace($thissurvey['surveyls_url']); // to do INSERTANS substitutions
$content='';
$content .= templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
//Check for assessments
if ($thissurvey['assessments']== "Y")
{
$assessments = doAssessment($surveyid);
if ($assessments)
{
$content .= templatereplace(file_get_contents("$thistpl/assessment.pstpl"));
}
}
//Update the token if needed and send a confirmation email
if (isset($clienttoken) && $clienttoken)
{
submittokens();
}
//Send notifications
SendSubmitNotifications();
$content='';
$content .= templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
//Check for assessments
if ($thissurvey['assessments']== "Y")
{
$assessments = doAssessment($surveyid);
if ($assessments)
{
$content .= templatereplace(file_get_contents("$thistpl/assessment.pstpl"));
}
}
if (trim(strip_tags($thissurvey['surveyls_endtext']))=='')
{
$completed = "<br /><span class='success'>".$clang->gT("Thank you!")."</span><br /><br />\n\n"
. $clang->gT("Your survey responses have been recorded.")."<br /><br />\n";
}
else
{
$completed = templatereplace($thissurvey['surveyls_endtext']);
}
// Link to Print Answer Preview **********
if ($thissurvey['printanswers']=='Y')
{
$completed .= "<br /><br />"
."<a class='printlink' href='printanswers.php?sid=$surveyid' target='_blank'>"
.$clang->gT("Print your answers.")
."</a><br />\n";
}
//*****************************************
if ($thissurvey['publicstatistics']=='Y' && $thissurvey['printanswers']=='Y') {$completed .='<br />'.$clang->gT("or");}
// Link to Public statistics **********
if ($thissurvey['publicstatistics']=='Y')
{
$completed .= "<br /><br />"
."<a class='publicstatisticslink' href='statistics_user.php?sid=$surveyid&lang=".$_SESSION['s_lang']."' target='_blank'>"
.$clang->gT("View the statistics for this survey.")
."</a><br />\n";
}
//*****************************************
$_SESSION['finished']=true;
$_SESSION['sid']=$surveyid;
sendcacheheaders();
if (isset($thissurvey['autoredirect']) && $thissurvey['autoredirect'] == "Y" && $thissurvey['surveyls_url'])
{
//Automatically redirect the page to the "url" setting for the survey
$url = templatereplace($thissurvey['surveyls_url']); // TODO - check safety of this - provides access to any replacement value
$url = passthruReplace($url, $thissurvey);
$url = str_replace("{SAVEDID}",$saved_id, $url); // to activate the SAVEDID in the END URL
$url = str_replace("{TOKEN}",$clienttoken, $url); // to activate the TOKEN in the END URL
$url = str_replace("{SID}", $surveyid, $url); // to activate the SID in the END URL
$url = str_replace("{LANG}", $clang->getlangcode(), $url); // to activate the LANG in the END URL
header("Location: {$url}");
}
if($thissurvey['printanswers'] != 'Y')
{
killSession();
}
doHeader();
echo $content;
}
echo templatereplace(file_get_contents("$thistpl/completed.pstpl"));
echo "\n<br />\n";
if ((($LEMdebugLevel & LEM_DEBUG_TIMING) == LEM_DEBUG_TIMING)) {
echo LimeExpressionManager::GetDebugTimingMessage();
}
if ((($LEMdebugLevel & LEM_DEBUG_VALIDATION_SUMMARY) == LEM_DEBUG_VALIDATION_SUMMARY)) {
echo "<table><tr><td align='left'><b>Group/Question Validation Results:</b>".$moveResult['message']."</td></tr></table>\n";
}
echo templatereplace(file_get_contents("$thistpl/endpage.pstpl"));
doFooter();
exit;
}
}
// IF GOT THIS FAR, THEN DISPLAY THE ACTIVE GROUP OF QUESTIONSs
//SEE IF $surveyid EXISTS ####################################################################
if ($surveyexists <1)
{
//SURVEY DOES NOT EXIST. POLITELY EXIT.
echo templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
echo "\t<center><br />\n";
echo "\t".$clang->gT("Sorry. There is no matching survey.")."<br /></center> \n";
echo templatereplace(file_get_contents("$thistpl/endpage.pstpl"));
doFooter();
exit;
}
//GET GROUP DETAILS
if ($surveyMode == 'group' && $previewgrp)
{
setcookie("limesurvey_timers", "0");
$_gid = sanitize_int($_REQUEST['gid']);
LimeExpressionManager::StartSurvey($thissurvey['sid'], 'group', $surveyOptions, false,$LEMdebugLevel);
$gseq = LimeExpressionManager::GetGroupSeq($_gid);
if ($gseq == -1) {
echo $clang->gT('Invalid group number for this survey: ') . $_gid;
exit;
}
$moveResult = LimeExpressionManager::JumpTo($gseq+1,true);
if (is_null($moveResult)) {
echo $clang->gT('This group contains no questions. You must add questions to this group before you can preview it');
exit;
}
if (isset($moveResult)) {
$_SESSION['step']= $moveResult['seq']+1; // step is index base 1?
}
$stepInfo = LimeExpressionManager::GetStepIndexInfo($moveResult['seq']);
$gid = $stepInfo['gid'];
$groupname = $stepInfo['gname'];
$groupdescription = $stepInfo['gtext'];
}
else
{
if (($show_empty_group)||!isset($_SESSION['grouplist']))
{
$gid=-1; // Make sure the gid is unused. This will assure that the foreach (fieldarray as ia) has no effect.
$groupname=$clang->gT("Submit your answers");
$groupdescription=$clang->gT("There are no more questions. Please press the <Submit> button to finish this survey.");
}
else if ($surveyMode != 'survey')
{
$stepInfo = LimeExpressionManager::GetStepIndexInfo($moveResult['seq']);
$gid = $stepInfo['gid'];
$groupname = $stepInfo['gname'];
$groupdescription = $stepInfo['gtext'];
}
}
if ($_SESSION['step'] > $_SESSION['maxstep'])
{
$_SESSION['maxstep'] = $_SESSION['step'];
}
//******************************************************************************************************
//PRESENT SURVEY
//******************************************************************************************************
$okToShowErrors = (!$previewgrp && (isset($invalidLastPage) || $_SESSION['prevstep'] == $_SESSION['step']));
require_once("qanda.php");
//store id's of all the question in $idlist array.
$idlist = array();
//Iterate through the questions about to be displayed:
$inputnames=array();
if (isset($_SESSION['grouplist']))
foreach ($_SESSION['grouplist'] as $gl)
{
$gid = $gl[0];
$qnumber = 0;
if ($surveyMode != 'survey')
{
$onlyThisGID = $stepInfo['gid'];
if ($onlyThisGID != $gid)
{
continue;
}
}
// TMSW - could iterate through LEM::currentQset instead
foreach ($_SESSION['fieldarray'] as $key => $ia)
{
++$qnumber;
$ia[9] = $qnumber; // incremental question count;
if ((isset($ia[10]) && $ia[10] == $gid) || (!isset($ia[10]) && $ia[5] == $gid))
{
if ($surveyMode == 'question' && $ia[0] != $stepInfo['qid'])
{
continue;
}
$qidattributes = getQuestionAttributes($ia[0], $ia[4]);
if ($ia[4] != '*' && ($qidattributes === false || $qidattributes['hidden'] == 1))
{
continue;
}
//Get the answers/inputnames
// TMSW - can content of retrieveAnswers() be provided by LEM? Review scope of what it provides.
// TODO - retrieveAnswers is slow - queries database separately for each question. May be fixed in _CI or _YII ports, so ignore for now
list($plus_qanda, $plus_inputnames) = retrieveAnswers($ia);
//can eliminate extra space for these 2 arrays if $_SESSION['fieldmap'] is used directly!
$idlist[] = $ia[1];
if ($plus_qanda)
{
$plus_qanda[] = $ia[4];
$plus_qanda[] = $ia[6]; // adds madatory identifyer for adding mandatory class to question wrapping div
$qanda[] = $plus_qanda;
}
if ($plus_inputnames)
{
$inputnames = addtoarray_single($inputnames, $plus_inputnames);
}
//Display the "mandatory" popup if necessary
// TMSW - get question-level error messages - don't call **_popup() directly
if ($okToShowErrors && $stepInfo['mandViolation'])
{
list($mandatorypopup, $popup) = mandatory_popup($ia, $notanswered);
}
//Display the "validation" popup if necessary
if ($okToShowErrors && !$stepInfo['valid'])
{
list($validationpopup, $vpopup) = validation_popup($ia, $notvalidated);
}
// Display the "file validation" popup if necessary
if ($okToShowErrors && isset($filenotvalidated))
{
list($filevalidationpopup, $fpopup) = file_validation_popup($ia, $filenotvalidated);
}
}
if ($ia[4] == "|")
$upload_file = TRUE;
} //end iteration
}
if ($surveyMode != 'survey' && isset($thissurvey['showprogress']) && $thissurvey['showprogress'] == 'Y')
{
if ($show_empty_group)
{
$percentcomplete = makegraph($_SESSION['totalsteps']+1, $_SESSION['totalsteps']);
}
else
{
$percentcomplete = makegraph($_SESSION['step'], $_SESSION['totalsteps']);
}
}
if (!(isset($languagechanger) && strlen($languagechanger) > 0) && function_exists('makelanguagechanger')) {
$languagechanger = makelanguagechanger();
}
//READ TEMPLATES, INSERT DATA AND PRESENT PAGE
sendcacheheaders();
doHeader();
if (isset($popup)) {echo $popup;}
if (isset($vpopup)) {echo $vpopup;}
if (isset($fpopup)) {echo $fpopup;}
echo templatereplace(file_get_contents("$thistpl/startpage.pstpl"));
//ALTER PAGE CLASS TO PROVIDE WHOLE-PAGE ALTERNATION
if ($surveyMode != 'survey' && $_SESSION['step'] != $_SESSION['prevstep'] ||
(isset($_SESSION['stepno']) && $_SESSION['stepno'] % 2))
{
if (!isset($_SESSION['stepno'])) $_SESSION['stepno'] = 0;
if ($_SESSION['step'] != $_SESSION['prevstep']) ++$_SESSION['stepno'];
if ($_SESSION['stepno'] % 2)
{
echo "<script type=\"text/javascript\">\n"
. " $(\"body\").addClass(\"page-odd\");\n"
. "</script>\n";
}
}
$hiddenfieldnames=implode("|", $inputnames);
if (isset($upload_file) && $upload_file)
echo "<form enctype=\"multipart/form-data\" method='post' action='{$publicurl}/index.php' id='limesurvey' name='limesurvey' autocomplete='off'>
<!-- INPUT NAMES -->
<input type='hidden' name='fieldnames' value='{$hiddenfieldnames}' id='fieldnames' />\n";
else
echo "<form method='post' action='{$publicurl}/index.php' id='limesurvey' name='limesurvey' autocomplete='off'>
<!-- INPUT NAMES -->
<input type='hidden' name='fieldnames' value='{$hiddenfieldnames}' id='fieldnames' />\n";
echo sDefaultSubmitHandler();
if ($surveyMode == 'survey')
{
if(isset($thissurvey['showwelcome']) && $thissurvey['showwelcome'] == 'N') {
//Hide the welcome screen if explicitly set
} else {
echo templatereplace(file_get_contents("$thistpl/welcome.pstpl"))."\n";
}
if ($thissurvey['anonymized'] == "Y")
{
echo templatereplace(file_get_contents("$thistpl/privacy.pstpl"))."\n";
}
}
// <-- START THE SURVEY -->
if ($surveyMode != 'survey') {
echo templatereplace(file_get_contents("{$thistpl}/survey.pstpl"));
}
// the runonce element has been changed from a hidden to a text/display:none one
// in order to workaround an not-reproduced issue #4453 (lemeur)
echo "<input type='text' id='runonce' value='0' style='display: none;'/>
<!-- JAVASCRIPT FOR CONDITIONAL QUESTIONS -->
<script type='text/javascript'>
<!--\n";
echo "var LEMradix='" . $radix . "';\n";
print <<<END
function fixnum_checkconditions(value, name, type, evt_type)
{
newval = value;
if (LEMradix === ',') {
newval = new String(value);
newval = newval.split(',').join('.');
}
if (newval != parseFloat(newval)) {
newval = '';
if (name.match(/other$/)) {
$('#answer'+name+'text').val('');
}
$('#answer'+name).val('');
}
if (typeof evt_type === 'undefined')
{
evt_type = 'onchange';
}
checkconditions(newval, name, type, evt_type);
}
function checkconditions(value, name, type, evt_type)
{
if (typeof evt_type === 'undefined')
{
evt_type = 'onchange';
}
if (type == 'radio' || type == 'select-one')
{
var hiddenformname='java'+name;
document.getElementById(hiddenformname).value=value;
}
else if (type == 'checkbox')
{
if (document.getElementById('answer'+name).checked)
{
$('#java'+name).val('Y');
} else
{
$('#java'+name).val('');
}
}
else if (type == 'text' && name.match(/other$/) && typeof document.getElementById('java'+name) !== 'undefined' && document.getElementById('java'+name) != null)
{
$('#java'+name).val(value);
}
ExprMgr_process_relevance_and_tailoring(evt_type,name,type);
END;
if ($previewgrp)
{
// force the group to be visible, even if irrelevant - will not always work
print <<<END
$('#relevanceG' + LEMgseq).val(1);
$(document).ready(function() {
$('#group-' + LEMgseq).show();
});
$(document).change(function() {
$('#group-' + LEMgseq).show();
});
$(document).bind('keydown',function(e) {
if (e.keyCode == 9) {
$('#group-' + LEMgseq).show();
return true;
}
return true;
});
END;
}
print <<<END
}
// -->
</script>
END;
//Display the "mandatory" message on page if necessary
if (isset($showpopups) && $showpopups == 0 && $stepInfo['mandViolation'] && $okToShowErrors)
{
echo "<p><span class='errormandatory'>" . $clang->gT("One or more mandatory questions have not been answered. You cannot proceed until these have been completed.") . "</span></p>";
}
//Display the "validation" message on page if necessary
if (isset($showpopups) && $showpopups == 0 && !$stepInfo['valid'] && $okToShowErrors)
{
echo "<p><span class='errormandatory'>" . $clang->gT("One or more questions have not been answered in a valid manner. You cannot proceed until these answers are valid.") . "</span></p>";
}
//Display the "file validation" message on page if necessary
if (isset($showpopups) && $showpopups == 0 && isset($filenotvalidated) && $filenotvalidated == true && $okToShowErrors)
{
echo "<p><span class='errormandatory'>" . $clang->gT("One or more uploaded files are not in proper format/size. You cannot proceed until these files are valid.") . "</span></p>";
}
if (isset($_SESSION['grouplist']))
$_gseq = -1;
foreach ($_SESSION['grouplist'] as $gl)
{
$gid=$gl[0];
++$_gseq;
$groupname=$gl[1];
$groupdescription=$gl[2];
if ($surveyMode != 'survey' && $gid != $onlyThisGID) {
continue;
}
echo "\n\n<!-- START THE GROUP -->\n";
echo "\n\n<div id='group-$_gseq'";
$gnoshow = LimeExpressionManager::GroupIsIrrelevantOrHidden($_gseq);
if ($gnoshow && !$previewgrp)
{
echo " style='display: none;'";
}
echo ">\n";
echo templatereplace(file_get_contents("$thistpl/startgroup.pstpl"));
echo "\n";
if ($groupdescription)
{
echo templatereplace(file_get_contents("$thistpl/groupdescription.pstpl"));
}
echo "\n";
echo "\n\n<!-- PRESENT THE QUESTIONS -->\n";
$i=0;
foreach ($qanda as $qa) // one entry per QID
{
if ($gid != $qa[6]) {
continue;
}
$qid = $qa[4];
$qinfo = LimeExpressionManager::GetQuestionStatus($qid);
$lastgrouparray = explode("X",$qa[7]);
$lastgroup = $lastgrouparray[0]."X".$lastgrouparray[1]; // id of the last group, derived from question id
$lastanswer = $qa[7];
$q_class = question_class($qinfo['info']['type']);
$man_class = '';
if ($qinfo['info']['mandatory']=='Y') {
$man_class .= ' mandatory';
}
if ($qinfo['anyUnanswered'] && $_SESSION['maxstep'] != $_SESSION['step']) {
$man_class .= ' missing';
}
$n_q_display = '';
if ($qinfo['hidden'] && $qinfo['info']['type'] != '*') {
continue; // skip this one
}
if (!$qinfo['relevant'] || ($qinfo['hidden'] && $qinfo['info']['type'] == '*')) {
$n_q_display = ' style="display: none;"';
}
$question= $qa[0];
//===================================================================
// The following four variables offer the templating system the
// capacity to fully control the HTML output for questions making the
// above echo redundant if desired.
$question['essentials'] = 'id="question'.$qa[4].'"'.$n_q_display;
$question['class'] = $q_class;
$question['man_class'] = $man_class;
$question['code']=$qa[5];
$question['sgq']=$qa[7];
$question['aid']=$qinfo['info']['aid'];
$question['sqid']=$qinfo['info']['sqid'];
$question['type']=$qinfo['info']['type'];
//===================================================================
$answer=$qa[1];
$help=$qinfo['info']['help']; // $qa[2];
$answer_id = $idlist[$i];
$i++;
$question_template = file_get_contents($thistpl.'/question.pstpl');
if( preg_match( '/\{QUESTION_ESSENTIALS\}/' , $question_template ) === false || preg_match( '/\{QUESTION_CLASS\}/' , $question_template ) === false )
{
// if {QUESTION_ESSENTIALS} is present in the template but not {QUESTION_CLASS} remove it because you don't want id="" and display="" duplicated.
$question_template = str_replace( '{QUESTION_ESSENTIALS}' , '' , $question_template );
$question_template = str_replace( '{QUESTION_CLASS}' , '' , $question_template );
echo '
<!-- NEW QUESTION -->
<div id="question'.$qa[4].'" class="'.$q_class.$man_class.'"'.$n_q_display.'>
';
echo templatereplace($question_template,NULL,false,$qa[4]);
echo '
</div>
';
}
else
{
// TMSW - eventually refactor so that only substitutes the QUESTION_** fields - doesn't need full power of template replace
// TMSW - also, want to return a string, and call templatereplace once on that result string once all done.
echo templatereplace($question_template,NULL,false,$qa[4]);
};
}
if ($surveyMode == 'group') {
echo "<input type='hidden' name='lastgroup' value='$lastgroup' id='lastgroup' />\n"; // for counting the time spent on each group
}
if ($surveyMode == 'question') {
echo "<input type='hidden' name='lastanswer' value='$lastanswer' id='lastanswer' />\n";
}
echo "\n\n<!-- END THE GROUP -->\n";
echo templatereplace(file_get_contents("$thistpl/endgroup.pstpl"));
echo "\n\n</div>\n";
}
LimeExpressionManager::FinishProcessingGroup($LEMskipReprocessing);
echo LimeExpressionManager::GetRelevanceAndTailoringJavaScript();
LimeExpressionManager::FinishProcessingPage();
if (!$previewgrp){
$navigator = surveymover(); //This gets globalised in the templatereplace function
echo "\n\n<!-- PRESENT THE NAVIGATOR -->\n";
echo templatereplace(file_get_contents("$thistpl/navigator.pstpl"));
echo "\n";
if ($thissurvey['active'] != "Y")
{
echo "<p style='text-align:center' class='error'>".$clang->gT("This survey is currently not active. You will not be able to save your responses.")."</p>\n";
}
if($surveyMode != 'survey' && $thissurvey['allowjumps']=='Y')
{
echo "\n\n<!-- PRESENT THE INDEX -->\n";
echo '<div id="index"><div class="container"><h2>' . $clang->gT("Question index") . '</h2>';
$stepIndex = LimeExpressionManager::GetStepIndexInfo();
$lastGseq=-1;
$gseq = -1;
$grel=true;
for($v = 0, $n = 0; $n != $_SESSION['maxstep']; ++$n)
{
if (!isset($stepIndex[$n])) {
continue; // this is an invalid group - skip it
}
$stepInfo = $stepIndex[$n];
if ($surveyMode == 'question')
{
if ($lastGseq != $stepInfo['gseq']) {
// show the group label
++$gseq;
$g = $_SESSION['grouplist'][$gseq];
$grel = !LimeExpressionManager::GroupIsIrrelevantOrHidden($gseq);
if ($grel)
{
$gtitle = LimeExpressionManager::ProcessString($g[1]);
echo '<h3>' . FlattenText($gtitle) . "</h3>";
}
$lastGseq = $stepInfo['gseq'];
}
if (!$grel || !$stepInfo['show'])
continue;
$q = $_SESSION['fieldarray'][$n];
}
else
{
++$gseq;
if (!$stepInfo['show'])
continue;
$g = $_SESSION['grouplist'][$gseq];
}
if ($surveyMode == 'group')
{
$indexlabel = LimeExpressionManager::ProcessString($g[1]);
}
else
{