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