Repository navigation
Conditional questions: go to a question based on the answer - #3833
AndyScherzinger wants to merge 3 commits into
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
b833f24 to
9d9b414
Compare
21f29c0 to
c2cf51a
Compare
I personally also would prefer this Version as this could allow trigger questions to be seperated from the conditional question. (E.g Last question is only shown when first question is selected). Grouping functionality would get implemented independently by other running PRs. |
b444385 to
025620c
Compare
This comment was marked as outdated.
This comment was marked as outdated.
|
@timedin-de thanks, that is the direction the stack has taken now: there is no I looked at how MS Forms does it. There, each option of a choice question jumps forward to a later question, a section or the end of the form, and "nested" comes from chaining those jumps. Two things from your comment are not covered by this stack yet:
Both would come as follow-up PRs after this stack, so the stack stays reviewable. Section navigation ("after this section go to …", "go to section" per option, "end of the form") belongs to the sections work in #3664. Does that cover what you had in mind, or do you need jumps to single questions? |
ce2e434 to
06bd420
Compare
34b0757 to
4837c59
Compare
9e36cd2 to
1b7bc4e
Compare
|
looks good, also checked it with few edge cases and all pass. i have few points in regard to the code though which ill mention for improvement. @AndyScherzinger just one concern, maybe im wrong but please correct me if so @Chartman123 , if a form has a mandataroy question, and with the jump that question is skipped and form is submitted, wouldnt that be an issue? i just tested it and the mandatory question was jumped and i was able to submit the form but im just wondering could this be a potential issue? |
| page | ||
| .getByRole('listitem', { name: 'Question number 1' }) | ||
| .locator('.jump-target-select') | ||
| .filter({ |
There was a problem hiding this comment.
the path rules are written twice: 'getVisibleQuestions', 'awaitsAnswer' and 'getJumpTarget' here, and the same three in SubmissionService. tests/fixtures/jump-paths.json is run by both PHPUnit and Playwright, which is a good safeguard. maybe a good idea to add a case to that fixture whenever one of these rules changes.
There was a problem hiding this comment.
| * @param array<int, int> $optionIdMap the ids of the copied options by original id | ||
| * @return array the extra settings of the copy | ||
| */ | ||
| public static function mapJumps(array $extraSettings, ?array $questionIdMap, array $optionIdMap): array { |
There was a problem hiding this comment.
'mapJumps', 'mapJumpsOfCopies', 'removeBackwardJumps' and 'getAllowedExtraSettings' are all static methods, which is already very large. since theyre called from 'ApiController', 'SubmissionService' and 'FormsMigrator', a small helper class would match src/utils/Jumps.ts and make these easier to test on their own.
There was a problem hiding this comment.
I'd keep them in FormsService for this stack. Moving them now means renaming the call sites in every one of the 11 stacked PRs, and the sections PRs add more jump helpers next to these (removeDisallowedJumps, moveNextSectionUp). They are pure static functions and FormsServiceTest already tests them directly, so the extraction is a mechanical follow-up once the stack is merged. I'm happy to do that PR then.
Assisted by ClaudeCode:claude-opus-5-5
There was a problem hiding this comment.
Changed my mind and did it now, in f542660 and through the whole stack. The static jump rules (isJumpTarget, isGoToValid, getJumpAnswer, mapJumps, mapJumpsOfCopies, removeBackwardJumps, which becomes removeDisallowedJumps in #4008, and removeJumpsOutsideScale from #3837) now live in OCA\Forms\Helper\JumpHelper, and their tests are in tests/Unit/Helper/JumpHelperTest.php. ApiController and FormsMigrator call the helper directly.
A few things stay in FormsService:
getAllowedExtraSettings, because it is the extra-settings whitelist of all question types, not a jump rule.areJumpsOfFormandremoveJumpsOfMissingOptions, because they are instance methods: one needs the option mapper, and the controller tests mock the other.countQuestionsOutsideSections(sections stack), because it is a section rule.
The fixes from my other replies are in f542660 too.
Assisted by ClaudeCode:claude-opus-5-5
| @@ -658,8 +678,10 @@ private static function isShownToRespondents(array $question): bool { | |||
| * @throw \InvalidArgumentException if validation failed | |||
| */ | |||
| public function validateSubmission(array $questions, array $answers, string $formOwnerId, int $formId): void { | |||
There was a problem hiding this comment.
'validateSubmission' checks the path but returns nothin. 'newSubmission' and 'updateSubmission' then call 'getAnswersOnPath' which works out the same path again. isnt it maybe better if 'validateSubmission' return the answers that are on the path, so a later caller cannot store answers to skipped questions by forgetting the second call?
There was a problem hiding this comment.
Good point, changed in f542660: validateSubmission now returns the answers on the path, and newSubmission and updateSubmission store exactly what it returns. getAnswersOnPath is gone, so the path is worked out once and there is no second call to forget.
Assisted by ClaudeCode:claude-opus-5-5
| elseif ($question['type'] === Constants::ANSWER_TYPE_GRID) { | ||
| $this->validateGridQuestion($question, $answers[$questionId]); | ||
| } elseif ($answer === Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX && self::isOtherAnswer($question, $answer)) { | ||
| throw new \InvalidArgumentException(sprintf('The answer "Other" for question "%s" needs a text.', $question['text'])); |
There was a problem hiding this comment.
this rejects an empty 'other' answer for every form, not only forms with jumps, including an optional question. the form UI already requires the text, so people filling in the form will not see it. an API client that sends an empty Other to an optional question now gets a 400. this is a separate behavior change, so it should be mentioned in the description or put in its own commit.
There was a problem hiding this comment.
Right, that was broader than needed. Fixed in f542660: an empty "Other" is only rejected where the question has a jump for "Other", because that is where the answer decides the path. Every other question accepts it as before, a test covers both cases, and docs/API_v3.md says so.
Assisted by ClaudeCode:claude-opus-5-5
That's by design :) mandatory questions are only mandatory when they are reachable from the current path |
@samin-z Thanks for raising it and to answer the question: No it is fine to skip mandatory questions because you are not skipping them logically speaking. What happens due to the jumps is that the one linear patch (your form) becomes multiple "navigational paths" (think: tree) to the "end" (the submission). So without the jumps any mandatory question is mandatory because you come to that question on your path. With jumps only mandatory questions in your path are mandatory. so technically we save a sequence of questions+answers but logically we defined a tree. Feel free to comment on this @Chartman123 Also for transparency: This is also how Google and Microsoft Forms do it. Mandatory is always enforced on the path, not on the form. EDIT: Did refresh, hence missed the reply to the question.... Sorry for the noise |
As in Microsoft Forms, an answer can lead to a later question or to the end of the form, while the form stays a flat list of questions. Radio and dropdown questions store a target per option, and radio questions one for the answer "Other", in extraSettings.goTo. The other question types with jumps (short and long text, file, color, ranking, grid) store one target after the question in extraSettings.next. A question type has either goTo or next, never both: sending the other one is rejected as invalid extraSettings, and an answer without a jump leads to the following question. A target is the id of a question of the same form or "submit", and an option id belongs to the question; ids of questions of other forms are rejected, while ids of deleted questions of the form are accepted. Updating a question removes its jumps for options that are not options of the question, reported as removed-jumps. The path of a respondent starts at the first question and follows these targets. As in Microsoft Forms, it ends at a question with jumps per answer that has no answer yet: the questions after it appear once it is answered, and an optional one left empty lets the form be submitted without them. Only the first value of an answer decides, which keeps the walk linear in the size of a submission, and answers to questions the submit view does not show, such as questions without a title, do not count and their jumps per answer do not end the path, so they cannot skip required questions. A target that is not a later question of the form, for example after deleting it, counts as the following question. Answers to the questions off the path are ignored, also when a form changed while it was answered, and required only applies on the path. An answer "Other" needs a text where the question has a jump for "Other", as it decides the path there. Jumps never go back. Updating, reordering and copying a question remove the jumps to the question itself or to an earlier question; the request still succeeds with its usual status and a payload of the same shape, and the response header X-Forms-Adjusted lists the questions concerned as removed-jumps, or holds removed-jumps alone for more than 100 questions, which keeps the header small enough for proxies. A reorder returns the new extraSettings of these questions in its own response type QuestionOrder. Copying or importing a form maps the targets and the option ids of each question and drops such jumps as well, without the header, together with jumps whose target or option was not copied, jumps for an option of another question, jumps per answer with an invalid structure, and goTo or next on a question type without it. Exports keep the option ids for that. The server keeps its static jump rules in OCA\Forms\Helper\JumpHelper, so they can be tested on their own. tests/fixtures/jump-paths.json holds the path cases, shared with the frontend. Based on the conditional questions of #3123 by Micke Nordin and TimedIn. Co-Authored-By: Micke Nordin <kano@sunet.se> Co-Authored-By: TimedIn <git@timedin.net> Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
"Go to based on answer" in the question menu shows a select at the end of each option row of radio and dropdown questions, and of the row "Other": the next question, a later question or the end of the form. The other question types with jumps get the select "After question N" below them instead. Each select of an answer has the visible label "Go to" and the accessible name After "<answer>" go to. Below a width of 480px the option text takes the first line and its select shares the second line with the move and delete buttons, so the keyboard focus follows the visual order. Option rows without a select are as high as rows with one, so turning the jumps on or off does not move them. A target that was deleted or moved before the question is shown as going to the next question. Unchecking "Go to" closes the menu and asks before it removes the jumps of the question. The warnings let jumps of questions the submit view does not show lead nowhere, give dropdowns no answer "Other", as the server does, and count a jump of the answer "Other" as broken while "Other" is off, as a reorder removes it as well. A question with jumps per answer that is not required, and that the submit view shows with answers to choose from, shows a hint to make it required, as respondents see the questions after it only once they answer it. Only this hint is announced to screen readers when it appears, with the number of its question, as a change of another question can make it appear. Without an answer the path ends at such a question, so the editor no longer counts it as leading to the following question when it finds questions no respondent reaches. Changes are kept in the question and saved as complete lists. After a reorder the editor removes the jumps the server removed, those that go back in the saved order, from the current extra settings, and keeps jumps set in the meantime; other extra settings the server adjusted replace the local ones unless they changed in the meantime, and deleting an option drops its jumps once the server confirms the deletion. As the server rewrites the jumps of the questions concerned, deleting an option waits until the earlier requests of its question are done, and adding, copying, deleting and reordering questions wait until the earlier requests of all questions are done. Later requests wait for them in turn, so a jump saved meanwhile is not lost and a reorder lists the questions the server has. While a question of the form is added, copied or deleted, its questions cannot be moved, a second deletion of the same question is ignored, and the editor ignores the answer if another form was opened meanwhile. playwright/e2e/jump-paths.spec.ts runs the path cases shared with the server against the frontend walk. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
The submit view shows the questions on the path of the current answers, so the questions after a question with jumps per answer appear once it is answered, and announces when questions are added or skipped. Answers to skipped questions are kept while the form is open, but not submitted. When an answer of a submission being edited no longer matches an option of its question, or an answer it restores is off the path now, the view asks whether to clear the form, as saving drops the answers to the questions skipped now. A question with jumps per answer whose earlier answer the form no longer offers is required while the submission is edited and asks to choose again, and once the dialog closes the focus moves to the first such question, so the respondent cannot save without the answers after it unnoticed. An option renamed after the submission comes back as an answer "Other" where the question allows one; when restored answers are off the path then, the answer "Other" of a question with jumps per answer is not restored either and the question asks to choose again. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Conditional questions as in Microsoft Forms, based on #3123 by @mickenordin with the fixes by @timedin-de, split into a stack so it can be reviewed in parts (as suggested by @Chartman123 in #3123 (comment)).
Fixes #358
Stack
Sections build on conditional questions. Each PR is based on the one before it and works on its own. Review and merge from the top.
Conditional questions:
Sections:
This PR
An answer can lead to a later question or to the end of the form. The form stays a flat list of questions; the jumps live in the question's
extraSettings, as suggested by @Chartman123 in #3833 (review).Editor
After "<answer>" go to).Data
goToholds one target per answer,nextone target after the question; a type has one of them, never both."submit"; questions of other forms are rejected with a 400.X-Forms-Adjusted, so a stale jump never blocks saving the question.X-Forms-Adjusted(RFC 8941, e.g.removed-jumps=(3 7)); status and payload shape stay the same.X-Forms-Adjusted.Answers
tests/fixtures/jump-paths.jsonholds the path cases, run by PHPUnit and Playwright, so both sides walk the same way.Follow-ups
Sections with "go to section" (#357, #624) follow as a separate feature, see #3664; a section will be a valid jump target without a format change.
Tests
🤖 AI (if applicable)