<?php
namespace QxCMS\Modules\Api\Controllers;
use Illuminate\Http\Request;
use QxCMS\Http\Controllers\Controller;
/*use QxCMS\Modules\Client\Repositories\Participants\AnswersRepositoryInterface as Answer;*/
use QxCMS\Modules\Client\Repositories\Questionnaire\QuestionRepositoryInterface as Question;
use QxCMS\Modules\Client\Repositories\Participants\ParticipantRepositoryInterface as Participant;
use Auth;
use \Carbon\Carbon;
class ParticipantController extends Controller
{
protected $participant;
protected $question;
public function __construct(Participant $participant, Question $question)
{
$this->participant = $participant;
$this->question = $question;
}
public function saveParticipant(Request $request)
{
$location = $request->get('pos');
$participant_input = array(
'email' => 'danilo@quantumx.com',
'name' => 'Danilo R. Dela Cruz',
'template_id' => $request->get('template_id'),
'loc_lat' => $location['loc_lat'],
'loc_long' => $location['loc_long']
);
$participant = $this->participant->create($participant_input);
$answers = $request->get('answers');
$types = $this->question->getTypes();
$str = 'question';
$answers_input = [];
foreach ($answers as $answer_key => $answer) {
if(starts_with($answer_key, $str)) {
$question_id = str_replace($str, '', $answer_key);
$answers_input['question_id'] = $question_id;
if(!empty($question_id)) {
$question = $this->question->findById($question_id);
if(!empty($question)) {
$type = $question->question_type;
if($type == 2 || $type == 3) {
if(is_array($answer)) {
foreach ($answer as $ans_key => $ans_val) {
$answers_input['answer_id'] = $ans_val;
$participant->answers()->create($answers_input);
}
continue;
}
$answers_input['answer_id'] = $answer;
}
if($type == 1) {
$answers_input['answer'] = $answer;
}
}
$participant->answers()->create($answers_input);
}
}
unset($answers_input);
}
return $this->participant->select(['*'])->with(['answers'])->get();
return response()->json($responses)->withCallback($request->input('callback'));
}
}