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

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

Added integer.

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