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

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

When answers change, the questionID gets removed so that a new one is made.

File size: 27.7 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               
121                removeQuestionID(numQ);
122               
123                answersDiv.answerCount = 1;
124                answersDiv.clicked = null;
125                                                                                                                                                                                                                                                       
126                switch (type) {
127                    case 'mc':
128                        answersDiv.innerHTML = "";
129                        if (answers != null)
130                        {
131                            for (var i = 1; i < answers.length; i++)
132                            {
133                                addOption(numQ, answers[i]);
134                            }
135                        }
136                        else
137                            addOption(numQ);
138                                                                                                                                                                                                                                                                                       
139                        break;
140                    case 'text':
141                        answersDiv.innerHTML = "";
142                        break;
143                    case 'int':   
144                        answersDiv.innerHTML = "";
145                                                                                                               
146                        //min max
147                        if (answers != null)
148                            minMax(numQ, answers[1], answers[2]);
149                        else
150                            minMax(numQ);
151                        break;
152                    case 'checkboxes':
153                        answersDiv.innerHTML = "";
154                        if (answers != null)
155                        {
156                            for (var i = 1; i < answers.length; i++)
157                            {
158                                addOption(numQ, answers[i]);
159                            }
160                        }
161                        else
162                            addOption(numQ);
163                        break;
164                    case 'scale':
165                        answersDiv.innerHTML = "";
166                        //what scale (min max incr)
167                        if (answers != null)
168                            minMaxIncr(numQ, answers[1], answers[2], answers[3]);
169                        else
170                            minMaxIncr(numQ);
171                        break;
172                    default:
173                        break;
174                }
175
176            }
177           
178            function handleAnswerChange(questionNumber)
179            {
180                removeQuestionID(questionNumber);
181            }
182           
183            function removeQuestionID(questionNumber)
184            {
185                /* When type changes, remove question ID, because question changes */
186                var questionIDInput = document.getElementById("questionID" + questionNumber);
187                if (questionIDInput != null)
188                {
189                    var questionsDiv = document.getElementById("questionsDiv");
190                    questionsDiv.removeChild(questionIDInput);
191                }
192
193            }
194                     
195                                                                                                                                                                                                                                                                           
196            function addOption(questionNumber, optionStr)
197            {       
198                var answersDiv = document.getElementById("answersDiv" + questionNumber);
199                var answerCount = answersDiv.answerCount;
200                var answerDiv = document.createElement("div");
201                                               
202                if (optionStr == null)
203                    var optionStr = "Option " + answerCount;
204                                               
205                                               
206                answerDiv.className = "answerDiv";
207                                                                                                                                                                                                                                                                               
208                var htmlStr = "<input type='text' name='q" +
209                    questionNumber + "ans" + answerCount + "' onchange='handleAnswerChange(" + questionNumber + ")' value='" + optionStr + "' />";
210                                                                                                                                                                                                                                                       
211                if (answersDiv.clicked == null)
212                {
213                    htmlStr += "<input type='button' id='addOpt'"
214                        + " class='surveyButton' onclick='addOption(" + questionNumber + ")' value='Add Option' />" +
215                        "<input type='button' class='surveyButton' onclick='removeOption(" + questionNumber + ")' value='x' />";
216                                                                                                                                                                                                                           
217                    answersDiv.clicked = true;
218                }
219                                                                                                                                                                                                                                                                                       
220                answerDiv.innerHTML = htmlStr;
221                                                                                                                                                                                                                       
222                answerDiv.prev = answersDiv.lastAnswer; //singly linked list
223                answersDiv.lastAnswer = answerDiv;
224                                                                                                                                                                                                                                                                                       
225                answersDiv.appendChild(answerDiv);
226                answersDiv.answerCount
227               
228                handleAnswerChange(questionNumber);
229            }
230                                                                                                                                                                                                                   
231            function removeOption(questionNumber)
232            {
233                var answersDiv = document.getElementById("answersDiv" + questionNumber);
234                                                                                                                                                                                                                       
235                if (answersDiv.lastAnswer.prev != null)
236                {
237                    answersDiv.removeChild(answersDiv.lastAnswer);
238                    answersDiv.lastAnswer = answersDiv.lastAnswer.prev;
239                    answersDiv.answerCount--;
240                }
241               
242                handleAnswerChange(questionNumber);
243            }
244                                                                                                                                                                                                                                           
245            function minMax(questionNumber, min, max)
246            {
247                if (min == null)
248                    var min = '';
249                if (max == null)
250                    var max = '';
251                                                                                                       
252                var answersDiv = document.getElementById("answersDiv" + questionNumber);
253                                                                                                                                                                                                                                               
254                var answerDiv = document.createElement("div");
255                                                                                                                                                                                                               
256                answerDiv.className = "answerDiv";
257                answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' value='" + min + "' name='q" + questionNumber + "ans1' />" +
258                    "<label for='max'>Max</label><input type='text' value='" + max + "' name='q" + questionNumber + "ans2' />";
259                                                                                                                                                                                                                                           
260                answersDiv.appendChild(answerDiv);
261            } 
262                                                                                                                                                                                                                                           
263            function minMaxIncr(questionNumber, left, right, incr)
264            {
265                if (left == null)
266                    var left = '';
267                if (right == null)
268                    var right = '';
269                if (incr == null)
270                    var incr = '';
271                                                       
272                var answersDiv = document.getElementById("answersDiv" + questionNumber);
273                                                                                                                                                                                                                                               
274                var answerDiv = document.createElement("div");
275                answerDiv.className = "answerDiv";
276                answerDiv.innerHTML = "<label>Left label</label><input type='text' value='" + left + "' onchange='handleAnswerChange(" + questionNumber + ")' name='q" + questionNumber + "ans1' />" +
277                    "<label>Right label</label><input type='text' value='" + right + "' onchange='handleAnswerChange(" + questionNumber + ")' name='q" + questionNumber + "ans2' />" +
278                    "<label>Scale count</label><input type='text' value='" + incr + "' onchange='handleAnswerChange(" + questionNumber + ")' name='q" + questionNumber + "ans3' />" +
279                    "<div id='inputCheckFeedback'" + questionNumber + "";
280                                                                                                                                                                                                                                           
281                answersDiv.appendChild(answerDiv);
282            }         
283                                                                                                                                                                                                                                                                                                 
284                                                                                                                                                                                                                                                                                                         
285            function addQuestion(title, description)
286            {
287                var questionsDiv = document.getElementById('questionsDiv');
288                var newQuestion = getNewQuestion(title, description);
289                                                                                                                                                                                                                                       
290
291                newQuestion.prev = document.lastQuestion;
292                document.lastQuestion = newQuestion;
293
294                questionsDiv.appendChild(newQuestion);
295                questionCount++;
296            }
297                                                                                                                                                                                                                                   
298            function removeLastQuestion()
299            {
300                var questionsDiv = document.getElementById('questionsDiv');
301                                                                                                                                                                                                                                       
302                if (document.lastQuestion.prev != null)
303                {
304                    questionsDiv.removeChild(document.lastQuestion);
305                    document.lastQuestion = document.lastQuestion.prev;
306                    questionCount--; 
307                }
308                else
309                {
310                    // do nothing
311                }               
312            }
313                                                                                                                                                                                                             
314            function save(surveyID)
315            {
316                var form = document.getElementById('survey');
317                var questionsDiv = document.getElementById('questionsDiv');
318                form.action = "surveycreation.php";
319                                                                                                                                                                               
320                /* extra time stamp */
321                var date = new Date();
322                var minutes = date.getUTCMinutes();
323                if (minutes < 10)
324                    minutes = "0" + minutes;
325                var timeStamp = date.getHours() + ":" + minutes;
326                var timeStampInput = document.createElement("input");
327                timeStampInput.name = "timeStamp";
328                timeStampInput.value = timeStamp;
329                timeStampInput.type = "hidden";
330                                                                                                                                   
331                var surveyIDInput = document.createElement("input");
332                surveyIDInput.name = "surveyID";
333                surveyIDInput.value = surveyID;
334                surveyIDInput.type = "hidden";
335                                                                                                                                                             
336                questionsDiv.appendChild(timeStampInput);
337                questionsDiv.appendChild(surveyIDInput);
338                form.submit();
339            }
340                                                                                                                                                                                                                                           
341                                                                                                                                                                                                                                                                                                                                                                                                                           
342        </script>
343        <?php
344    }
345
346    private function surveyHead() {
347        ?><div id="surveyHead">
348        <?php
349        $this->titleBox();
350        $this->saveSurvey();
351        $this->descriptionBox();
352        ?></div>
353        <?php
354    }
355
356    private function titleBox() {
357        if (isset($this->survey->title))
358            $value = $this->survey->title;
359        else
360            $value = 'Untitled Survey';
361        ?>
362        <input type="text" id="surveyTitle" name="surveyTitle" value="<?php echo $value ?>" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
363        <?php
364    }
365
366    private function saveSurvey() {
367        if (isset($this->timeStamp))
368            echo "<div id='timeStamp'>Last saved " . $this->timeStamp . '</div>';
369        if (isset($this->survey->id))
370            $id = $this->survey->id;
371        else
372            $id = null;
373        ?>
374        <input id="surveySaveButton" type="button" onclick="save('<?php echo $id; ?>')" class='surveyButton' value='Save' />
375        <?php
376    }
377
378    private function descriptionBox() {
379        if (isset($this->survey->description))
380            $value = $this->survey->description;
381        else
382            $value = 'Write a helpful description for this survey here.';
383        ?>
384        <textarea id="surveyDescription" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)"><?php echo $value; ?></textarea>
385        <?php
386    }
387
388    private function questionCreationForm() {
389        ?>
390        <div id='questionsDiv'>
391        </div>
392        <?php
393        if (isset($this->survey)) {
394            foreach ($this->survey->questions as $numQ => $question) {
395                ?>
396                <script type="text/javascript">
397                    var answers = new Array();
398                <?php
399                /* Put all answers in js array */
400                foreach ($question->answers as $numA => $answer) {
401                    echo "answers[" . $numA . "] = '" . $answer . "'; ";
402                }
403                ?>
404                                                                                                   
405                    addQuestion('<?php echo $question->title; ?>', '<?php echo $question->description; ?>');
406                                                                                                                                                                                                   
407                    var select = document.getElementById('<?php echo $numQ; ?>');
408                    var type = '<?php echo $question->type; ?>';
409
410                    for (var i = 0; i < select.options.length; i++) {
411                        if (select.options[i].value == type)
412                        {
413                            select.options[i].selected = true;
414                            handleType(select, answers)
415                            break;
416                        }
417                    }     
418                                   
419                                   
420                    /* questionID stuff */
421                    var questionIDInput = document.createElement("input");
422                    questionIDInput.id = "questionID<?php echo $numQ; ?>";
423                    questionIDInput.name = "questionID<?php echo $numQ; ?>";
424                    questionIDInput.value = "<?php echo $question->id; ?>";
425                    questionIDInput.type = "hidden";
426                                                                                                                                                                                     
427                    document.getElementById("questionsDiv").appendChild(questionIDInput);
428
429                </script>
430                <?php
431            }
432        } else {
433            ?>
434            <script type="text/javascript"> addQuestion();</script>
435            <?php
436        }
437    }
438
439    private static function addQuestionButton() {
440        ?>
441        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
442        <?php
443    }
444
445    private static function removeLastQuestionButton() {
446        ?>
447        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
448        <?php
449    }
450
451    private static function submitbutton() {
452        ?>
453        <input type="submit" name="submitSurvey"
454               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
455        <?php
456    }
457
458}
459?>
Note: See TracBrowser for help on using the repository browser.