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

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

Saving now gives the link to participant survey.

File size: 27.8 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
[32]18        if (isset($this->survey->id))
[30]19            SurveyCreationTool::javascript($this->survey->id);
20        else
21            SurveyCreationTool::javascript();
[11]22        ?>
[17]23
[50]24        <div id="surveyCreation"><form id="survey" action="surveycreation.php" method="post">
[11]25                <?php
[29]26                $this->surveyHead();
27                $this->questionCreationForm();
[17]28                SurveyCreationTool::addQuestionButton();
[19]29                SurveyCreationTool::removeLastQuestionButton();
[11]30                ?></form></div>
31        <?php
[10]32    }
33
[30]34    private static function javascript($id = null) {
[11]35        ?>
36        <script type="text/javascript">
[24]37            /* autosave every 3 minutes */
[30]38            setTimeout("save('<?php echo $id; ?>')", 180000);
[36]39                                                                                                                                                                           
[30]40            var questionCount = 1;         
[36]41                                                                                                                                                                                                                                                                                                                   
[30]42            function getNewQuestion(title, description)
[17]43            {
[33]44                if (title == null)
[30]45                    var title = 'Untitled Question';
[36]46                                                                                                               
[30]47                if (description != null)
48                    var description = description;
49                else
50                    var description = 'Write a question description here.';
[36]51                                                                                                                                   
[30]52                var questionDiv = document.createElement("div");
53                var htmlStr =
54                    "<div id='question" + questionCount + "' class='question'>" +
55                    "<table class='questionTable'>" +
56                    "<th>Question " + questionCount + "</th>" +
57                    "<tr><td><label for='questionTitle'>Title</label></td>" +
58                    "<td><input type='text' class='questionTitle' name='questionTitle" + questionCount + "' onfocus='handleFocus(this)' size='30' value='" + title + "' /></td></tr>" +
59                    "<tr><td><label for='questionDescription'>Description</label></td>" +
60                    "<td><input type='text' class='questionDescription' name='questionDescription" + questionCount + "' onfocus='handleFocus(this)' size='60' value='" + description + "' /></td>" +
61                    "<tr><td><label for='questionType'>Answer type</label></td>" +
62                    "<td><select id='" + questionCount + "' name='questionType" + questionCount + "' onchange='handleType(this)'><br />"+
63                    "<option value='text' selected='selected'>Text</option>" +
64                    "<option value='int'>Integer</option>" +
65                    "<option value='mc'>Multiple choice</option>" +
66                    "<option value='checkboxes'>Checkboxes</option>" +
67                    "<option value='scale'>Scale</option>" +
68                    "</select></td></tr>" +
69                    "</table>" +
70                    "<div id='answersDiv" + questionCount + "' class='answersDiv'></div>" +
71                    "</div>";
[36]72                                                                                                                                                                                                                                                                                                               
[30]73                questionDiv.innerHTML = htmlStr;
[36]74                                                                                                                                                                                                                                                                                                               
[30]75                return questionDiv;
[17]76            }
[36]77                                                                                                                                                                                                                                                                                                                   
[30]78            function handleFocus(input)
[11]79            {
[34]80                /* this is such ugly code it makes me sad */
81                if (input.clicked == null &&
82                    (input.value == "Untitled Survey"
[35]83                    || input.value == "Write a helpful description for this survey here."
84                    || input.value == "Write a question description here."
85                    || input.value == "Untitled Question"))
[30]86                {
87                    input.value = "";
88                    input.style.color = "black";   
89                    input.clicked = true;
90                }
91            }
[36]92                                                                                                                                                                                                                                                                                                                                                   
[30]93            function handleBlur(input)
94            {       
95                var surveyTitle = document.getElementById('surveyTitle');
96                var surveyDescription = document.getElementById('surveyDescription');
[36]97                                                                                                                                                                                                                                                                                                                                   
[30]98                if (input.value == "")
[11]99                {
[30]100                    input.style.color = "gray";
101                    input.clicked = null;
[36]102                                                                                                                                                                                                                                                                                                                                                           
[30]103                    if (input == surveyTitle)
104                    {
105                        input.value = "Untitled Survey";
106                    }
107                    else if (input == surveyDescription)
108                    {
109                        input.value = "Write a helpful description for this survey here.";
110                    }
111                }                           
112            }
[36]113                                                                                                                                                                                                                                                                                                   
[32]114            function handleType(select, answers)
[30]115            {
[32]116                var numQ = select.id; // questionNumber
[30]117                var type = select.valueOf().value;
[32]118                var answersDiv = document.getElementById("answersDiv" + numQ );
[36]119               
[37]120                removeQuestionID(numQ);
121               
[30]122                answersDiv.answerCount = 1;
123                answersDiv.clicked = null;
[36]124                                                                                                                                                                                                                                                       
[30]125                switch (type) {
126                    case 'mc':
127                        answersDiv.innerHTML = "";
[32]128                        if (answers != null)
129                        {
130                            for (var i = 1; i < answers.length; i++)
131                            {
132                                addOption(numQ, answers[i]);
133                            }
134                        }
135                        else
136                            addOption(numQ);
[36]137                                                                                                                                                                                                                                                                                       
[30]138                        break;
139                    case 'text':
140                        answersDiv.innerHTML = "";
141                        break;
142                    case 'int':   
143                        answersDiv.innerHTML = "";
[36]144                                                                                                               
[30]145                        //min max
[32]146                        if (answers != null)
147                            minMax(numQ, answers[1], answers[2]);
148                        else
149                            minMax(numQ);
[30]150                        break;
151                    case 'checkboxes':
[32]152                        answersDiv.innerHTML = "";
153                        if (answers != null)
154                        {
155                            for (var i = 1; i < answers.length; i++)
156                            {
157                                addOption(numQ, answers[i]);
158                            }
159                        }
160                        else
161                            addOption(numQ);
[30]162                        break;
163                    case 'scale':
164                        answersDiv.innerHTML = "";
165                        //what scale (min max incr)
[32]166                        if (answers != null)
167                            minMaxIncr(numQ, answers[1], answers[2], answers[3]);
168                        else
169                            minMaxIncr(numQ);
[30]170                        break;
171                    default:
172                        break;
[11]173                }
[30]174
175            }
[37]176           
177            function handleAnswerChange(questionNumber)
178            {
179                removeQuestionID(questionNumber);
180            }
181           
182            function removeQuestionID(questionNumber)
183            {
184                /* When type changes, remove question ID, because question changes */
185                var questionIDInput = document.getElementById("questionID" + questionNumber);
186                if (questionIDInput != null)
187                {
188                    var questionsDiv = document.getElementById("questionsDiv");
189                    questionsDiv.removeChild(questionIDInput);
190                }
191
192            }
193                     
[36]194                                                                                                                                                                                                                                                                           
[32]195            function addOption(questionNumber, optionStr)
[30]196            {       
197                var answersDiv = document.getElementById("answersDiv" + questionNumber);
198                var answerCount = answersDiv.answerCount;
199                var answerDiv = document.createElement("div");
[36]200                                               
[32]201                if (optionStr == null)
202                    var optionStr = "Option " + answerCount;
[36]203                                               
204                                               
[30]205                answerDiv.className = "answerDiv";
[36]206                                                                                                                                                                                                                                                                               
[30]207                var htmlStr = "<input type='text' name='q" +
[37]208                    questionNumber + "ans" + answerCount + "' onchange='handleAnswerChange(" + questionNumber + ")' value='" + optionStr + "' />";
[36]209                                                                                                                                                                                                                                                       
[30]210                if (answersDiv.clicked == null)
[11]211                {
[30]212                    htmlStr += "<input type='button' id='addOpt'"
213                        + " class='surveyButton' onclick='addOption(" + questionNumber + ")' value='Add Option' />" +
214                        "<input type='button' class='surveyButton' onclick='removeOption(" + questionNumber + ")' value='x' />";
[36]215                                                                                                                                                                                                                           
[30]216                    answersDiv.clicked = true;
[25]217                }
[36]218                                                                                                                                                                                                                                                                                       
[30]219                answerDiv.innerHTML = htmlStr;
[36]220                                                                                                                                                                                                                       
[30]221                answerDiv.prev = answersDiv.lastAnswer; //singly linked list
222                answersDiv.lastAnswer = answerDiv;
[36]223                                                                                                                                                                                                                                                                                       
[30]224                answersDiv.appendChild(answerDiv);
[49]225                answersDiv.answerCount++;
[37]226               
227                handleAnswerChange(questionNumber);
[17]228            }
[36]229                                                                                                                                                                                                                   
[30]230            function removeOption(questionNumber)
[17]231            {
[30]232                var answersDiv = document.getElementById("answersDiv" + questionNumber);
[36]233                                                                                                                                                                                                                       
[30]234                if (answersDiv.lastAnswer.prev != null)
235                {
236                    answersDiv.removeChild(answersDiv.lastAnswer);
237                    answersDiv.lastAnswer = answersDiv.lastAnswer.prev;
238                    answersDiv.answerCount--;
239                }
[37]240               
241                handleAnswerChange(questionNumber);
[11]242            }
[36]243                                                                                                                                                                                                                                           
[32]244            function minMax(questionNumber, min, max)
[20]245            {
[32]246                if (min == null)
247                    var min = '';
248                if (max == null)
249                    var max = '';
[36]250                                                                                                       
[30]251                var answersDiv = document.getElementById("answersDiv" + questionNumber);
[36]252                                                                                                                                                                                                                                               
[30]253                var answerDiv = document.createElement("div");
[36]254                                                                                                                                                                                                               
[30]255                answerDiv.className = "answerDiv";
[32]256                answerDiv.innerHTML = "<label for='min'>Min</label><input type='text' value='" + min + "' name='q" + questionNumber + "ans1' />" +
257                    "<label for='max'>Max</label><input type='text' value='" + max + "' name='q" + questionNumber + "ans2' />";
[36]258                                                                                                                                                                                                                                           
[30]259                answersDiv.appendChild(answerDiv);
260            } 
[36]261                                                                                                                                                                                                                                           
[32]262            function minMaxIncr(questionNumber, left, right, incr)
[30]263            {
[32]264                if (left == null)
265                    var left = '';
266                if (right == null)
267                    var right = '';
268                if (incr == null)
269                    var incr = '';
[36]270                                                       
[30]271                var answersDiv = document.getElementById("answersDiv" + questionNumber);
[36]272                                                                                                                                                                                                                                               
[30]273                var answerDiv = document.createElement("div");
274                answerDiv.className = "answerDiv";
[37]275                answerDiv.innerHTML = "<label>Left label</label><input type='text' value='" + left + "' onchange='handleAnswerChange(" + questionNumber + ")' name='q" + questionNumber + "ans1' />" +
276                    "<label>Right label</label><input type='text' value='" + right + "' onchange='handleAnswerChange(" + questionNumber + ")' name='q" + questionNumber + "ans2' />" +
277                    "<label>Scale count</label><input type='text' value='" + incr + "' onchange='handleAnswerChange(" + questionNumber + ")' name='q" + questionNumber + "ans3' />" +
[30]278                    "<div id='inputCheckFeedback'" + questionNumber + "";
[36]279                                                                                                                                                                                                                                           
[30]280                answersDiv.appendChild(answerDiv);
281            }         
[36]282                                                                                                                                                                                                                                                                                                 
283                                                                                                                                                                                                                                                                                                         
[30]284            function addQuestion(title, description)
285            {
286                var questionsDiv = document.getElementById('questionsDiv');
287                var newQuestion = getNewQuestion(title, description);
[36]288                                                                                                                                                                                                                                       
[19]289
[30]290                newQuestion.prev = document.lastQuestion;
291                document.lastQuestion = newQuestion;
[19]292
[30]293                questionsDiv.appendChild(newQuestion);
294                questionCount++;
295            }
[36]296                                                                                                                                                                                                                                   
[30]297            function removeLastQuestion()
[19]298            {
[30]299                var questionsDiv = document.getElementById('questionsDiv');
[36]300                                                                                                                                                                                                                                       
[30]301                if (document.lastQuestion.prev != null)
302                {
303                    questionsDiv.removeChild(document.lastQuestion);
304                    document.lastQuestion = document.lastQuestion.prev;
305                    questionCount--; 
306                }
307                else
308                {
309                    // do nothing
310                }               
[19]311            }
[36]312                                                                                                                                                                                                             
[30]313            function save(surveyID)
[24]314            {
[30]315                var form = document.getElementById('survey');
316                var questionsDiv = document.getElementById('questionsDiv');
317                form.action = "surveycreation.php";
[36]318                                                                                                                                                                               
[30]319                /* extra time stamp */
320                var date = new Date();
321                var minutes = date.getUTCMinutes();
322                if (minutes < 10)
323                    minutes = "0" + minutes;
324                var timeStamp = date.getHours() + ":" + minutes;
325                var timeStampInput = document.createElement("input");
326                timeStampInput.name = "timeStamp";
327                timeStampInput.value = timeStamp;
328                timeStampInput.type = "hidden";
[36]329                                                                                                                                   
[30]330                var surveyIDInput = document.createElement("input");
331                surveyIDInput.name = "surveyID";
332                surveyIDInput.value = surveyID;
333                surveyIDInput.type = "hidden";
[36]334                                                                                                                                                             
[30]335                questionsDiv.appendChild(timeStampInput);
336                questionsDiv.appendChild(surveyIDInput);
337                form.submit();
338            }
[50]339           
340            function selectAll(input)
341            {
342                input.select();
343            }
344                                                                                                                                                                                                                                                                                                                                                                                                           
[11]345        </script>
346        <?php
347    }
348
[29]349    private function surveyHead() {
[24]350        ?><div id="surveyHead">
351        <?php
[29]352        $this->titleBox();
353        $this->saveSurvey();
354        $this->descriptionBox();
[50]355        $this->surveyLink();
[24]356        ?></div>
357        <?php
358    }
359
[29]360    private function titleBox() {
361        if (isset($this->survey->title))
362            $value = $this->survey->title;
363        else
364            $value = 'Untitled Survey';
[11]365        ?>
[29]366        <input type="text" id="surveyTitle" name="surveyTitle" value="<?php echo $value ?>" onblur="handleBlur(this)" onfocus="handleFocus(this)" />
[11]367        <?php
[10]368    }
[11]369
[29]370    private function saveSurvey() {
371        if (isset($this->timeStamp))
372            echo "<div id='timeStamp'>Last saved " . $this->timeStamp . '</div>';
373        if (isset($this->survey->id))
374            $id = $this->survey->id;
375        else
376            $id = null;
[24]377        ?>
[29]378        <input id="surveySaveButton" type="button" onclick="save('<?php echo $id; ?>')" class='surveyButton' value='Save' />
[24]379        <?php
380    }
381
[29]382    private function descriptionBox() {
383        if (isset($this->survey->description))
384            $value = $this->survey->description;
385        else
386            $value = 'Write a helpful description for this survey here.';
[11]387        ?>
[29]388        <textarea id="surveyDescription" name="surveyDescription" onblur="handleBlur(this)" onfocus="handleFocus(this)"><?php echo $value; ?></textarea>
[11]389        <?php
[10]390    }
[50]391   
392    private function surveyLink() {
393        if (isset($this->survey))
394        {
395                $surveyID = $this->survey->id;
396               
397                $link = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) .
398                    '/survey.php?id=' . $surveyID;
399        ?>
400        <input type="text" value="<?php echo $link; ?>" id="surveyLink" onclick="selectAll(this)">
401        <?php
402        }
403    }
[11]404
[29]405    private function questionCreationForm() {
[17]406        ?>
407        <div id='questionsDiv'>
408        </div>
409        <?php
[30]410        if (isset($this->survey)) {
[32]411            foreach ($this->survey->questions as $numQ => $question) {
[30]412                ?>
[36]413                <script type="text/javascript">
[32]414                    var answers = new Array();
415                <?php
416                /* Put all answers in js array */
417                foreach ($question->answers as $numA => $answer) {
418                    echo "answers[" . $numA . "] = '" . $answer . "'; ";
419                }
420                ?>
[36]421                                                                                                   
[32]422                    addQuestion('<?php echo $question->title; ?>', '<?php echo $question->description; ?>');
[36]423                                                                                                                                                                                                   
[32]424                    var select = document.getElementById('<?php echo $numQ; ?>');
425                    var type = '<?php echo $question->type; ?>';
426
427                    for (var i = 0; i < select.options.length; i++) {
428                        if (select.options[i].value == type)
429                        {
430                            select.options[i].selected = true;
431                            handleType(select, answers)
432                            break;
433                        }
[36]434                    }     
435                                   
436                                   
437                    /* questionID stuff */
438                    var questionIDInput = document.createElement("input");
439                    questionIDInput.id = "questionID<?php echo $numQ; ?>";
440                    questionIDInput.name = "questionID<?php echo $numQ; ?>";
441                    questionIDInput.value = "<?php echo $question->id; ?>";
442                    questionIDInput.type = "hidden";
443                                                                                                                                                                                     
444                    document.getElementById("questionsDiv").appendChild(questionIDInput);
[32]445
[30]446                </script>
447                <?php
448            }
449        } else {
450            ?>
451            <script type="text/javascript"> addQuestion();</script>
452            <?php
453        }
[11]454    }
455
456    private static function addQuestionButton() {
[17]457        ?>
458        <input id="addQuestionButton" type="button" onclick="addQuestion()" value="Add New Question" class="surveyButton" />
459        <?php
[10]460    }
461
[19]462    private static function removeLastQuestionButton() {
463        ?>
464        <input type="button" onclick="removeLastQuestion()" value="x" class="surveyButton" />
465        <?php
466    }
467
[10]468}
469?>
Note: See TracBrowser for help on using the repository browser.