source: Dev/trunk/classes/SurveyCreationTool.php @ 37

Last change on this file since 37 was 37, checked in by fpvanagthoven, 14 years ago

When answers change, the questionID gets removed so that a new one is made.

File size: 27.7 KB
RevLine 
[10]1<?php
2
3/**
4 * Description of SurveyTool
5 *
6 * SurveyTool is where the survey can be created.
7 *
8 */
9class SurveyCreationTool {
10
[29]11    private $survey;
12    private $timeStamp;
13
[24]14    public function __construct($survey = null, $timeStamp = null) {
[29]15        $this->survey = $survey;
16        $this->timeStamp = $timeStamp;
17
[32]18        if (isset($this->survey->id))
[30]19            SurveyCreationTool::javascript($this->survey->id);
20        else
21            SurveyCreationTool::javascript();
[11]22        ?>
[17]23
[24]24        <div id="surveyCreation"><form id="survey" action="submitsurvey.php" method="post">
[11]25                <?php
[29]26                $this->surveyHead();
27                $this->questionCreationForm();
[17]28                SurveyCreationTool::addQuestionButton();
[19]29                SurveyCreationTool::removeLastQuestionButton();
[11]30                SurveyCreationTool::submitButton();
31                ?></form></div>
32        <?php
[10]33    }
34
[30]35    private static function javascript($id = null) {
[11]36        ?>
37        <script type="text/javascript">
[24]38            /* autosave every 3 minutes */
[30]39            setTimeout("save('<?php echo $id; ?>')", 180000);
[36]40                                                                                                                                                                           
[30]41            var questionCount = 1;         
[36]42                                                                                                                                                                                                                                                                                                                   
[30]43            function getNewQuestion(title, description)
[17]44            {
[33]45                if (title == null)
[30]46                    var title = 'Untitled Question';
[36]47                                                                                                               
[30]48                if (description != null)
49                    var description = description;
50                else
51                    var description = 'Write a question description here.';
[36]52                                                                                                                                   
[30]53                var questionDiv = document.createElement("div");
54                var htmlStr =
55                    "<div id='question" + questionCount + "' class='question'>" +
56                    "<table class='questionTable'>" +
57                    "<th>Question " + questionCount + "</th>" +
58                    "<tr><td><label for='questionTitle'>Title</label></td>" +
59                    "<td><input type='text' class='questionTitle' name='questionTitle" + questionCount + "' onfocus='handleFocus(this)' size='30' value='" + title + "' /></td></tr>" +
60                    "<tr><td><label for='questionDescription'>Description</label></td>" +
61                    "<td><input type='text' class='questionDescription' name='questionDescription" + questionCount + "' onfocus='handleFocus(this)' size='60' value='" + description + "' /></td>" +
62                    "<tr><td><label for='questionType'>Answer type</label></td>" +
63                    "<td><select id='" + questionCount + "' name='questionType" + questionCount + "' onchange='handleType(this)'><br />"+
64                    "<option value='text' selected='selected'>Text</option>" +
65                    "<option value='int'>Integer</option>" +
66                    "<option value='mc'>Multiple choice</option>" +
67                    "<option value='checkboxes'>Checkboxes</option>" +
68                    "<option value='scale'>Scale</option>" +
69                    "</select></td></tr>" +
70                    "</table>" +
71                    "<div id='answersDiv" + questionCount + "' class='answersDiv'></div>" +
72                    "</div>";
[36]73                                                                                                                                                                                                                                                                                                               
[30]74                questionDiv.innerHTML = htmlStr;
[36]75                                                                                                                                                                                                                                                                                                               
[30]76                return questionDiv;
[17]77            }
[36]78                                                                                                                                                                                                                                                                                                                   
[30]79            function handleFocus(input)
[11]80            {
[34]81                /* this is such ugly code it makes me sad */
82                if (input.clicked == null &&
83                    (input.value == "Untitled Survey"
[35]84                    || input.value == "Write a helpful description for this survey here."
85                    || input.value == "Write a question description here."
86                    || input.value == "Untitled Question"))
[30]87                {
88                    input.value = "";
89                    input.style.color = "black";   
90                    input.clicked = true;
91                }
92            }
[36]93                                                                                                                                                                                                                                                                                                                                                   
[30]94            function handleBlur(input)
95            {       
96                var surveyTitle = document.getElementById('surveyTitle');
97                var surveyDescription = document.getElementById('surveyDescription');
[36]98                                                                                                                                                                                                                                                                                                                                   
[30]99                if (input.value == "")
[11]100                {
[30]101                    input.style.color = "gray";
102                    input.clicked = null;
[36]103                                                                                                                                                                                                                                                                                                                                                           
[30]104                    if (input == surveyTitle)
105                    {
106                        input.value = "Untitled Survey";
107                    }
108                    else if (input == surveyDescription)
109                    {
110                        input.value = "Write a helpful description for this survey here.";
111                    }
112                }                           
113            }
[36]114                                                                                                                                                                                                                                                                                                   
[32]115            function handleType(select, answers)
[30]116            {
[32]117                var numQ = select.id; // questionNumber
[30]118                var type = select.valueOf().value;
[32]119                var answersDiv = document.getElementById("answersDiv" + numQ );
[36]120               
[37]121                removeQuestionID(numQ);
122               
[30]123                answersDiv.answerCount = 1;
124                answersDiv.clicked = null;
[36]125                                                                                                                                                                                                                                                       
[30]126                switch (type) {
127                    case 'mc':
128                        answersDiv.innerHTML = "";
[32]129                        if (answers != null)
130                        {
131                            for (var i = 1; i < answers.length; i++)
132                            {
133                                addOption(numQ, answers[i]);
134                            }
135                        }
136                        else
137                            addOption(numQ);
[36]138                                                                                                                                                                                                                                                                                       
[30]139                        break;
140                    case 'text':
141                        answersDiv.innerHTML = "";
142                        break;
143                    case 'int':   
144                        answersDiv.innerHTML = "";
[36]145                                                                                                               
[30]146                        //min max
[32]147                        if (answers != null)
148                            minMax(numQ, answers[1], answers[2]);
149                        else
150                            minMax(numQ);
[30]151                        break;
152                    case 'checkboxes':
[32]153                        answersDiv.innerHTML = "";
154                        if (answers != null)
155                        {
156                            for (var i = 1; i < answers.length; i++)
157                            {
158                                addOption(numQ, answers[i]);
159                            }
160                        }
161                        else
162                            addOption(numQ);
[30]163                        break;
164                    case 'scale':
165                        answersDiv.innerHTML = "";
166                        //what scale (min max incr)
[32]167                        if (answers != null)
168                            minMaxIncr(numQ, answers[1], answers[2], answers[3]);
169                        else
170                            minMaxIncr(numQ);
[30]171                        break;
172                    default:
173                        break;
[11]174                }
[30]175
176            }
[37]177           
178            function handleAnswerChange(questionNumber)
179            {
180                removeQuestionID(questionNumber);
181            }
182           
183            function removeQuestionID(questionNumber)
184            {
185                /* When type changes, remove question ID, because question changes */
186                var questionIDInput = document.getElementById("questionID" + questionNumber);
187                if (questionIDInput != null)
188                {
189                    var questionsDiv = document.getElementById("questionsDiv");
190                    questionsDiv.removeChild(questionIDInput);
191                }
192
193            }
194                     
[36]195                                                                                                                                                                                                                                                                           
[32]196            function addOption(questionNumber, optionStr)
[30]197            {       
198                var answersDiv = document.getElementById("answersDiv" + questionNumber);
199                var answerCount = answersDiv.answerCount;
200                var answerDiv = document.createElement("div");
[36]201                                               
[32]202                if (optionStr == null)
203                    var optionStr = "Option " + answerCount;
[36]204                                               
205                                               
[30]206                answerDiv.className = "answerDiv";
[36]207                                                                                                                                                                                                                                                                               
[30]208                var htmlStr = "<input type='text' name='q" +
[37]209                    questionNumber + "ans" + answerCount + "' onchange='handleAnswerChange(" + questionNumber + ")' value='" + optionStr + "' />";
[36]210                                                                                                                                                                                                                                                       
[30]211                if (answersDiv.clicked == null)
[11]212                {
[30]213                    htmlStr += "<input type='button' id='addOpt'"
214                        + " class='surveyButton' onclick='addOption(" + questionNumber + ")' value='Add Option' />" +
215                        "<input type='button' class='surveyButton' onclick='removeOption(" + questionNumber + ")' value='x' />";
[36]216                                                                                                                                                                                                                           
[30]217                    answersDiv.clicked = true;
[25]218                }
[36]219                                                                                                                                                                                                                                                                                       
[30]220                answerDiv.innerHTML = htmlStr;
[36]221                                                                                                                                                                                                                       
[30]222                answerDiv.prev = answersDiv.lastAnswer; //singly linked list
223                answersDiv.lastAnswer = answerDiv;
[36]224                                                                                                                                                                                                                                                                                       
[30]225                answersDiv.appendChild(answerDiv);
[37]226                answersDiv.answerCount
227               
228                handleAnswerChange(questionNumber);
[17]229            }
[36]230                                                                                                                                                                                                                   
[30]231            function removeOption(questionNumber)
[17]232            {
[30]233                var answersDiv = document.getElementById("answersDiv" + questionNumber);
[36]234                                                                                                                                                                                                                       
[30]235                if (answersDiv.lastAnswer.prev != null)
236                {
237                    answersDiv.removeChild(answersDiv.lastAnswer);
238                    answersDiv.lastAnswer = answersDiv.lastAnswer.prev;
239                    answersDiv.answerCount--;
240                }
[37]241               
242                handleAnswerChange(questionNumber);
[11]243            }
[36]244                                                                                                                                                                                                                                           
[32]245            function minMax(questionNumber, min, max)
[20]246            {
[32]247                if (min == null)
248                    var min = '';
249                if (max == null)
250                    var max = '';
[36]251                                                                                                       
[30]252                var answersDiv = document.getElementById("answersDiv" + questionNumber);
[36]253                                                                                                                                                                                                                                               
[30]254                var answerDiv = document.createElement("div");
[36]255                                                                                                                                                                                                               
[30]256                answerDiv.className = "answerDiv";
[32]257                answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' value='" + min + "' name='q" + questionNumber + "ans1' />" +
258                    "<label for='max'>Max</label><input type='text' value='" + max + "' name='q" + questionNumber + "ans2' />";
[36]259                                                                                                                                                                                                                                           
[30]260                answersDiv.appendChild(answerDiv);
261            } 
[36]262                                                                                                                                                                                                                                           
[32]263            function minMaxIncr(questionNumber, left, right, incr)
[30]264            {
[32]265                if (left == null)
266                    var left = '';
267                if (right == null)
268                    var right = '';
269                if (incr == null)
270                    var incr = '';
[36]271                                                       
[30]272                var answersDiv = document.getElementById("answersDiv" + questionNumber);
[36]273                                                                                                                                                                                                                                               
[30]274                var answerDiv = document.createElement("div");
275                answerDiv.className = "answerDiv";
[37]276                answerDiv.innerHTML = "<label>Left label</label><input type='text' value='" + left + "' onchange='handleAnswerChange(" + questionNumber + ")' name='q" + questionNumber + "ans1' />" +
277                    "<label>Right label</label><input type='text' value='" + right + "' onchange='handleAnswerChange(" + questionNumber + ")' name='q" + questionNumber + "ans2' />" +
278                    "<label>Scale count</label><input type='text' value='" + incr + "' onchange='handleAnswerChange(" + questionNumber + ")' name='q" + questionNumber + "ans3' />" +
[30]279                    "<div id='inputCheckFeedback'" + questionNumber + "";
[36]280                                                                                                                                                                                                                                           
[30]281                answersDiv.appendChild(answerDiv);
282            }         
[36]283                                                                                                                                                                                                                                                                                                 
284                                                                                                                                                                                                                                                                                                         
[30]285            function addQuestion(title, description)
286            {
287                var questionsDiv = document.getElementById('questionsDiv');
288                var newQuestion = getNewQuestion(title, description);
[36]289                                                                                                                                                                                                                                       
[19]290
[30]291                newQuestion.prev = document.lastQuestion;
292                document.lastQuestion = newQuestion;
[19]293
[30]294                questionsDiv.appendChild(newQuestion);
295                questionCount++;
296            }
[36]297                                                                                                                                                                                                                                   
[30]298            function removeLastQuestion()
[19]299            {
[30]300                var questionsDiv = document.getElementById('questionsDiv');
[36]301                                                                                                                                                                                                                                       
[30]302                if (document.lastQuestion.prev != null)
303                {
304                    questionsDiv.removeChild(document.lastQuestion);
305                    document.lastQuestion = document.lastQuestion.prev;
306                    questionCount--; 
307                }
308                else
309                {
310                    // do nothing
311                }               
[19]312            }
[36]313                                                                                                                                                                                                             
[30]314            function save(surveyID)
[24]315            {
[30]316                var form = document.getElementById('survey');
317                var questionsDiv = document.getElementById('questionsDiv');
318                form.action = "surveycreation.php";
[36]319                                                                                                                                                                               
[30]320                /* extra time stamp */
321                var date = new Date();
322                var minutes = date.getUTCMinutes();
323                if (minutes < 10)
324                    minutes = "0" + minutes;
325                var timeStamp = date.getHours() + ":" + minutes;
326                var timeStampInput = document.createElement("input");
327                timeStampInput.name = "timeStamp";
328                timeStampInput.value = timeStamp;
329                timeStampInput.type = "hidden";
[36]330                                                                                                                                   
[30]331                var surveyIDInput = document.createElement("input");
332                surveyIDInput.name = "surveyID";
333                surveyIDInput.value = surveyID;
334                surveyIDInput.type = "hidden";
[36]335                                                                                                                                                             
[30]336                questionsDiv.appendChild(timeStampInput);
337                questionsDiv.appendChild(surveyIDInput);
338                form.submit();
339            }
[36]340                                                                                                                                                                                                                                           
341                                                                                                                                                                                                                                                                                                                                                                                                                           
[11]342        </script>
343        <?php
344    }
345
[29]346    private function surveyHead() {
[24]347        ?><div id="surveyHead">
348        <?php
[29]349        $this->titleBox();
350        $this->saveSurvey();
351        $this->descriptionBox();
[24]352        ?></div>
353        <?php
354    }
355
[29]356    private function titleBox() {
357        if (isset($this->survey->title))
358            $value = $this->survey->title;
359        else
360            $value = 'Untitled Survey';
[11]361        ?>
[29]362        <input type="text" id="surveyTitle" name="surveyTitle" value="<?php echo $value ?>" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
[11]363        <?php
[10]364    }
[11]365
[29]366    private function saveSurvey() {
367        if (isset($this->timeStamp))
368            echo "<div id='timeStamp'>Last saved " . $this->timeStamp . '</div>';
369        if (isset($this->survey->id))
370            $id = $this->survey->id;
371        else
372            $id = null;
[24]373        ?>
[29]374        <input id="surveySaveButton" type="button" onclick="save('<?php echo $id; ?>')" class='surveyButton' value='Save' />
[24]375        <?php
376    }
377
[29]378    private function descriptionBox() {
379        if (isset($this->survey->description))
380            $value = $this->survey->description;
381        else
382            $value = 'Write a helpful description for this survey here.';
[11]383        ?>
[29]384        <textarea id="surveyDescription" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)"><?php echo $value; ?></textarea>
[11]385        <?php
[10]386    }
[11]387
[29]388    private function questionCreationForm() {
[17]389        ?>
390        <div id='questionsDiv'>
391        </div>
392        <?php
[30]393        if (isset($this->survey)) {
[32]394            foreach ($this->survey->questions as $numQ => $question) {
[30]395                ?>
[36]396                <script type="text/javascript">
[32]397                    var answers = new Array();
398                <?php
399                /* Put all answers in js array */
400                foreach ($question->answers as $numA => $answer) {
401                    echo "answers[" . $numA . "] = '" . $answer . "'; ";
402                }
403                ?>
[36]404                                                                                                   
[32]405                    addQuestion('<?php echo $question->title; ?>', '<?php echo $question->description; ?>');
[36]406                                                                                                                                                                                                   
[32]407                    var select = document.getElementById('<?php echo $numQ; ?>');
408                    var type = '<?php echo $question->type; ?>';
409
410                    for (var i = 0; i < select.options.length; i++) {
411                        if (select.options[i].value == type)
412                        {
413                            select.options[i].selected = true;
414                            handleType(select, answers)
415                            break;
416                        }
[36]417                    }     
418                                   
419                                   
420                    /* questionID stuff */
421                    var questionIDInput = document.createElement("input");
422                    questionIDInput.id = "questionID<?php echo $numQ; ?>";
423                    questionIDInput.name = "questionID<?php echo $numQ; ?>";
424                    questionIDInput.value = "<?php echo $question->id; ?>";
425                    questionIDInput.type = "hidden";
426                                                                                                                                                                                     
427                    document.getElementById("questionsDiv").appendChild(questionIDInput);
[32]428
[30]429                </script>
430                <?php
431            }
432        } else {
433            ?>
434            <script type="text/javascript"> addQuestion();</script>
435            <?php
436        }
[11]437    }
438
439    private static function addQuestionButton() {
[17]440        ?>
441        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
442        <?php
[10]443    }
444
[19]445    private static function removeLastQuestionButton() {
446        ?>
447        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
448        <?php
449    }
450
[11]451    private static function submitbutton() {
452        ?>
453        <input type="submit" name="submitSurvey"
[17]454               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
[11]455        <?php
456    }
457
[10]458}
459?>
Note: See TracBrowser for help on using the repository browser.