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

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

ID passed. Simple Question and Survey php data classes.

File size: 16.5 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
[11]18        SurveyCreationTool::javascript();
19        ?>
[17]20
[24]21        <div id="surveyCreation"><form id="survey" action="submitsurvey.php" method="post">
[11]22                <?php
[29]23                $this->surveyHead();
24                $this->questionCreationForm();
[17]25                SurveyCreationTool::addQuestionButton();
[19]26                SurveyCreationTool::removeLastQuestionButton();
[11]27                SurveyCreationTool::submitButton();
28                ?></form></div>
29        <?php
[10]30    }
31
[11]32    private static function javascript() {
33        ?>
34        <script type="text/javascript">
[24]35            /* autosave every 3 minutes */
[25]36        setTimeout("save()", 180000);
[29]37                                                   
[25]38        var questionCount = 1;         
[29]39                                                                                                                                                                                           
[25]40        function getNewQuestion()
41        {
[29]42                       
[25]43            var questionDiv = document.createElement("div");
44            var htmlStr =
45                "<div id='question" + questionCount + "' class='question'>" +
46                "<table class='questionTable'>" +
47                "<th>Question " + questionCount + "</th>" +
48                "<tr><td><label for='questionTitle'>Title</label></td>" +
[29]49                "<td><input type='text' class='questionTitle' name='questionTitle" + questionCount + "' onfocus='handleFocus(this)' size='30' value='' /></td></tr>" +
[25]50                "<tr><td><label for='questionDescription'>Description</label></td>" +
51                "<td><input type='text' class='questionDescription' name='questionDescription" + questionCount + "' onfocus='handleFocus(this)' size='60' value='Write a question description here.' /></td>" +
52                "<tr><td><label for='questionType'>Answer type</label></td>" +
53                "<td><select id='" + questionCount + "' name='questionType" + questionCount + "' onchange='handleType(this)'><br />"+
54                "<option value='text' selected='selected'>Text</option>" +
55                "<option value='int'>Integer</option>" +
56                "<option value='mc'>Multiple choice</option>" +
57                "<option value='checkboxes'>Checkboxes</option>" +
58                "<option value='scale'>Scale</option>" +
59                "</select></td></tr>" +
60                "</table>" +
61                "<div id='answersDiv" + questionCount + "' class='answersDiv'></div>" +
62                "</div>";
[29]63                                                                                                                                                                                       
[25]64            questionDiv.innerHTML = htmlStr;
[29]65                                                                                                                                                                                       
[25]66            return questionDiv;
67        }
[29]68                                                                                                                                                                                           
[25]69        function handleFocus(input)
70        {
71            if (input.clicked == null)
[17]72            {
[25]73                input.value = "";
74                input.style.color = "black";   
75                input.clicked = true;
[17]76            }
[25]77        }
[29]78                                                                                                                                                                                                                           
[25]79        function handleBlur(input)
80        {       
81            var surveyTitle = document.getElementById('surveyTitle');
82            var surveyDescription = document.getElementById('surveyDescription');
[29]83                                                                                                                                                                                                           
[25]84            if (input.value == "")
[11]85            {
[25]86                input.style.color = "gray";
87                input.clicked = null;
[29]88                                                                                                                                                                                                                                   
[25]89                if (input == surveyTitle)
[11]90                {
[25]91                    input.value = "Untitled Survey";
[11]92                }
[25]93                else if (input == surveyDescription)
[11]94                {
[25]95                    input.value = "Write a helpful description for this survey here.";
96                }
97            }                           
98        }
[29]99                                                                                                                                                                           
[25]100        function handleType(select)
101        {
102            var type = select.valueOf().value;
103            var answersDiv = document.getElementById("answersDiv" +  select.id);
104            answersDiv.answerCount = 1;
105            answersDiv.clicked = null;
[29]106                                                                                                                               
[25]107            switch (type) {
108                case 'mc':
109                    answersDiv.innerHTML = "";
110                    addOption(select.id);
[29]111                                                                                                                                                               
[25]112                    break;
113                case 'text':
114                    answersDiv.innerHTML = "";
115                    break;
116                case 'int':   
117                    answersDiv.innerHTML = "";
118                    //min max
119                    minMax(select.id);
120                    break;
121                case 'checkboxes':
122                    answersDiv.innerHTML = ""; 
123                    addOption(select.id);
124                    break;
125                case 'scale':
126                    answersDiv.innerHTML = "";
127                    //what scale (min max incr)
128                    minMaxIncr(select.id);
129                    break;
130                default:
131                    break;
[17]132            }
[25]133
134        }
[29]135                                                                                                                                                           
136                                                                                                                                                   
[25]137        function addOption(questionNumber)
138        {       
139            var answersDiv = document.getElementById("answersDiv" + questionNumber);
140            var answerCount = answersDiv.answerCount;
141            var answerDiv = document.createElement("div");
142            answerDiv.className = "answerDiv";
[29]143                                                                                                                                                       
[25]144            var htmlStr = "<input type='text' name='q" +
145                questionNumber + "ans" + answerCount + "' value='Option " + answerCount + "' />";
[29]146                                                                                                                               
[25]147            if (answersDiv.clicked == null)
[17]148            {
[25]149                htmlStr += "<input type='button' id='addOpt'"
150                    + " class='surveyButton' onclick='addOption(" + questionNumber + ")' value='Add Option' />" +
151                    "<input type='button' class='surveyButton' onclick='removeOption(" + questionNumber + ")' value='x' />";
[29]152                                                                                                   
[25]153                answersDiv.clicked = true;
[11]154            }
[29]155                                                                                                                                                               
[25]156            answerDiv.innerHTML = htmlStr;
[29]157                                                                                               
[25]158            answerDiv.prev = answersDiv.lastAnswer; //singly linked list
159            answersDiv.lastAnswer = answerDiv;
[29]160                                                                                                                                                               
[25]161            answersDiv.appendChild(answerDiv);
162            answersDiv.answerCount++;
163        }
[29]164                                                                                           
[25]165        function removeOption(questionNumber)
166        {
167            var answersDiv = document.getElementById("answersDiv" + questionNumber);
[29]168                                                                                               
[25]169            if (answersDiv.lastAnswer.prev != null)
[20]170            {
[25]171                answersDiv.removeChild(answersDiv.lastAnswer);
172                answersDiv.lastAnswer = answersDiv.lastAnswer.prev;
173                answersDiv.answerCount--;
[20]174            }
[25]175        }
[29]176                                                                                                                   
[25]177        function minMax(questionNumber)
178        {
179            var answersDiv = document.getElementById("answersDiv" + questionNumber);
[29]180                                                                                                                       
[25]181            var answerDiv = document.createElement("div");
[29]182                                                                                       
[25]183            answerDiv.className = "answerDiv";
184            answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' name='q" + questionNumber + "ans1' />" +
185                "<label for='max'>Max</label><input type='text' name='q" + questionNumber + "ans2' />";
[29]186                                                                                                                   
[25]187            answersDiv.appendChild(answerDiv);
188        } 
[29]189                                                                                                                   
[25]190        function minMaxIncr(questionNumber)
191        {
192            var answersDiv = document.getElementById("answersDiv" + questionNumber);
[29]193                                                                                                                       
[25]194            var answerDiv = document.createElement("div");
195            answerDiv.className = "answerDiv";
196            answerDiv.innerHTML = "<label>Left label</label><input type='text' name='q" + questionNumber + "ans1' />" +
197                "<label>Right label</label><input type='text' name='q" + questionNumber + "ans2' />" +
[29]198                "<label>Scale count</label><input type='text' name='q" + questionNumber + "ans3' />" +
199                "<div id='inputCheckFeedback'" + questionNumber + "";
200                                                                                                                   
[25]201            answersDiv.appendChild(answerDiv);
202        }         
[29]203                                                                                                                                                                         
204                                                                                                                                                                                 
205        function addQuestion(title, description)
[25]206        {
207            var questionsDiv = document.getElementById('questionsDiv');
[29]208            var newQuestion = getNewQuestion(title, description);
209                                                                                                               
[19]210
[25]211            newQuestion.prev = document.lastQuestion;
212            document.lastQuestion = newQuestion;
[19]213
[25]214            questionsDiv.appendChild(newQuestion);
215            questionCount++;
216        }
[29]217                                                                                                           
[25]218        function removeLastQuestion()
219        {
220            var questionsDiv = document.getElementById('questionsDiv');
[29]221                                                                                                               
[25]222            if (document.lastQuestion.prev != null)
[19]223            {
[25]224                questionsDiv.removeChild(document.lastQuestion);
225                document.lastQuestion = document.lastQuestion.prev;
226                questionCount--; 
[19]227            }
[25]228            else
[24]229            {
[25]230                // do nothing
231            }               
232        }
[29]233                                                                                     
234        function save(surveyID)
[25]235        {
236            var form = document.getElementById('survey');
237            var questionsDiv = document.getElementById('questionsDiv');
238            form.action = "surveycreation.php";
[29]239                                                       
[25]240            /* extra time stamp */
241            var date = new Date();
242            var minutes = date.getUTCMinutes();
243            if (minutes < 10)
244                minutes = "0" + minutes;
245            var timeStamp = date.getHours() + ":" + minutes;
246            var timeStampInput = document.createElement("input");
247            timeStampInput.name = "timeStamp";
248            timeStampInput.value = timeStamp;
249            timeStampInput.type = "hidden";
[29]250           
251            var surveyIDInput = document.createElement("input");
252            surveyIDInput.name = "surveyID";
253            surveyIDInput.value = surveyID;
254            surveyIDInput.type = "hidden";
255                                     
[25]256            questionsDiv.appendChild(timeStampInput);
[29]257            questionsDiv.appendChild(surveyIDInput);
[25]258            form.submit();
259        }
[29]260                                                                                                                   
261                                                                                                                                                                                                                                                                                                   
[11]262        </script>
263        <?php
264    }
265
[29]266    private function surveyHead() {
[24]267        ?><div id="surveyHead">
268        <?php
[29]269        $this->titleBox();
270        $this->saveSurvey();
271        $this->descriptionBox();
[24]272        ?></div>
273        <?php
274    }
275
[29]276    private function titleBox() {
277        if (isset($this->survey->title))
278            $value = $this->survey->title;
279        else
280            $value = 'Untitled Survey';
[11]281        ?>
[29]282        <input type="text" id="surveyTitle" name="surveyTitle" value="<?php echo $value ?>" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
[11]283        <?php
[10]284    }
[11]285
[29]286    private function saveSurvey() {
287        if (isset($this->timeStamp))
288            echo "<div id='timeStamp'>Last saved " . $this->timeStamp . '</div>';
289        if (isset($this->survey->id))
290            $id = $this->survey->id;
291        else
292            $id = null;
[24]293        ?>
[29]294        <input id="surveySaveButton" type="button" onclick="save('<?php echo $id; ?>')" class='surveyButton' value='Save' />
[24]295        <?php
296    }
297
[29]298    private function descriptionBox() {
299        if (isset($this->survey->description))
300            $value = $this->survey->description;
301        else
302            $value = 'Write a helpful description for this survey here.';
[11]303        ?>
[29]304        <textarea id="surveyDescription" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)"><?php echo $value; ?></textarea>
[11]305        <?php
[10]306    }
[11]307
[29]308    private function questionCreationForm() {
[17]309        ?>
310        <div id='questionsDiv'>
311        </div>
[29]312
[17]313        <script type="text/javascript"> addQuestion(); </script>
314        <?php
[11]315    }
316
317    private static function addQuestionButton() {
[17]318        ?>
319        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
320        <?php
[10]321    }
322
[19]323    private static function removeLastQuestionButton() {
324        ?>
325        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
326        <?php
327    }
328
[11]329    private static function submitbutton() {
330        ?>
331        <input type="submit" name="submitSurvey"
[17]332               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
[11]333        <?php
334    }
335
[10]336}
337?>
Note: See TracBrowser for help on using the repository browser.