[51] | 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 | |
---|
[54] | 15 | $this->javascript(); |
---|
[51] | 16 | $this->init(); |
---|
| 17 | } |
---|
| 18 | |
---|
[54] | 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 | |
---|
[51] | 52 | private function init() { |
---|
| 53 | ?> |
---|
[54] | 54 | <form name="survey" action="" onsubmit="return checksPassed(this)" method="post"> |
---|
[51] | 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 | ?> |
---|
[52] | 73 | <p class='centerBoxed'><?php echo $this->survey->description; ?></p> |
---|
[51] | 74 | <?php |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | private function displayQuestions() { |
---|
| 78 | foreach ($this->survey->questions as $numQ => $question) { |
---|
| 79 | |
---|
| 80 | $this->displayQuestion($question, $numQ); |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | private function displayQuestion($question, $numQ) { |
---|
[52] | 85 | echo "<fieldset>"; |
---|
[51] | 86 | $this->displayQuestionTitle($question); |
---|
| 87 | $this->displayQuestionDescription($question); |
---|
| 88 | $this->displayAnswerBox($question, $numQ); |
---|
[52] | 89 | echo "</fieldset>"; |
---|
[51] | 90 | } |
---|
| 91 | |
---|
| 92 | private function displayQuestionTitle($question) { |
---|
| 93 | ?> |
---|
[52] | 94 | <?php echo "<legend><h2>" . $question->title . "</h2></legend>"; ?> |
---|
[51] | 95 | <?php |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | private function displayQuestionDescription($question) { |
---|
| 99 | ?> |
---|
[52] | 100 | <p><?php echo "<div class='questionDescription noPadding'>" . $question->description . "</div>"; ?></p> |
---|
[51] | 101 | <?php |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | private function displayAnswerBox($question, $numQ) { |
---|
[52] | 105 | echo "<div class='answerBox'>"; |
---|
[51] | 106 | switch ($question->type) { |
---|
| 107 | case 'text': |
---|
[52] | 108 | ?> |
---|
[55] | 109 | <input type="text" name="<?php echo 'q' . $numQ . 'result1' ?>" value="" class="textBox"/> |
---|
[52] | 110 | <?php |
---|
[51] | 111 | break; |
---|
| 112 | case 'int': |
---|
[52] | 113 | ?> |
---|
[55] | 114 | <input type="text" name="<?php echo 'q' . $numQ . 'result1' ?>" value="" onchange="checkInt(this)" class="intBox"/> |
---|
[52] | 115 | <?php |
---|
[51] | 116 | break; |
---|
| 117 | case 'mc': |
---|
| 118 | foreach ($question->answers as $answer) { |
---|
| 119 | ?> |
---|
[55] | 120 | <input type="radio" name="<?php echo 'q' . $numQ . 'result1' ?>" value="<?php echo $answer; ?>" /><?php echo $answer; ?><br /> |
---|
[51] | 121 | <?php |
---|
| 122 | } |
---|
| 123 | break; |
---|
| 124 | case 'checkboxes': |
---|
| 125 | foreach ($question->answers as $numA => $answer) { |
---|
| 126 | ?> |
---|
[55] | 127 | <input type="checkbox" name="<?php echo 'q' . $numQ . 'result' . $numA; ?>" value="<?php echo $answer; ?>" /><?php echo $answer; ?><br /> |
---|
[51] | 128 | <?php |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | break; |
---|
| 132 | case 'scale': |
---|
[52] | 133 | ?> |
---|
| 134 | <div class="scaleLabel"><?php echo $question->answers[1]; ?></div> |
---|
| 135 | <?php |
---|
| 136 | for ($i = 1; $i < $question->answers[3] + 1; $i++) { |
---|
[55] | 137 | ?><input type="radio" class="scaleRadio" name="<?php echo 'q' . $numQ . 'result1' ?>" value="<?php echo $i; ?>"/><?php } ?> |
---|
[52] | 138 | <div class="scaleLabel"><?php echo $question->answers[2]; ?></div> |
---|
| 139 | <?php |
---|
[51] | 140 | break; |
---|
| 141 | default: |
---|
| 142 | break; |
---|
| 143 | } |
---|
[52] | 144 | echo "</div>"; |
---|
[51] | 145 | } |
---|
| 146 | |
---|
| 147 | private function submitSurvey() { |
---|
| 148 | ?> |
---|
[52] | 149 | <input type="submit" value="Submit survey!" class="topMargin surveyButton bigSurveyButton"/> |
---|
[51] | 150 | <?php |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | } |
---|
| 154 | ?> |
---|