[10] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * Description of SurveyTool |
---|
| 5 | * |
---|
| 6 | * SurveyTool is where the survey can be created. |
---|
| 7 | * |
---|
| 8 | */ |
---|
| 9 | class SurveyCreationTool { |
---|
| 10 | |
---|
| 11 | public function __construct() { |
---|
[11] | 12 | SurveyCreationTool::javascript(); |
---|
| 13 | ?> |
---|
[17] | 14 | |
---|
[11] | 15 | <div id="surveyCreation"><form action="submitsurvey.php" method="post"> |
---|
| 16 | <?php |
---|
| 17 | SurveyCreationTool::titleBox(); |
---|
| 18 | SurveyCreationTool::descriptionBox(); |
---|
| 19 | SurveyCreationTool::questionCreationForm(); |
---|
[17] | 20 | SurveyCreationTool::addQuestionButton(); |
---|
[19] | 21 | SurveyCreationTool::removeLastQuestionButton(); |
---|
[11] | 22 | SurveyCreationTool::submitButton(); |
---|
| 23 | ?></form></div> |
---|
| 24 | <?php |
---|
[10] | 25 | } |
---|
| 26 | |
---|
[11] | 27 | private static function javascript() { |
---|
| 28 | ?> |
---|
| 29 | <script type="text/javascript"> |
---|
[17] | 30 | var questionCount = 1; |
---|
[20] | 31 | |
---|
[17] | 32 | function getNewQuestion() |
---|
| 33 | { |
---|
| 34 | var questionDiv = document.createElement("div"); |
---|
| 35 | var htmlStr = |
---|
| 36 | "<div id='question" + questionCount + "' class='question'>" + |
---|
| 37 | "<table class='questionTable'>" + |
---|
| 38 | "<th>Question " + questionCount + "</th>" + |
---|
| 39 | "<tr><td><label for='questionTitle'>Title</label></td>" + |
---|
| 40 | "<td><input type='text' class='questionTitle' name='questionTitle" + questionCount + "' size='30' value='Untitled Question' /></td></tr>" + |
---|
| 41 | "<tr><td><label for='questionDescription'>Description</label></td>" + |
---|
| 42 | "<td><input type='text' class='questionDescription' name='questionDescription" + questionCount + "' size='60' value='Write a question description here.' /></td>" + |
---|
| 43 | "<tr><td><label for='questionType'>Answer type</label></td>" + |
---|
| 44 | "<td><select id='" + questionCount + "' name='questionType" + questionCount + "' onchange='handleType(this)'><br />"+ |
---|
| 45 | "<option value='text' selected='selected'>Text</option>" + |
---|
[18] | 46 | "<option value='int'>Integer</option>" + |
---|
| 47 | "<option value='mc'>Multiple choice</option>" + |
---|
[17] | 48 | "<option value='checkboxes'>Checkboxes</option>" + |
---|
| 49 | "<option value='scale'>Scale</option>" + |
---|
| 50 | "</select></td></tr>" + |
---|
| 51 | "</table>" + |
---|
| 52 | "<div id='answersDiv" + questionCount + "'></div>" + |
---|
| 53 | "</div>"; |
---|
[20] | 54 | |
---|
[17] | 55 | questionDiv.innerHTML = htmlStr; |
---|
[20] | 56 | |
---|
[17] | 57 | return questionDiv; |
---|
| 58 | } |
---|
[20] | 59 | |
---|
[11] | 60 | function handleFocus(input) |
---|
| 61 | { |
---|
| 62 | if (input.clicked == null) |
---|
| 63 | { |
---|
| 64 | input.value = ""; |
---|
| 65 | input.style.color = "black"; |
---|
| 66 | input.clicked = true; |
---|
| 67 | } |
---|
| 68 | } |
---|
[20] | 69 | |
---|
[11] | 70 | function handleBlur(input) |
---|
[17] | 71 | { |
---|
| 72 | var surveyTitle = document.getElementById('surveyTitle'); |
---|
| 73 | var surveyDescription = document.getElementById('surveyDescription'); |
---|
[20] | 74 | |
---|
[11] | 75 | if (input.value == "") |
---|
| 76 | { |
---|
| 77 | input.style.color = "gray"; |
---|
| 78 | input.clicked = null; |
---|
[20] | 79 | |
---|
[17] | 80 | if (input == surveyTitle) |
---|
| 81 | { |
---|
| 82 | input.value = "Untitled Survey"; |
---|
| 83 | } |
---|
| 84 | else if (input == surveyDescription) |
---|
| 85 | { |
---|
| 86 | input.value = "Write a helpful description for this survey here."; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|
[20] | 90 | |
---|
[17] | 91 | function handleType(select) |
---|
| 92 | { |
---|
| 93 | var type = select.valueOf().value; |
---|
| 94 | var answersDiv = document.getElementById("answersDiv" + select.id); |
---|
| 95 | answersDiv.answerCount = 1; |
---|
| 96 | answersDiv.clicked = null; |
---|
[20] | 97 | |
---|
[17] | 98 | switch (type) { |
---|
[18] | 99 | case 'mc': |
---|
[17] | 100 | answersDiv.innerHTML = ""; |
---|
| 101 | addOption(select.id); |
---|
[20] | 102 | |
---|
[17] | 103 | break; |
---|
| 104 | case 'text': |
---|
| 105 | answersDiv.innerHTML = ""; |
---|
| 106 | break; |
---|
[18] | 107 | case 'int': |
---|
| 108 | answersDiv.innerHTML = ""; |
---|
| 109 | //min max |
---|
| 110 | minMax(select.id); |
---|
| 111 | break; |
---|
[17] | 112 | case 'checkboxes': |
---|
[18] | 113 | answersDiv.innerHTML = ""; |
---|
| 114 | addOption(select.id); |
---|
[17] | 115 | break; |
---|
| 116 | case 'scale': |
---|
[18] | 117 | answersDiv.innerHTML = ""; |
---|
| 118 | //what scale (min max incr) |
---|
[19] | 119 | minMaxIncr(select.id); |
---|
[17] | 120 | break; |
---|
| 121 | default: |
---|
| 122 | break; |
---|
[11] | 123 | } |
---|
[17] | 124 | |
---|
[11] | 125 | } |
---|
[20] | 126 | |
---|
[19] | 127 | |
---|
[17] | 128 | function addOption(questionNumber) |
---|
| 129 | { |
---|
| 130 | var answersDiv = document.getElementById("answersDiv" + questionNumber); |
---|
| 131 | var answerCount = answersDiv.answerCount; |
---|
| 132 | var answerDiv = document.createElement("div"); |
---|
| 133 | answerDiv.className = "answerDiv"; |
---|
[20] | 134 | |
---|
[17] | 135 | var htmlStr = "<input type='text' name='q" + |
---|
| 136 | questionNumber + "ans" + answerCount + "' value='Option " + answerCount + "' />"; |
---|
[20] | 137 | |
---|
[17] | 138 | if (answersDiv.clicked == null) |
---|
| 139 | { |
---|
| 140 | htmlStr += "<input type='button' id='addOpt'" |
---|
[20] | 141 | + " class='surveyButton' onclick='addOption(" + questionNumber + ")' value='Add Option' />" + |
---|
| 142 | "<input type='button' class='surveyButton' onclick='removeOption(" + questionNumber + ")' value='x' />"; |
---|
| 143 | |
---|
[17] | 144 | answersDiv.clicked = true; |
---|
| 145 | } |
---|
[20] | 146 | |
---|
[17] | 147 | answerDiv.innerHTML = htmlStr; |
---|
[20] | 148 | |
---|
| 149 | answerDiv.prev = answersDiv.lastAnswer; //singly linked list |
---|
| 150 | answersDiv.lastAnswer = answerDiv; |
---|
| 151 | |
---|
[17] | 152 | answersDiv.appendChild(answerDiv); |
---|
| 153 | answersDiv.answerCount++; |
---|
| 154 | } |
---|
[20] | 155 | |
---|
| 156 | function removeOption(questionNumber) |
---|
| 157 | { |
---|
| 158 | var answersDiv = document.getElementById("answersDiv" + questionNumber); |
---|
| 159 | |
---|
| 160 | if (answersDiv.lastAnswer.prev != null) |
---|
| 161 | { |
---|
| 162 | answersDiv.removeChild(answersDiv.lastAnswer); |
---|
| 163 | answersDiv.lastAnswer = answersDiv.lastAnswer.prev; |
---|
| 164 | answersDiv.answerCount--; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
[18] | 168 | function minMax(questionNumber) |
---|
| 169 | { |
---|
[19] | 170 | var answersDiv = document.getElementById("answersDiv" + questionNumber); |
---|
[20] | 171 | |
---|
[19] | 172 | var answerDiv = document.createElement("div"); |
---|
[21] | 173 | |
---|
[19] | 174 | answerDiv.className = "answerDiv"; |
---|
| 175 | answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' name='min" + questionNumber + "' />" + |
---|
| 176 | "<label for='max'>Max</label><input type='text' name='max" + questionNumber + "' />"; |
---|
[20] | 177 | |
---|
[18] | 178 | answersDiv.appendChild(answerDiv); |
---|
[19] | 179 | } |
---|
[20] | 180 | |
---|
[19] | 181 | function minMaxIncr(questionNumber) |
---|
| 182 | { |
---|
| 183 | minMax(questionNumber); |
---|
| 184 | var answersDiv = document.getElementById("answersDiv" + questionNumber); |
---|
[20] | 185 | |
---|
[19] | 186 | var answerDiv = document.createElement("div"); |
---|
| 187 | answerDiv.className = "answerDiv"; |
---|
| 188 | answerDiv.innerHTML = "<label for='incr'>Increment by</label><input type='text' name='incr" + questionNumber + "' />"; |
---|
[20] | 189 | |
---|
[19] | 190 | answersDiv.appendChild(answerDiv); |
---|
| 191 | } |
---|
| 192 | |
---|
[20] | 193 | |
---|
[17] | 194 | function addQuestion() |
---|
| 195 | { |
---|
| 196 | var questionsDiv = document.getElementById('questionsDiv'); |
---|
| 197 | var newQuestion = getNewQuestion(); |
---|
[20] | 198 | |
---|
[19] | 199 | |
---|
| 200 | newQuestion.prev = document.lastQuestion; |
---|
| 201 | document.lastQuestion = newQuestion; |
---|
| 202 | |
---|
[17] | 203 | questionsDiv.appendChild(newQuestion); |
---|
| 204 | questionCount++; |
---|
| 205 | } |
---|
[20] | 206 | |
---|
[19] | 207 | function removeLastQuestion() |
---|
| 208 | { |
---|
| 209 | var questionsDiv = document.getElementById('questionsDiv'); |
---|
[20] | 210 | |
---|
[19] | 211 | if (document.lastQuestion.prev != null) |
---|
| 212 | { |
---|
| 213 | questionsDiv.removeChild(document.lastQuestion); |
---|
| 214 | document.lastQuestion = document.lastQuestion.prev; |
---|
| 215 | questionCount--; |
---|
| 216 | } |
---|
| 217 | else |
---|
| 218 | { |
---|
| 219 | // do nothing |
---|
| 220 | } |
---|
| 221 | } |
---|
[20] | 222 | |
---|
| 223 | |
---|
| 224 | |
---|
[11] | 225 | </script> |
---|
| 226 | <?php |
---|
| 227 | } |
---|
| 228 | |
---|
[10] | 229 | private static function titleBox() { |
---|
[11] | 230 | ?> |
---|
[17] | 231 | <input type="text" id="surveyTitle" name="surveyTitle" value="Untitled Survey" size="30" onblur="handleBlur(this)" onfocus="handleFocus(this)" /> |
---|
[11] | 232 | <?php |
---|
[10] | 233 | } |
---|
[11] | 234 | |
---|
| 235 | private static function descriptionBox() { |
---|
| 236 | ?> |
---|
[17] | 237 | <textarea id="surveyDescription" rows="3" cols="80" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)">Write a helpful description for this survey here.</textarea> |
---|
[11] | 238 | <?php |
---|
[10] | 239 | } |
---|
[11] | 240 | |
---|
| 241 | private static function questionCreationForm() { |
---|
[17] | 242 | ?> |
---|
| 243 | <div id='questionsDiv'> |
---|
| 244 | </div> |
---|
| 245 | <script type="text/javascript"> addQuestion(); </script> |
---|
| 246 | <?php |
---|
[11] | 247 | } |
---|
| 248 | |
---|
| 249 | private static function addQuestionButton() { |
---|
[17] | 250 | ?> |
---|
| 251 | <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" /> |
---|
| 252 | <?php |
---|
[10] | 253 | } |
---|
| 254 | |
---|
[19] | 255 | private static function removeLastQuestionButton() { |
---|
| 256 | ?> |
---|
| 257 | <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" /> |
---|
| 258 | <?php |
---|
| 259 | } |
---|
| 260 | |
---|
[11] | 261 | private static function submitbutton() { |
---|
| 262 | ?> |
---|
| 263 | <input type="submit" name="submitSurvey" |
---|
[17] | 264 | value="Submit Survey!" class="surveyButton" id="submitSurvey"/> |
---|
[11] | 265 | <?php |
---|
| 266 | } |
---|
| 267 | |
---|
[10] | 268 | } |
---|
| 269 | ?> |
---|