Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring: move code to separate function storeAnswersForQuestion #1866

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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