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
Line 
1<?php
2
3/**
4 * Description of SurveyTool
5 *
6 * SurveyTool is where the survey can be created.
7 *
8 */
9class SurveyCreationTool {
10
11    private $survey;
12    private $timeStamp;
13
14    public function __construct($survey = null, $timeStamp = null) {
15        $this->survey = $survey;
16        $this->timeStamp = $timeStamp;
17
18        SurveyCreationTool::javascript();
19        ?>
20
21        <div id="surveyCreation"><form id="survey" action="submitsurvey.php" method="post">
22                <?php
23                $this->surveyHead();
24                $this->questionCreationForm();
25                SurveyCreationTool::addQuestionButton();
26                SurveyCreationTool::removeLastQuestionButton();
27                SurveyCreationTool::submitButton();
28                ?></form></div>
29        <?php
30    }
31
32    private static function javascript() {
33        ?>
34        <script type="text/javascript">
35            /* autosave every 3 minutes */
36        setTimeout("save()", 180000);
37                                                   
38        var questionCount = 1;         
39                                                                                                                                                                                           
40        function getNewQuestion()
41        {
42                       
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>" +
49                "<td><input type='text' class='questionTitle' name='questionTitle" + questionCount + "' onfocus='handleFocus(this)' size='30' value='' /></td></tr>" +
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>";
63                                                                                                                                                                                       
64            questionDiv.innerHTML = htmlStr;
65                                                                                                                                                                                       
66            return questionDiv;
67        }
68                                                                                                                                                                                           
69        function handleFocus(input)
70        {
71            if (input.clicked == null)
72            {
73                input.value = "";
74                input.style.color = "black";   
75                input.clicked = true;
76            }
77        }
78                                                                                                                                                                                                                           
79        function handleBlur(input)
80        {       
81            var surveyTitle = document.getElementById('surveyTitle');
82            var surveyDescription = document.getElementById('surveyDescription');
83                                                                                                                                                                                                           
84            if (input.value == "")
85            {
86                input.style.color = "gray";
87                input.clicked = null;
88                                                                                                                                                                                                                                   
89                if (input == surveyTitle)
90                {
91                    input.value = "Untitled Survey";
92                }
93                else if (input == surveyDescription)
94                {
95                    input.value = "Write a helpful description for this survey here.";
96                }
97            }                           
98        }
99                                                                                                                                                                           
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;
106                                                                                                                               
107            switch (type) {
108                case 'mc':
109                    answersDiv.innerHTML = "";
110                    addOption(select.id);
111                                                                                                                                                               
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;
132            }
133
134        }
135                                                                                                                                                           
136                                                                                                                                                   
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";
143                                                                                                                                                       
144            var htmlStr = "<input type='text' name='q" +
145                questionNumber + "ans" + answerCount + "' value='Option " + answerCount + "' />";
146                                                                                                                               
147            if (answersDiv.clicked == null)
148            {
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' />";
152                                                                                                   
153                answersDiv.clicked = true;
154            }
155                                                                                                                                                               
156            answerDiv.innerHTML = htmlStr;
157                                                                                               
158            answerDiv.prev = answersDiv.lastAnswer; //singly linked list
159            answersDiv.lastAnswer = answerDiv;
160                                                                                                                                                               
161            answersDiv.appendChild(answerDiv);
162            answersDiv.answerCount++;
163        }
164                                                                                           
165        function removeOption(questionNumber)
166        {
167            var answersDiv = document.getElementById("answersDiv" + questionNumber);
168                                                                                               
169            if (answersDiv.lastAnswer.prev != null)
170            {
171                answersDiv.removeChild(answersDiv.lastAnswer);
172                answersDiv.lastAnswer = answersDiv.lastAnswer.prev;
173                answersDiv.answerCount--;
174            }
175        }
176                                                                                                                   
177        function minMax(questionNumber)
178        {
179            var answersDiv = document.getElementById("answersDiv" + questionNumber);
180                                                                                                                       
181            var answerDiv = document.createElement("div");
182                                                                                       
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' />";
186                                                                                                                   
187            answersDiv.appendChild(answerDiv);
188        } 
189                                                                                                                   
190        function minMaxIncr(questionNumber)
191        {
192            var answersDiv = document.getElementById("answersDiv" + questionNumber);
193                                                                                                                       
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' />" +
198                "<label>Scale count</label><input type='text' name='q" + questionNumber + "ans3' />" +
199                "<div id='inputCheckFeedback'" + questionNumber + "";
200                                                                                                                   
201            answersDiv.appendChild(answerDiv);
202        }         
203                                                                                                                                                                         
204                                                                                                                                                                                 
205        function addQuestion(title, description)
206        {
207            var questionsDiv = document.getElementById('questionsDiv');
208            var newQuestion = getNewQuestion(title, description);
209                                                                                                               
210
211            newQuestion.prev = document.lastQuestion;
212            document.lastQuestion = newQuestion;
213
214            questionsDiv.appendChild(newQuestion);
215            questionCount++;
216        }
217                                                                                                           
218        function removeLastQuestion()
219        {
220            var questionsDiv = document.getElementById('questionsDiv');
221                                                                                                               
222            if (document.lastQuestion.prev != null)
223            {
224                questionsDiv.removeChild(document.lastQuestion);
225                document.lastQuestion = document.lastQuestion.prev;
226                questionCount--; 
227            }
228            else
229            {
230                // do nothing
231            }               
232        }
233                                                                                     
234        function save(surveyID)
235        {
236            var form = document.getElementById('survey');
237            var questionsDiv = document.getElementById('questionsDiv');
238            form.action = "surveycreation.php";
239                                                       
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";
250           
251            var surveyIDInput = document.createElement("input");
252            surveyIDInput.name = "surveyID";
253            surveyIDInput.value = surveyID;
254            surveyIDInput.type = "hidden";
255                                     
256            questionsDiv.appendChild(timeStampInput);
257            questionsDiv.appendChild(surveyIDInput);
258            form.submit();
259        }
260                                                                                                                   
261                                                                                                                                                                                                                                                                                                   
262        </script>
263        <?php
264    }
265
266    private function surveyHead() {
267        ?><div id="surveyHead">
268        <?php
269        $this->titleBox();
270        $this->saveSurvey();
271        $this->descriptionBox();
272        ?></div>
273        <?php
274    }
275
276    private function titleBox() {
277        if (isset($this->survey->title))
278            $value = $this->survey->title;
279        else
280            $value = 'Untitled Survey';
281        ?>
282        <input type="text" id="surveyTitle" name="surveyTitle" value="<?php echo $value ?>" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
283        <?php
284    }
285
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;
293        ?>
294        <input id="surveySaveButton" type="button" onclick="save('<?php echo $id; ?>')" class='surveyButton' value='Save' />
295        <?php
296    }
297
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.';
303        ?>
304        <textarea id="surveyDescription" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)"><?php echo $value; ?></textarea>
305        <?php
306    }
307
308    private function questionCreationForm() {
309        ?>
310        <div id='questionsDiv'>
311        </div>
312
313        <script type="text/javascript"> addQuestion(); </script>
314        <?php
315    }
316
317    private static function addQuestionButton() {
318        ?>
319        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
320        <?php
321    }
322
323    private static function removeLastQuestionButton() {
324        ?>
325        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
326        <?php
327    }
328
329    private static function submitbutton() {
330        ?>
331        <input type="submit" name="submitSurvey"
332               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
333        <?php
334    }
335
336}
337?>
Note: See TracBrowser for help on using the repository browser.