-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresponse.php
534 lines (503 loc) · 18.4 KB
/
response.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php
//include_once 'C:\xampp\htdocs\wordpress\wp-content\plugins\GWU_Builder\models\GWWrapper.php';
include_once dirname( __FILE__ ) . '/models/GWWrapper.php';
use WordPress\ORM\Model\GWWrapper;
/**
* Description of response
*
* Display the questionnaire and save users' response
*
* @author Kaihua Wu(Michael)
* Some part by Darshan suchin
*
*/
add_shortcode('questionnaire', 'Response_questions');
/*get next question sequence function*/
function getNextQuestion($SessionID,$ConditionID)
{
$Wrapper= new GWWrapper();
$FlagValues = array();
//1) get all the responses for this session id..i.e we need listResponses($SessionID)
//2) iterate all the responses and get the flag values
//3) store all the flagnames value
$arr_responsID = $Wrapper->listResponsesBySessionId($SessionID);
if($arr_responsID!=null){
foreach($arr_responsID as $temp) {
$ResponseId = $temp->get_ResponseID();
$arrAllResponses = $Wrapper->getFlagsByQuestionnaireQuestionOption($temp->get_QuestionnaireID(),$temp->get_QuestSequence(),$temp->get_OptionNumber());
//$flagObject= new GWFlag();
if($arrAllResponses!=null)
{
$flagObject = $arrAllResponses[0];
$FlagName = $flagObject->get_FlagName();
$FlagValue = $flagObject->get_FlagValue();
$FlagValues[$FlagName]= $FlagValue;
}
}}
$Conditions=$Wrapper->getCondition($ConditionID);
$Condition=$Conditions[0];
$LogicString= $Condition->get_LogicStatement();
$AndParts=explode(" or ",$LogicString);//compute "and" first
$IfSucess = FALSE;//if one "and" is true,it's true
$NextSeqNum = '1';
foreach ($AndParts as $AndPart)
{
$IfAndSucess = TRUE;//if one value in and is false,it's false
$ValueParts = explode(" and ",$AndPart);//compute each value in and
foreach ($ValueParts as $ValuePart) {
$Parts = explode(" ",$ValuePart);
If(!isset($FlagValues[$Parts[1]]))
$IfAndSucess = FALSE;
elseif($Parts[2] == '==')
{If($FlagValues[$Parts[1]] <> $Parts[3])
$IfAndSucess = FALSE;
}
elseif($Parts[2] == '>=')
{If($FlagValues[$Parts[1]] < $Parts[3])
$IfAndSucess = FALSE;}
elseif($Parts[2] == '<=')
{If($FlagValues[$Parts[1]] > $Parts[3])
$IfAndSucess = FALSE;}
elseif($Parts[2] == '>')
{If($FlagValues[$Parts[1]] <= $Parts[3])
$IfAndSucess = FALSE;}
elseif($Parts[2] == '<')
{If($FlagValues[$Parts[1]] >= $Parts[3])
$IfAndSucess = FALSE;}
}
If($IfAndSucess == TRUE)
{
$IfSucess = TRUE;}
}
If($IfSucess)
$NextSeqNum= $Condition->get_JumpQNoOnSuccess();
else{
$NextSeqNum= $Condition->get_JumpQNoOnFailure();}
return $NextSeqNum;
}
/*get ip function*/
function Get_IP()
{
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if ($_SERVER["HTTP_CLIENT_IP"])
$ip = $_SERVER["HTTP_CLIENT_IP"];
else if ($_SERVER["REMOTE_ADDR"])
$ip = $_SERVER["REMOTE_ADDR"];
else if (getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "Unknown";
return $ip;
}
/*remove edit button function*/
function wpse_remove_edit_post_link( $link )
{
return '';
}
add_filter('edit_post_link', 'wpse_remove_edit_post_link');
/*main funciton to show the questionnaire one by one*/
function Response_questions($atts)
{
extract(shortcode_atts(array( "id"=> '1'),$atts));
$Wrapper= new GWWrapper();//use orm
$QSession = null;
$QuestionnaireID = $id; //Get questionnaire id
//get the questionaire information through the $id
$Questionnaires=$Wrapper->getQuestionnaire($QuestionnaireID);
$Questionnaire=$Questionnaires[0];
/*if there is no session for qno then set it to zero.this parts is for the case have Qsession but do not have sqno*/
if(!isset($_SESSION['sqno']))
{
$_SESSION['sqno'] = '0';
}
/*this part is for the case that change to another questionnaire*/
if(!isset($_SESSION['Qid']))
{
$_SESSION['Qid']=$id;
}
else if($_SESSION['Qid'] !=$id)
{
$_SESSION['Qid']=$id;
unset($_SESSION['QSession']);
}
/*this part is to record the session for the first time*/
if(!isset($_SESSION['QSession'])) { //Create new Session object
$current_user= wp_get_current_user();
if($current_user->user_login=='' && $Questionnaire->get_AllowAnnonymous()==0)
{
return 'Sorry, you need to login to do this survey';
}
$sessionID = $Wrapper->saveSession($current_user->user_login, Get_IP(), 'Washington', 'USA', '00:00:00.000000', date('Y-m-d'),0);
$QSessions = $Wrapper->getSession($sessionID['SessionID']);
$QSession = $QSessions[0];
$_SESSION['QSession'] = $QSession;
$_SESSION['sqno'] = '0';
} else { //Get current session object
$QSession = $_SESSION['QSession'];
}
$Questions=$Wrapper->listQuestion($QuestionnaireID);
if(empty($Questions))
{
$totalQuestionNum=0;
}
else
{
$totalQuestionNum= sizeof($Questions);
}
/* write down questionnaire title*/
$output='<p><font color="#545454"><small></small></font><br/>';
/*Check if it's the first question*/
/*if it's new, set the $qno=0 else store last questionno in $qno */
$qno=$_SESSION['sqno'];
if($qno != '0' && $_POST["qno"]==$qno && $_POST["IfJump"]!= 1)
{
$questions=$Wrapper->getQuestion($qno, $QuestionnaireID);
$question=$questions[0];
/*else store the last question's response depends on it's type*/
if($question->get_AnsType()=='Text Box')
$Wrapper->saveResponse($qno, $QSession->get_SessionID(), $QuestionnaireID, 1, $question->get_AnsType(), $_POST["response"], null, null);
elseif($question->get_AnsType()=='Multiple Choice, Single Value')
{
$Wrapper->saveResponse($qno, $QSession->get_SessionID(), $QuestionnaireID, $_POST["response"], $question->get_AnsType(), $_POST[$_POST["response"]], null, null);
}
elseif($question->get_AnsType()=='Multiple Choice, Multiple Value')
{
if(!empty($_POST["response"]))
{
foreach($_POST["response"] as $response)
$Wrapper->saveResponse($qno, $QSession->get_SessionID(), $QuestionnaireID, $response, $question->get_AnsType(), $_POST[$response], null, null);
}
}
else //NPS
{
$Wrapper->saveResponse($qno, $QSession->get_SessionID(), $QuestionnaireID, $_POST["response"], $question->get_AnsType(), $_POST["response"], null, null);
}
}/*$QSession->get_SessionID()*/
/*if last question is the final question show thankyou*/
if($qno == $totalQuestionNum && $_POST["qno"]==$qno || $totalQuestionNum==0)
{
$gwsession = $_SESSION['QSession']; //Get GWSession object from browser session
$gwsession->set_SurveyCompleted(1); //set properties to be updated
$gwsession->update(); //This will update the GWSession object in the database
unset($_SESSION['QSession']);
return 'Thank your for participating our survey';
}
/*else $qno +=1 Get the question and answerchoice from the database with $qno and $QuestionnaireID
* store in $quesion and $Anserchoices*/
else
{
if($_POST["qno"]==$qno || $qno=='0')
{
$qno += 1;
$_SESSION['sqno'] = $qno;
$QueFoCons=$Wrapper->getQuestion($qno, $QuestionnaireID);
$QueFoCon=$QueFoCons[0];
if($QueFoCon->get_ConditionID() != null )
{
$JumpNum = getNextQuestion($QSession->get_SessionID(),$QueFoCon->get_ConditionID());
while($JumpNum != $qno)
{
$qno = $JumpNum;
$QueFoCons=$Wrapper->getQuestion($qno, $QuestionnaireID);
$QueFoCon=$QueFoCons[0];
if($QueFoCon->get_ConditionID() != null )
$JumpNum = getNextQuestion($QSession->get_SessionID(),$QueFoCon->get_ConditionID());
}
$_SESSION['sqno'] = $qno;
}
}
$questions=$Wrapper->getQuestion($qno, $QuestionnaireID);
$question=$questions[0];
$Answerchoices=$Wrapper->listAnswerChoice($QuestionnaireID,$qno);
$Actions=$Wrapper->listActions($QuestionnaireID,$qno);
$IfMandatory='';
$CheckFun='';
$IfMCMVMandatory='';
If($question->get_Mandatory()==1)
{
$IfMandatory='style="display: none"';
if($question->get_AnsType()=='Text Box')
{
$IfMCMVMandatory='onsubmit="javascript:return chkTextBox();"';
$CheckFun='<script>
function chkTextBox() {
var obj = document.getElementById("response");
var objYN = false;
var value = obj.value;
value = value.replace(/\s/g,"");
if(value=="")
{
objYN=true;
}
if (objYN) {
alert("This is a mandatory question.You have to write your answer");
return false;
}
else {
return true;
}
}</script>';
}
if($question->get_AnsType()=='Multiple Choice, Multiple Value')
{
$IfMCMVMandatory='onsubmit="javascript:return chkCheckBox();"';
$CheckFun='<script>
function chkCheckBox() {
var obj = document.getElementsByName("response[]");
var objLen = obj.length;
var objYN = false;
for (var i = 0; i < objLen; i++) {
if (obj [i].checked == true) {
objYN = true;
break;
}
}
if (!objYN) {
alert("This is a mandatory question,you need at least choose one");
return false;
}
else {
return true;
}
}</script>';
}
}
/*show question text*/
$output .= $CheckFun.'<form action="" method="post" '.$IfMCMVMandatory.'>
<strong>'.$qno.". ".$question->get_Text().'</strong><br/>
<input type="checkbox" value=1 name="IfJump" '.$IfMandatory.'/><font '.$IfMandatory.'>Skip this quesion</font><hr/>';
/*show action*/
if(!empty($Actions))
{
$max=0;
$links=array();
$types = array();
foreach ($Actions as $Action) {
if($Action->get_Sequence()>$max)
$max=$Action->get_Sequence();
}
for($i=1;$i<=$max;$i++)//put the action order by sequence, put the action with same sequence together
{
foreach ($Actions as $Action) {
if($Action->get_Sequence()==$i)
{
if(!isset($links[$i]))
{
$links[$i] = array();
$types[$i] = array();
$links[$i][0] = $Action->get_LinkToAction();
$types[$i][0] = $Action->get_ActionType();
}
else
{
$j=0;
while(isset($links[$i][$j]))
{
$j++;
}
$links[$i][$j] = $Action->get_LinkToAction();
$types[$i][$j] = $Action->get_ActionType();
}
}
}
}
$output .='<body onload="LoadAction()"><p>references:<br/>
<style type="text/css">
img
{
max-height: 415px;
max-width: 560px
}
</style>
<script type="text/javascript">
var links = new Array();
var types = new Array();
var num = '.sizeof($links).';
';
$j=0;
for($i=1;$i<=$max;$i++)
{
if(isset($links[$i]))
{
$output .='links['.$j.'] = new Array();
types['.$j.'] = new Array();';
for($z=0;$z<sizeof($links[$i]);$z++)
{
$output .='links['.$j.']['.$z.']="'.$links[$i][$z].'";
types['.$j.']['.$z.']="'.$types[$i][$z].'"
';
}
$j++;
}
}
$output .='
function ClearAllNode(parentNode)
{
while (parentNode.firstChild)
{
var oldNode = parentNode.removeChild(parentNode.firstChild);
oldNode = null;
}
}
function LoadAction()
{
document.getElementById("ActSeq").value = "0";
var ShowAct= document.getElementById("ShowAct");
ClearAllNode(ShowAct);
document.getElementById("NextAct").style.display = "none";
var NewAct=Array();
for(var i=0;i<links[0].length;i++)
{
if(types[0][i]=="Image")
{
NewAct[i] = document.createElement("img")
NewAct[i].src=links[0][i];
NewAct[i].alt = "not";
ShowAct.appendChild(NewAct[i]);
var newline= document.createElement("br");
ShowAct.appendChild(newline);
}
else if(types[0][i]=="Video")
{
NewAct[i] = document.createElement("iframe")
NewAct[i].style.width = "560px";
NewAct[i].style.height = "415px";
NewAct[i].src=links[0][i];
ShowAct.appendChild(NewAct[i]);
var newline= document.createElement("br");
ShowAct.appendChild(newline);
}
}
if (num != 1)
{
document.getElementById("NextAct").style.display = "inline"
}
}
function changeaction()
{
document.getElementById("ActSeq").value++;
var ShowAct= document.getElementById("ShowAct");
ClearAllNode(ShowAct);
document.getElementById("NextAct").style.display = "none";
var ActSeq = document.getElementById("ActSeq").value;
if (ActSeq != num - 1)
{
document.getElementById("NextAct").style.display = "inline"
}
var NewAct=Array();
for(var i=0;i<links[ActSeq].length;i++)
{
if(types[ActSeq][i]=="Image")
{
NewAct[i] = document.createElement("img")
NewAct[i].src = links[ActSeq][i];
NewAct[i].alt = "not";
ShowAct.appendChild(NewAct[i]);
var newline = document.createElement("br");
ShowAct.appendChild(newline);
}
else if(types[ActSeq][i]=="Video")
{
NewAct[i] = document.createElement("iframe")
NewAct[i].style.width = "560px";
NewAct[i].style.height = "415px";
NewAct[i].src = links[ActSeq][i];
ShowAct.appendChild(NewAct[i]);
var newline = document.createElement("br");
ShowAct.appendChild(newline);
}
}
}
</script>
<input type="hidden" id="ActSeq" value="0"/>
<div id="ShowAct">
</div>
<button id="NextAct" onclick="changeaction()" type="button">next</button>
</p><hr/></body>
';
}
/*show the qno's quesion depends on it's type*/
if($question->get_AnsType()=='Text Box')//text
{
$output .='
<input type="hidden" name="qno" value="'.$qno.'"/>
<textarea id="response" cols="60" rows="9" name="response" ></textarea><br/>
<br/><input type="submit" value="next"></form>';
}
elseif($question->get_AnsType()=='Multiple Choice, Single Value')//mcsv
{
$output .='
<input type="hidden" name="qno" value="'.$qno.'"/>';
if(empty($Answerchoices))
{
$output .='<br/>your data is invalid because there is no answerchoice';
}
else
{
$no = 0;
foreach($Answerchoices as $answerchoice)
{
$no++;
$checked='';
if($no==1)
$checked='checked="checked"';
$output .= '<br/><input type="hidden" name="'.$answerchoice->get_OptionNumber().'" value="'.$answerchoice->get_AnsValue().'" />
<input name="response" type="radio" value="'.$answerchoice->get_OptionNumber() .'"'.$checked.' />'.$answerchoice->get_AnsValue();
}
}
$output .='<br/><br/><input type="submit" value="next"></form>';
}
elseif($question->get_AnsType()=='Multiple Choice, Multiple Value')//mcmv
{
$output .='
<input type="hidden" name="qno" value="'.$qno.'"/>';
if(empty($Answerchoices))
{
$output .='<br/>your data is invalid because there is no answerchoice';
}
else
{
foreach($Answerchoices as $answerchoice)
{
$output .= '<br/><input type="hidden" name="'.$answerchoice->get_OptionNumber().'" value="'.$answerchoice->get_AnsValue().'"/>
<input name="response[]" type="checkbox" value="'.$answerchoice->get_OptionNumber() .'" />'.$answerchoice->get_AnsValue();
}
}
$output .='<br/><br/><input type="submit" value="next"></form>';
}
else //nps
{
$output .= '
<input type="hidden" name="qno" value="'.$qno.'"/><table><tr><td></td>';
if(empty($Answerchoices)||sizeof($Answerchoices)<13)
{
$output .='<br/>your data is invalid because the answerchoices is not enough, please update your db';
}
else
{
for ($i = 0; $i < 11; $i++)
{
$checked='';
if($i==0)
$checked='checked="checked"';
$output .= '<td><input name="response" type="radio" value="'.$i .'" '.$checked.'/> </td>';
}
$output .= '<td></td></tr><tr><td>' . $Answerchoices[11]->get_AnsValue() . ' </td>';
for ($i = 0; $i < 11; $i++)
{
$output .= '<td>' . $i . '</td>';
}
$output .= '<td>' . $Answerchoices[12]->get_AnsValue(). ' </td></tr></table>';
}
$output .= '<br/><br/><input type="submit" value="next"></form>';
}
}
/*return html*/
return $output;
}
?>