forked from Combodo/customer-survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quizzwizard.class.inc.php
819 lines (760 loc) · 26 KB
/
quizzwizard.class.inc.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
<?php
require_once(APPROOT.'setup/wizardcontroller.class.inc.php');
class UnknownTokenException extends Exception
{
protected $m_sToken;
public function __construct($sToken)
{
parent::__construct('Unknown token or closed survey: '.$sToken);
$this->m_sToken = $sToken;
}
public function GetToken()
{
return $this->m_sToken;
}
}
/**
* The controller that drives the whole display of the Quizz form
*/
class QuizzController extends WizardController
{
protected $oQuizz;
protected $oSurveyTargetAnswer;
protected $oSurvey;
public function __construct($sInitialStepClass, $sInitialState = '', $iQuizzId = 0, $sToken = '')
{
parent::__construct($sInitialStepClass, $sInitialState);
$this->oSurveyTargetAnswer = null;
$this->oSurvey = null;
if ($sToken != '')
{
$oTargetSearch = DBObjectSearch::FromOQL_AllData("SELECT SurveyTargetAnswer WHERE token = :token");
$oTargetSet = new CMDBObjectSet($oTargetSearch, array(), array('token' => $sToken));
if ($oTargetSet->Count() == 0)
{
throw new UnknownTokenException($sToken);
}
$this->oSurveyTargetAnswer = $oTargetSet->Fetch();
$this->oSurvey = MetaModel::GetObject('Survey', $this->oSurveyTargetAnswer->Get('survey_id'), true, true /*allow all data*/);
$this->oQuizz = MetaModel::GetObject('Quizz', $this->oSurvey->Get('quizz_id'), true, true /*allow all data*/);
}
else
{
$this->oQuizz = MetaModel::GetObject('Quizz', $iQuizzId);
}
Dict::SetUserLanguage($this->oQuizz->Get('language'));
}
public function GetQuizz()
{
return $this->oQuizz;
}
public function GetSurveyTargetAnswer()
{
return $this->oSurveyTargetAnswer;
}
public function GetSurvey()
{
return $this->oSurvey;
}
/**
* Displays the specified 'step' of the wizard
* @param WizardStep $oStep The 'step' to display
*/
protected function DisplayStep(WizardStep $oStep)
{
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') < 0) {
$oPage = new QuizzWebPage($oStep->GetTitle());
} else {
$oPage = new UnauthenticatedWebPage($oStep->GetTitle());
$oPage->add_saas('env-'.utils::GetCurrentEnvironment().'/customer-survey/css/style.scss');
$oPage->add_script(
<<<EOF
function CheckSelection(sMessage)
{
var bResult = ($('input:checked').length > 0);
if (!bResult)
{
alert(sMessage);
}
return bResult;
}
function GoBack()
{
var form = $('#request_form');
var step = $('input[name=step]');
form.unbind('submit'); // De-activate validation
step.val(step.val() -2); // To go Back one step: next step is x, current step is x-1, previous step is x-2
form.submit(); // Go
}
EOF
);
}
$oPage->add_linked_script(utils::GetAbsoluteUrlModulesRoot().'customer-survey/js/quizzwizard.js');
$oPage->add_script("function CanMoveForward()\n{\n".$oStep->JSCanMoveForward()."\n}\n");
$oPage->add_script("function CanMoveBackward()\n{\n".$oStep->JSCanMoveBackward()."\n}\n");
$iQuizz = $this->GetQuizz()->GetKey();
$sToken = '';
if ($this->GetSurveyTargetAnswer())
{
$sToken = addslashes($this->GetSurveyTargetAnswer()->Get('token'));
}
$oPage->add_script(
<<<EOF
var iQuizz = $iQuizz;
var sToken = '$sToken';
EOF
);
$sConfirmationMessage = addslashes(Dict::S('Survey-exit-confirmation-message')); // The message is not displayed by all browsers
$oPage->add_ready_script(
<<<EOF
$('div.question input[type=radio]').bind('click change', function() { MarkAsModified(); WizardUpdateButtons(); });
$('div.question textarea').bind('keyup change', function() { MarkAsModified(); WizardUpdateButtons(); });
window.onbeforeunload = function() {
if (!bInSubmit && IsModified())
{
return '$sConfirmationMessage';
}
// return nothing ! safer for IE
};
EOF
);
if ($this->oSurveyTargetAnswer == null)
{
$oPage->add('<div class="preview_watermark">'.Dict::S('Survey-Preview Mode').'</div>');
}
$oPage->add('<div class="page_content"><form id="wiz_form" method="post">');
$oStep->Display($oPage);
// Add the back / next buttons and the hidden form
// to store the parameters
$oPage->add('<input type="hidden" id="_class" name="_class" value="'.get_class($oStep).'"/>');
$oPage->add('<input type="hidden" id="_state" name="_state" value="'.$oStep->GetState().'"/>');
$sModified = utils::ReadParam('_modified', '');
$oPage->add('<input type="hidden" id="_modified" name="_modified" value="'.$sModified.'"/>');
foreach($this->aParameters as $sCode => $value)
{
$oPage->add('<input type="hidden" id="_params_'.$sCode.'" name="_params['.$sCode.']" value="'.htmlentities($value, ENT_QUOTES, 'UTF-8').'"/>');
}
$oPage->add('<input type="hidden" name="_steps" value="'.htmlentities(json_encode($this->aSteps), ENT_QUOTES, 'UTF-8').'"/>');
$sBackButton = '';
if ((count($this->aSteps) > 0) && ($oStep->CanMoveBackward()))
{
$sBackButton = '<button id="btn_back" type="submit" name="operation" value="back" class="btn btn-default form_btn_cancel">'.Dict::S('UI:Button:Back').'</button>';
}
$sNextButtons = '';
if ($oStep->CanMoveForward())
{
if ($this->oSurveyTargetAnswer != null)
{
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') <0) {
$sNextButtons .= '<span id="suspend_indicator" style="display: none;"><img src="../images/indicator.gif"/></span><button id="btn_suspend" type="button" name="suspend" value="suspend">'.htmlentities(Dict::S('Survey-SuspendButton'), ENT_QUOTES, 'UTF-8').'</button>';
} else {
$sNextButtons .= '<span id="suspend_indicator" class="fas fa-sync-alt fa-spin" style="display: none;"></span><button id="btn_suspend" class="default btn btn-secondary" type="button" name="suspend" value="suspend">'.htmlentities(Dict::S('Survey-SuspendButton'), ENT_QUOTES, 'UTF-8').'</button>';
}
}
$sNextButtons .= '<button id="btn_next" class="default btn btn-primary form_btn_submit" type="submit" name="operation" value="next" >'.htmlentities($oStep->GetNextButtonLabel(), ENT_QUOTES, 'UTF-8').'</button>';
}
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') <0) {
$oPage->add('<table class="buttons_footer"><tr>');
$oPage->add('<td style="text-align: left">'.$sBackButton.'</td>');
$oPage->add('<td style="text-align: right">'.$sNextButtons.'</td>');
$oPage->add('</tr></table>');
} else {
$sTitle = addslashes(Dict::S('Survey-suspend-confirmation-title'));
$sOkButtonLabel = Dict::S('UI:Button:Ok');
$oPage->add(<<<HTML
<div id="dlg_suspend" class="modal fade" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">$sTitle</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<a href="#" class="btn btn-default" data-dismiss="modal" >$sOkButtonLabel</a>
</div>
</div>
</div>
</div>
HTML
);
$oPage->add('<div class="form_buttons"><div class="form_btn_regular" id="uwp-bottom-buttons">');
$oPage->add($sBackButton);
$oPage->add($sNextButtons);
$oPage->add('</div></div>');
}
$oPage->add("</form></div>");
$oPage->add('<div id="async_action" style="display:none;overflow:auto;max-height:100px;color:#F00;font-size:small;"></div>'); // The div may become visible in case of error
// Hack to have the "Next >>" button, be the default button, since the first submit button in the form is the default one
$oPage->add_ready_script(
<<<EOF
$('form').each(function () {
var thisform = $(this);
thisform.prepend(thisform.find('button.default').clone().removeAttr('id').prop('disabled', false).css({
position: 'absolute',
left: '-999px',
top: '-999px',
height: 0,
width: 0
}));
});
$('#btn_back').click(function() { $('#wiz_form').data('back', true); });
$('#btn_suspend').click(function() { Suspend(); });
$('#wiz_form').submit(function() {
bInSubmit = true;
if ($(this).data('back'))
{
return CanMoveBackward();
}
else
{
return CanMoveForward();
}
});
$('#wiz_form').data('back', false);
WizardUpdateButtons();
EOF
);
$oPage->output();
}
/**
* Move one step back: OVERLOADED/ REDEFINED to workaround a bug in iTop 2.0
* ProcessParams was not called with false as an argument, thus making no difference
* between moving back and forward !!
*/
protected function Back()
{
// let the current step save its parameters
$sCurrentStepClass = utils::ReadParam('_class', $this->sInitialStepClass);
$sCurrentState = utils::ReadParam('_state', $this->sInitialState);
$oStep = new $sCurrentStepClass($this, $sCurrentState);
$aNextStepInfo = $oStep->ProcessParams(false); // false => Moving backwards
// Display the previous step
$aCurrentStepInfo = $this->PopStep();
$oStep = new $aCurrentStepInfo['class']($this, $aCurrentStepInfo['state']);
$this->DisplayStep($oStep);
}
}
/**
* The main "step" of the wizard
* Actually this "step" has several sub-steps (driven by its internal 'state')
* depending on the number of questions (and "pages") defined in the Quizz
*
*/
class QuizzWizStepQuestions extends WizardStep
{
protected $aQuestions;
protected $aPages;
protected $bAnswerCommitted;
protected $bSurveyFinished;
public function __construct($oWizard, $sCurrentState)
{
parent::__construct($oWizard, $sCurrentState);
$oQuizz = $oWizard->GetQuizz();
$oQuestionsSet = $oQuizz->Get('question_list');
$this->bSurveyFinished = false;
$this->bAnswerCommitted = false;
$oSurveyTargetAnswer = $this->oWizard->GetSurveyTargetAnswer();
if ($oSurveyTargetAnswer != null)
{
// A real survey is under way... or is it ?
if ($oSurveyTargetAnswer->Get('status') == 'finished')
{
$this->bAnswerCommitted = true;
}
$oSurvey = $this->oWizard->GetSurvey();
if ($oSurvey->Get('status') == 'closed')
{
$this->bSurveyFinished = true;
}
}
$this->aQuestions = array(0 => array());
$this->aPages = array(0 => 'default');
$iPage = 0;
while($oQuestion = $oQuestionsSet->Fetch())
{
if ($oQuestion->IsNewPage())
{
$iPage++;
$this->aPages[$iPage] = $oQuestion;
$this->aQuestions[$iPage] = array();
}
else
{
$this->aQuestions[$iPage][] = $oQuestion;
}
}
}
public function GetTitle()
{
return $this->oWizard->GetQuizz()->Get('title');
}
public function GetPossibleSteps()
{
return array('QuizzWizStepQuestions', 'QuizzWizStepDone');
}
public function ProcessParams($bMoveForward = true)
{
if ($this->bSurveyFinished || $this->bAnswerCommitted)
{
// Should never happen... but just in case someone tweaks the form
throw new Exception("Survey closed or already completed, not accepting answers anymore");
}
$index = $this->GetStepIndex();
$aQuestions = $this->GetStepQuestions($index);
if ( $aQuestions === null)
{
throw new Exception('Internal error: invalid step "'.$index.'" for quizz.');
}
else if ($bMoveForward)
{
// Save the "answers"
$aPageAnswers = utils::ReadPostedParam('answer', array(), 'raw_data');
$aAnswers = json_decode($this->oWizard->GetParameter('answer', '[]'), true);
foreach($aQuestions as $oQuestion)
{
if ($oQuestion->HasValue())
{
$aAnswers[$oQuestion->GetKey()] = $oQuestion->ValidateValue(isset($aPageAnswers[$oQuestion->GetKey()]) ? $aPageAnswers[$oQuestion->GetKey()] : '');
}
}
$this->oWizard->SetParameter('answer', json_encode($aAnswers));
if ($this->GetStepQuestions(1 + $index) != null)
{
return array('class' => 'QuizzWizStepQuestions', 'state' => (1+$index));
}
else
{
$oSurveyTargetAnswer = $this->oWizard->GetSurveyTargetAnswer();
if ($oSurveyTargetAnswer != null)
{
// Save the values, mark the survey as "finished" and call the trigger
$this->SubmitAnswers($oSurveyTargetAnswer, $aAnswers, true /* bFinished */);
$oSurvey = $this->oWizard->GetSurvey();
if ($oSurvey != null)
{
if ($oSurvey->Get('email_on_completion') == 'yes')
{
$oContact = MetaModel::GetObject('Contact', $oSurvey->Get('on_behalf_of'), false);
if ($oContact && ($oContact->Get('email') != ''))
{
// Send a notification to the person "on behalf"
$oEmail = new EMail();
if ($oSurvey->IsAnonymous())
{
$oEmail->SetSubject(Dict::Format('Survey-CompletionNotificationSubject_name', $oSurvey->GetName()));
$sSurveyUrl = ApplicationContext::MakeObjectUrl('Survey', $oSurvey->GetKey(), null, false);
$sBody = Dict::Format('Survey-CompletionNotificationBody_url', $sSurveyUrl);
}
else
{
$oEmail->SetSubject(Dict::Format('Survey-CompletionNotificationSubject_name_contact', $oSurvey->GetName(), $oSurveyTargetAnswer->Get('contact_name')));
$sSurveyUrl = ApplicationContext::MakeObjectUrl('Survey', $oSurvey->GetKey(), null, false);
$sBody = Dict::Format('Survey-CompletionNotificationBody_url_contact', $sSurveyUrl, $oSurveyTargetAnswer->Get('contact_name'));
}
$oEmail->SetBody($sBody);
$oEmail->SetRecipientTO($oContact->Get('email'));
$oEmail->SetRecipientFrom($oContact->Get('email'));
$aIssues = array();
$iRes = $oEmail->Send($aIssues);
switch ($iRes)
{
case EMAIL_SEND_ERROR:
IssueLog::Error("Failed to send email on survey completion. Errors: ".implode(', ', $aErrors));
}
}
}
}
}
// Move to the next step
return array('class' => 'QuizzWizStepDone', 'state' => '');
}
}
else
{
// Moving backwards, save the previous' page values
$aPageAnswers = utils::ReadPostedParam('answer', array(), 'raw_data');
$aAnswers = json_decode($this->oWizard->GetParameter('answer', '[]'), true);
foreach($aQuestions as $oQuestion)
{
if ($oQuestion->HasValue())
{
$aAnswers[$oQuestion->GetKey()] = $oQuestion->ValidateValue(isset($aPageAnswers[$oQuestion->GetKey()]) ? $aPageAnswers[$oQuestion->GetKey()] : '');
}
}
$this->oWizard->SetParameter('answer', json_encode($aAnswers));
}
}
/**
* Display a "step" of the wizard, according to its internal "state"
* @param WebPage $oPage The page to use for displaying the form
*/
public function Display(WebPage $oPage)
{
if ($this->bSurveyFinished)
{
// Too late the survey is 'closed' answers are no longer accepted
$oPage->p(Dict::S('Survey-SurveyFinished'));
// Hide the buttons
$oPage->add_ready_script("$('#btn_suspend').hide(); $('#btn_next').hide();");
}
else if ($this->bAnswerCommitted)
{
// The user has already submitted her answers, no more modification allowed
$oPage->p(Dict::S('Survey-AnswerAlreadyCommitted'));
// Hide the buttons
$oPage->add_ready_script("$('#btn_suspend').hide(); $('#btn_next').hide();");
}
else
{
// Normal behavior, let's proceeed with the display if the form
$this->DisplayStep($oPage);
}
}
/**
* Displays a "page" of the wizard, according to the current internal state
* @param WebPage $oPage The page to use for displaying the form
*/
protected function DisplayStep(WebPage $oPage)
{
$index = $this->GetStepIndex();
$aQuestions = $this->GetStepQuestions($index);
// Check for any previous answer
$aPreviousAnswers = array();
$oSurveyTargetAnswer = $this->oWizard->GetSurveyTargetAnswer();
if ($oSurveyTargetAnswer != null)
{
$sOQL = "SELECT SurveyAnswer AS A WHERE A.survey_target_id = :id";
$oAnswerSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL, array('id' => $oSurveyTargetAnswer->GetKey())));
while($oAnswer = $oAnswerSet->Fetch())
{
$aPreviousAnswers[$oAnswer->Get('question_id')] = $oAnswer;
}
}
// Build the form
//
$question = $this->GetStepInfo($index);
$iNbPages = $this->GetPagesCount();
$oPage->add("<div class=\"quizz_header\">\n");
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') < 0) {
$sTitleTag='h1';
} else {
$sTitleTag='h4';
}
if ($index == 0)
{
$sTitle = $this->oWizard->GetQuizz()->GetAsHtml('title');
$sDescription = $this->oWizard->GetQuizz()->GetAsHtml('introduction');
if (($question != 'default'))
{
$oPage->add("<$sTitleTag class=\"quizz_title\">$sTitle</$sTitleTag>\n");
$oPage->add("<div class=\"quizz_intro\">$sDescription</div>\n");
// Additional "page" info on the first page
$sDescription = $question->GetAsHTML('description');
$oPage->add("<div class=\"page_intro\">$sDescription</div>\n");
}
else
{
$oPage->add("<$sTitleTag class=\"page_title\">".Dict::Format('Survey-Title-Page_X_of_Y', $sTitle, (1+$index), $iNbPages)."</$sTitleTag>\n");
$oPage->add("<div class=\"quizz_intro\">$sDescription</div>\n");
}
}
else // Not on the first page
{
$sTitle = $question->GetAsHTML('title');
$sDescription = $question->GetAsHTML('description');
$oPage->add("<$sTitleTag class=\"page_title\">".Dict::Format('Survey-Title-Page_X_of_Y', $sTitle, (1+$index), $iNbPages)."</$sTitleTag>\n");
$oPage->add("<div class=\"page_intro\">$sDescription</div>\n");
}
$oPage->add("</div>\n");
$oPage->add("<div class=\"quizz_questions\">\n");
$bHasMandatoryQuestions = false;
if (count($aQuestions) > 0)
{
$oPage->add("<div class=\"wizContainer\">\n");
$aAnswers = json_decode($this->oWizard->GetParameter('answer'), true);
foreach($aQuestions as $oQuestion)
{
$sMandatory = 'false';
if ($oQuestion->Get('mandatory') == 'yes')
{
$bHasMandatoryQuestions = true;
$sMandatory = 'true';
}
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') >= 0) {
$oPage->add('<fieldset>');
}
$oPage->add('<div class="question" data-mandatory="'.$sMandatory.'">');
$sSavedValue = '';
if (isset($aPreviousAnswers[$oQuestion->GetKey()]))
{
// Retrieve the previously saved values (via the "suspend" function)
$sSavedValue = $aPreviousAnswers[$oQuestion->GetKey()]->Get('value');
}
$sAnswer = isset($aAnswers[$oQuestion->GetKey()]) ? $aAnswers[$oQuestion->GetKey()] : $sSavedValue;
$oQuestion->DisplayForm($oPage, $sAnswer);
$oPage->add('</div>');
if (version_compare(ITOP_DESIGN_LATEST_VERSION , '3.0') >= 0) {
$oPage->add('</fieldset>');
}
}
$oPage->add("</div>");
}
$oPage->add("</div>");
if ($bHasMandatoryQuestions)
{
$oPage->add("<div class=\"mandatory_footer\"><span class=\"mandatory_asterisk\">*</span> ".Dict::S('Survey-MandatoryQuestion').'</div>');
}
}
/**
* Get the current internal state
* @return integer The internal state of this "step" of the wizard
*/
protected function GetStepIndex()
{
switch($this->sCurrentState)
{
case 'start':
$index = 0;
break;
default:
$index = (integer)$this->sCurrentState;
}
return $index;
}
/**
* Get the list of "questions" for the "page" defined by the given state
* @param integer $idx The internal state for which to get the list of questions
* @return array An array of QuizzElement
*/
protected function GetStepQuestions($idx)
{
$iPage = 0;
if (array_key_exists($idx, $this->aQuestions))
{
return $this->aQuestions[$idx];
}
return null;
}
/**
* Get the number of pages for the wizard (i.e. sub-steps for this step of the wizard)
* @return integer The number of pages > 0
*/
protected function GetPagesCount()
{
return count($this->aQuestions);
}
/**
* Gets information about the "page" defined by the given state
*
* @param integer $idx The internal state for which to get the information
* @return A QuizzNewPageElement or the string 'default' if there is none (can happen only on the first page)
*/
protected function GetStepInfo($idx)
{
$iPage = 0;
if (array_key_exists($idx, $this->aPages))
{
return $this->aPages[$idx];
}
return null;
}
public function GetNextButtonLabel()
{
if ($this->GetStepQuestions(1 + $this->GetStepIndex()) == null)
{
return Dict::S('Survey-FinishButton');
}
else
{
return Dict::S('Survey-NextButton');
}
}
public function JSCanMoveForward()
{
if ($this->bSurveyFinished || $this->bAnswerCommitted)
{
return "return false";
}
else
{
return "return CheckMandatoryAnswers();";
}
}
/**
* Saves the answers into the database. Either as a final "commit" or just temporarily
* @param SurveyTargetAnswer $oSurveyTargetAnswer
* @param hash $aAnswers Hash of QuestionId => AnswerValue (string)
* @param bool $bFinished Whether the SurveyTargetAnswer should be marked as "finished" or not
* @param integer $iLastQuestionId Index of the last question really filled to restart from the end... not really implemented
* @throws Exception
* @return void
*/
function SubmitAnswers(SurveyTargetAnswer $oSurveyTargetAnswer, $aAnswers, $bFinished = true, $iLastQuestionId = 0)
{
$oQuestionSearch = DBObjectSearch::FromOQL_AllData("SELECT QuizzElement WHERE quizz_id = :Quizz");
$oQuestionSet = new CMDBObjectSet($oQuestionSearch, array('order' => true), array('Quizz' => $this->oWizard->GetQuizz()->GetKey()));
if ($oQuestionSet->Count() == 0)
{
throw new Exception("Sorry, there is no question for this Quizz (?!)");
}
// Check for any previous answer
$sOQL = "SELECT SurveyAnswer AS A WHERE A.survey_target_id = :id";
$oAnswerSet = new DBObjectSet(DBObjectSearch::FromOQL($sOQL, array('id' => $oSurveyTargetAnswer->GetKey())));
$aPreviousAnswers = array();
while($oAnswer = $oAnswerSet->Fetch())
{
$aPreviousAnswers[$oAnswer->Get('question_id')] = $oAnswer;
}
// For each question, retrieve the answer and store it
//
while($oQuestion = $oQuestionSet->Fetch())
{
$iQuestion = $oQuestion->GetKey();
if ($oQuestion->HasValue() && isset($aAnswers[$iQuestion]))
{
if (isset($aPreviousAnswers[$iQuestion]))
{
// Update the previous answer
$oAnswer = $aPreviousAnswers[$iQuestion];
}
else
{
// Create a new record
$oAnswer = new SurveyAnswer();
$oAnswer->Set('survey_target_id', $oSurveyTargetAnswer->GetKey());
$oAnswer->Set('question_id', $iQuestion);
}
$oAnswer->Set('value', $oQuestion->ValidateValue($aAnswers[$iQuestion]));
$oAnswer->DBWrite();
}
}
// Update the target record
//
$oSurveyTargetAnswer->Set('date_response', time());
$oSurveyTargetAnswer->Set('status', $bFinished ? 'finished' : 'ongoing');
$oSurveyTargetAnswer->Set('last_question_id', $bFinished ? null : $iLastQuestionId);
$oSurveyTargetAnswer->DBUpdate();
}
/**
* Asynchronous action(s) (AJAX) for this step: only one is supported "suspend" to save the answers
* @param string $sCode The code of the action (if several actions need to be distinguished)
* @param hash $aParameters The action's parameters name => value
* @return void
*/
public function AsyncAction(WebPage $oPage, $sCode, $aParameters)
{
switch($sCode)
{
case 'suspend':
$oSurveyTargetAnswer = $this->oWizard->GetSurveyTargetAnswer();
if ($oSurveyTargetAnswer != null) {
$iLastQuestionId = 0;
$aAnswers = array();
// Values from other pages (or previous values from the current page)
$aValues = json_decode(isset($aParameters['other_answers']) ? $aParameters['other_answers'] : '[]', true);
foreach ($aValues as $sKey => $sValue) {
$iQuestionId = (int)$sKey;
$iLastQuestionId = max($iLastQuestionId, $iQuestionId);
$aAnswers[$iQuestionId] = trim($sValue);
}
// Values from the current page
foreach ($aParameters as $sKey => $sValue) {
if (preg_match('/^answer\[([0-9]+)$/', $sKey, $aMatches)) {
$iQuestionId = (int)$aMatches[1];
$iLastQuestionId = max($iLastQuestionId, $iQuestionId);
$aAnswers[$iQuestionId] = trim($sValue);
}
}
if ($iLastQuestionId > 0) {
// Save the current state but don't mark the survey answer as finished
$this->SubmitAnswers($oSurveyTargetAnswer, $aAnswers, false, $iLastQuestionId);
}
if (version_compare(ITOP_DESIGN_LATEST_VERSION, '3.0') < 0) {
$sTitle = addslashes(Dict::S('Survey-suspend-confirmation-title'));
$sUrl = utils::GetAbsoluteUrlModulesRoot().'customer-survey/run_survey.php?token='.urlencode($oSurveyTargetAnswer->Get('token'));
$sHyperlink = '<a href="'.$sUrl.'">'.$sUrl.'</a>';
$sOkButtonLabel = Dict::S('UI:Button:Ok');
$sMessage = addslashes(Dict::Format('Survey-suspend-confirmation-message_hyperlink', $sHyperlink));
$oPage->add_ready_script(
<<<EOF
$('#suspend_indicator').hide();
$('#btn_suspend').prop('disabled', false);
var oDlg = $('<div>$sMessage</div>');
oDlg.appendTo('body');
oDlg.dialog({
width: 400,
modal: true,
title: '$sTitle',
buttons: [
{ text: "$sOkButtonLabel", click: function() {
$(this).dialog( "close" );
$(this).remove();
}
}
],
close: function() { $(this).remove(); }
});
ClearModified();
EOF
);
} else {
$sUrl = utils::GetAbsoluteUrlModulesRoot().'customer-survey/run_survey.php?token='.urlencode($oSurveyTargetAnswer->Get('token'));
$sHyperlink = '<a href="'.$sUrl.'">'.$sUrl.'</a>';
$sMessage = addslashes(Dict::Format('Survey-suspend-confirmation-message_hyperlink', $sHyperlink));
$oPage->add_ready_script(
<<<EOF
$('#suspend_indicator').hide();
$('#btn_suspend').prop('disabled', false);
$("#dlg_suspend .modal-body").html('$sMessage');
$("#dlg_suspend").modal('show');
ClearModified();
EOF
);
}
}
}
}
}
/**
* Last step of the wizard: just displays a final conclusion message
* and no back button
*/
class QuizzWizStepDone extends WizardStep
{
public function GetTitle()
{
$sTitle = Dict::S('Survey-SurveyCompleted-Title');
return $sTitle;
}
public function GetPossibleSteps()
{
return array();
}
public function ProcessParams($bMoveForward = true)
{
// Do nothing...
}
public function Display(WebPage $oPage)
{
// Display either the quizz specific message or a default
// one taken from the dictionary
$oQuizz = $this->oWizard->GetQuizz();
$sConclusion = $oQuizz->GetAsHtml('conclusion');
if ($sConclusion == '')
{
$sConclusion = Dict::S('Survey-SurveyCompleted-Default-Text');
}
$oPage->p($sConclusion);
$oPage->add_ready_script("ClearModified();"); // Values were saved anyhow... except in preview mode where we don't care
}
public function CanMoveForward()
{
return false;
}
public function CanMoveBackward()
{
return false;
}
}