Skip to content

Commit

Permalink
Merge pull request #1866 from SolidCharity/TP-202401-refactoring_stor…
Browse files Browse the repository at this point in the history
…eAnswersForQuestion

refactoring: move code to separate function storeAnswersForQuestion
  • Loading branch information
Chartman123 authored Jan 5, 2024
2 parents 3d19e39 + f24a595 commit f08c222
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f08c222

Please sign in to comment.