1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Description of SurveyTool |
---|
5 | * |
---|
6 | * @author fpvanagthoven |
---|
7 | */ |
---|
8 | class SurveyTool { |
---|
9 | |
---|
10 | private $survey; |
---|
11 | |
---|
12 | public function __construct($survey) { |
---|
13 | $this->survey = $survey; |
---|
14 | |
---|
15 | $this->javascript(); |
---|
16 | $this->init(); |
---|
17 | } |
---|
18 | |
---|
19 | private function javascript() { |
---|
20 | ?> |
---|
21 | <script type="text/javascript"> |
---|
22 | /* --- input checking --- */ |
---|
23 | function checksPassed(form) |
---|
24 | { |
---|
25 | for (var i = 0; i < form.length; i++) |
---|
26 | { |
---|
27 | if (form.elements[i].checkPassed == 'no') |
---|
28 | return false; |
---|
29 | } |
---|
30 | return true; |
---|
31 | } |
---|
32 | |
---|
33 | function checkInt(input) |
---|
34 | { |
---|
35 | input.style.borderWidth = '1px' ; |
---|
36 | var value = input.value; |
---|
37 | if (isNaN(value)) |
---|
38 | { |
---|
39 | input.style.borderColor = 'red'; |
---|
40 | input.checkPassed = 'no'; |
---|
41 | } |
---|
42 | else |
---|
43 | { |
---|
44 | input.style.border = '1px solid #abadb3'; |
---|
45 | input.checkPassed = null; |
---|
46 | } |
---|
47 | } |
---|
48 | </script> |
---|
49 | <?php |
---|
50 | } |
---|
51 | |
---|
52 | private function init() { |
---|
53 | ?> |
---|
54 | <form name="survey" action="" onsubmit="return checksPassed(this)" method="post"> |
---|
55 | <?php |
---|
56 | $this->displayTitle(); |
---|
57 | $this->displayDescription(); |
---|
58 | $this->displayQuestions(); |
---|
59 | $this->submitSurvey(); |
---|
60 | ?> |
---|
61 | </form> |
---|
62 | <?php |
---|
63 | } |
---|
64 | |
---|
65 | private function displayTitle() { |
---|
66 | ?> |
---|
67 | <h1><?php echo $this->survey->title; ?></h1> |
---|
68 | <?php |
---|
69 | } |
---|
70 | |
---|
71 | private function displayDescription() { |
---|
72 | if ($this->survey->description != Survey::DEFAULT_DESCRIPTION) |
---|
73 | $description = $this->survey->description; |
---|
74 | else |
---|
75 | $description = ""; |
---|
76 | ?> |
---|
77 | <p class='centerBoxed'><?php echo $description; ?></p> |
---|
78 | <?php |
---|
79 | } |
---|
80 | |
---|
81 | private function displayQuestions() { |
---|
82 | foreach ($this->survey->questions as $numQ => $question) { |
---|
83 | |
---|
84 | $this->displayQuestion($question, $numQ); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | private function displayQuestion($question, $numQ) { |
---|
89 | echo "<fieldset>"; |
---|
90 | $this->displayQuestionTitle($question); |
---|
91 | $this->displayQuestionDescription($question); |
---|
92 | $this->displayAnswerBox($question, $numQ); |
---|
93 | echo "</fieldset>"; |
---|
94 | } |
---|
95 | |
---|
96 | private function displayQuestionTitle($question) { |
---|
97 | ?> |
---|
98 | <?php echo "<legend><h2>" . $question->title . "</h2></legend>"; ?> |
---|
99 | <?php |
---|
100 | } |
---|
101 | |
---|
102 | private function displayQuestionDescription($question) { |
---|
103 | if ($question->description != "Write a question description here.") |
---|
104 | $description = $question->description; |
---|
105 | else |
---|
106 | $description = ""; |
---|
107 | ?> |
---|
108 | <p><?php echo "<div class='questionDescription noPadding'>" . $description . "</div>"; ?></p> |
---|
109 | <?php |
---|
110 | } |
---|
111 | |
---|
112 | private function displayAnswerBox($question, $numQ) { |
---|
113 | echo "<div class='answerBox'>"; |
---|
114 | switch ($question->type) { |
---|
115 | case 'text': |
---|
116 | ?> |
---|
117 | <input type="text" name="<?php echo 'q' . $numQ . 'result1' ?>" value="" class="textBox"/> |
---|
118 | <?php |
---|
119 | break; |
---|
120 | case 'int': |
---|
121 | ?> |
---|
122 | <input type="text" name="<?php echo 'q' . $numQ . 'result1' ?>" value="" onchange="checkInt(this)" class="intBox"/> |
---|
123 | <?php |
---|
124 | break; |
---|
125 | case 'mc': |
---|
126 | foreach ($question->answers as $answer) { |
---|
127 | ?> |
---|
128 | <input type="radio" name="<?php echo 'q' . $numQ . 'result1' ?>" value="<?php echo $answer; ?>" /><?php echo $answer; ?><br /> |
---|
129 | <?php |
---|
130 | } |
---|
131 | break; |
---|
132 | case 'checkboxes': |
---|
133 | foreach ($question->answers as $numA => $answer) { |
---|
134 | ?> |
---|
135 | <input type="checkbox" name="<?php echo 'q' . $numQ . 'result' . $numA; ?>" value="<?php echo $answer; ?>" /><?php echo $answer; ?><br /> |
---|
136 | <?php |
---|
137 | } |
---|
138 | |
---|
139 | break; |
---|
140 | case 'scale': |
---|
141 | ?> |
---|
142 | <div class="scaleLabel"><?php echo $question->answers[1]; ?></div> |
---|
143 | <?php |
---|
144 | for ($i = 1; $i < $question->answers[3] + 1; $i++) { |
---|
145 | ?><input type="radio" class="scaleRadio" name="<?php echo 'q' . $numQ . 'result1' ?>" value="<?php echo $i; ?>"/><?php } ?> |
---|
146 | <div class="scaleLabel"><?php echo $question->answers[2]; ?></div> |
---|
147 | <?php |
---|
148 | break; |
---|
149 | default: |
---|
150 | break; |
---|
151 | } |
---|
152 | echo "</div>"; |
---|
153 | } |
---|
154 | |
---|
155 | private function submitSurvey() { |
---|
156 | ?> |
---|
157 | <input type="submit" value="Submit survey!" id="submitSurvey" class="topMargin surveyButton bigSurveyButton"/> |
---|
158 | <?php |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|
162 | ?> |
---|