/
ajax.php
332 lines (280 loc) · 11.4 KB
/
ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/*
------------------------------------------------------------------------
Copyright (C) 2015 Albert Weerman
This library/program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
------------------------------------------------------------------------
*/
error_reporting(E_ALL);
ini_set("display_errors", 1);
session_start();
//TEST FOR TIMOUT
//print_r($_SESSION);
//echo '<br/>';
//print_r($_POST);
//echo '<hr>';
if (!isset($_SESSION["primkey"])){
showSurveyTimedOut();
}
$_SESSION['data']['primkey'] = $_SESSION["primkey"];
require_once('globals.php');
//setup metadata path
set_include_path($xi['surveys']['metadata']['path']);
require_once($xi['surveys']['metadata']['surveyslocation']);
//setup survey object
$survey = $surveys[$_SESSION['syid']]; // new Survey();
if (!isset($_SESSION['data']['startts']) && $survey->getDatabase()->findRecord($_SESSION["primkey"]) != null){ //preload data if there is data already for this primkey
$_SESSION['data'] = $survey->getDatabase()->findRecord($_SESSION["primkey"]);
}
if (isset($_POST['button'])){
$_SESSION['buttonClicked'] = $_POST['button'];
}
$_SESSION['lastQuestion'] = array();
if (isset($_SESSION['currentQuestion'])){
$_SESSION['lastQuestion'] = $_SESSION['currentQuestion'];
$currentQuestion = $_SESSION['currentQuestion'];
//STORE OLD ANSWER
if (is_array($_SESSION['currentQuestion'])){ //always an array
foreach($_SESSION['currentQuestion'] as $var){
$postvar = '';
if (isset($_POST[loadFromPostRemoveBrackets($var)]) && $_POST[loadFromPostRemoveBrackets($var)] != '' ){
$postvar = $_POST[loadFromPostRemoveBrackets($var)];
}
elseif (isset($_POST[loadFromPostRemoveBrackets($var) . '_button']) && $_POST[loadFromPostRemoveBrackets($var) . '_button'] != '') { //rf/dk clicked
$postvar = $_POST[loadFromPostRemoveBrackets($var) . '_button'];
}
if ($postvar == ''){
$postvar = '.e'; //set to empty
}
$_SESSION['data'][$var] = $postvar;
$survey->getDatabase()->insertLogRecord($_SESSION["primkey"], $var, $postvar);
}
}
$survey->getDatabase()->updateRecord($_SESSION["primkey"], $_SESSION['data']);
//load all questions LOAD HERE OTHERWISE FILLS FOR THIS Q WON'T BE UPDATED with previous answer!
//require_once($survey->getQuestionsLocation());
require_once($survey->getTemplatesLocation());
require_once($survey->getTypesLocation());
foreach ($survey->getSections() as $section)
require_once($section->getQuestionLocation());
//validate last answer!
$errorcodes = array();
foreach($_SESSION['currentQuestion'] as $var){
$question = getCurrentQuestion($var); //for arrays
if ($question->validate() > 0){
$errorcodes[$question->validate()] = $question;
}
}
if (sizeof($errorcodes) > 0){ //this needs to move to the question!
//Just display the current question again: error display is handled in the question
$currentRouting = $_SESSION['currentRouting'];
}
else {
//HANDLE NAVIGATION AND FIND NEXT QUESTION
$currentRouting = null;
try {
$_SESSION['parsedQuestion'] = '';
$_SESSION['nextQuestion'] = 0;
foreach ($survey->getSections() as $section){
require_once($section->getRulesLocation());
}
}
catch (Exception $e) {
$currentRouting = $e->getMessage();
}
}
}
else {
//load all questions LOAD HERE OTHERWISE FILLS FOR THIS Q WON'T BE UPDATED
require_once($survey->getTemplatesLocation());
require_once($survey->getTypesLocation());
foreach ($survey->getSections() as $section) {
require_once($section->getQuestionLocation());
}
$currentRouting = null;
$_SESSION['currentRouting'] = null;
try {
$_SESSION['parsedQuestion'] = '';
$_SESSION['nextQuestion'] = 1; //get the first question
foreach ($survey->getSections() as $section){
require_once($section->getRulesLocation());
}
}
catch (Exception $e) {
$currentRouting = $e->getMessage();
}
// echo '--------' . $currentRouting ;
if (isset($_SESSION['data']['startts'])){ // echo '<b>Return to survey</b>';
if ($_SESSION['data']['currentRouting']){
$currentRouting = $_SESSION['data']['currentRouting']; //question left off
}
}
else { // echo '<b>FIRST TIME!</b>';
$_SESSION['data']['startts'] = date('Y-m-d H:i:s');
$_SESSION['data']['browserinfo'] = $_SERVER['HTTP_USER_AGENT'];
$_SESSION['data']['language'] = $_SESSION['language'];
}
}
if ($currentRouting == null){ //no questions found
//this was the last question
if (!isset($_SESSION['data']['endts'])){ //don't overwrite when going back
$_SESSION['data']['endts'] = date('Y-m-d H:i:s');
//clean data!!
global $cleanQuestions;
$cleanQuestions = array('startts', 'browserinfo', 'language', 'endts');
$_SESSION['clean'] = 1; //set clean session so ->ask knows to just store the questions
//get all basic variablenames and set the as global;
global $xi;
foreach ($survey->getSections() as $section){
eval('?>' . file_get_contents($xi['surveys']['metadata']['path'] . $section->getRulesLocation()) . '<?');
}
unset($_SESSION['clean']);
$_SESSION['data'] = array_intersect_key($_SESSION['data'], array_flip($cleanQuestions)); //delete the ones we don't want
$survey->getDatabase()->updateRecord($_SESSION["primkey"], $_SESSION['data']); //store endts
}
showSurveyPastLastQuestion();
}
//DISPLAY QUESTION OR QUESTIONGROUP
$question = getCurrentQuestion($currentRouting); //for arrays
/*if (isset($_SESSION['currentQuestion'])){
$_SESSION['lastQuestion'] = $_SESSION['currentQuestion'];
}
else {
$_SESSION['lastQuestion'] = array();
} */
$_SESSION['currentQuestion'] = setCurrentQuestion($question, array());
$_SESSION['currentRouting'] = $question->getName();
$_SESSION['data']['currentRouting'] = $question->getName(); //store as last question answered for when we get back.
echo $question->showQuestion();
//adjust next/back buttons
echo setNextButton($question->showNextButton());
echo setBackButton($question->showBackButton());
echo '<script>
$("form input").click(function(){ //catch button click
if ( $(this).is( "input[type=\'button\']" ) ) { //a button has been clicked!
if ($(this).attr(\'id\').indexOf(\'_button\') >= 0){ //dk or rf button
//erase answer from other components
var inputElement = $(this).attr(\'id\').replace(\'_button\',\'\'); //get the question name from the button (remove _button from the end)
$(\'[id="\' + inputElement + \'"]:radio\').attr(\'checked\', false); //set radio/checkboxes to unselect
$(\'[id="\' + inputElement + \'"]:checkbox\').attr(\'checked\', false); //set radio/checkboxes to unselect
$(\'[id="\' + inputElement + \'"]:text\').val(\'\'); //set input to empty
$(\'select[id="\' + inputElement + \'"]\').val(\'\'); //set input to empty
$(\'textarea[id="\' + inputElement + \'"]\').val(\'\'); //set input to empty
//reset dk/rf buttons
$(\'[id="\' + $(this).attr(\'id\') + \'"]:input\').removeClass("btn_dkrf_selected"); //remove class from buttons (in case switch from rf to dk)
$(this).addClass("btn_dkrf_selected"); //set the class of the pressed button (blue background)
}
else {
//next or back button clicked: do nothing
}
}
});
$("form input, select").change(function(){updateComponent(this);}); //catch input change
$("form textarea").keydown(function(){updateComponent(this);}); //catch input change
$("form input").keydown(function(){updateComponent(this);}); //catch input change
function updateComponent(element){
if ( $(element).is( "input" ) || $(element).is( "select") || $(element).is( "textarea" )) {//other component has been clicked
$(\'[id="\' + $(element).attr(\'id\') + \'_button"]:input\').removeClass("btn_dkrf_selected"); //remove blue from rf/dk button in case someone changes their minds after clicking dk/rf
$(\'[name="\' + $(element).attr(\'id\') + \'_button"]\').val(\'\'); //set the hidden type to empty
}
}
</script>';
//echo mt_rand(1,23123123321);
function setNextButton($visible = true){
if ($visible){
return '<script>
if (! $("#nextbutton").is(":visible") ) {
$("#nextbutton").show();
}
</script>';
}
else {
return '<script>
if ( $("#nextbutton").is(":visible") ) {
$("#nextbutton").hide();
}
</script>';
}
}
function setBackButton($visible = true){
if ($visible){
return '<script>
if (! $("#backbutton").is(":visible") ) {
$("#backbutton").show();
}
</script>';
}
else {
return '<script>
if ( $("#backbutton").is(":visible") ) {
$("#backbutton").hide();
}
</script>';
}
}
function getCurrentQuestion($currentQuestion){
preg_match_all('/\[([A-Za-z0-9 ]+?)\]/', $currentQuestion, $out);
$out = $out[1];
if (sizeof($out) > 0){
$currentQuestion = substr($currentQuestion, 0, stripos($currentQuestion, '['));
global $$currentQuestion;
$question = $$currentQuestion;
foreach ($out as $index){
$question = $question[$index];
}
}
else {
global $$currentQuestion;
$question = $$currentQuestion;
}
return $question;
}
/*
function getIndicesFromArray($currentQuestion){
preg_match_all('/\[([A-Za-z0-9 ]+?)\]/', $currentQuestion, $out);
$out = $out[0];
if (sizeof($out) > 0){
return explode($out, '');
}
else {
return '';
}
}
function getVariablenameWithoutArray($currentQuestion){
if (strpos($currentQuestion,'[') !== false) {
return substr($currentQuestion, 0, stripos($currentQuestion, '['));
}
return $currentQuestion;
}
*/
function showSurveyTimedOut(){
echo 'Survey timed out. Please login again!';
echo setNextButton(false);
echo setBackButton(false);
exit;
}
function showSurveyPastLastQuestion(){
echo 'No more questions, thank you!';
echo setNextButton(false);
echo setBackButton(false);
exit;
}
function setCurrentQuestion($question, $thisarray){
if ($question instanceof QuestionGroup){
foreach ($question->getQuestions() as $subQuestion){ //sub can be a group too!
$thisarray = setCurrentQuestion($subQuestion, $thisarray);
}
return $thisarray;
}
else {
$thisarray[] = $question->getName();
return $thisarray;
}
}
function getNextQuestion($survey){
//HANDLE NAVIGATION AND FIND NEXT QUESTION
return $currentRouting;
}
?>