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

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

Now able to delete last question. Scale implemented with incr.

File size: 11.2 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() {
12        SurveyCreationTool::javascript();
13        ?>
14
15        <div id="surveyCreation"><form action="submitsurvey.php" method="post">
16                <?php
17                SurveyCreationTool::titleBox();
18                SurveyCreationTool::descriptionBox();
19                SurveyCreationTool::questionCreationForm();
20                SurveyCreationTool::addQuestionButton();
21                SurveyCreationTool::removeLastQuestionButton();
22                SurveyCreationTool::submitButton();
23                ?></form></div>
24        <?php
25    }
26
27    private static function javascript() {
28        ?>
29        <script type="text/javascript">
30            var questionCount = 1;           
31                                                                                                           
32            function getNewQuestion()
33            {
34                var questionDiv = document.createElement("div");
35                var htmlStr =
36                    "<div id='question" + questionCount + "' class='question'>" +
37                    "<table class='questionTable'>" +
38                    "<th>Question " + questionCount + "</th>" +
39                    "<tr><td><label for='questionTitle'>Title</label></td>" +
40                    "<td><input type='text' class='questionTitle' name='questionTitle" + questionCount + "' size='30' value='Untitled Question' /></td></tr>" +
41                    "<tr><td><label for='questionDescription'>Description</label></td>" +
42                    "<td><input type='text' class='questionDescription' name='questionDescription" + questionCount + "' size='60' value='Write a question description here.' /></td>" +
43                    "<tr><td><label for='questionType'>Answer type</label></td>" +
44                    "<td><select id='" + questionCount + "' name='questionType" + questionCount + "' onchange='handleType(this)'><br />"+
45                    "<option value='text' selected='selected'>Text</option>" +
46                    "<option value='int'>Integer</option>" +
47                    "<option value='mc'>Multiple choice</option>" +
48                    "<option value='checkboxes'>Checkboxes</option>" +
49                    "<option value='scale'>Scale</option>" +
50                    "</select></td></tr>" +
51                    "</table>" +
52                    "<div id='answersDiv" + questionCount + "'></div>" +
53                    "</div>";
54                                                                                                       
55                questionDiv.innerHTML = htmlStr;
56                                                                                                       
57                return questionDiv;
58            }
59                                                                                                           
60            function handleFocus(input)
61            {
62                if (input.clicked == null)
63                {
64                    input.value = "";
65                    input.style.color = "black";   
66                    input.clicked = true;
67                }
68            }
69                                                                                                                                           
70            function handleBlur(input)
71            {       
72                var surveyTitle = document.getElementById('surveyTitle');
73                var surveyDescription = document.getElementById('surveyDescription');
74                                                                                                                           
75                if (input.value == "")
76                {
77                    input.style.color = "gray";
78                    input.clicked = null;
79                                                                                                                                                   
80                    if (input == surveyTitle)
81                    {
82                        input.value = "Untitled Survey";
83                    }
84                    else if (input == surveyDescription)
85                    {
86                        input.value = "Write a helpful description for this survey here.";
87                    }
88                }                           
89            }
90                                                                                           
91            function handleType(select)
92            {
93                var type = select.valueOf().value;
94                var answersDiv = document.getElementById("answersDiv" +  select.id);
95                answersDiv.answerCount = 1;
96                answersDiv.clicked = null;
97                                               
98                switch (type) {
99                    case 'mc':
100                        answersDiv.innerHTML = "";
101                        addOption(select.id);
102                                                                               
103                        break;
104                    case 'text':
105                        answersDiv.innerHTML = "";
106                        break;
107                    case 'int':   
108                        answersDiv.innerHTML = "";
109                        //min max
110                        minMax(select.id);
111                        break;
112                    case 'checkboxes':
113                        answersDiv.innerHTML = ""; 
114                        addOption(select.id);
115                        break;
116                    case 'scale':
117                        answersDiv.innerHTML = "";
118                        //what scale (min max incr)
119                        minMaxIncr(select.id);
120                        break;
121                    default:
122                        break;
123                }
124
125            }
126                                                                           
127                                                                   
128            function addOption(questionNumber)
129            {       
130                var answersDiv = document.getElementById("answersDiv" + questionNumber);
131                var answerCount = answersDiv.answerCount;
132                var answerDiv = document.createElement("div");
133                answerDiv.className = "answerDiv";
134                                                                       
135                var htmlStr = "<input type='text' name='q" +
136                    questionNumber + "ans" + answerCount + "' value='Option " + answerCount + "' />";
137                                               
138                if (answersDiv.clicked == null)
139                {
140                    htmlStr += "<input type='button' id='addOpt'"
141                        + " class='surveyButton' onclick='addOption(" + questionNumber + ")' value='Add Option' />";
142                    answersDiv.clicked = true;
143                }
144                                                                               
145                answerDiv.innerHTML = htmlStr;
146                                                                               
147                answersDiv.appendChild(answerDiv);
148                answersDiv.answerCount++;
149            }
150                                   
151            function minMax(questionNumber)
152            {
153                var answersDiv = document.getElementById("answersDiv" + questionNumber);
154                                       
155                var answerDiv = document.createElement("div");
156                answerDiv.className = "answerDiv";
157                answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' name='min" + questionNumber + "' />" +
158                    "<label for='max'>Max</label><input type='text' name='max" + questionNumber + "' />";
159                                   
160                answersDiv.appendChild(answerDiv);
161            } 
162                                   
163            function minMaxIncr(questionNumber)
164            {
165                minMax(questionNumber);
166                var answersDiv = document.getElementById("answersDiv" + questionNumber);
167                                       
168                var answerDiv = document.createElement("div");
169                answerDiv.className = "answerDiv";
170                answerDiv.innerHTML = "<label for='incr'>Increment by</label><input type='text' name='incr" + questionNumber + "' />";
171                                   
172                answersDiv.appendChild(answerDiv);
173            }         
174                                                                                         
175                                                                                                 
176            function addQuestion()
177            {
178                var questionsDiv = document.getElementById('questionsDiv');
179                var newQuestion = getNewQuestion();
180                               
181
182                newQuestion.prev = document.lastQuestion;
183                document.lastQuestion = newQuestion;
184
185                questionsDiv.appendChild(newQuestion);
186                questionCount++;
187            }
188                           
189            function removeLastQuestion()
190            {
191                var questionsDiv = document.getElementById('questionsDiv');
192                               
193                if (document.lastQuestion.prev != null)
194                {
195                    questionsDiv.removeChild(document.lastQuestion);
196                    document.lastQuestion = document.lastQuestion.prev;
197                    questionCount--; 
198                }
199                else
200                {
201                    // do nothing
202                }               
203            }
204                         
205                                   
206                                                                                                                                                                                                                   
207        </script>
208        <?php
209    }
210
211    private static function titleBox() {
212        ?>
213        <input type="text" id="surveyTitle" name="surveyTitle" value="Untitled Survey" size="30" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
214        <?php
215    }
216
217    private static function descriptionBox() {
218        ?>
219        <textarea id="surveyDescription" rows="3" cols="80" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)">Write a helpful description for this survey here.</textarea>
220        <?php
221    }
222
223    private static function questionCreationForm() {
224        ?>
225        <div id='questionsDiv'>
226        </div>
227        <script type="text/javascript"> addQuestion(); </script>
228        <?php
229    }
230
231    private static function addQuestionButton() {
232        ?>
233        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
234        <?php
235    }
236
237    private static function removeLastQuestionButton() {
238        ?>
239        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
240        <?php
241    }
242
243    private static function submitbutton() {
244        ?>
245        <input type="submit" name="submitSurvey"
246               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
247        <?php
248    }
249
250}
251?>
Note: See TracBrowser for help on using the repository browser.