Changeset 116 for Dev


Ignore:
Timestamp:
09/28/11 15:06:23 (14 years ago)
Author:
fpvanagthoven
Message:

Some refactoring and commenting, plus, the questions within the categories in QuestionCreationTool? are now sorted aswell; on code.

Location:
Dev/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/classes/Loader.php

    r114 r116  
    22
    33/**
    4  * Helper class to load stuff
     4 * Helper class to load arrays of objects from the database.
    55 *
    66 * @author fpvanagthoven
  • Dev/trunk/classes/QuestionCreationTool.php

    r115 r116  
    1212    public function __construct($questions) {
    1313        $this->questions = $questions;
     14
     15        $this->loadQuestionsToJavascript();
    1416        $this->javascript();
    1517        $this->displayList();
    1618        $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       
    1729    }
    1830
     
    2436                var type = select.valueOf().value;
    2537                clearSpecifications();
    26                                                                                
     38                                                                                                       
    2739                /* set answerCount for options */
    2840                document.getElementById("answerSpecifications").answerCount = 1;
    29                                                                                
     41                                                                                                       
    3042                switch (type) {
    3143                    case "text":
     
    4456                        break;
    4557                    default:
    46                                                                                        
     58                                                                                                               
    4759                        break;
    4860                }
    4961
    5062            }
    51                                                                            
     63                                                                                                   
    5264            function clearSpecifications()
    5365            {
     
    5668                specs.clicked = null;
    5769                specs.lastAnswer = null;
    58                                                                
     70                                                                                       
    5971                var questionType = document.getElementById("questionType");
    6072                var buttons = document.getElementById("questionButtons");
     
    6274                    questionType.removeChild(buttons);
    6375            }
    64                                                                            
     76                                                                                                   
    6577            function addOption()
    6678            {                       
    6779                var specs = document.getElementById("answerSpecifications");
    68                                                                                
     80                                                                                                       
    6981                addAddRemoveOptionButtonsOnce(specs);
    7082                addAnswerInput(specs);
    71                                                                                
    72             }
    73                                                                            
     83                                                                                                       
     84            }
     85                                                                                                   
    7486            function removeOption()
    7587            {
    7688                var specs = document.getElementById("answerSpecifications");
    77                                                                                
     89                                                                                                       
    7890                if(specs.lastAnswer.prev != null)
    7991                {
     
    8395                }
    8496            }
    85                                                                            
     97                                                                                                   
    8698            function addAnswerInput(specs)
    8799            {
    88100                var answerCount = specs.answerCount;
    89                                                                                
     101                                                                                                       
    90102                /* set value for option textbox */
    91103                var optionStr = "Option " + answerCount;
    92                                                                                
     104                                                                                                       
    93105                /* the input textbox */
    94106                var answerInput = document.createElement("input");
     
    97109                answerInput.setAttribute("value", optionStr);
    98110                answerInput.className = "answerInput";
    99                                                                                
     111                                                                                                       
    100112                specs.appendChild(answerInput);
    101                                                                                
     113                                                                                                       
    102114                /* Singly linked list */
    103115                answerInput.prev = specs.lastAnswer; // remember last one
    104116                specs.lastAnswer = answerInput; // new lastAnswer
    105                                                                                
     117                                                                                                       
    106118                specs.answerCount++;               
    107119            }
    108                                                                            
     120                                                                                                   
    109121            function addAddRemoveOptionButtonsOnce(specs)
    110122            {
     
    116128                    var typeBox = document.getElementById("questionType");
    117129                    typeBox.appendChild(buttonsDiv);
    118                                                                    
     130                                                                                           
    119131                    addAddOptionButton(buttonsDiv);                                 
    120132                    addRemoveOptionButton(buttonsDiv);
    121                                                                                    
     133                                                                                                           
    122134                    specs.clicked = true;
    123135                }
    124136            }
    125                                                                            
     137                                                                                                   
    126138            function addAddOptionButton(buttonsDiv)
    127139            {   
     
    134146                buttonsDiv.appendChild(addOpt);                                 
    135147            }
    136                                                                            
     148                                                                                                   
    137149            function addRemoveOptionButton(buttonsDiv)
    138150            {
     
    144156                buttonsDiv.appendChild(removeOpt);
    145157            }
    146                                                                            
     158                                                                                                  
    147159            function minMax(min, max)
    148160            {
     
    151163                if (max == null)
    152164                    var max = '';
    153                                                                                                                                                                                                                                                                                                        
     165                                                                                                                                                                                                                                                                                                                               
    154166                var specs = document.getElementById("answerSpecifications");
    155                                                                                                                                                                                                                                                                                                                                                                                                                                                
     167                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
    156168                var answerDiv = document.createElement("div");
    157                                                                                                                                                                                                                                                                                                                                                                                                                
     169                                                                                                                                                                                                                                                                                                                                                                                                                                       
    158170                answerDiv.className = "answerDiv";
    159171                answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' class='intBox' onchange='checkInt(this)' value='" + min + "' name='ans1' />" +
    160172                    "<label for='max'>Max</label><input type='text' class='intBox' onchange='checkInt(this)' value='" + max + "' name='ans2' />";
    161                                                                                                                                                                                                                                                                                                                                                                                                                                            
     173                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    162174                specs.appendChild(answerDiv);
    163175            }     
    164                                                    
     176                                                                           
    165177            function checkInt(input)
    166178            {
     
    178190                }
    179191            }
    180                                    
     192                                                           
    181193            function minMaxIncr(left, right, incr)
    182194            {
     
    191203                if (incr == null)
    192204                    var incr = '';
    193                                                                                                                                                                                                                                
     205                                                                                                                                                                                                                                                       
    194206                var specs = document.getElementById("answerSpecifications");
    195                                                                                                                                                                                                                                                                                                                                                                                                                        
     207                                                                                                                                                                                                                                                                                                                                                                                                                                              
    196208                var answerDiv = document.createElement("div");
    197209                answerDiv.className = "answerDiv";
    198                                                                                                                                                        
     210                                                                                                                                                                              
    199211                var leftLabel = document.createElement("label");
    200212                var rightLabel = document.createElement("label");
     
    203215                rightLabel.innerHTML = "Right label";
    204216                scaleLabel.innerHTML = "Scale count";
    205                                                                                                                                                        
     217                                                                                                                                                                               
    206218                var leftInput = document.createElement("input");
    207219                leftInput.type = "text";
     
    209221                leftInput.name= "ans1";
    210222                leftInput.setAttribute("onchange", "handleAnswerChange()");
    211                                                                                                                                                        
     223                                                                                                                                                                               
    212224                var rightInput = document.createElement("input");
    213225                rightInput.type = "text";
     
    215227                rightInput.name = "ans2";
    216228                rightInput.setAttribute("onchange", "handleAnswerChange()");
    217                                                                                                                                                        
     229                                                                                                                                                                               
    218230                var scaleInput = document.createElement("input");
    219231                scaleInput.type = "text";
     
    223235                scaleInput.setAttribute("onchange", "");
    224236                scaleInput.name = "ans3";
    225                                                                                                                                                        
     237                                                                                                                                                                               
    226238                answerDiv.appendChild(leftLabel);
    227239                answerDiv.appendChild(leftInput);
     
    230242                answerDiv.appendChild(scaleLabel);
    231243                answerDiv.appendChild(scaleInput);
    232                                                                                                                                                                                                                                                                                                                                                                                                    
     244                                                                                                                                                                                                                                                                                                                                                                                                                          
    233245                specs.appendChild(answerDiv);
    234246            }   
    235                    
     247                                           
    236248            function newCategory()
    237249            {
    238250                var categoryBox = document.getElementById("categoryBox");
    239251                categoryBox.innerHTML = "";
    240                        
     252                                               
    241253                var categoryInput = document.createElement("input");
    242254                categoryInput.name = "questionCategory";
    243                        
     255                                               
    244256                categoryBox.appendChild(categoryInput);
    245257            }
    246                                
     258                                                      
    247259            function clearFields()
    248260            {
     
    253265                clearType();
    254266            }
    255                    
     267                                           
    256268            function clearCategory()
    257269            {
    258270                var categoryBox = document.getElementById("categoryBox");
    259271                categoryBox.innerHTML = "";
    260                        
     272                                               
    261273                var questionCategorySelect = document.createElement("select");
    262274                questionCategorySelect.setAttribute("name", "questionCategory");
    263275                questionCategorySelect.id = "questionCategorySelect";
    264                        
     276                                               
    265277                var newCategoryButton = document.createElement("input");
    266278                newCategoryButton.setAttribute("type", "button");
     
    268280                newCategoryButton.setAttribute("value", "New Category");
    269281                newCategoryButton.className = "surveyButton";
    270                        
     282                                               
    271283                categoryBox.appendChild(questionCategorySelect);
    272284                categoryBox.appendChild(newCategoryButton);
    273285            }
    274                    
     286                                           
    275287            function clearCode()
    276288            {
     
    278290                codeInput.value = "";
    279291            }
    280                    
     292                                           
    281293            function clearTitle()
    282294            {
     
    284296                titleInput.value ="";
    285297            }
    286                    
     298                                           
    287299            function clearDescription()
    288300            {
     
    290302                descriptionInput.value = "";
    291303            }
    292                    
     304                                           
    293305            function clearType()
    294306            {
     
    297309                clearSpecifications();
    298310            }
    299                    
     311                                           
    300312        </script>
    301313        <?php
     
    306318        <div id="questionListWrapper">
    307319            <select id="questionsList" size="9999">
    308         <?php $this->populateList(); ?>
     320                <?php $this->populateList(); ?>
    309321            </select>
    310322        </div>
     
    315327        ?>
    316328        <div id="questionCreation" class="floatLeft">
    317         <?php $this->displayForm(); ?>
     329            <?php $this->displayForm(); ?>
    318330        </div>
    319331        <?php
     
    327339                    <td><label for="questionCategory">Category</label></td>
    328340                    <td id="categoryBox"><!-- Select should be filled with existing categories-->
    329         <?php $this->questionCategorySelect(); ?>
     341                        <?php $this->questionCategorySelect(); ?>
    330342                    </td>
    331343                </tr>
     
    372384        <?php
    373385    }
    374    
    375     private function generateCategoryOptions()
    376     {
     386
     387    private function generateCategoryOptions() {
    377388        $categories = array();
    378        
     389
    379390        foreach ($this->questions as $question) {
    380391            $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);
    385393        }
    386        
     394        $categories = array_unique($categories);
     395
    387396        // You might want to escape quotes and stuff here
    388397        foreach ($categories as $category) {
    389398            ?>
    390         <option value="<?php echo $category;?>"><?php echo $category; ?></option>
    391         <?php
     399            <option value="<?php echo $category; ?>"><?php echo $category; ?></option>
     400            <?php
    392401        }
    393402    }
    394403
     404    /**
     405     * Populates the question list. Sorts the list first by question category.
     406     */
    395407    private function populateList() {
     408
     409        $this->questions = $this->sortByCategory($this->questions);
     410
     411        $currentCategory = null;
     412
    396413        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            }
    397423            ?>
    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
    401429                <?php
    402430            }
    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
     449function 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}
    406458    ?>
  • Dev/trunk/mainmenu.php

    r112 r116  
    22require 'classes/master.php'; //should be at top of every page   
    33     
    4 //$_SESSION['username'] = 'hai'; //temp, until bug is fixed
    5 
    64if (isset($_POST['login'])) {
    75    if ($_POST['username'] == '')
  • Dev/trunk/questioncreation.php

    r115 r116  
    2626
    2727            <div id="content">
    28                 <?php var_dump($_POST); ?>
     28                <?php var_dump($_POST);      ?>
    2929                <?php
    3030                    new QuestionCreationTool($questions);
Note: See TracChangeset for help on using the changeset viewer.