-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowsurvey.php
3461 lines (3392 loc) · 146 KB
/
showsurvey.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
require_once("includes/config.php");
require_once("includes/getuser.php");
require_once("includes/isauthor.php");
require_once("includes/quote_smart.php");
require_once("includes/ODBCDateToTextDate.php");
//require_once("includes/gettextoutput.php");
require_once("includes/getinstancesforblockandsection.php");
require_once("includes/renderstringforjavascript.php");
//error_reporting(E_ALL);
//check that we are coming from one of the accepted referring pages (defined in config.php)
//$referer = getenv('http_referer');
//$refererIsOK = false;
//if(!empty($referer))
// {
// for($i=0;$i<count($aPermittedReferers);$i++)
// {
// if (strpos($referer,$aPermittedReferers[$i])!=FALSE)
// {
// $refererIsOK = true;
// }
// }
// }
$refererIsOK = true;
if ($refererIsOK==true || IsAuthor($heraldID))
{
//ie. authors don't have to come from a certain referrer
//check that we have a surveyInstanceID
if ((isset($_POST['surveyInstanceID']) && $_POST['surveyInstanceID']!="")||(isset($_GET['surveyInstanceID']) && $_GET['surveyInstanceID']!=""))
{
//pass surveyInstanceID on
if(isset($_GET['surveyInstanceID']))
{
$surveyInstanceID = $_GET['surveyInstanceID'];
}
else
{
$surveyInstanceID = $_POST['surveyInstanceID'];
//print_r($surveyInstanceID);
}
//find out which survey this is an instance of
$qSurvey = " SELECT surveyID, title, startDate, finishDate
FROM SurveyInstances
WHERE surveyInstanceID = $surveyInstanceID";
$qResSurvey = mysqli_query($db_connection, $qSurvey);
if (($qResSurvey == false))
{
echo "problem querying SurveyInstances" . mysqli_error($db_connection);
}
else
{
$rowSurvey = mysqli_fetch_array($qResSurvey);
$surveyID = $rowSurvey['surveyID'];
$surveyInstanceTitle = $rowSurvey['title'];
if($rowSurvey['startDate'] == "")
{
$instanceStartDate = "null";
}
else
{
$instanceStartDate = $rowSurvey['startDate'];
}
if($rowSurvey['finishDate'] == "")
{
$instanceFinishDate = "null";
}
else
{
$instanceFinishDate = $rowSurvey['finishDate'];
}
//****************************************************************************************************************
//First read the blocks, sections, questions and items into arrays so that we do not need to repeatedly query them
//[] = block
//[][] = section
//[][][] = question
//[][][][] = item
//****************************************************************************************************************
$aItems = array();
$qBlocks = " SELECT Blocks.blockID, Blocks.text, Blocks.introduction, Blocks.epilogue, SurveyBlocks.position, Blocks.instanceable
FROM Blocks, SurveyBlocks
WHERE SurveyBlocks.surveyID = $surveyID
AND SurveyBlocks.visible = 1
AND Blocks.blockID = SurveyBlocks.blockID
ORDER BY SurveyBlocks.position";
$qResBlocks = mysqli_query($db_connection, $qBlocks);
$iBlock = 1;
while($rowBlocks = mysqli_fetch_array($qResBlocks))
{
$aItems[$iBlock][0]["blockID"] = $rowBlocks['blockID'];
$aItems[$iBlock][0]["text"] = $rowBlocks['text'];
$aItems[$iBlock][0]["introduction"] = $rowBlocks['introduction'];
$aItems[$iBlock][0]["epilogue"] = $rowBlocks['epilogue'];
$aItems[$iBlock][0]["position"] = $rowBlocks['position'];
$aItems[$iBlock][0]["instanceable"] = $rowBlocks['instanceable'];
$qSections = " SELECT Sections.sectionID, Sections.text, Sections.introduction, Sections.epilogue, BlockSections.position, Sections.sectionTypeID, Sections.instanceable
FROM Sections, BlockSections
WHERE BlockSections.blockID = ".$rowBlocks['blockID']."
AND BlockSections.visible = 1
AND Sections.sectionID = BlockSections.sectionID
ORDER BY BlockSections.position";
$qResSections = mysqli_query($db_connection, $qSections);
$iSection = 1;
while($rowSections = mysqli_fetch_array($qResSections))
{
$aItems[$iBlock][$iSection][0]["sectionID"] = $rowSections['sectionID'];
$aItems[$iBlock][$iSection][0]["text"] = $rowSections['text'];
$aItems[$iBlock][$iSection][0]["introduction"] = $rowSections['introduction'];
$aItems[$iBlock][$iSection][0]["epilogue"] = $rowSections['epilogue'];
$aItems[$iBlock][$iSection][0]["position"] = $rowSections['position'];
$aItems[$iBlock][$iSection][0]["sectionTypeID"] = $rowSections['sectionTypeID'];
$aItems[$iBlock][$iSection][0]["instanceable"] = $rowSections['instanceable'];
$qQuestions = " SELECT Questions.questionID, Questions.comments, Questions.questionTypeID, Questions.text, SectionQuestions.position
FROM Questions, SectionQuestions
WHERE SectionQuestions.sectionID = ".$rowSections['sectionID']."
AND SectionQuestions.visible = 1
AND Questions.questionID = SectionQuestions.questionID
ORDER BY SectionQuestions.position";
$qResQuestions = mysqli_query($db_connection, $qQuestions);
$iQuestion = 1;
while($rowQuestions = mysqli_fetch_array($qResQuestions))
{
$aItems[$iBlock][$iSection][$iQuestion][0]["questionID"] = $rowQuestions['questionID'];
$aItems[$iBlock][$iSection][$iQuestion][0]["comments"] = $rowQuestions['comments'];
$aItems[$iBlock][$iSection][$iQuestion][0]["questionTypeID"] = $rowQuestions['questionTypeID'];
$aItems[$iBlock][$iSection][$iQuestion][0]["text"] = $rowQuestions['text'];
$aItems[$iBlock][$iSection][$iQuestion][0]["position"] = $rowQuestions['position'];
$qItems = " SELECT Items.itemID, Items.text, QuestionItems.position
FROM Items, QuestionItems
WHERE QuestionItems.questionID = ".$rowQuestions['questionID']."
AND QuestionItems.visible = 1
AND Items.itemID = QuestionItems.itemID
ORDER BY QuestionItems.position";
$qResItems = mysqli_query($db_connection, $qItems);
$iItem = 1;
while($rowItems = mysqli_fetch_array($qResItems))
{
$aItems[$iBlock][$iSection][$iQuestion][$iItem]["itemID"] = $rowItems['itemID'];
$aItems[$iBlock][$iSection][$iQuestion][$iItem]["text"] = $rowItems['text'];
$aItems[$iBlock][$iSection][$iQuestion][$iItem]["position"] = $rowItems['position'];
$iItem++;
}
$iQuestion++;
}
$iSection ++;
}
$iBlock ++;
}
//print_r($aItems);
}
}
else
{
//if not don't give any URL parameter - just pass them back to the index page.
header("Location: index.php");
}
}
else
{
echo "Please access your survey through Weblearn";
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--<style type="text/css" media="all">
@import "script/widgEditor/css/widgEditor.css";
</style>
<script language="javascript" src="script/widgEditor/scripts/widgEditor.js"></script>-->
<!--<script type="text/javascript" src="script/fckeditor/fckeditor.js"></script>-->
<!--<script type="text/javascript" src="script/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>-->
<script type="text/javascript" src="script/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckfinder/ckfinder.js"></script>
<script language="javascript" type="text/javascript">
function getElementsByClass(searchClass,node,tag)
{
//from http://www.dustindiaz.com/getelementsbyclass/
var classElements = new Array();
if ( node == null ) node = document;
if ( tag == null ) tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
for (i = 0, j = 0; i < elsLen; i++)
{
if ( pattern.test(els[i].className) )
{
classElements[j] = els[i];
j++;
}
}
return classElements;
}
function bodyOnLoad(){
CKFinder.setupCKEditor( null, 'https://learntech.imsu.ox.ac.uk/feedback/ckfinder/' );
}
//tinyMCE.init({
//mode : "specific_textareas",
//theme : "advanced",
//theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,|,bullist,numlist,|,outdent,indent,|,undo,redo",
//theme_advanced_buttons2 : "",
//theme_advanced_buttons3 : "",
//theme_advanced_buttons4 : "",
//theme_advanced_toolbar_location : "top",
//theme_advanced_toolbar_align : "left",
//width : "100%",
//editor_selector : "richtext"
//});
//chnages all textareas with the given class to fckeditors on load
//function changeTextAreasToFCK()
//{
//var allTextAreas = getElementsByClass('richtext',document,'textarea');
//for (var i=0; i < allTextAreas.length; i++)
//{
//var oFCKeditor = new FCKeditor( allTextAreas[i].name ) ;
//oFCKeditor.BasePath = "script/fckeditor/" ;
//oFCKeditor.ToolbarSet = 'ElectiveReport' ;
//oFCKeditor.ReplaceTextarea() ;
//}
//}
function displayErrorMesage(qNumber, qText)
{
alert('You have not completed all of the questions. Please answer Q' + qNumber + ' - "' + qText + '" before clicking Submit again.')
}
//dynamically filling date lists
function buildsel(Month,Year,DayList)
{
//Calculate no of days in month
days = daysInMonth(Month,Year);
// get reference for list that is being updated
sel=DayList;
//remember currently selected day
selectedDay=sel.selectedIndex;
/* delete entries for list that is being updated */
for (i=0; i<sel.length; i++)
{
sel.options[i] = null;
}
/* add new entries to list that is being updated */
if (selectedDay>days)
{
selectedDay=days;
}
sel.options[0] = new Option('dd',0);
for (i=1; i<=days; i++)
{
sel.options[i] = new Option(i,i);
}
sel.options[selectedDay].selected=true;
}
//function to return no of days in a month
function daysInMonth(Month, Year)
{
if (Month==1)
{
return 31;
}
else if (Month==2)
{
if ((Year-1980)%4 == 0)
{
return 29;
}
else
{
return 28;
}
}
if (Month==3) return 31;
if (Month==4) return 30;
if (Month==5) return 31;
if (Month==6) return 30;
if (Month==7) return 31;
if (Month==8) return 31;
if (Month==9) return 30;
if (Month==10) return 31;
if (Month==11) return 30;
if (Month==12) return 31;
}
function goTo(URL)
{
window.location.href = URL;
}
</script>
<?php
//used to track whether any data are
$iNoOfMissingAnswers = 0;
//**************************************************************
//DEALING WITH BRANCHES
//**************************************************************
//******************************************************************
//Functions to show and hide branches
//******************************************************************
echo " <script language=\"javascript\" type=\"text/javascript\">";
echo " function showAndHideForMCHOIC(sShow,sHide)
{
if(sShow!=\"\")
{
aShow = sShow.split(\"+\");
showDivs(aShow);
}
if(sHide!=\"\")
{
aHide = sHide.split(\"+\");
hideDivs(aHide);
}
}
function showAndHideForMSELEC(CheckBox,sItems)
{
if(sItems!=\"\")
{
aItems = sItems.split(\"+\");
if(CheckBox.checked == true)
{
showDivs(aItems);
}
else
{
hideDivs(aItems);
}
}
}
function showAndHideForDRDOWN(selectedItem,branchItem,sItems)
{
if(sItems!=\"\")
{
aItemsArray = sItems.split(\"|\");
}
if(branchItem!=\"\")
{
abranchItems = branchItem.split(\"|\");
var i = 0;
while (i<abranchItems.length)
{
aItems = aItemsArray[i].split(\"+\");
if(selectedItem == abranchItems[i])
{
showDivs(aItems);
}
else
{
hideDivs(aItems);
}
i++;
}
}
}
function showDivs(aShow)
{
for(var j=0;j<aShow.length;j++)
{
if (document.getElementById(aShow[j]))
{
document.getElementById(aShow[j]).style.display=\"block\";
}
}
}
function hideDivs(aHide)
{
for(var j=0;j<aHide.length;j++)
{
if (document.getElementById(aHide[j]))
{
document.getElementById(aHide[j]).style.display=\"none\";
}
}
}";
//**************************************************************
//END DEALING WITH BRANCHES
//**************************************************************
echo " </script>";
//***********************************************************************
// CHECKING THAT ALL QUESTIONS HAVE BEEN ANSWERED - CLIENT-SIDE VALIDATION
//***********************************************************************
echo "<script language=\"javascript\" type=\"text/javascript\">
//create a function which is called by the Add button before the form is submitted to change the value of hAdded to true;
function sethAddedToTrue()
{
document.getElementById('hAdded').value = true;
}
//create a function which is called by the Delete button before the form is submitted to change the value of hDeleted to true;
function sethDeletedToTrue()
{
document.getElementById('hDeleted').value = true;
}
//create a function which is called by the SaveSurvey button before the form is submitted to change the value of hSaved to true;
function sethSavedToTrue()
{
alert(\"Your answers are being saved. Don't forget to return to this survey and submit your final answers.\");
document.getElementById('hSaved').value = true;
}
function ValidateForm()
{";
//$qBlocks = " SELECT Blocks.blockID, Blocks.title, Blocks.introduction, Blocks.epilogue, SurveyBlocks.position, Blocks.instanceable
//FROM Blocks, SurveyBlocks
//WHERE SurveyBlocks.surveyID = $surveyID
//AND SurveyBlocks.visible = 1
//AND Blocks.blockID = SurveyBlocks.blockID
//ORDER BY SurveyBlocks.position";
//$qResBlocks = mysql_query($qBlocks);
$questionNo = 1;
echo "var aQuestions = new Array();";
for ($iBlock=1; $iBlock<=count($aItems);$iBlock++)
//while($rowBlocks = mysql_fetch_array($qResBlocks))
{
//$blockID = $rowBlocks['blockID'];
$blockID = $aItems[$iBlock][0]["blockID"];
$blockIsInstanceable = false;
//if($rowBlocks[instanceable]==1)
if($aItems[$iBlock][0]["instanceable"]==1)
{
$blockIsInstanceable = true;
if (isset($_POST["bAddInstance_" . $blockID]))
{
$noOfCurrentInstances = $_POST["hCurrentInstance_" . $blockID];
$noOfInstances = $noOfCurrentInstances + 1;
}
elseif (isset($_POST["bDeleteInstance_" . $blockID]))
{
$noOfCurrentInstances = $_POST["hCurrentInstance_" . $blockID];
$noOfInstances = $noOfCurrentInstances - 1;
}
else
{
$noOfInstances = getInstancesForBlock($blockID);
}
}
else
{
$blockIsInstanceable = false;
$noOfInstances = 1;
}
//$qSections = " SELECT Sections.sectionID, Sections.title, Sections.introduction, Sections.epilogue, BlockSections.position, Sections.sectionTypeID, Sections.instanceable
//FROM Sections, BlockSections
//WHERE BlockSections.blockID = $blockID
//AND BlockSections.visible = 1
//AND Sections.sectionID = BlockSections.sectionID
//ORDER BY BlockSections.position";
//$qResSections = mysql_query($qSections);
for($inst=1;$inst<=$noOfInstances;$inst++)
{
//if(mysql_num_rows($qResSections)>0)
//{
//mysql_data_seek($qResSections, 0);
//}
for($iSection=1; $iSection<=count($aItems[$iBlock])-1;$iSection++)
//while($rowSections = mysql_fetch_array($qResSections))
{
//$sectionID = $rowSections['sectionID'];
$sectionID = $aItems[$iBlock][$iSection][0]["sectionID"];
$sectionIsInstanceable = false;
//if($rowSections[instanceable]==1)
if($aItems[$iBlock][$iSection][0]["instanceable"]==1)
{
$sectionIsInstanceable = true;
if (isset($_POST["bAddSectionInstance" . "_" . $blockID . "_" . $sectionID."_i".$inst]))
{
$noOfCurrentSectionInstances = $_POST["hCurrentSectionInstance" . "_" . $blockID . "_" . $sectionID."_i".$inst];
$noOfSectionInstances = $noOfCurrentSectionInstances + 1;
}
elseif (isset($_POST["bDeleteSectionInstance" . "_" . $blockID . "_" . $sectionID."_i".$inst]))
{
$noOfCurrentSectionInstances = $_POST["hCurrentSectionInstance" . "_" . $blockID . "_" . $sectionID."_i".$inst];
$noOfSectionInstances = $noOfCurrentSectionInstances - 1;
}
else
{
$noOfSectionInstances = getInstancesForSection($blockID,$sectionID,$inst);
}
}
else
{
$sectionIsInstanceable = false;
$noOfSectionInstances = 1;
}
//$qQuestions = " SELECT Questions.questionID, Questions.comments, Questions.questionTypeID, Questions.text, SectionQuestions.position
//FROM Questions, SectionQuestions
//WHERE SectionQuestions.sectionID = $sectionID
//AND SectionQuestions.visible = 1
//AND Questions.questionID = SectionQuestions.questionID
//ORDER BY SectionQuestions.position";
$qResQuestions = mysqli_query($db_connection, $qQuestions);
for($sinst=1;$sinst<=$noOfSectionInstances;$sinst++)
{
//if(mysql_num_rows($qResQuestions)>0)
//{
//mysql_data_seek($qResQuestions, 0);
//}
for($iQuestion=1; $iQuestion<=count($aItems[$iBlock][$iSection])-1;$iQuestion++)
//while($rowQuestions = mysql_fetch_array($qResQuestions))
{
//$questionID = $rowQuestions['questionID'];
$questionID = $aItems[$iBlock][$iSection][$iQuestion][0]["questionID"];
echo "aQuestions[".$questionNo."]=new Array(8);\n";
echo "aQuestions[".$questionNo."][0]='".$blockID."_".$sectionID."_".$questionID."_i".$inst."_si".$sinst."';\n";//questionID
echo "aQuestions[".$questionNo."][1]=".$questionNo.";\n";
//now we need to escape any charcaters which might cause the javascript to throw an error
$escapedText = renderStringForJavaScript($aItems[$iBlock][$iSection][$iQuestion][0]["text"]);
echo "aQuestions[".$questionNo."][2]='".$escapedText."';\n";
//echo "aQuestions[".$questionNo."][3]=".$rowQuestions['questionTypeID'].";\n";
echo "aQuestions[".$questionNo."][3]=".$aItems[$iBlock][$iSection][$iQuestion][0]["questionTypeID"].";\n";
//$qItems = " SELECT Items.itemID, Items.text, QuestionItems.position
//FROM Items, QuestionItems
//WHERE QuestionItems.questionID = $questionID
//AND QuestionItems.visible = 1
//AND Items.itemID = QuestionItems.itemID
//ORDER BY QuestionItems.position";
//$qResItems = mysql_query($qItems);
//$itemArraySize = mysql_num_rows($qResItems) + 1;
$itemArraySize = count($aItems[$iBlock][$iSection][$iQuestion]);
if($itemArraySize>0)
{
echo "aQuestions[".$questionNo."][4]=new Array(".$itemArraySize.");\n";
//$itemNo = 1;
for($iItem=1; $iItem<=count($aItems[$iBlock][$iSection][$iQuestion])-1;$iItem++)
//while($rowItems = mysql_fetch_array($qResItems))
{
//$itemID = $rowItems[itemID];
$itemID = $aItems[$iBlock][$iSection][$iQuestion][$iItem]["itemID"];
echo "aQuestions[".$questionNo."][4][".$iItem."]='".$blockID."_".$sectionID."_".$questionID."_".$itemID."_i".$inst."_si".$sinst."';\n";
//$itemNo++;
}
}
echo "aQuestions[".$questionNo."][5]='div".$blockID."_i".$inst."';\n";
echo "aQuestions[".$questionNo."][6]='div".$blockID."_".$sectionID."_i".$inst."_si".$sinst."';\n";
//don't asign a question div for matrix questoins, these canot be branched to individually
if ($aItems[$iBlock][$iSection][0]["sectionTypeID"] == 1)
{
echo "aQuestions[".$questionNo."][7]='div".$blockID."_".$sectionID."_".$questionID."_i".$inst."_si".$sinst."';\n";
}
else
{
echo "aQuestions[".$questionNo."][7]=null;\n";
}
$questionNo++;
}
}
}
}
}
echo " if (document.getElementById('hAdded').value == 'true')
{
//don't validate if we're just adding an instance
return true;
}
if (document.getElementById('hDeleted').value == 'true')
{
//don't validate if we're just adding an instance
return true;
}
else if (document.getElementById('hSaved').value == 'true')
{
//don't validate if the user is saving rather than submitting their data
return true;
}
else
{
for(i=1;i<aQuestions.length;i++)
{
//check whether question is visible to the user
var questionIsVisible = false;
if(document.getElementById(aQuestions[i][5]).style.display!=\"none\" && document.getElementById(aQuestions[i][6]).style.display!=\"none\")
{
if (aQuestions[i][7] != null)
{
if(document.getElementById(aQuestions[i][7]).style.display!=\"none\")
{
questionIsVisible = true;
}
}
else
{
questionIsVisible = true;
}
}
if(questionIsVisible)
{
if(aQuestions[i][3]==1) // || aQuestions[i][3]==2) Don't requirte an answer to a multiple choice
{
var itemCount = 0;
for(j=1;j<=aQuestions[i][4].length-1;j++)
{
if(document.getElementById(aQuestions[i][4][j]).checked)
{
itemCount++;
}
}
if(itemCount == 0)
{
alert('Please answer the question: ' + aQuestions[i][1] + ': \"' + aQuestions[i][2] + '\" before clicking the \"Submit\" button.');
document.getElementById(aQuestions[i][4][1]).focus();
return(false);
}
}
else if(aQuestions[i][3]==3)
{
if(document.getElementById(aQuestions[i][0]).options[0].selected == true)
{
alert('Please answer the question: ' + aQuestions[i][1] + ': \"' + aQuestions[i][2] + '\" before clicking the \"Submit\" button.');
document.getElementById(aQuestions[i][0]).focus();
return(false);
}
}
else if(aQuestions[i][3]==6)
{
var dayID = 'day_' + aQuestions[i][0];
var monthID = 'month_' + aQuestions[i][0];
var yearID = 'year_' + aQuestions[i][0];
if(document.getElementById(dayID).options[0].selected == true || document.getElementById(monthID).options[0].selected == true || document.getElementById(yearID).options[0].selected == true)
{
alert('Please answer the question: ' + aQuestions[i][1] + ': \"' + aQuestions[i][2] + '\" before clicking the \"Submit\" button.');
document.getElementById(dayID).focus();
return(false);
}
}
}
}
return(true);
}
";
echo " }";
echo " </script>";
//***********************************************************************
// END CHECKING THAT ALL QUESTIONS HAVE BEEN ANSWERED
//***********************************************************************
$qSurveys = "SELECT title, introduction, epilogue, allowSave, allowViewByStudent
FROM Surveys
WHERE surveyID = $surveyID";
$qResSurvey = mysqli_query($db_connection, $qSurveys);
$rowSurvey = mysqli_fetch_array($qResSurvey);
$surveyTitle = $rowSurvey['title'];
$surveyIntroduction = $rowSurvey['introduction'];
$surveyEpilogue = $rowSurvey['epilogue'];
$surveyAllowSave = $rowSurvey['allowSave'];
$allowViewByStudent = $rowSurvey['allowViewByStudent'];
echo "<title>$surveyInstanceTitle</title>";
?>
<link rel="stylesheet" type="text/css" href="css/SurveyStyles.css"></link>
<?php
//if(IsAuthor($heraldID))
// {
echo"<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msdstyle2.css\" media=\"screen\"/>";
// }
//else
// {
// echo"<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msdstyle2student.css\" media=\"screen\"/>";
// }
?>
<link rel="stylesheet" type="text/css" href="css/msdprint.css" media="print" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<!--<body onload="changeTextAreasToFCK()">-->
<body onLoad="bodyOnLoad()">
<?php
//*******************************************************************************************************************************
//BEGIN WRITING SURVEY RESULTS
//*******************************************************************************************************************************
$allAnswered = true;
if (isset($_POST['bSubmitSurvey'])||isset($_POST['bSaveSurvey']))
{
//***********************************************************************
// Writes $text to AnswerComments table for $answerID
//***********************************************************************
function writeComment($answerID,$text,&$bSuccess,&$textOutput)
{
global $db_connection; //makes this available within the function
$text = quote_smart($db_connection, $text);
//now need to check whether the record exists in the AnswerComments table
$qAnswerComments = "SELECT answerCommentID
FROM AnswerComments
WHERE answerID = $answerID";
$qResAnswerComments = mysqli_query($db_connection, $qAnswerComments);
$textOutput = $textOutput ."<tr><td></td><td colspan=\"2\">";
//if so, write the text into the textarea
$textOutput = $textOutput . $text;
$textOutput = $textOutput ."</td></tr>";
if (mysqli_num_rows($qResAnswerComments)==1)
//if it does, update it
{
$rowAnswerComments = mysqli_fetch_array($qResAnswerComments);
$uAnswerComments = "UPDATE AnswerComments
SET answerID = $answerID,
text = $text
WHERE answerCommentID = $rowAnswerComments[answerCommentID]";
$result_query = @mysqli_query($db_connection,$uAnswerComments);
//note: don't check if mysqli_affected_rows($db_connection) == 0 for
//updates as my_sql returns 0 if the updated values are the same as
//those already there.
if (($result_query == false))
{
echo "problem updating AnswerComments" . mysqli_error($db_connection);
$bSuccess = false;
}
}
else if (mysqli_num_rows($qResAnswerComments)==0)
{//if not, insert it
$iAnswerComments = "INSERT INTO AnswerComments
VALUES(0,$text,$answerID)";
$result_query = @mysqli_query($db_connection, $iAnswerComments);
if (($result_query == false) && mysqli_affected_rows($db_connection) == 0)
{
echo "problem inserting into AnswerComments" . mysqli_error($db_connection);
$bSuccess = false;
}
}
else
{
//generate an error message
echo "There appears to be more than one entry for this answerID in AnswerComments.";
$bSuccess = false;
}
}
//***********************************************************************
// Writes $date to AnswerDates table for $answerID
//***********************************************************************
function writeDate($answerID,$date,&$bSuccess,&$textOutput)
{
global $db_connection; //makes this available within the function
$date = quote_smart($db_connection, $date);
//now need to check whether the record exists in the AnswerComments table
$qAnswerDates = "SELECT answerDateID
FROM AnswerDates
WHERE answerID = $answerID";
$qResAnswerDates = mysqli_query($db_connection, $qAnswerDates);
$textOutput = $textOutput . "<td>";
$textOutput = $textOutput . $date;
$textOutput = $textOutput . "</td></tr>";
if (mysqli_num_rows($qResAnswerDates)==1)
//if it does, update it
{
$rowAnswerDates = mysqli_fetch_array($qResAnswerDates);
$uAnswerDates = "UPDATE AnswerDates
SET answerID = $answerID,
date = $date
WHERE answerDateID = $rowAnswerDates[answerDateID]";
$result_query = @mysqli_query($db_connection, $uAnswerDates);
//note: don't check if mysqli_affected_rows($db_connection) == 0 for
//updates as my_sql returns 0 if the updated values are the same as
//those already there.
if (($result_query == false))
{
echo "problem updating AnswerDates" . mysqli_error($db_connection);
$bSuccess = false;
}
}
else if (mysqli_num_rows($qResAnswerDates)==0)
{//if not, insert it
$iAnswerDates = "INSERT INTO AnswerDates
VALUES(0,$date,$answerID)";
$result_query = @mysqli_query($db_connection, $iAnswerDates);
if (($result_query == false) && mysqli_affected_rows($db_connection) == 0)
{
echo "problem inserting into AnswerDates" . mysqli_error($db_connection);
$bSuccess = false;
}
}
else
{
//generate an error message
echo "There appears to be more than one entry for this answerID in AnswerDates.";
$bSuccess = false;
}
}
//***********************************************************************
// Writes $itemID to AnswerItems table for $answerID
//***********************************************************************
function writeItem($answerID,$itemID,&$bSuccess,&$textOutput)
{
//echo "(".$answerID." ".$itemID."<br/>";
global $db_connection; //makes this available within the function
//now need to check whether the record exists in the AnswerItems table
//ensure $answerID and itemID are ints
$answerID=intval($answerID);
$itemID=intval($itemID);
$qAnswerItems = "SELECT answerItemID
FROM AnswerItems
WHERE answerID = $answerID";
//print_r($qAnswerItems);
$qResAnswerItems = mysqli_query($db_connection, $qAnswerItems);
//print_r($qResAnswerItems);
$qItems = " SELECT text
FROM Items
WHERE itemID = $itemID";
$qResItems = mysqli_query($db_connection, $qItems);
$rowItems = mysqli_fetch_array($qResItems);
$textOutput = $textOutput . "<td>";
$textOutput = $textOutput .$rowItems['text'];
$textOutput = $textOutput . "</td></tr>";
if (mysqli_num_rows($qResAnswerItems)==1)
//if it does, update it
{
//$rowAnswerItems = mysql_fetch_array($qResAnswerItems,$db_connection);
$rowAnswerItems = mysqli_fetch_array($qResAnswerItems);
//print_r($rowAnswerItems);
//print_r($rowAnswerItems);
$answerItemID = intval($rowAnswerItems[0]);//seemed to be coming in as a string?!!
$uAnswerItems = "UPDATE AnswerItems
SET answerID = $answerID,
itemID = $itemID
WHERE answerItemID = $answerItemID";
//print_r($uAnswerItems);
$result_query = @mysqli_query($db_connection, $uAnswerItems);
if (($result_query == false))
{
echo "problem updating AnswerItems: " . mysqli_error($db_connection);
$bSuccess = false;
}
}
else if (mysqli_num_rows($qResAnswerItems)==0)
{//if not, insert it
$iAnswerItems = "INSERT INTO AnswerItems
VALUES(0,$itemID,$answerID)";
$result_query = @mysqli_query($db_connection, $iAnswerItems);
if (($result_query == false) && mysqli_affected_rows($db_connection) == 0)
{
echo "problem inserting into AnswerItems: " . mysqli_error($db_connection);
$bSuccess = false;
}
}
else
{
//generate an error message
echo "There appears to be more than one entry for this answerID in AnswerItems.";
$bSuccess = false;
}
}
function writeCheckBoxItem($answerID,$aItems,$questionID, $sectionID, $blockID, $inst, $sinst, &$bSuccess,&$textOutput)
{
global $db_connection; //makes this available within the function
//now go through and check for any existing items for this answerID
$qAnswerItems = "SELECT answerItemID
FROM AnswerItems
WHERE answerID = $answerID";
$qResAnswerItems = mysqli_query($db_connection, $qAnswerItems);
if (mysqli_num_rows($qResAnswerItems)>0)
//if any records exist, delete them
{
while($rowAnswerItems = mysqli_fetch_array($qResAnswerItems))
{
$answerItemID = $rowAnswerItems['answerItemID'];
$delAnswerItems = "DELETE FROM AnswerItems
WHERE answerItemID = $answerItemID";
$result_query = @mysqli_query($db_connection, $delAnswerItems);
if (($result_query == false) && mysqli_affected_rows($db_connection) == 0)
{
echo "problem deleting from Answers" . mysqli_error($db_connection);
$bSuccess = false;
}
}
}
$textOutput = $textOutput . "<td>";
//get item ids and text for this question
$qItems = " SELECT Items.itemID, Items.text
FROM Items, QuestionItems, SectionQuestions, BlockSections
WHERE BlockSections.blockID = $blockID
AND BlockSections.sectionID = SectionQuestions.sectionID
AND SectionQuestions.sectionID = $sectionID
AND SectionQuestions.questionID = QuestionItems.questionID
AND QuestionItems.questionID = $questionID
AND Items.itemID = QuestionItems.itemID";
$qResItems = mysqli_query($db_connection, $qItems);
while($rowItems = mysqli_fetch_array($qResItems))
{
$mselecName = $blockID . "_" . $sectionID . "_" . $questionID. "_" . $rowItems['itemID'] . "_i" . $inst . "_si" . $sinst;
//echo $mselecName;
if(isset($_POST[$mselecName]) && $_POST[$mselecName]=="on")
{
$textOutput = $textOutput . " " . $rowItems["text"];
$itemID = $rowItems['itemID'];
$iAnswerItems = "INSERT INTO AnswerItems
VALUES(0,$itemID,$answerID)";
$result_query = @mysqli_query($db_connection, $iAnswerItems);
if (($result_query == false) && mysqli_affected_rows($db_connection) == 0)
{
echo "problem inserting into AnswerItems: " . mysqli_error($db_connection);
$bSuccess = false;
}
}
}
//run through each item and insert new answerItems
//for($iItem=1; $iItem<=count($aItems[$iBlock][$iSection][$iQuestion])-1;$iItem++)
//{
//$itemID = $aItems[$iBlock][$iSection][$iQuestion][$iItem]["itemID"];
//$mselecName = $blockID . "_" . $sectionID . "_" . $questionID. "_" . $itemID . "_i" . $inst . "_si" . $sinst;
//echo $mselecName;
//if(isset($_POST[$mselecName]) && $_POST[$mselecName]=="on")
//{
//$textOutput = $textOutput . " " . $aItems[$iBlock][$iSection][$iQuestion][$iItem]["text"];
//$iAnswerItems = "INSERT INTO AnswerItems
// VALUES(0,$itemID,$answerID)";
//$result_query = @mysql_query($iAnswerItems,$db_connection);
//if (($result_query == false) && mysqli_affected_rows($db_connection) == 0)
// {
// echo "problem inserting into AnswerItems: " . mysqli_error($db_connection);
// $bSuccess = false;
// }
//}
//}
$textOutput = $textOutput . "</td></tr>";
}
//***************************************************************************
//CHECKING THAT ALL QUESTIONS HAVE BEEN ANSWERED - SERVER-SIDE VALIDATION
//ESSENTIALLY A PHP-REWRITE OF THE CLIENT-SIDE VALIDATION
//***************************************************************************
if($_POST['hSaved']=="false")
{
$questionNo = 1;
$aQuestions = array();
for ($iBlock=1; $iBlock<=count($aItems);$iBlock++)
{
$blockID = $aItems[$iBlock][0]["blockID"];
if($aItems[$iBlock][0]["instanceable"]==1)
{
$noOfInstances = $_POST[hCurrentInstance . "_" . $blockID];
}
else
{
$noOfInstances = 1;
}
for($inst=1;$inst<=$noOfInstances;$inst++)
{
for($iSection=1; $iSection<=count($aItems[$iBlock])-1;$iSection++)
{
$sectionID = $aItems[$iBlock][$iSection][0]["sectionID"];
if($aItems[$iBlock][$iSection][0]["instanceable"]==1)
{
$noOfSectionInstances = $_POST["hCurrentSectionInstance" . "_" . $blockID . "_" . $sectionID . "_i" . $inst];
}
else
{
$noOfSectionInstances = 1;
}
for($sinst=1;$sinst<=$noOfSectionInstances;$sinst++)
{
for($iQuestion=1; $iQuestion<=count($aItems[$iBlock][$iSection])-1;$iQuestion++)
{
$questionID = $aItems[$iBlock][$iSection][$iQuestion][0]["questionID"];
$aQuestions[$questionNo]= array();
$aQuestions[$questionNo][0]= $blockID."_".$sectionID."_".$questionID."_i".$inst."_si".$sinst;//questionID
$aQuestions[$questionNo][1]= $questionNo;
$aQuestions[$questionNo][2]= $aItems[$iBlock][$iSection][$iQuestion][0]["text"];
$aQuestions[$questionNo][3]= $aItems[$iBlock][$iSection][$iQuestion][0]["questionTypeID"];
$itemArraySize = count($aItems[$iBlock][$iSection][$iQuestion])-1;
if($itemArraySize>0)
{
$aQuestions[$questionNo][4]= array();
for($iItem=1; $iItem<=count($aItems[$iBlock][$iSection][$iQuestion])-1;$iItem++)
{
$itemID = $aItems[$iBlock][$iSection][$iQuestion][$iItem]["itemID"];
$aQuestions[$questionNo][4][$iItem]=$blockID."_".$sectionID."_".$questionID."_".$itemID."_i".$inst."_si".$sinst;
}
}
$aQuestions[$questionNo][5]= ""; //will be used to hold data on whether the question has been answered correctly;
//is this block, section or question branched to?
$qBranchDestinations = "SELECT BranchDestinations.branchID, Branches.blockID, Branches.sectionID, Branches.questionID, Branches.itemID, Questions.questionTypeID
FROM BranchDestinations, Branches, Questions
WHERE ((BranchDestinations.blockID = $blockID
AND BranchDestinations.sectionID IS NULL
AND BranchDestinations.questionID IS NULL)
OR (BranchDestinations.blockID = $blockID
AND BranchDestinations.sectionID = $sectionID
AND BranchDestinations.questionID IS NULL)
OR (BranchDestinations.blockID = $blockID
AND BranchDestinations.sectionID = $sectionID
AND BranchDestinations.questionID = $questionID))
AND BranchDestinations.branchID = Branches.branchID
AND Questions.questionID = Branches.questionID
AND Branches.surveyID = $surveyID
";
$qResBranchDestinations = mysqli_query($db_connection, $qBranchDestinations);
if (mysqli_num_rows($qResBranchDestinations)==0)
{
$aQuestions[$questionNo][6] = NULL;
}
else
{