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
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        if (isset($this->survey->id))
19            SurveyCreationTool::javascript($this->survey->id);
20        else
21            SurveyCreationTool::javascript();
22        ?>
23
24        <div id="surveyCreation"><form id="survey" action="submitsurvey.php" method="post">
25                <?php
26                $this->surveyHead();
27                $this->questionCreationForm();
28                SurveyCreationTool::addQuestionButton();
29                SurveyCreationTool::removeLastQuestionButton();
30                SurveyCreationTool::submitButton();
31                ?></form></div>
32        <?php
33    }
34
35    private static function javascript($id = null) {
36        ?>
37        <script type="text/javascript">
38            /* autosave every 3 minutes */
39            setTimeout("save('<?php echo $id; ?>')", 180000);
40                                                                                                                                                   
41            var questionCount = 1;         
42                                                                                                                                                                                                                                                                                           
43            function getNewQuestion(title, description)
44            {
45                if (title == null)
46                    var title = 'Untitled Question';
47                                                                                       
48                if (description != null)
49                    var description = description;
50                else
51                    var description = 'Write a question description here.';
52                                                                                                           
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>";
73                                                                                                                                                                                                                                                                                       
74                questionDiv.innerHTML = htmlStr;
75                                                                                                                                                                                                                                                                                       
76                return questionDiv;
77            }
78                                                                                                                                                                                                                                                                                           
79            function handleFocus(input)
80            {
81                if (input.clicked == null)
82                {
83                    input.value = "";
84                    input.style.color = "black";   
85                    input.clicked = true;
86                }
87            }
88                                                                                                                                                                                                                                                                                                                           
89            function handleBlur(input)
90            {       
91                var surveyTitle = document.getElementById('surveyTitle');
92                var surveyDescription = document.getElementById('surveyDescription');
93                                                                                                                                                                                                                                                                                                           
94                if (input.value == "")
95                {
96                    input.style.color = "gray";
97                    input.clicked = null;
98                                                                                                                                                                                                                                                                                                                                   
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            }
109                                                                                                                                                                                                                                                                           
110            function handleType(select, answers)
111            {
112                var numQ = select.id; // questionNumber
113                var type = select.valueOf().value;
114                var answersDiv = document.getElementById("answersDiv" + numQ );
115                answersDiv.answerCount = 1;
116                answersDiv.clicked = null;
117                                                                                                                                                                                                                               
118                switch (type) {
119                    case 'mc':
120                        answersDiv.innerHTML = "";
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                                                                                                                                                                                                                                                               
131                        break;
132                    case 'text':
133                        answersDiv.innerHTML = "";
134                        break;
135                    case 'int':   
136                        answersDiv.innerHTML = "";
137                                                                                       
138                        //min max
139                        if (answers != null)
140                            minMax(numQ, answers[1], answers[2]);
141                        else
142                            minMax(numQ);
143                        break;
144                    case 'checkboxes':
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);
155                        break;
156                    case 'scale':
157                        answersDiv.innerHTML = "";
158                        //what scale (min max incr)
159                        if (answers != null)
160                            minMaxIncr(numQ, answers[1], answers[2], answers[3]);
161                        else
162                            minMaxIncr(numQ);
163                        break;
164                    default:
165                        break;
166                }
167
168            }
169                                                                                                                                                                                                                                                           
170                                                                                                                                                                                                                                                   
171            function addOption(questionNumber, optionStr)
172            {       
173                var answersDiv = document.getElementById("answersDiv" + questionNumber);
174                var answerCount = answersDiv.answerCount;
175                var answerDiv = document.createElement("div");
176                       
177                if (optionStr == null)
178                    var optionStr = "Option " + answerCount;
179                       
180                       
181                answerDiv.className = "answerDiv";
182                                                                                                                                                                                                                                                       
183                var htmlStr = "<input type='text' name='q" +
184                    questionNumber + "ans" + answerCount + "' value='" + optionStr + "' />";
185                                                                                                                                                                                                                               
186                if (answersDiv.clicked == null)
187                {
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' />";
191                                                                                                                                                                                                   
192                    answersDiv.clicked = true;
193                }
194                                                                                                                                                                                                                                                               
195                answerDiv.innerHTML = htmlStr;
196                                                                                                                                                                                               
197                answerDiv.prev = answersDiv.lastAnswer; //singly linked list
198                answersDiv.lastAnswer = answerDiv;
199                                                                                                                                                                                                                                                               
200                answersDiv.appendChild(answerDiv);
201                answersDiv.answerCount++;
202            }
203                                                                                                                                                                                           
204            function removeOption(questionNumber)
205            {
206                var answersDiv = document.getElementById("answersDiv" + questionNumber);
207                                                                                                                                                                                               
208                if (answersDiv.lastAnswer.prev != null)
209                {
210                    answersDiv.removeChild(answersDiv.lastAnswer);
211                    answersDiv.lastAnswer = answersDiv.lastAnswer.prev;
212                    answersDiv.answerCount--;
213                }
214            }
215                                                                                                                                                                                                                   
216            function minMax(questionNumber, min, max)
217            {
218                if (min == null)
219                    var min = '';
220                if (max == null)
221                    var max = '';
222                                                                               
223                var answersDiv = document.getElementById("answersDiv" + questionNumber);
224                                                                                                                                                                                                                       
225                var answerDiv = document.createElement("div");
226                                                                                                                                                                                       
227                answerDiv.className = "answerDiv";
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                                                                                                                                                                                                                   
231                answersDiv.appendChild(answerDiv);
232            } 
233                                                                                                                                                                                                                   
234            function minMaxIncr(questionNumber, left, right, incr)
235            {
236                if (left == null)
237                    var left = '';
238                if (right == null)
239                    var right = '';
240                if (incr == null)
241                    var incr = '';
242                               
243                var answersDiv = document.getElementById("answersDiv" + questionNumber);
244                                                                                                                                                                                                                       
245                var answerDiv = document.createElement("div");
246                answerDiv.className = "answerDiv";
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' />" +
250                    "<div id='inputCheckFeedback'" + questionNumber + "";
251                                                                                                                                                                                                                   
252                answersDiv.appendChild(answerDiv);
253            }         
254                                                                                                                                                                                                                                                                         
255                                                                                                                                                                                                                                                                                 
256            function addQuestion(title, description)
257            {
258                var questionsDiv = document.getElementById('questionsDiv');
259                var newQuestion = getNewQuestion(title, description);
260                                                                                                                                                                                                               
261
262                newQuestion.prev = document.lastQuestion;
263                document.lastQuestion = newQuestion;
264
265                questionsDiv.appendChild(newQuestion);
266                questionCount++;
267            }
268                                                                                                                                                                                                           
269            function removeLastQuestion()
270            {
271                var questionsDiv = document.getElementById('questionsDiv');
272                                                                                                                                                                                                               
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                }               
283            }
284                                                                                                                                                                                     
285            function save(surveyID)
286            {
287                var form = document.getElementById('survey');
288                var questionsDiv = document.getElementById('questionsDiv');
289                form.action = "surveycreation.php";
290                                                                                                                                                       
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";
301                                                                                                           
302                var surveyIDInput = document.createElement("input");
303                surveyIDInput.name = "surveyID";
304                surveyIDInput.value = surveyID;
305                surveyIDInput.type = "hidden";
306                                                                                                                                     
307                questionsDiv.appendChild(timeStampInput);
308                questionsDiv.appendChild(surveyIDInput);
309                form.submit();
310            }
311                                                                                                                                                                                                                   
312                                                                                                                                                                                                                                                                                                                                                                                                   
313        </script>
314        <?php
315    }
316
317    private function surveyHead() {
318        ?><div id="surveyHead">
319        <?php
320        $this->titleBox();
321        $this->saveSurvey();
322        $this->descriptionBox();
323        ?></div>
324        <?php
325    }
326
327    private function titleBox() {
328        if (isset($this->survey->title))
329            $value = $this->survey->title;
330        else
331            $value = 'Untitled Survey';
332        ?>
333        <input type="text" id="surveyTitle" name="surveyTitle" value="<?php echo $value ?>" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
334        <?php
335    }
336
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;
344        ?>
345        <input id="surveySaveButton" type="button" onclick="save('<?php echo $id; ?>')" class='surveyButton' value='Save' />
346        <?php
347    }
348
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.';
354        ?>
355        <textarea id="surveyDescription" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)"><?php echo $value; ?></textarea>
356        <?php
357    }
358
359    private function questionCreationForm() {
360        ?>
361        <div id='questionsDiv'>
362        </div>
363        <?php
364        if (isset($this->survey)) {
365            foreach ($this->survey->questions as $numQ => $question) {
366                ?>
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
390                </script>
391                <?php
392            }
393        } else {
394            ?>
395            <script type="text/javascript"> addQuestion();</script>
396            <?php
397        }
398    }
399
400    private static function addQuestionButton() {
401        ?>
402        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
403        <?php
404    }
405
406    private static function removeLastQuestionButton() {
407        ?>
408        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
409        <?php
410    }
411
412    private static function submitbutton() {
413        ?>
414        <input type="submit" name="submitSurvey"
415               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
416        <?php
417    }
418
419}
420?>
Note: See TracBrowser for help on using the repository browser.