Skip to content

Commit

Permalink
refactoring: move code to separate function storeAnswersForQuestion
Browse files Browse the repository at this point in the history
this helps in preparation for nextcloud#1110

Signed-off-by: Timotheus Pokorra <timotheus.pokorra@solidcharity.com>
  • Loading branch information
tpokorra committed Jan 5, 2024
1 parent 3d19e39 commit f24a595
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 f24a595

Please sign in to comment.