diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index b255ca7ca..4df9116bc 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -958,6 +958,42 @@ public function getSubmissions(string $hash): DataResponse { return new DataResponse($response); } + /** + * Insert answers for a question + * + * @param int $submissionId + * @param array $question + * @param array $answerArray [arrayOfString] + */ + private function storeAnswersForQuestion($submissionId, array $question, array $answerArray) { + foreach ($answerArray as $answer) { + $answerText = ''; + + // Are we using answer ids as values + if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED)) { + // Search corresponding option, skip processing if not found + $optionIndex = array_search($answer, array_column($question['options'], 'id')); + if ($optionIndex !== false) { + $answerText = $question['options'][$optionIndex]['text']; + } elseif (!empty($question['extraSettings']['allowOtherAnswer']) && strpos($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX) === 0) { + $answerText = str_replace(Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX, "", $answer); + } + } else { + $answerText = $answer; // Not a multiple-question, answerText is given answer + } + + if ($answerText === "") { + continue; + } + + $answerEntity = new Answer(); + $answerEntity->setSubmissionId($submissionId); + $answerEntity->setQuestionId($question['id']); + $answerEntity->setText($answerText); + $this->answerMapper->insert($answerEntity); + } + } + /** * @CORS * @PublicCORSFix @@ -1053,35 +1089,8 @@ public function insertSubmission(int $formId, array $answers, string $shareHash if ($questionIndex === false) { continue; } - - $question = $questions[$questionIndex]; - - foreach ($answerArray as $answer) { - $answerText = ''; - - // Are we using answer ids as values - if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED)) { - // Search corresponding option, skip processing if not found - $optionIndex = array_search($answer, array_column($question['options'], 'id')); - if ($optionIndex !== false) { - $answerText = $question['options'][$optionIndex]['text']; - } elseif (!empty($question['extraSettings']['allowOtherAnswer']) && strpos($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX) === 0) { - $answerText = str_replace(Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX, "", $answer); - } - } else { - $answerText = $answer; // Not a multiple-question, answerText is given answer - } - - if ($answerText === "") { - continue; - } - $answerEntity = new Answer(); - $answerEntity->setSubmissionId($submissionId); - $answerEntity->setQuestionId($question['id']); - $answerEntity->setText($answerText); - $this->answerMapper->insert($answerEntity); - } + $this->storeAnswersForQuestion($submission->getId(), $questions[$questionIndex], $answerArray); } $this->formsService->setLastUpdatedTimestamp($formId);