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

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

Fixed some minor usability issue.

File size: 25.1 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                /* this is such ugly code it makes me sad */
82                if (input.clicked == null &&
83                    (input.value == "Untitled Survey"
84                        || input.value == "Write a helpful description for this survey here."
85                        || input.value == "Write a question description here."
86                        || input.value == "Untitled Question"))
87                {
88                    input.value = "";
89                    input.style.color = "black";   
90                    input.clicked = true;
91                }
92            }
93                                                                                                                                                                                                                                                                                                                           
94            function handleBlur(input)
95            {       
96                var surveyTitle = document.getElementById('surveyTitle');
97                var surveyDescription = document.getElementById('surveyDescription');
98                                                                                                                                                                                                                                                                                                           
99                if (input.value == "")
100                {
101                    input.style.color = "gray";
102                    input.clicked = null;
103                                                                                                                                                                                                                                                                                                                                   
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            }
114                                                                                                                                                                                                                                                                           
115            function handleType(select, answers)
116            {
117                var numQ = select.id; // questionNumber
118                var type = select.valueOf().value;
119                var answersDiv = document.getElementById("answersDiv" + numQ );
120                answersDiv.answerCount = 1;
121                answersDiv.clicked = null;
122                                                                                                                                                                                                                               
123                switch (type) {
124                    case 'mc':
125                        answersDiv.innerHTML = "";
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);
135                                                                                                                                                                                                                                                               
136                        break;
137                    case 'text':
138                        answersDiv.innerHTML = "";
139                        break;
140                    case 'int':   
141                        answersDiv.innerHTML = "";
142                                                                                       
143                        //min max
144                        if (answers != null)
145                            minMax(numQ, answers[1], answers[2]);
146                        else
147                            minMax(numQ);
148                        break;
149                    case 'checkboxes':
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);
160                        break;
161                    case 'scale':
162                        answersDiv.innerHTML = "";
163                        //what scale (min max incr)
164                        if (answers != null)
165                            minMaxIncr(numQ, answers[1], answers[2], answers[3]);
166                        else
167                            minMaxIncr(numQ);
168                        break;
169                    default:
170                        break;
171                }
172
173            }
174                                                                                                                                                                                                                                                           
175                                                                                                                                                                                                                                                   
176            function addOption(questionNumber, optionStr)
177            {       
178                var answersDiv = document.getElementById("answersDiv" + questionNumber);
179                var answerCount = answersDiv.answerCount;
180                var answerDiv = document.createElement("div");
181                       
182                if (optionStr == null)
183                    var optionStr = "Option " + answerCount;
184                       
185                       
186                answerDiv.className = "answerDiv";
187                                                                                                                                                                                                                                                       
188                var htmlStr = "<input type='text' name='q" +
189                    questionNumber + "ans" + answerCount + "' value='" + optionStr + "' />";
190                                                                                                                                                                                                                               
191                if (answersDiv.clicked == null)
192                {
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' />";
196                                                                                                                                                                                                   
197                    answersDiv.clicked = true;
198                }
199                                                                                                                                                                                                                                                               
200                answerDiv.innerHTML = htmlStr;
201                                                                                                                                                                                               
202                answerDiv.prev = answersDiv.lastAnswer; //singly linked list
203                answersDiv.lastAnswer = answerDiv;
204                                                                                                                                                                                                                                                               
205                answersDiv.appendChild(answerDiv);
206                answersDiv.answerCount++;
207            }
208                                                                                                                                                                                           
209            function removeOption(questionNumber)
210            {
211                var answersDiv = document.getElementById("answersDiv" + questionNumber);
212                                                                                                                                                                                               
213                if (answersDiv.lastAnswer.prev != null)
214                {
215                    answersDiv.removeChild(answersDiv.lastAnswer);
216                    answersDiv.lastAnswer = answersDiv.lastAnswer.prev;
217                    answersDiv.answerCount--;
218                }
219            }
220                                                                                                                                                                                                                   
221            function minMax(questionNumber, min, max)
222            {
223                if (min == null)
224                    var min = '';
225                if (max == null)
226                    var max = '';
227                                                                               
228                var answersDiv = document.getElementById("answersDiv" + questionNumber);
229                                                                                                                                                                                                                       
230                var answerDiv = document.createElement("div");
231                                                                                                                                                                                       
232                answerDiv.className = "answerDiv";
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' />";
235                                                                                                                                                                                                                   
236                answersDiv.appendChild(answerDiv);
237            } 
238                                                                                                                                                                                                                   
239            function minMaxIncr(questionNumber, left, right, incr)
240            {
241                if (left == null)
242                    var left = '';
243                if (right == null)
244                    var right = '';
245                if (incr == null)
246                    var incr = '';
247                               
248                var answersDiv = document.getElementById("answersDiv" + questionNumber);
249                                                                                                                                                                                                                       
250                var answerDiv = document.createElement("div");
251                answerDiv.className = "answerDiv";
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' />" +
255                    "<div id='inputCheckFeedback'" + questionNumber + "";
256                                                                                                                                                                                                                   
257                answersDiv.appendChild(answerDiv);
258            }         
259                                                                                                                                                                                                                                                                         
260                                                                                                                                                                                                                                                                                 
261            function addQuestion(title, description)
262            {
263                var questionsDiv = document.getElementById('questionsDiv');
264                var newQuestion = getNewQuestion(title, description);
265                                                                                                                                                                                                               
266
267                newQuestion.prev = document.lastQuestion;
268                document.lastQuestion = newQuestion;
269
270                questionsDiv.appendChild(newQuestion);
271                questionCount++;
272            }
273                                                                                                                                                                                                           
274            function removeLastQuestion()
275            {
276                var questionsDiv = document.getElementById('questionsDiv');
277                                                                                                                                                                                                               
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                }               
288            }
289                                                                                                                                                                                     
290            function save(surveyID)
291            {
292                var form = document.getElementById('survey');
293                var questionsDiv = document.getElementById('questionsDiv');
294                form.action = "surveycreation.php";
295                                                                                                                                                       
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";
306                                                                                                           
307                var surveyIDInput = document.createElement("input");
308                surveyIDInput.name = "surveyID";
309                surveyIDInput.value = surveyID;
310                surveyIDInput.type = "hidden";
311                                                                                                                                     
312                questionsDiv.appendChild(timeStampInput);
313                questionsDiv.appendChild(surveyIDInput);
314                form.submit();
315            }
316                                                                                                                                                                                                                   
317                                                                                                                                                                                                                                                                                                                                                                                                   
318        </script>
319        <?php
320    }
321
322    private function surveyHead() {
323        ?><div id="surveyHead">
324        <?php
325        $this->titleBox();
326        $this->saveSurvey();
327        $this->descriptionBox();
328        ?></div>
329        <?php
330    }
331
332    private function titleBox() {
333        if (isset($this->survey->title))
334            $value = $this->survey->title;
335        else
336            $value = 'Untitled Survey';
337        ?>
338        <input type="text" id="surveyTitle" name="surveyTitle" value="<?php echo $value ?>" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
339        <?php
340    }
341
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;
349        ?>
350        <input id="surveySaveButton" type="button" onclick="save('<?php echo $id; ?>')" class='surveyButton' value='Save' />
351        <?php
352    }
353
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.';
359        ?>
360        <textarea id="surveyDescription" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)"><?php echo $value; ?></textarea>
361        <?php
362    }
363
364    private function questionCreationForm() {
365        ?>
366        <div id='questionsDiv'>
367        </div>
368        <?php
369        if (isset($this->survey)) {
370            foreach ($this->survey->questions as $numQ => $question) {
371                ?>
372                <script type="text/javascript">
373                    var answers = new Array();
374                <?php
375                /* Put all answers in js array */
376                foreach ($question->answers as $numA => $answer) {
377                    echo "answers[" . $numA . "] = '" . $answer . "'; ";
378                }
379                ?>
380                                                   
381                    addQuestion('<?php echo $question->title; ?>', '<?php echo $question->description; ?>');
382                                                                                                                                                   
383                    var select = document.getElementById('<?php echo $numQ; ?>');
384                    var type = '<?php echo $question->type; ?>';
385
386                    for (var i = 0; i < select.options.length; i++) {
387                        if (select.options[i].value == type)
388                        {
389                            select.options[i].selected = true;
390                            handleType(select, answers)
391                            break;
392                        }
393                    }                                                                                                                   
394
395                </script>
396                <?php
397            }
398        } else {
399            ?>
400            <script type="text/javascript"> addQuestion();</script>
401            <?php
402        }
403    }
404
405    private static function addQuestionButton() {
406        ?>
407        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
408        <?php
409    }
410
411    private static function removeLastQuestionButton() {
412        ?>
413        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
414        <?php
415    }
416
417    private static function submitbutton() {
418        ?>
419        <input type="submit" name="submitSurvey"
420               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
421        <?php
422    }
423
424}
425?>
Note: See TracBrowser for help on using the repository browser.