Skip to content

Commit 55add4c

Browse files
feat(section): go to a chosen section after a section
A section can name where respondents go after it, as "After this section go to" in Microsoft Forms: the next section, a later section or the end of the form. It applies when the path leaves the section by default, so a jump of an answer out of the section still wins, and a jump within the section still ends at it. The editor shows the choice on every section and counts the questions it skips as unreachable. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent e0eb191 commit 55add4c

15 files changed

Lines changed: 579 additions & 44 deletions

File tree

‎docs/DataStructure.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ Optional extra settings for some [Question Types](#question-types)
282282
| `questionType` | `linearscale` | String | `rating`, `promoter` | Sub type, fixed at creation; `rating` is a 1 to N scale drawn as icons, `promoter` is a 0 to 10 scale whose results show the promoter score; absent for a plain scale |
283283
| `ratingIcon` | `linearscale` with `questionType` `rating` | string | `'star', 'heart', 'thumb'` | Set the icon of the rating, default: `'star'` |
284284
| `goTo` | `dropdown, linearscale, multiple_unique` | Array | see [Jumps](#jumps) | Where to go next for an answer |
285-
| `next` | `color, date, file, grid, long, multiple, ranking, short, time` | Integer / string | question id, `'submit'` | Where to go after the question, see [Jumps](#jumps) |
285+
| `next` | `color, date, file, grid, long, multiple, ranking, section, short, time` | Integer / string | question id, `'submit'` | Where to go after the question or, on a section, after the section, see [Jumps](#jumps) |
286286

287287
### Jumps
288288

@@ -299,6 +299,7 @@ An answer can lead to a later question or to the end of the form, as in Microsof
299299
- Only the deprecated `datetime` accepts neither `goTo` nor `next` (400).
300300
- A jump of the answer "Other" is for `multiple_unique` questions, as dropdowns have no answer "Other", and only matches while `allowOtherAnswer` is on.
301301
- `next` also applies when the question is left unanswered.
302+
- `next` on a section is where respondents go after the section, as "After this section go to" in Microsoft Forms: the id of a later section or `"submit"`; other targets are removed like other disallowed jumps. It applies when the path leaves the section towards the following section or the end of the form, so a jump of an answer out of the section wins over it, while a jump within the section still ends at it.
302303

303304
The respondent's path starts at the first question and follows these targets. It ends at a question that has no answer yet and a jump per answer past the following question, a jump of the answer "Other" only while `allowOtherAnswer` is on, and the questions after it appear once it is answered, as in Microsoft Forms. If such a question is optional and left empty, the form can be submitted without the questions after it. The first value of an answer decides, and answers to questions the submit view does not show, such as questions without a title, do not count. A target that is not a later question of the form, for example after the target was deleted, counts as the following question, and a jump of an option that no longer exists never matches. Questions off the path are skipped: answers to them are ignored, and their required flag does not apply. Editing a submission replaces all its stored answers, so answers to questions that are skipped now are removed.
304305

‎lib/Constants.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ class Constants {
287287
'next' => ['integer', 'string'],
288288
];
289289

290+
public const EXTRA_SETTINGS_SECTION = [
291+
'next' => ['integer', 'string'],
292+
];
293+
290294
public const EXTRA_SETTINGS_GRID_QUESTION_TYPE = [
291295
self::ANSWER_GRID_TYPE_CHECKBOX,
292296
self::ANSWER_GRID_TYPE_NUMBER,

‎lib/Helper/JumpHelper.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public static function mapJumpsOfCopies(array $questions, array $questionIdMap,
167167
}
168168

169169
/**
170-
* Remove the jumps that go to the question itself, to an earlier question or into the middle of another section, keeping the jumps to the end of the form, to later questions of the same section, to later sections and to questions that are not in the list
170+
* Remove the jumps that go to the question itself, to an earlier question or into the middle of another section, and the jumps of a section to anything but a later section, keeping the jumps to the end of the form, to later questions of the same section, to later sections and to questions that are not in the list
171171
*
172172
* @param list<Question> $questions the questions of the form in their order
173173
* @return list<Question> the questions whose jumps were removed, with their new extra settings set
@@ -187,7 +187,8 @@ public static function removeDisallowedJumps(array $questions): array {
187187
foreach ($questions as $position => $question) {
188188
$isDisallowed = static fn (mixed $target): bool => is_int($target) && isset($positions[$target])
189189
&& ($positions[$target] <= $position
190-
|| ($questions[$positions[$target]]->getType() !== Constants::ANSWER_TYPE_SECTION && $sections[$positions[$target]] !== $sections[$position]));
190+
|| ($questions[$positions[$target]]->getType() !== Constants::ANSWER_TYPE_SECTION
191+
&& ($question->getType() === Constants::ANSWER_TYPE_SECTION || $sections[$positions[$target]] !== $sections[$position])));
191192
$extraSettings = $question->getExtraSettings();
192193
$newSettings = $extraSettings;
193194

‎lib/Service/FormsService.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ public static function getAllowedExtraSettings(string $questionType): array {
909909
Constants::ANSWER_TYPE_COLOR => Constants::EXTRA_SETTINGS_COLOR,
910910
Constants::ANSWER_TYPE_TIME => Constants::EXTRA_SETTINGS_TIME,
911911
Constants::ANSWER_TYPE_LINEARSCALE => Constants::EXTRA_SETTINGS_LINEARSCALE,
912+
Constants::ANSWER_TYPE_SECTION => Constants::EXTRA_SETTINGS_SECTION,
912913
default => [],
913914
};
914915
}

‎lib/Service/SubmissionService.php‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ public function validateSubmission(array $questions, array $answers, string $for
859859
* The questions on the path of the respondent: from the first question, each question leads to the target of its
860860
* answer in `goTo`, else to its `next` target, else to the following question, until the end of the form.
861861
* A target that is not a later question of the form counts as the following question.
862+
* Leaving a section towards the following question or the end of the form goes to the `next` target of the section instead, if it has one.
862863
* The path ends at a question that has no answer yet and a jump per answer past the following question.
863864
*
864865
* @param array $questions the questions of the form in their order
@@ -869,12 +870,18 @@ public static function getVisibleQuestions(array $questions, array $answers): ar
869870
$questions = array_values($questions);
870871
$positions = array_flip(array_column($questions, 'id'));
871872
$visibleQuestions = [];
873+
$sectionNext = null;
872874

873875
$position = 0;
874876
while ($position < count($questions)) {
875877
$question = $questions[$position];
876878
$visibleQuestions[] = $question;
879+
$toPosition = static fn (mixed $target): ?int => is_int($target) && isset($positions[$target]) && $positions[$target] > $position ? $positions[$target] : null;
877880

881+
if (($question['type'] ?? null) === Constants::ANSWER_TYPE_SECTION) {
882+
$next = $question['extraSettings']['next'] ?? null;
883+
$sectionNext = is_int($next) || is_string($next) ? $next : null;
884+
}
878885
$answer = $answers[$question['id']] ?? [];
879886
if (self::awaitsAnswer($question, $answer, $position, $positions, count($questions))) {
880887
break;
@@ -883,8 +890,15 @@ public static function getVisibleQuestions(array $questions, array $answers): ar
883890
if ($target === Constants::JUMP_TARGET_SUBMIT) {
884891
break;
885892
}
886-
$targetPosition = is_int($target) && isset($positions[$target]) ? $positions[$target] : null;
887-
$position = $targetPosition !== null && $targetPosition > $position ? $targetPosition : $position + 1;
893+
$targetPosition = $toPosition($target);
894+
if ($targetPosition === null && $sectionNext !== null
895+
&& ($position + 1 === count($questions) || ($questions[$position + 1]['type'] ?? null) === Constants::ANSWER_TYPE_SECTION)) {
896+
if ($sectionNext === Constants::JUMP_TARGET_SUBMIT) {
897+
break;
898+
}
899+
$targetPosition = $toPosition($sectionNext);
900+
}
901+
$position = $targetPosition ?? $position + 1;
888902
}
889903

890904
return $visibleQuestions;
@@ -947,7 +961,7 @@ private static function awaitsAnswer(array $question, mixed $answer, int $positi
947961
}
948962

949963
/**
950-
* The jump target of a question for its answer: the target of the `goTo` entry matching the first value of the answer, else `next` on question types with `next`
964+
* The jump target of a question for its answer: the target of the `goTo` entry matching the first value of the answer, else `next` on question types with `next` other than sections
951965
*
952966
* @param array $question the question
953967
* @param mixed $answer the values of the answer to the question
@@ -968,7 +982,8 @@ private static function getJumpTarget(array $question, mixed $answer): string|in
968982
}
969983
}
970984

971-
$next = isset(FormsService::getAllowedExtraSettings((string)($question['type'] ?? ''))['next']) ? ($extraSettings['next'] ?? null) : null;
985+
$type = (string)($question['type'] ?? '');
986+
$next = $type !== Constants::ANSWER_TYPE_SECTION && isset(FormsService::getAllowedExtraSettings($type)['next']) ? ($extraSettings['next'] ?? null) : null;
972987
return is_int($next) || is_string($next) ? $next : null;
973988
}
974989

‎playwright/e2e/jump-issues.spec.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,73 @@ test('jumps: in a form with sections, a jump into the middle of another section
319319
])
320320
})
321321

322+
test('jumps: a section jumps only to a later section or the end of the form', () => {
323+
const section = (
324+
id: number,
325+
extraSettings: FormsQuestion['extraSettings'] = {},
326+
): FormsQuestion => ({ ...short(id, extraSettings), type: 'section' })
327+
const allowed = withoutDisallowedJumps([
328+
section(1, { next: 2 }),
329+
short(2),
330+
section(3, { next: 5 }),
331+
short(4),
332+
section(5, { next: 'submit' }),
333+
short(6),
334+
])
335+
expect(allowed.map((question) => question.extraSettings)).toEqual([
336+
{},
337+
{},
338+
{ next: 5 },
339+
{},
340+
{ next: 'submit' },
341+
{},
342+
])
343+
})
344+
345+
test('jumps: questions skipped by the next target of a section are unreachable', () => {
346+
const section = (
347+
id: number,
348+
extraSettings: FormsQuestion['extraSettings'] = {},
349+
): FormsQuestion => ({ ...short(id, extraSettings), type: 'section' })
350+
expect([
351+
...getJumpIssues([
352+
section(1, { next: 5 }),
353+
short(2),
354+
section(3),
355+
short(4),
356+
section(5),
357+
short(6),
358+
]).unreachable,
359+
]).toEqual([3, 4])
360+
})
361+
362+
test('jump issues: two questions of a section that jump out of it are both named', () => {
363+
const section = (id: number): FormsQuestion => ({
364+
...short(id),
365+
type: 'section',
366+
})
367+
expect([
368+
...getJumpIssues([
369+
section(1),
370+
radio(2, { goTo: [{ optionId: 11, target: 6 }] }),
371+
short(3, { next: 'submit' }),
372+
short(4, { next: 5 }),
373+
short(5),
374+
section(6),
375+
short(7),
376+
]).leavingSection,
377+
]).toEqual([2, 3])
378+
expect([
379+
...getJumpIssues([
380+
section(1),
381+
radio(2, { goTo: [{ optionId: 11, target: 4 }] }),
382+
short(3),
383+
section(4),
384+
short(5),
385+
]).leavingSection,
386+
]).toEqual([])
387+
})
388+
322389
test('jumps: only the jumps a reorder removed are removed from newer extra settings', () => {
323390
const before = {
324391
goTo: [

‎playwright/e2e/sections.spec.ts‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,115 @@ test.describe('Sections', () => {
474474
expect(stored.extraSettings.goTo).toEqual([])
475475
})
476476

477+
test('After a section, respondents go to the section it names', async ({
478+
page,
479+
submitView,
480+
}) => {
481+
const form = await api(page.request, 'POST', '/forms')
482+
for (const [type, text] of [
483+
['section', 'Part one'],
484+
['short', 'First'],
485+
['section', 'Part two'],
486+
['short', 'Second'],
487+
['section', 'Part three'],
488+
['short', 'Third'],
489+
]) {
490+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
491+
type,
492+
text,
493+
})
494+
}
495+
await page.goto(`apps/forms/${form.hash}/edit`)
496+
497+
const select = page
498+
.getByRole('listitem', { name: 'Section number 1' })
499+
.getByRole('combobox', { name: 'After section 1 go to' })
500+
await select.click()
501+
await expect(page.getByRole('option')).toHaveText([
502+
'Next section',
503+
'Section 2: Part two',
504+
'Section 3: Part three',
505+
'End of the form',
506+
])
507+
const saved = page.waitForResponse(
508+
(response) =>
509+
response.request().method() === 'PATCH'
510+
&& response.url().includes(`/forms/${form.id}/questions/`),
511+
)
512+
await page.getByRole('option', { name: 'Section 3: Part three' }).click()
513+
expect((await saved).ok()).toBeTruthy()
514+
await expect(
515+
page
516+
.getByRole('listitem', { name: 'Section number 3' })
517+
.getByRole('combobox', { name: 'After section 3 go to' }),
518+
).toBeVisible()
519+
520+
await page.goto(`apps/forms/${form.hash}/submit`)
521+
await submitView.fillText('First', 'one')
522+
await submitView.nextButton.click()
523+
await expect(submitView.getQuestion('Part three')).toBeVisible()
524+
await expect(submitView.nextButton).toHaveCount(0)
525+
await submitView.fillText('Third', 'three')
526+
const response = await submitView.submit()
527+
expect(Object.keys(response.request().postDataJSON().answers)).toHaveLength(
528+
2,
529+
)
530+
await expect(submitView.successMessage).toBeVisible()
531+
})
532+
533+
test('A section that goes to a deleted section is named as a section and shows where respondents go', async ({
534+
page,
535+
}) => {
536+
const form = await api(page.request, 'POST', '/forms')
537+
const ids: number[] = []
538+
for (const [type, text] of [
539+
['section', 'Part one'],
540+
['short', 'First'],
541+
['section', 'Part two'],
542+
['short', 'Second'],
543+
['section', 'Part three'],
544+
]) {
545+
ids.push(
546+
(
547+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
548+
type,
549+
text,
550+
})
551+
).id,
552+
)
553+
}
554+
await api(page.request, 'PATCH', `/forms/${form.id}/questions/${ids[0]}`, {
555+
keyValuePairs: { extraSettings: { next: ids[4] } },
556+
})
557+
await api(page.request, 'PATCH', `/forms/${form.id}/questions/${ids[2]}`, {
558+
keyValuePairs: { extraSettings: { next: 'submit' } },
559+
})
560+
await page.goto(`apps/forms/${form.hash}/edit`)
561+
562+
await page
563+
.getByRole('listitem', { name: 'Section number 3' })
564+
.getByRole('button', { name: 'Actions', exact: true })
565+
.click()
566+
await page
567+
.getByRole('menuitem', { name: /^Delete (question|section)$/ })
568+
.click()
569+
await expect(
570+
page
571+
.getByRole('status')
572+
.getByText(
573+
'Section 1 went to a section that was deleted, so respondents now go to the next section instead.',
574+
),
575+
).toBeVisible()
576+
const label = (number: number) =>
577+
page
578+
.getByRole('listitem', { name: `Section number ${number}` })
579+
.locator('.jump-target-select__label')
580+
await expect(label(1)).toHaveText(
581+
'Next section (the chosen section was deleted or moved up)',
582+
)
583+
await expect(label(2)).toHaveText('End of the form')
584+
})
585+
477586
test('Each page checks its required questions, an empty section is an info page', async ({
478587
page,
479588
submitView,
@@ -784,4 +893,58 @@ test.describe('Sections', () => {
784893
'Second',
785894
])
786895
})
896+
897+
test('An empty section sends respondents on to its target without a page of its own', async ({
898+
page,
899+
submitView,
900+
}) => {
901+
const form = await api(page.request, 'POST', '/forms')
902+
const ids: Record<string, number> = {}
903+
for (const [type, text] of [
904+
['section', 'Start'],
905+
['short', 'First'],
906+
['section', 'Jump'],
907+
['section', 'Skipped'],
908+
['short', 'Second'],
909+
['section', 'End'],
910+
['short', 'Third'],
911+
['section', 'Finish'],
912+
['section', 'Not reached'],
913+
['short', 'Never'],
914+
]) {
915+
ids[text] = (
916+
await api(page.request, 'POST', `/forms/${form.id}/questions`, {
917+
type,
918+
text: ['Jump', 'Finish'].includes(text) ? '' : text,
919+
})
920+
).id
921+
}
922+
for (const [section, next] of [
923+
['Jump', ids.End],
924+
['End', ids.Finish],
925+
['Finish', 'submit'],
926+
] as const) {
927+
await api(
928+
page.request,
929+
'PATCH',
930+
`/forms/${form.id}/questions/${ids[section]}`,
931+
{ keyValuePairs: { extraSettings: { next } } },
932+
)
933+
}
934+
await page.goto(`apps/forms/${form.hash}/submit`)
935+
936+
await submitView.fillText('First', 'one')
937+
await submitView.nextButton.click()
938+
await expect(submitView.getQuestion('End')).toBeVisible()
939+
await expect(submitView.getQuestion('Skipped')).toBeHidden()
940+
await expect(submitView.nextButton).toHaveCount(0)
941+
await submitView.fillText('Third', 'three')
942+
const response = await submitView.submit()
943+
expect(
944+
Object.keys(response.request().postDataJSON().answers)
945+
.map(Number)
946+
.sort((a, b) => a - b),
947+
).toEqual([ids.First, ids.Third])
948+
await expect(submitView.successMessage).toBeVisible()
949+
})
787950
})

‎src/components/Questions/JumpTargetSelect.vue‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ export default defineComponent({
7171
type: String,
7272
default: null,
7373
},
74+
75+
/**
76+
* Label of a target that is not offered any more, by default going to the next question
77+
*/
78+
fallbackLabel: {
79+
type: String,
80+
default: () =>
81+
t(
82+
'forms',
83+
'Next question (the chosen question was deleted or moved up)',
84+
),
85+
},
7486
},
7587
7688
emits: ['update:modelValue'],
@@ -80,10 +92,7 @@ export default defineComponent({
8092
return (
8193
this.options.find((option) => option.id === this.modelValue) ?? {
8294
id: this.modelValue,
83-
label: t(
84-
'forms',
85-
'Next question (the chosen question was deleted or moved up)',
86-
),
95+
label: this.fallbackLabel,
8796
}
8897
)
8998
},

0 commit comments

Comments
 (0)