This repository has been archived by the owner on Jul 20, 2023. It is now read-only.
/
QuestionResponseController.php
149 lines (130 loc) · 4.66 KB
/
QuestionResponseController.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
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Question;
use App\Survey;
use App\QuestionResponse;
use App\ResponseTranscription;
use Twilio\Twiml;
use Cookie;
class QuestionResponseController extends Controller
{
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function storeVoice($surveyId, $questionId, Request $request)
{
$question = Question::find($questionId);
$newResponse = $question->responses()->create(
['response' => $this->_responseFromVoiceRequest($question, $request),
'type' => 'voice',
'session_sid' => $request->input('CallSid')]
);
$nextQuestion = $this->_questionAfter($question);
if (is_null($nextQuestion)) {
return $this->_responseWithXmlType($this->_voiceMessageAfterLastQuestion());
} else {
return $this->_responseWithXmlType(
$this->_redirectToQuestion($nextQuestion, 'question.show.voice')
);
}
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return Response
*/
public function storeSms($surveyId, $questionId, Request $request)
{
$answer = trim($request->input('Body'));
$question = Question::find($questionId);
if ($question->kind === 'yes-no') {
$answer = strtolower($answer) === 'yes' ? 1 : 0;
}
$newResponse = $question->responses()->create(
['response' => $answer,
'type' => 'sms',
'session_sid' => $request->cookie('survey_session')]
);
$nextQuestion = $this->_questionAfter($question);
if (is_null($nextQuestion)) {
return $this->_responseWithXmlType($this->_smsMessageAfterLastQuestion());
} else {
return $this->_responseWithXmlType(
$this->_redirectToQuestion($nextQuestion, 'question.show.sms')
);
}
}
public function storeTranscription($surveyId, $questionId, Request $request)
{
$callSid = $request->input('CallSid');
$question = Question::find($questionId);
$questionResponse = $question->responses()->where('session_sid', $callSid)->firstOrFail();
$questionResponse->responseTranscription()->create(
['transcription' => $this->_transcriptionMessageIfCompleted($request)]
);
}
private function _responseFromVoiceRequest($question, $request)
{
if ($question->kind === 'free-answer') {
return $request->input('RecordingUrl');
} else {
return $request->input('Digits');
}
}
private function _questionAfter($question)
{
$survey = Survey::find($question->survey_id);
$allQuestions = $survey->questions()->orderBy('id', 'asc')->get();
$position = $allQuestions->search($question);
$nextQuestion = $allQuestions->get($position + 1);
return $nextQuestion;
}
private function _redirectToQuestion($question, $route)
{
$questionUrl = route(
$route,
['question' => $question->id, 'survey' => $question->survey->id]
);
$redirectResponse = new Twiml();
$redirectResponse->redirect($questionUrl, ['method' => 'GET']);
return response($redirectResponse);
}
private function _voiceMessageAfterLastQuestion()
{
$voiceResponse = new Twiml();
$voiceResponse->say('That was the last question');
$voiceResponse->say('Thank you for participating in this survey');
$voiceResponse->say('Good-bye');
$voiceResponse->hangup();
return response($voiceResponse);
}
private function _smsMessageAfterLastQuestion() {
$messageResponse = new Twiml();
$messageResponse->message(
"That was the last question.\n" .
"Thank you for participating in this survey.\n" .
'Good bye.'
);
return response($messageResponse)
->withCookie(Cookie::forget('survey_session'))
->withCookie(Cookie::forget('current_question'));
}
private function _transcriptionMessageIfCompleted($request)
{
if ($request->input('TranscriptionStatus') === 'completed') {
return $request->input('TranscriptionText');
}
return 'An error occurred while transcribing the answer';
}
private function _responseWithXmlType($response)
{
return $response->header('Content-Type', 'application/xml');
}
}