Example #1
0
 public function testExpiration()
 {
     $s = new SurveyState(time() - 500);
     $this->assertTrue($s->isExpired(10));
     $s = new SurveyState(time());
     $this->assertTrue($s->isExpired(1000));
     $s = new SurveyState(time() - 500);
     $s->setSurveyExecutionState(SurveyExecutionState::InProgress());
     $this->assertTrue($s->isExpired(10));
     $s = new SurveyState(time());
     $s->setSurveyExecutionState(SurveyExecutionState::InProgress());
     $this->assertFalse($s->isExpired(1000));
 }
 /**
  * Public entry point to service
  * @return boolean
  */
 public function service()
 {
     if (!$this->shouldService()) {
         $this->response("Sorry, could not find a survey by that name.");
         return false;
     }
     $qindex = $this->surveyState->getQuestionIndex();
     //No qindex exists, or we are at the first question.
     if (is_null($qindex)) {
         $qindex = 0;
         $this->surveyState->setQuestionIndex($qindex);
         //return first question
         $question = $this->surveyState->getQuestion();
         $this->response($question);
         return true;
     }
     $question = $this->surveyState->getQuestionObj();
     $validator = $this->answerValidatorProvider->getValidator($question->getType());
     $answer = $this->getRequestBody();
     if ($validator->isValid($answer)) {
         //move to next question
         $qindex++;
         //persist our question index state
         $this->surveyState->setQuestionIndex($qindex);
         if (!$this->surveyState->hasId()) {
             $from = session_id();
             if (array_key_exists('From', $this->request)) {
                 $from = $this->request['From'];
             }
             $this->surveyState->setId($from);
         }
         $answer = $validator->normalize($answer);
         $this->persistAnswer($this->surveyState->getId(), $question, $answer);
     } else {
         $helperText = $validator->getHelperText();
         $response = "Sorry, we couldn't understand your response. {$helperText}";
         $this->response($response);
         return true;
     }
     $next_question = $this->surveyState->getQuestion($qindex);
     if (is_null($next_question)) {
         //no more questions. Send thank you message.
         $this->surveyState->setSurveyExecutionState(SurveyExecutionState::Completed());
         $next_question = $this->surveyState->getSurvey()->getThankYouMessage();
     }
     $this->response($next_question);
     return true;
 }