Changeset 116 for Dev/trunk/classes/QuestionCreationTool.php
- Timestamp:
- 09/28/11 15:06:23 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/trunk/classes/QuestionCreationTool.php
r115 r116 12 12 public function __construct($questions) { 13 13 $this->questions = $questions; 14 15 $this->loadQuestionsToJavascript(); 14 16 $this->javascript(); 15 17 $this->displayList(); 16 18 $this->displayEditPanel(); 19 } 20 21 /** 22 * Loads the questions that PHP retrieved from database 23 * into javascript. This is done so that the page does not need 24 * to refresh in order to load an entire question in to the form. 25 */ 26 private function loadQuestionsToJavascript() 27 { 28 17 29 } 18 30 … … 24 36 var type = select.valueOf().value; 25 37 clearSpecifications(); 26 38 27 39 /* set answerCount for options */ 28 40 document.getElementById("answerSpecifications").answerCount = 1; 29 41 30 42 switch (type) { 31 43 case "text": … … 44 56 break; 45 57 default: 46 58 47 59 break; 48 60 } 49 61 50 62 } 51 63 52 64 function clearSpecifications() 53 65 { … … 56 68 specs.clicked = null; 57 69 specs.lastAnswer = null; 58 70 59 71 var questionType = document.getElementById("questionType"); 60 72 var buttons = document.getElementById("questionButtons"); … … 62 74 questionType.removeChild(buttons); 63 75 } 64 76 65 77 function addOption() 66 78 { 67 79 var specs = document.getElementById("answerSpecifications"); 68 80 69 81 addAddRemoveOptionButtonsOnce(specs); 70 82 addAnswerInput(specs); 71 72 } 73 83 84 } 85 74 86 function removeOption() 75 87 { 76 88 var specs = document.getElementById("answerSpecifications"); 77 89 78 90 if(specs.lastAnswer.prev != null) 79 91 { … … 83 95 } 84 96 } 85 97 86 98 function addAnswerInput(specs) 87 99 { 88 100 var answerCount = specs.answerCount; 89 101 90 102 /* set value for option textbox */ 91 103 var optionStr = "Option " + answerCount; 92 104 93 105 /* the input textbox */ 94 106 var answerInput = document.createElement("input"); … … 97 109 answerInput.setAttribute("value", optionStr); 98 110 answerInput.className = "answerInput"; 99 111 100 112 specs.appendChild(answerInput); 101 113 102 114 /* Singly linked list */ 103 115 answerInput.prev = specs.lastAnswer; // remember last one 104 116 specs.lastAnswer = answerInput; // new lastAnswer 105 117 106 118 specs.answerCount++; 107 119 } 108 120 109 121 function addAddRemoveOptionButtonsOnce(specs) 110 122 { … … 116 128 var typeBox = document.getElementById("questionType"); 117 129 typeBox.appendChild(buttonsDiv); 118 130 119 131 addAddOptionButton(buttonsDiv); 120 132 addRemoveOptionButton(buttonsDiv); 121 133 122 134 specs.clicked = true; 123 135 } 124 136 } 125 137 126 138 function addAddOptionButton(buttonsDiv) 127 139 { … … 134 146 buttonsDiv.appendChild(addOpt); 135 147 } 136 148 137 149 function addRemoveOptionButton(buttonsDiv) 138 150 { … … 144 156 buttonsDiv.appendChild(removeOpt); 145 157 } 146 158 147 159 function minMax(min, max) 148 160 { … … 151 163 if (max == null) 152 164 var max = ''; 153 165 154 166 var specs = document.getElementById("answerSpecifications"); 155 167 156 168 var answerDiv = document.createElement("div"); 157 169 158 170 answerDiv.className = "answerDiv"; 159 171 answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' class='intBox' onchange='checkInt(this)' value='" + min + "' name='ans1' />" + 160 172 "<label for='max'>Max</label><input type='text' class='intBox' onchange='checkInt(this)' value='" + max + "' name='ans2' />"; 161 173 162 174 specs.appendChild(answerDiv); 163 175 } 164 176 165 177 function checkInt(input) 166 178 { … … 178 190 } 179 191 } 180 192 181 193 function minMaxIncr(left, right, incr) 182 194 { … … 191 203 if (incr == null) 192 204 var incr = ''; 193 205 194 206 var specs = document.getElementById("answerSpecifications"); 195 207 196 208 var answerDiv = document.createElement("div"); 197 209 answerDiv.className = "answerDiv"; 198 210 199 211 var leftLabel = document.createElement("label"); 200 212 var rightLabel = document.createElement("label"); … … 203 215 rightLabel.innerHTML = "Right label"; 204 216 scaleLabel.innerHTML = "Scale count"; 205 217 206 218 var leftInput = document.createElement("input"); 207 219 leftInput.type = "text"; … … 209 221 leftInput.name= "ans1"; 210 222 leftInput.setAttribute("onchange", "handleAnswerChange()"); 211 223 212 224 var rightInput = document.createElement("input"); 213 225 rightInput.type = "text"; … … 215 227 rightInput.name = "ans2"; 216 228 rightInput.setAttribute("onchange", "handleAnswerChange()"); 217 229 218 230 var scaleInput = document.createElement("input"); 219 231 scaleInput.type = "text"; … … 223 235 scaleInput.setAttribute("onchange", ""); 224 236 scaleInput.name = "ans3"; 225 237 226 238 answerDiv.appendChild(leftLabel); 227 239 answerDiv.appendChild(leftInput); … … 230 242 answerDiv.appendChild(scaleLabel); 231 243 answerDiv.appendChild(scaleInput); 232 244 233 245 specs.appendChild(answerDiv); 234 246 } 235 247 236 248 function newCategory() 237 249 { 238 250 var categoryBox = document.getElementById("categoryBox"); 239 251 categoryBox.innerHTML = ""; 240 252 241 253 var categoryInput = document.createElement("input"); 242 254 categoryInput.name = "questionCategory"; 243 255 244 256 categoryBox.appendChild(categoryInput); 245 257 } 246 258 247 259 function clearFields() 248 260 { … … 253 265 clearType(); 254 266 } 255 267 256 268 function clearCategory() 257 269 { 258 270 var categoryBox = document.getElementById("categoryBox"); 259 271 categoryBox.innerHTML = ""; 260 272 261 273 var questionCategorySelect = document.createElement("select"); 262 274 questionCategorySelect.setAttribute("name", "questionCategory"); 263 275 questionCategorySelect.id = "questionCategorySelect"; 264 276 265 277 var newCategoryButton = document.createElement("input"); 266 278 newCategoryButton.setAttribute("type", "button"); … … 268 280 newCategoryButton.setAttribute("value", "New Category"); 269 281 newCategoryButton.className = "surveyButton"; 270 282 271 283 categoryBox.appendChild(questionCategorySelect); 272 284 categoryBox.appendChild(newCategoryButton); 273 285 } 274 286 275 287 function clearCode() 276 288 { … … 278 290 codeInput.value = ""; 279 291 } 280 292 281 293 function clearTitle() 282 294 { … … 284 296 titleInput.value =""; 285 297 } 286 298 287 299 function clearDescription() 288 300 { … … 290 302 descriptionInput.value = ""; 291 303 } 292 304 293 305 function clearType() 294 306 { … … 297 309 clearSpecifications(); 298 310 } 299 311 300 312 </script> 301 313 <?php … … 306 318 <div id="questionListWrapper"> 307 319 <select id="questionsList" size="9999"> 308 <?php $this->populateList(); ?>320 <?php $this->populateList(); ?> 309 321 </select> 310 322 </div> … … 315 327 ?> 316 328 <div id="questionCreation" class="floatLeft"> 317 <?php $this->displayForm(); ?>329 <?php $this->displayForm(); ?> 318 330 </div> 319 331 <?php … … 327 339 <td><label for="questionCategory">Category</label></td> 328 340 <td id="categoryBox"><!-- Select should be filled with existing categories--> 329 <?php $this->questionCategorySelect(); ?>341 <?php $this->questionCategorySelect(); ?> 330 342 </td> 331 343 </tr> … … 372 384 <?php 373 385 } 374 375 private function generateCategoryOptions() 376 { 386 387 private function generateCategoryOptions() { 377 388 $categories = array(); 378 389 379 390 foreach ($this->questions as $question) { 380 391 $category = $question->category; 381 if (! array_search($category, $categories)) // if not in categories 382 { 383 array_push($categories, $category); 384 } 392 array_push($categories, $category); 385 393 } 386 394 $categories = array_unique($categories); 395 387 396 // You might want to escape quotes and stuff here 388 397 foreach ($categories as $category) { 389 398 ?> 390 <option value="<?php echo $category;?>"><?php echo $category; ?></option>391 <?php399 <option value="<?php echo $category; ?>"><?php echo $category; ?></option> 400 <?php 392 401 } 393 402 } 394 403 404 /** 405 * Populates the question list. Sorts the list first by question category. 406 */ 395 407 private function populateList() { 408 409 $this->questions = $this->sortByCategory($this->questions); 410 411 $currentCategory = null; 412 396 413 foreach ($this->questions as $question) { 414 if (is_null($currentCategory)) { 415 ?> <optgroup label="<?php echo $question->category; ?>"><?php 416 $currentCategory = $question->category; 417 } else if ($currentCategory != $question->category) { 418 /* Next category */ 419 ?></optgroup><?php 420 $currentCategory = $question->category; 421 ?> <optgroup label="<?php echo $question->category; ?>"><?php 422 } 397 423 ?> 398 <option value="<?php echo $question->code; ?>"> 399 <?php echo $question->code . " - " . $question->title; ?> 400 </option> 424 /* function loadQuestionIn() should still be made */ 425 <option value="<?php echo $question->code; ?>" onfocus="loadQuestionIn()"> 426 <?php echo "[" . $question->code . "] " . $question->title; ?> 427 </option> 428 401 429 <?php 402 430 } 403 } 404 405 } 431 ?></optgroup><?php 432 } 433 434 /** 435 * 436 * @param type $questions 437 */ 438 private function sortByCategory($questions) { 439 usort($questions, "cmp"); 440 return $questions; 441 } 442 443 } 444 445 /* This function is placed outside the class on purpose. 446 * It's only used as callback function. 447 */ 448 449 function cmp($q1, $q2) { 450 if ($q1->category == $q2->category) { 451 if($q1->code < $q2->code) 452 return -1; 453 else 454 return 1; 455 } 456 return ($q1->category < $q2->category) ? -1 : 1; 457 } 406 458 ?>
Note: See TracChangeset
for help on using the changeset viewer.