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

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

All gets rebuilt except for answers and answer type

File size: 19.0 KB
RevLine 
[10]1<?php
2
3/**
4 * Description of SurveyTool
5 *
6 * SurveyTool is where the survey can be created.
7 *
8 */
9class SurveyCreationTool {
10
[29]11    private $survey;
12    private $timeStamp;
13
[24]14    public function __construct($survey = null, $timeStamp = null) {
[29]15        $this->survey = $survey;
16        $this->timeStamp = $timeStamp;
17
[30]18        if( isset($this->survey->id))
19            SurveyCreationTool::javascript($this->survey->id);
20        else
21            SurveyCreationTool::javascript();
[11]22        ?>
[17]23
[24]24        <div id="surveyCreation"><form id="survey" action="submitsurvey.php" method="post">
[11]25                <?php
[29]26                $this->surveyHead();
27                $this->questionCreationForm();
[17]28                SurveyCreationTool::addQuestionButton();
[19]29                SurveyCreationTool::removeLastQuestionButton();
[11]30                SurveyCreationTool::submitButton();
31                ?></form></div>
32        <?php
[10]33    }
34
[30]35    private static function javascript($id = null) {
[11]36        ?>
37        <script type="text/javascript">
[24]38            /* autosave every 3 minutes */
[30]39            setTimeout("save('<?php echo $id; ?>')", 180000);
40                                                                           
41            var questionCount = 1;         
42                                                                                                                                                                                                                   
43            function getNewQuestion(title, description)
[17]44            {
[30]45                if (title != null)
46                    var title = title;
47                else
48                    var title = 'Untitled Question';
49               
50                if (description != null)
51                    var description = description;
52                else
53                    var description = 'Write a question description here.';
54                                   
55                var questionDiv = document.createElement("div");
56                var htmlStr =
57                    "<div id='question" + questionCount + "' class='question'>" +
58                    "<table class='questionTable'>" +
59                    "<th>Question " + questionCount + "</th>" +
60                    "<tr><td><label for='questionTitle'>Title</label></td>" +
61                    "<td><input type='text' class='questionTitle' name='questionTitle" + questionCount + "' onfocus='handleFocus(this)' size='30' value='" + title + "' /></td></tr>" +
62                    "<tr><td><label for='questionDescription'>Description</label></td>" +
63                    "<td><input type='text' class='questionDescription' name='questionDescription" + questionCount + "' onfocus='handleFocus(this)' size='60' value='" + description + "' /></td>" +
64                    "<tr><td><label for='questionType'>Answer type</label></td>" +
65                    "<td><select id='" + questionCount + "' name='questionType" + questionCount + "' onchange='handleType(this)'><br />"+
66                    "<option value='text' selected='selected'>Text</option>" +
67                    "<option value='int'>Integer</option>" +
68                    "<option value='mc'>Multiple choice</option>" +
69                    "<option value='checkboxes'>Checkboxes</option>" +
70                    "<option value='scale'>Scale</option>" +
71                    "</select></td></tr>" +
72                    "</table>" +
73                    "<div id='answersDiv" + questionCount + "' class='answersDiv'></div>" +
74                    "</div>";
75                                                                                                                                                                                                               
76                questionDiv.innerHTML = htmlStr;
77                                                                                                                                                                                                               
78                return questionDiv;
[17]79            }
[30]80                                                                                                                                                                                                                   
81            function handleFocus(input)
[11]82            {
[30]83                if (input.clicked == null)
84                {
85                    input.value = "";
86                    input.style.color = "black";   
87                    input.clicked = true;
88                }
89            }
90                                                                                                                                                                                                                                                   
91            function handleBlur(input)
92            {       
93                var surveyTitle = document.getElementById('surveyTitle');
94                var surveyDescription = document.getElementById('surveyDescription');
[29]95                                                                                                                                                                                                                                   
[30]96                if (input.value == "")
[11]97                {
[30]98                    input.style.color = "gray";
99                    input.clicked = null;
100                                                                                                                                                                                                                                                           
101                    if (input == surveyTitle)
102                    {
103                        input.value = "Untitled Survey";
104                    }
105                    else if (input == surveyDescription)
106                    {
107                        input.value = "Write a helpful description for this survey here.";
108                    }
109                }                           
110            }
111                                                                                                                                                                                                   
112            function handleType(select)
113            {
114                var type = select.valueOf().value;
115                var answersDiv = document.getElementById("answersDiv" +  select.id);
116                answersDiv.answerCount = 1;
117                answersDiv.clicked = null;
118                                                                                                                                                       
119                switch (type) {
120                    case 'mc':
121                        answersDiv.innerHTML = "";
122                        addOption(select.id);
123                                                                                                                                                                                       
124                        break;
125                    case 'text':
126                        answersDiv.innerHTML = "";
127                        break;
128                    case 'int':   
129                        answersDiv.innerHTML = "";
130                        //min max
131                        minMax(select.id);
132                        break;
133                    case 'checkboxes':
134                        answersDiv.innerHTML = ""; 
135                        addOption(select.id);
136                        break;
137                    case 'scale':
138                        answersDiv.innerHTML = "";
139                        //what scale (min max incr)
140                        minMaxIncr(select.id);
141                        break;
142                    default:
143                        break;
[11]144                }
[30]145
146            }
147                                                                                                                                                                                   
148                                                                                                                                                                           
149            function addOption(questionNumber)
150            {       
151                var answersDiv = document.getElementById("answersDiv" + questionNumber);
152                var answerCount = answersDiv.answerCount;
153                var answerDiv = document.createElement("div");
154                answerDiv.className = "answerDiv";
155                                                                                                                                                                               
156                var htmlStr = "<input type='text' name='q" +
157                    questionNumber + "ans" + answerCount + "' value='Option " + answerCount + "' />";
158                                                                                                                                                       
159                if (answersDiv.clicked == null)
[11]160                {
[30]161                    htmlStr += "<input type='button' id='addOpt'"
162                        + " class='surveyButton' onclick='addOption(" + questionNumber + ")' value='Add Option' />" +
163                        "<input type='button' class='surveyButton' onclick='removeOption(" + questionNumber + ")' value='x' />";
164                                                                                                                           
165                    answersDiv.clicked = true;
[25]166                }
[30]167                                                                                                                                                                                       
168                answerDiv.innerHTML = htmlStr;
169                                                                                                                       
170                answerDiv.prev = answersDiv.lastAnswer; //singly linked list
171                answersDiv.lastAnswer = answerDiv;
172                                                                                                                                                                                       
173                answersDiv.appendChild(answerDiv);
174                answersDiv.answerCount++;
[17]175            }
[30]176                                                                                                                   
177            function removeOption(questionNumber)
[17]178            {
[30]179                var answersDiv = document.getElementById("answersDiv" + questionNumber);
180                                                                                                                       
181                if (answersDiv.lastAnswer.prev != null)
182                {
183                    answersDiv.removeChild(answersDiv.lastAnswer);
184                    answersDiv.lastAnswer = answersDiv.lastAnswer.prev;
185                    answersDiv.answerCount--;
186                }
[11]187            }
[30]188                                                                                                                                           
189            function minMax(questionNumber)
[20]190            {
[30]191                var answersDiv = document.getElementById("answersDiv" + questionNumber);
192                                                                                                                                               
193                var answerDiv = document.createElement("div");
[29]194                                                                                                               
[30]195                answerDiv.className = "answerDiv";
196                answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' name='q" + questionNumber + "ans1' />" +
197                    "<label for='max'>Max</label><input type='text' name='q" + questionNumber + "ans2' />";
198                                                                                                                                           
199                answersDiv.appendChild(answerDiv);
200            } 
201                                                                                                                                           
202            function minMaxIncr(questionNumber)
203            {
204                var answersDiv = document.getElementById("answersDiv" + questionNumber);
205                                                                                                                                               
206                var answerDiv = document.createElement("div");
207                answerDiv.className = "answerDiv";
208                answerDiv.innerHTML = "<label>Left label</label><input type='text' name='q" + questionNumber + "ans1' />" +
209                    "<label>Right label</label><input type='text' name='q" + questionNumber + "ans2' />" +
210                    "<label>Scale count</label><input type='text' name='q" + questionNumber + "ans3' />" +
211                    "<div id='inputCheckFeedback'" + questionNumber + "";
212                                                                                                                                           
213                answersDiv.appendChild(answerDiv);
214            }         
215                                                                                                                                                                                                 
216                                                                                                                                                                                                         
217            function addQuestion(title, description)
218            {
219                var questionsDiv = document.getElementById('questionsDiv');
220                var newQuestion = getNewQuestion(title, description);
221                                                                                                                                       
[19]222
[30]223                newQuestion.prev = document.lastQuestion;
224                document.lastQuestion = newQuestion;
[19]225
[30]226                questionsDiv.appendChild(newQuestion);
227                questionCount++;
228            }
229                                                                                                                                   
230            function removeLastQuestion()
[19]231            {
[30]232                var questionsDiv = document.getElementById('questionsDiv');
233                                                                                                                                       
234                if (document.lastQuestion.prev != null)
235                {
236                    questionsDiv.removeChild(document.lastQuestion);
237                    document.lastQuestion = document.lastQuestion.prev;
238                    questionCount--; 
239                }
240                else
241                {
242                    // do nothing
243                }               
[19]244            }
[30]245                                                                                                             
246            function save(surveyID)
[24]247            {
[30]248                var form = document.getElementById('survey');
249                var questionsDiv = document.getElementById('questionsDiv');
250                form.action = "surveycreation.php";
251                                                                               
252                /* extra time stamp */
253                var date = new Date();
254                var minutes = date.getUTCMinutes();
255                if (minutes < 10)
256                    minutes = "0" + minutes;
257                var timeStamp = date.getHours() + ":" + minutes;
258                var timeStampInput = document.createElement("input");
259                timeStampInput.name = "timeStamp";
260                timeStampInput.value = timeStamp;
261                timeStampInput.type = "hidden";
262                                   
263                var surveyIDInput = document.createElement("input");
264                surveyIDInput.name = "surveyID";
265                surveyIDInput.value = surveyID;
266                surveyIDInput.type = "hidden";
267                                                             
268                questionsDiv.appendChild(timeStampInput);
269                questionsDiv.appendChild(surveyIDInput);
270                form.submit();
271            }
272                                                                                                                                           
273                                                                                                                                                                                                                                                                                                                           
[11]274        </script>
275        <?php
276    }
277
[29]278    private function surveyHead() {
[24]279        ?><div id="surveyHead">
280        <?php
[29]281        $this->titleBox();
282        $this->saveSurvey();
283        $this->descriptionBox();
[24]284        ?></div>
285        <?php
286    }
287
[29]288    private function titleBox() {
289        if (isset($this->survey->title))
290            $value = $this->survey->title;
291        else
292            $value = 'Untitled Survey';
[11]293        ?>
[29]294        <input type="text" id="surveyTitle" name="surveyTitle" value="<?php echo $value ?>" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
[11]295        <?php
[10]296    }
[11]297
[29]298    private function saveSurvey() {
299        if (isset($this->timeStamp))
300            echo "<div id='timeStamp'>Last saved " . $this->timeStamp . '</div>';
301        if (isset($this->survey->id))
302            $id = $this->survey->id;
303        else
304            $id = null;
[24]305        ?>
[29]306        <input id="surveySaveButton" type="button" onclick="save('<?php echo $id; ?>')" class='surveyButton' value='Save' />
[24]307        <?php
308    }
309
[29]310    private function descriptionBox() {
311        if (isset($this->survey->description))
312            $value = $this->survey->description;
313        else
314            $value = 'Write a helpful description for this survey here.';
[11]315        ?>
[29]316        <textarea id="surveyDescription" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)"><?php echo $value; ?></textarea>
[11]317        <?php
[10]318    }
[11]319
[29]320    private function questionCreationForm() {
[17]321        ?>
322        <div id='questionsDiv'>
323        </div>
324        <?php
[30]325        if (isset($this->survey)) {
326            foreach ($this->survey->questions as $question) {
327                ?>
328                <script type="text/javascript"> addQuestion('<?php echo $question->title; ?>', '<?php echo $question->description; ?>');
329                </script>
330                <?php
331            }
332        } else {
333            ?>
334            <script type="text/javascript"> addQuestion();</script>
335            <?php
336        }
[11]337    }
338
339    private static function addQuestionButton() {
[17]340        ?>
341        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
342        <?php
[10]343    }
344
[19]345    private static function removeLastQuestionButton() {
346        ?>
347        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
348        <?php
349    }
350
[11]351    private static function submitbutton() {
352        ?>
353        <input type="submit" name="submitSurvey"
[17]354               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
[11]355        <?php
356    }
357
[10]358}
359?>
Note: See TracBrowser for help on using the repository browser.