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
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
11    public function __construct() {
[11]12        SurveyCreationTool::javascript();
13        ?>
[17]14
[11]15        <div id="surveyCreation"><form action="submitsurvey.php" method="post">
16                <?php
17                SurveyCreationTool::titleBox();
18                SurveyCreationTool::descriptionBox();
19                SurveyCreationTool::questionCreationForm();
[17]20                SurveyCreationTool::addQuestionButton();
[11]21                SurveyCreationTool::submitButton();
22                ?></form></div>
23        <?php
[10]24    }
25
[11]26    private static function javascript() {
27        ?>
28        <script type="text/javascript">
[17]29            var questionCount = 1;           
[18]30                                                                                   
[17]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>" +
[18]45                    "<option value='int'>Integer</option>" +
46                    "<option value='mc'>Multiple choice</option>" +
[17]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>";
[18]53                                                                               
[17]54                questionDiv.innerHTML = htmlStr;
[18]55                                                                               
[17]56                return questionDiv;
57            }
[18]58                                                                                   
[11]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            }
[18]68                                                                                                                   
[11]69            function handleBlur(input)
[17]70            {       
71                var surveyTitle = document.getElementById('surveyTitle');
72                var surveyDescription = document.getElementById('surveyDescription');
[18]73                                                                                                   
[11]74                if (input.value == "")
75                {
76                    input.style.color = "gray";
77                    input.clicked = null;
[18]78                                                                                                                           
[17]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            }
[18]89                                                                   
[17]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;
[18]96                       
[17]97                switch (type) {
[18]98                    case 'mc':
[17]99                        answersDiv.innerHTML = "";
100                        addOption(select.id);
[18]101                                                       
[17]102                        break;
103                    case 'text':
104                        answersDiv.innerHTML = "";
105                        break;
[18]106                    case 'int':   
107                        answersDiv.innerHTML = "";
108                        //min max
109                        minMax(select.id);
110                        break;
[17]111                    case 'checkboxes':
[18]112                        answersDiv.innerHTML = ""; 
113                        addOption(select.id);
[17]114                        break;
115                    case 'scale':
[18]116                        answersDiv.innerHTML = "";
117                        //what scale (min max incr)
[17]118                        break;
119                    default:
120                        break;
[11]121                }
[17]122
[11]123            }
[18]124                                                   
[17]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";
[18]132                                               
[17]133                var htmlStr = "<input type='text' name='q" +
134                    questionNumber + "ans" + answerCount + "' value='Option " + answerCount + "' />";
[18]135                       
[17]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                }
[18]142                                                       
[17]143                answerDiv.innerHTML = htmlStr;
[18]144                                                       
[17]145                answersDiv.appendChild(answerDiv);
146                answersDiv.answerCount++;
147            }
[18]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                                                                 
[17]161            function addQuestion()
162            {
163                var questionsDiv = document.getElementById('questionsDiv');
164                var newQuestion = getNewQuestion();
[18]165                                                                               
[17]166                questionsDiv.appendChild(newQuestion);
167                questionCount++;
168            }
[18]169           
170                                                                                                                                                                                           
[11]171        </script>
172        <?php
173    }
174
[10]175    private static function titleBox() {
[11]176        ?>
[17]177        <input type="text" id="surveyTitle" name="surveyTitle" value="Untitled Survey" size="30" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
[11]178        <?php
[10]179    }
[11]180
181    private static function descriptionBox() {
182        ?>
[17]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>
[11]184        <?php
[10]185    }
[11]186
187    private static function questionCreationForm() {
[17]188        ?>
189        <div id='questionsDiv'>
190        </div>
191        <script type="text/javascript"> addQuestion(); </script>
192        <?php
[11]193    }
194
195    private static function addQuestionButton() {
[17]196        ?>
197        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
198        <?php
[10]199    }
200
[11]201    private static function submitbutton() {
202        ?>
203        <input type="submit" name="submitSurvey"
[17]204               value="Submit Survey!" class="surveyButton" id="submitSurvey"/>
[11]205        <?php
206    }
207
[10]208}
209?>
Note: See TracBrowser for help on using the repository browser.