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

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

Bas forgot } :D

File size: 24.8 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);
[32]40                                                                                                                                                   
[30]41            var questionCount = 1;         
[32]42                                                                                                                                                                                                                                                                                           
[30]43            function getNewQuestion(title, description)
[17]44            {
[33]45                if (title == null)
[30]46                    var title = 'Untitled Question';
[32]47                                                                                       
[30]48                if (description != null)
49                    var description = description;
50                else
51                    var description = 'Write a question description here.';
[32]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>";
[32]73                                                                                                                                                                                                                                                                                       
[30]74                questionDiv.innerHTML = htmlStr;
[32]75                                                                                                                                                                                                                                                                                       
[30]76                return questionDiv;
[17]77            }
[32]78                                                                                                                                                                                                                                                                                           
[30]79            function handleFocus(input)
[11]80            {
[30]81                if (input.clicked == null)
82                {
83                    input.value = "";
84                    input.style.color = "black";   
85                    input.clicked = true;
86                }
87            }
[32]88                                                                                                                                                                                                                                                                                                                           
[30]89            function handleBlur(input)
90            {       
91                var surveyTitle = document.getElementById('surveyTitle');
92                var surveyDescription = document.getElementById('surveyDescription');
[32]93                                                                                                                                                                                                                                                                                                           
[30]94                if (input.value == "")
[11]95                {
[30]96                    input.style.color = "gray";
97                    input.clicked = null;
[32]98                                                                                                                                                                                                                                                                                                                                   
[30]99                    if (input == surveyTitle)
100                    {
101                        input.value = "Untitled Survey";
102                    }
103                    else if (input == surveyDescription)
104                    {
105                        input.value = "Write a helpful description for this survey here.";
106                    }
107                }                           
108            }
[32]109                                                                                                                                                                                                                                                                           
110            function handleType(select, answers)
[30]111            {
[32]112                var numQ = select.id; // questionNumber
[30]113                var type = select.valueOf().value;
[32]114                var answersDiv = document.getElementById("answersDiv" + numQ );
[30]115                answersDiv.answerCount = 1;
116                answersDiv.clicked = null;
[32]117                                                                                                                                                                                                                               
[30]118                switch (type) {
119                    case 'mc':
120                        answersDiv.innerHTML = "";
[32]121                        if (answers != null)
122                        {
123                            for (var i = 1; i < answers.length; i++)
124                            {
125                                addOption(numQ, answers[i]);
126                            }
127                        }
128                        else
129                            addOption(numQ);
130                                                                                                                                                                                                                                                               
[30]131                        break;
132                    case 'text':
133                        answersDiv.innerHTML = "";
134                        break;
135                    case 'int':   
136                        answersDiv.innerHTML = "";
[32]137                                                                                       
[30]138                        //min max
[32]139                        if (answers != null)
140                            minMax(numQ, answers[1], answers[2]);
141                        else
142                            minMax(numQ);
[30]143                        break;
144                    case 'checkboxes':
[32]145                        answersDiv.innerHTML = "";
146                        if (answers != null)
147                        {
148                            for (var i = 1; i < answers.length; i++)
149                            {
150                                addOption(numQ, answers[i]);
151                            }
152                        }
153                        else
154                            addOption(numQ);
[30]155                        break;
156                    case 'scale':
157                        answersDiv.innerHTML = "";
158                        //what scale (min max incr)
[32]159                        if (answers != null)
160                            minMaxIncr(numQ, answers[1], answers[2], answers[3]);
161                        else
162                            minMaxIncr(numQ);
[30]163                        break;
164                    default:
165                        break;
[11]166                }
[30]167
168            }
[32]169                                                                                                                                                                                                                                                           
170                                                                                                                                                                                                                                                   
171            function addOption(questionNumber, optionStr)
[30]172            {       
173                var answersDiv = document.getElementById("answersDiv" + questionNumber);
174                var answerCount = answersDiv.answerCount;
175                var answerDiv = document.createElement("div");
[32]176                       
177                if (optionStr == null)
178                    var optionStr = "Option " + answerCount;
179                       
180                       
[30]181                answerDiv.className = "answerDiv";
[32]182                                                                                                                                                                                                                                                       
[30]183                var htmlStr = "<input type='text' name='q" +
[32]184                    questionNumber + "ans" + answerCount + "' value='" + optionStr + "' />";
185                                                                                                                                                                                                                               
[30]186                if (answersDiv.clicked == null)
[11]187                {
[30]188                    htmlStr += "<input type='button' id='addOpt'"
189                        + " class='surveyButton' onclick='addOption(" + questionNumber + ")' value='Add Option' />" +
190                        "<input type='button' class='surveyButton' onclick='removeOption(" + questionNumber + ")' value='x' />";
[32]191                                                                                                                                                                                                   
[30]192                    answersDiv.clicked = true;
[25]193                }
[32]194                                                                                                                                                                                                                                                               
[30]195                answerDiv.innerHTML = htmlStr;
[32]196                                                                                                                                                                                               
[30]197                answerDiv.prev = answersDiv.lastAnswer; //singly linked list
198                answersDiv.lastAnswer = answerDiv;
[32]199                                                                                                                                                                                                                                                               
[30]200                answersDiv.appendChild(answerDiv);
201                answersDiv.answerCount++;
[17]202            }
[32]203                                                                                                                                                                                           
[30]204            function removeOption(questionNumber)
[17]205            {
[30]206                var answersDiv = document.getElementById("answersDiv" + questionNumber);
[32]207                                                                                                                                                                                               
[30]208                if (answersDiv.lastAnswer.prev != null)
209                {
210                    answersDiv.removeChild(answersDiv.lastAnswer);
211                    answersDiv.lastAnswer = answersDiv.lastAnswer.prev;
212                    answersDiv.answerCount--;
213                }
[11]214            }
[32]215                                                                                                                                                                                                                   
216            function minMax(questionNumber, min, max)
[20]217            {
[32]218                if (min == null)
219                    var min = '';
220                if (max == null)
221                    var max = '';
222                                                                               
[30]223                var answersDiv = document.getElementById("answersDiv" + questionNumber);
[32]224                                                                                                                                                                                                                       
[30]225                var answerDiv = document.createElement("div");
[32]226                                                                                                                                                                                       
[30]227                answerDiv.className = "answerDiv";
[32]228                answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' value='" + min + "' name='q" + questionNumber + "ans1' />" +
229                    "<label for='max'>Max</label><input type='text' value='" + max + "' name='q" + questionNumber + "ans2' />";
230                                                                                                                                                                                                                   
[30]231                answersDiv.appendChild(answerDiv);
232            } 
[32]233                                                                                                                                                                                                                   
234            function minMaxIncr(questionNumber, left, right, incr)
[30]235            {
[32]236                if (left == null)
237                    var left = '';
238                if (right == null)
239                    var right = '';
240                if (incr == null)
241                    var incr = '';
242                               
[30]243                var answersDiv = document.getElementById("answersDiv" + questionNumber);
[32]244                                                                                                                                                                                                                       
[30]245                var answerDiv = document.createElement("div");
246                answerDiv.className = "answerDiv";
[32]247                answerDiv.innerHTML = "<label>Left label</label><input type='text' value='" + left + "' name='q" + questionNumber + "ans1' />" +
248                    "<label>Right label</label><input type='text' value='" + right + "' name='q" + questionNumber + "ans2' />" +
249                    "<label>Scale count</label><input type='text' value='" + incr + "' name='q" + questionNumber + "ans3' />" +
[30]250                    "<div id='inputCheckFeedback'" + questionNumber + "";
[32]251                                                                                                                                                                                                                   
[30]252                answersDiv.appendChild(answerDiv);
253            }         
[32]254                                                                                                                                                                                                                                                                         
255                                                                                                                                                                                                                                                                                 
[30]256            function addQuestion(title, description)
257            {
258                var questionsDiv = document.getElementById('questionsDiv');
259                var newQuestion = getNewQuestion(title, description);
[32]260                                                                                                                                                                                                               
[19]261
[30]262                newQuestion.prev = document.lastQuestion;
263                document.lastQuestion = newQuestion;
[19]264
[30]265                questionsDiv.appendChild(newQuestion);
266                questionCount++;
267            }
[32]268                                                                                                                                                                                                           
[30]269            function removeLastQuestion()
[19]270            {
[30]271                var questionsDiv = document.getElementById('questionsDiv');
[32]272                                                                                                                                                                                                               
[30]273                if (document.lastQuestion.prev != null)
274                {
275                    questionsDiv.removeChild(document.lastQuestion);
276                    document.lastQuestion = document.lastQuestion.prev;
277                    questionCount--; 
278                }
279                else
280                {
281                    // do nothing
282                }               
[19]283            }
[32]284                                                                                                                                                                                     
[30]285            function save(surveyID)
[24]286            {
[30]287                var form = document.getElementById('survey');
288                var questionsDiv = document.getElementById('questionsDiv');
289                form.action = "surveycreation.php";
[32]290                                                                                                                                                       
[30]291                /* extra time stamp */
292                var date = new Date();
293                var minutes = date.getUTCMinutes();
294                if (minutes < 10)
295                    minutes = "0" + minutes;
296                var timeStamp = date.getHours() + ":" + minutes;
297                var timeStampInput = document.createElement("input");
298                timeStampInput.name = "timeStamp";
299                timeStampInput.value = timeStamp;
300                timeStampInput.type = "hidden";
[32]301                                                                                                           
[30]302                var surveyIDInput = document.createElement("input");
303                surveyIDInput.name = "surveyID";
304                surveyIDInput.value = surveyID;
305                surveyIDInput.type = "hidden";
[32]306                                                                                                                                     
[30]307                questionsDiv.appendChild(timeStampInput);
308                questionsDiv.appendChild(surveyIDInput);
309                form.submit();
310            }
[32]311                                                                                                                                                                                                                   
312                                                                                                                                                                                                                                                                                                                                                                                                   
[11]313        </script>
314        <?php
315    }
316
[29]317    private function surveyHead() {
[24]318        ?><div id="surveyHead">
319        <?php
[29]320        $this->titleBox();
321        $this->saveSurvey();
322        $this->descriptionBox();
[24]323        ?></div>
324        <?php
325    }
326
[29]327    private function titleBox() {
328        if (isset($this->survey->title))
329            $value = $this->survey->title;
330        else
331            $value = 'Untitled Survey';
[11]332        ?>
[29]333        <input type="text" id="surveyTitle" name="surveyTitle" value="<?php echo $value ?>" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
[11]334        <?php
[10]335    }
[11]336
[29]337    private function saveSurvey() {
338        if (isset($this->timeStamp))
339            echo "<div id='timeStamp'>Last saved " . $this->timeStamp . '</div>';
340        if (isset($this->survey->id))
341            $id = $this->survey->id;
342        else
343            $id = null;
[24]344        ?>
[29]345        <input id="surveySaveButton" type="button" onclick="save('<?php echo $id; ?>')" class='surveyButton' value='Save' />
[24]346        <?php
347    }
348
[29]349    private function descriptionBox() {
350        if (isset($this->survey->description))
351            $value = $this->survey->description;
352        else
353            $value = 'Write a helpful description for this survey here.';
[11]354        ?>
[29]355        <textarea id="surveyDescription" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)"><?php echo $value; ?></textarea>
[11]356        <?php
[10]357    }
[11]358
[29]359    private function questionCreationForm() {
[17]360        ?>
361        <div id='questionsDiv'>
362        </div>
363        <?php
[30]364        if (isset($this->survey)) {
[32]365            foreach ($this->survey->questions as $numQ => $question) {
[30]366                ?>
[32]367                <script type="text/javascript">
368                    var answers = new Array();
369                <?php
370                /* Put all answers in js array */
371                foreach ($question->answers as $numA => $answer) {
372                    echo "answers[" . $numA . "] = '" . $answer . "'; ";
373                }
374                ?>
375                                                   
376                    addQuestion('<?php echo $question->title; ?>', '<?php echo $question->description; ?>');
377                                                                                                                                                   
378                    var select = document.getElementById('<?php echo $numQ; ?>');
379                    var type = '<?php echo $question->type; ?>';
380
381                    for (var i = 0; i < select.options.length; i++) {
382                        if (select.options[i].value == type)
383                        {
384                            select.options[i].selected = true;
385                            handleType(select, answers)
386                            break;
387                        }
388                    }                                                                                                                   
389
[30]390                </script>
391                <?php
392            }
393        } else {
394            ?>
395            <script type="text/javascript"> addQuestion();</script>
396            <?php
397        }
[11]398    }
399
400    private static function addQuestionButton() {
[17]401        ?>
402        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
403        <?php
[10]404    }
405
[19]406    private static function removeLastQuestionButton() {
407        ?>
408        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
409        <?php
410    }
411
[11]412    private static function submitbutton() {
413        ?>
414        <input type="submit" name="submitSurvey"
[17]415               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
[11]416        <?php
417    }
418
[10]419}
420?>
Note: See TracBrowser for help on using the repository browser.