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

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

QuestionID now gets passed.

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