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

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

Small fix in time (autosave notice)

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