public function loadanswer($questionid) { $Answer = Answer::where('question_id', $questionid)->first(); if ($Answer) { $question = $Answer->question; if ($question->type == 3 || $question->type == 7) { //single correct MCQ $choices = array_values($question->choices->toArray()); return ['answer' => $choices[$Answer->answer . "" + 1]]; //0 index is id so } else { if ($question->type == 4) { //multi correct MCQ $choices = array_values($question->choices->toArray()); $answerArr = explode(",", $Answer->answer); $out = ""; foreach ($answerArr as $a) { $out .= $choices[$a . "" + 1] . "<br>"; } return ['answer' => $out]; } } } else { // It is a paragraph $question = Question::find($questionid); $out = []; foreach ($question->paragraphQuestions as $pquestion) { array_push($out, $this->loadanswer($pquestion->id)); } return ['answer' => $out]; } return $Answer; }
public function createAnswer($type, $uuid, $answer) { $q_answer = Answer::where("question_id", $uuid)->first(); if (!$q_answer) { $q_answer = new Answer(); $q_answer->id = Uuid::generate(); } if ($type == 7 || $type == 4 || $type == 3) { $answer = implode(",", $answer); } $q_answer->question_id = $uuid; $q_answer->answer = $answer; $q_answer->save(); return $q_answer; }