Skip to content

Commit 94aefa7

Browse files
timedin-demickenordin
authored andcommitted
fix: backend support for multiple active branches
Signed-off-by: TimedIn <git@timedin.net>
1 parent a50b542 commit 94aefa7

2 files changed

Lines changed: 20 additions & 27 deletions

File tree

lib/Controller/ApiController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,9 +2169,8 @@ private function storeConditionalAnswer(Form $form, int $submissionId, array $qu
21692169
}
21702170
}
21712171

2172-
// Store subquestion answers only for the branch the trigger activated
2173-
$activeBranch = $this->submissionService->getActiveBranch($question, $triggerAnswers);
2174-
$branches = $activeBranch === null ? [] : [$activeBranch];
2172+
// Store subquestion answers only for the branches the trigger activated
2173+
$branches = $this->submissionService->getActiveBranches($question, $triggerAnswers);
21752174
foreach ($branches as $branch) {
21762175
$branchSubQuestions = $branch['subQuestions'] ?? [];
21772176
foreach ($branchSubQuestions as $subQuestion) {

lib/Service/SubmissionService.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ private function escapeCSV(string $value): string {
473473
* @param array $questions Array of the questions of the form
474474
* @param array $answers Array of the submitted answers
475475
* @param string $formOwnerId Owner of the form
476-
* @throw \InvalidArgumentException if validation failed
476+
* @throws \InvalidArgumentException if validation failed
477477
*/
478478
public function validateSubmission(array $questions, array $answers, string $formOwnerId): void {
479479
// Check by questions
@@ -767,10 +767,10 @@ private function validateConditionalQuestion(array $question, array $answerData,
767767
throw new \InvalidArgumentException(sprintf('Conditional question "%s" is missing trigger type configuration.', $question['text']));
768768
}
769769

770-
// Find the active branch based on trigger answer
771-
$activeBranch = $this->findActiveBranch($triggerType, $triggerAnswer, $branches, $question['options'] ?? []);
770+
// Find the active branches based on trigger answer
771+
$activeBranches = $this->findActiveBranches($triggerType, $triggerAnswer, $branches, $question['options'] ?? []);
772772

773-
if ($activeBranch === null && !empty($branches)) {
773+
if (\count($activeBranches) === 0 && !empty($branches)) {
774774
// No branch matched but branches are defined - this might be okay if trigger has no value yet
775775
// Only throw if trigger has a value that doesn't match any branch
776776
if (!empty($triggerAnswer)) {
@@ -783,8 +783,10 @@ private function validateConditionalQuestion(array $question, array $answerData,
783783
}
784784

785785
// Validate the active branch's subquestions with the full per-question rules
786-
if ($activeBranch !== null && isset($activeBranch['subQuestions'])) {
787-
$this->validateSubmission($activeBranch['subQuestions'], $subQuestionAnswers, $formOwnerId);
786+
if (\count($activeBranches) > 0) {
787+
// Merge subquestion of all active branches
788+
$subQuestions = array_merge(...array_column($activeBranches, 'subQuestions'));
789+
$this->validateSubmission($subQuestions, $subQuestionAnswers, $formOwnerId);
788790
}
789791
}
790792

@@ -793,11 +795,11 @@ private function validateConditionalQuestion(array $question, array $answerData,
793795
*
794796
* @param array $question The conditional question
795797
* @param array $triggerAnswer The trigger answer values
796-
* @return array|null The active branch or null if none matches
798+
* @return array The active branches or empty array if none matches
797799
*/
798-
public function getActiveBranch(array $question, array $triggerAnswer): ?array {
800+
public function getActiveBranches(array $question, array $triggerAnswer): ?array {
799801
$extraSettings = $question['extraSettings'] ?? [];
800-
return $this->findActiveBranch(
802+
return $this->findActiveBranches(
801803
$extraSettings['triggerType'] ?? '',
802804
$triggerAnswer,
803805
$extraSettings['branches'] ?? [],
@@ -806,30 +808,22 @@ public function getActiveBranch(array $question, array $triggerAnswer): ?array {
806808
}
807809

808810
/**
809-
* Find the active branch based on trigger answer
811+
* Find the active branches based on trigger answer
810812
*
811813
* @param string $triggerType The type of the trigger question
812814
* @param array $triggerAnswer The trigger answer values
813815
* @param array $branches The available branches
814816
* @param array $options The options for the trigger question
815-
* @return array|null The active branch or null if none matches
817+
* @return array The active branches or empty array if none matches
816818
*/
817-
private function findActiveBranch(string $triggerType, array $triggerAnswer, array $branches, array $options): ?array {
818-
foreach ($branches as $branch) {
819+
private function findActiveBranches(string $triggerType, array $triggerAnswer, array $branches, array $options): ?array {
820+
return array_filter($branches, function ($branch) use ($triggerType, $triggerAnswer) {
819821
$conditions = $branch['conditions'] ?? [];
820-
821822
if (empty($conditions)) {
822-
continue;
823-
}
824-
825-
$matches = $this->evaluateBranchConditions($triggerType, $triggerAnswer, $conditions);
826-
827-
if ($matches) {
828-
return $branch;
823+
return false;
829824
}
830-
}
831-
832-
return null;
825+
return $this->evaluateBranchConditions($triggerType, $triggerAnswer, $conditions);
826+
});
833827
}
834828

835829
/**

0 commit comments

Comments
 (0)