Skip to content

Commit e0eb191

Browse files
feat(section): limit jumps to the start of a section
In a form with sections, a jump goes to a later question of its own section, to a later section or to the end of the form, as in Microsoft Forms, so a page never starts in the middle of a section. The server removes other jumps wherever it removes backward jumps, and inserting a section removes the jumps that would go into its middle. The editor offers only these targets and asks before a move turns jumps into jumps into another section. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent 603cb46 commit e0eb191

12 files changed

Lines changed: 571 additions & 106 deletions

File tree

‎docs/API_v3.md‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Some requests succeed with their usual status, and their payload keeps its shape
3737

3838
| Key | Value | Set by | Meaning |
3939
|-----|-------|--------|---------|
40-
| `removed-jumps` | Inner list of question ids, e.g. `(3 7)` | Updating, reordering and copying questions; deleting the first section of a form; deleting options; changing the bounds of a linear scale | Jumps to the question itself or to an earlier question, jumps of the deleted option or of options that are not options of the question, or jumps for values outside the scale, were removed from the `extraSettings` of these questions, see [Jumps](DataStructure.md#jumps) |
40+
| `removed-jumps` | Inner list of question ids, e.g. `(3 7)` | Updating, reordering and copying questions; inserting a section; deleting the first section of a form; deleting options; changing the bounds of a linear scale | Jumps to the question itself, to an earlier question or into the middle of another section, jumps of the deleted option or of options that are not options of the question, or jumps for values outside the scale, were removed from the `extraSettings` of these questions, see [Jumps](DataStructure.md#jumps) |
4141

4242
```
4343
X-Forms-Adjusted: removed-jumps=(3 7)
@@ -64,7 +64,7 @@ When more than 100 questions lost jumps, which only a reorder can cause, `remove
6464

6565
### Other API changes
6666

67-
- Questions can jump to a later question or to the end of the form with the extra settings `goTo` (per answer, on dropdown, radio and linear scale questions) or `next` (after the question, on the other types with jumps), never both (see [Jumps](DataStructure.md#jumps)). Jumps to questions of other forms are rejected, jumps for options that are not options of the question are removed, deleting an option removes its jumps, and narrowing a linear scale removes the jumps of the values that are gone. Submissions and submission updates ignore answers to the questions the jumps skip and do not require them, the path ends at an unanswered question with a jump per answer past the following question, and an answer "Other" needs a text where the question has a jump for "Other". Updating, reordering and copying questions removes jumps to the question itself or to an earlier question; the response header `X-Forms-Adjusted` then lists the questions concerned under `removed-jumps` (see [Adjustments by the server](#adjustments-by-the-server)). Copying or importing a form drops such jumps as well, without the header, as these create a new form.
67+
- Questions can jump to a later question or to the end of the form with the extra settings `goTo` (per answer, on dropdown, radio and linear scale questions) or `next` (after the question, on the other types with jumps), never both (see [Jumps](DataStructure.md#jumps)). Jumps to questions of other forms are rejected, jumps for options that are not options of the question are removed, deleting an option removes its jumps, and narrowing a linear scale removes the jumps of the values that are gone. Submissions and submission updates ignore answers to the questions the jumps skip and do not require them, the path ends at an unanswered question with a jump per answer past the following question, and an answer "Other" needs a text where the question has a jump for "Other". Updating, reordering and copying questions removes jumps to the question itself, to an earlier question or into the middle of another section; the response header `X-Forms-Adjusted` then lists the questions concerned under `removed-jumps` (see [Adjustments by the server](#adjustments-by-the-server)). Copying or importing a form drops such jumps as well, without the header, as these create a new form.
6868
- In API version 3.0 the following endpoints were introduced/changed:
6969
- `GET /api/v3/forms/{formId}/questions` to get all questions of a form
7070
- `GET /api/v3/forms/{formId}/questions/{questionId}` to get a single question
@@ -276,7 +276,7 @@ Creates a clone of a form (without submissions).
276276
| Parameter | Type | Description |
277277
|-----------|---------|-------------|
278278
| _formId_ | Integer | ID of the form to clone |
279-
- Response: Returns the full object of the new form. See [Request full data of a Form](#request-full-data-of-a-form). The jumps of the copied questions point at the copied questions and options; jumps whose target or option was not copied, jumps that would go to the question itself or to an earlier question, and `goTo` or `next` on a question type without it, are dropped, without the header `X-Forms-Adjusted` (see [Jumps](DataStructure.md#jumps)).
279+
- Response: Returns the full object of the new form. See [Request full data of a Form](#request-full-data-of-a-form). The jumps of the copied questions point at the copied questions and options; jumps whose target or option was not copied, jumps that would go to the question itself, to an earlier question or into the middle of another section, and `goTo` or `next` on a question type without it, are dropped, without the header `X-Forms-Adjusted` (see [Jumps](DataStructure.md#jumps)).
280280

281281
```
282282
See section 'Request full data of a form'.
@@ -396,7 +396,7 @@ Returns the questions and options of the given form (without submissions).
396396
| _text_ | String | yes | _Optional_ The text of the new question. |
397397
| _position_ | Integer | yes | _(optional)_ 1-based position to insert the new question. When omitted the question is appended at the end. |
398398
- Restrictions: A `grid` question needs its cell type as _subtype_. A _subtype_ for a question type without sub types is rejected with **400 Bad Request**. In a form with sections every question is in a section (see [Sections](DataStructure.md#sections)): a question or section that would add to the questions before the first section, for example a section appended below the questions of a form without sections, is rejected with **400 Bad Request** "Every question must be in a section".
399-
- Response: The new question object.
399+
- Response: The new question object. A `section` inserted or cloned at a _position_ removes the jumps of other questions that would go into its middle (see [Jumps](DataStructure.md#jumps)), and the response header `X-Forms-Adjusted` then lists them under `removed-jumps` (see [Adjustments by the server](#adjustments-by-the-server)).
400400

401401
```
402402
"data": {
@@ -474,7 +474,7 @@ Update a single or multiple properties of a question-object.
474474
- It is **not allowed** to update one of the following key-value pairs: _id, formId, order, type_.
475475
- The sub type `extraSettings.questionType` is fixed when the question is created. An `extraSettings` value that adds, removes or changes `questionType` is rejected with **403 Forbidden**, so send the stored `questionType` with every `extraSettings` update.
476476
- A promoter score question (`questionType` `promoter`) keeps `optionsLowest` at `0` and `optionsHighest` at `10`; send both with every `extraSettings` update, or it is rejected with **400 Bad Request**.
477-
- Response: **Status-Code OK**, as well as the id of the updated question. Jumps in `extraSettings` that go to the question itself or to an earlier question, and jumps of a linear scale for values outside its bounds, are removed before saving (see [Jumps](DataStructure.md#jumps)), and the response header `X-Forms-Adjusted` then holds `removed-jumps=(<question id>)` (see [Adjustments by the server](#adjustments-by-the-server)). Jumps with an invalid structure, `goTo` or `next` on a question type without it, and ids of questions of other forms are rejected with **Status-Code Bad Request** "Invalid extraSettings, will not update." Jumps for options that are not options of the question are removed before saving, with the same header.
477+
- Response: **Status-Code OK**, as well as the id of the updated question. Jumps in `extraSettings` that go to the question itself, to an earlier question or into the middle of another section, and jumps of a linear scale for values outside its bounds, are removed before saving (see [Jumps](DataStructure.md#jumps)), and the response header `X-Forms-Adjusted` then holds `removed-jumps=(<question id>)` (see [Adjustments by the server](#adjustments-by-the-server)). Jumps with an invalid structure, `goTo` or `next` on a question type without it, and ids of questions of other forms are rejected with **Status-Code Bad Request** "Invalid extraSettings, will not update." Jumps for options that are not options of the question are removed before saving, with the same header.
478478

479479
```
480480
"data": 1
@@ -495,7 +495,7 @@ Reorders all Questions of a single form
495495
|-----------|---------|-------------|
496496
| _newOrder_ | Array | Array of **all** Question-IDs, ordered in the desired order |
497497
- Restrictions: The Array **must** contain all Question-IDs corresponding to the specified form and **must not** contain any duplicates. An order that puts more questions before the first section than before is rejected with **400 Bad Request** "Every question must be in a section" (see [Sections](DataStructure.md#sections)).
498-
- Response: Array of questionIDs and their corresponding order. Jumps that would go to the question itself or to an earlier question are removed (see [Jumps](DataStructure.md#jumps)): the questions concerned also carry their new `extraSettings`, and the response header `X-Forms-Adjusted` then lists their ids, e.g. `X-Forms-Adjusted: removed-jumps=(3 7)`, or holds `removed-jumps` alone for more than 100 questions (see [Adjustments by the server](#adjustments-by-the-server)).
498+
- Response: Array of questionIDs and their corresponding order. Jumps that would go to the question itself, to an earlier question or into the middle of another section are removed (see [Jumps](DataStructure.md#jumps)): the questions concerned also carry their new `extraSettings`, and the response header `X-Forms-Adjusted` then lists their ids, e.g. `X-Forms-Adjusted: removed-jumps=(3 7)`, or holds `removed-jumps` alone for more than 100 questions (see [Adjustments by the server](#adjustments-by-the-server)).
499499
An id of a deleted question gets a **Not Found** response.
500500

501501
```
@@ -542,7 +542,7 @@ Creates a clone of a question with all its options.
542542
| Parameter | Type | Optional | Description |
543543
|-----------|---------|----------|-------------|
544544
| _position_ | Integer | yes | _(optional)_ 1-based position to insert the cloned question. When omitted the clone is appended at the end. |
545-
- Response: Returns cloned question object with the new ID set. Jumps that would go to the clone itself or to an earlier question, for example when it is appended at the end, are removed from its `extraSettings` (see [Jumps](DataStructure.md#jumps)), and the response header `X-Forms-Adjusted` then holds `removed-jumps=(<clone id>)` (see [Adjustments by the server](#adjustments-by-the-server)).
545+
- Response: Returns cloned question object with the new ID set. Jumps that would go to the clone itself, to an earlier question or into the middle of another section, for example when it is appended at the end, are removed from its `extraSettings` (see [Jumps](DataStructure.md#jumps)), and the response header `X-Forms-Adjusted` then holds `removed-jumps=(<clone id>)` (see [Adjustments by the server](#adjustments-by-the-server)).
546546

547547
```
548548
See section 'Create a new question'.

‎docs/DataStructure.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ An answer can lead to a later question or to the end of the form, as in Microsof
293293
- A question has either jumps per answer or one jump after the question, depending on its type: `goTo` only on `dropdown`, `linearscale` and `multiple_unique`, `next` only on the other types listed above. Sending `next` on a question type with `goTo`, or `goTo` on a question type with `next`, is rejected with 400 "Invalid extraSettings". An answer without a `goTo` entry leads to the following question.
294294
- A target is the id of a later question of the same form or `"submit"` for the end of the form, and `optionId` is the id of an option of the question. Ids of questions of other forms are rejected as invalid `extraSettings`, while ids of deleted questions of the form are accepted. Updating a question removes its jumps for option ids that are not options of the question; the response header `X-Forms-Adjusted` then holds `removed-jumps` with the id of the question.
295295
- Jumps never go back: the server removes jumps to the question itself or to an earlier question when a question is updated, reordered or copied, and when a form is copied or imported; copying or importing a form also drops `goTo` or `next` on question types without it. Updating, reordering and copying a question still succeed with their usual status, and the payload keeps its shape: a reorder adds the new `extraSettings` of the questions concerned, and a copied question is returned with its reduced `extraSettings`. The response header `X-Forms-Adjusted` lists the questions that lost jumps, e.g. `removed-jumps=(3 7)`, see [Adjustments by the server](API_v3.md#adjustments-by-the-server).
296+
- In a form with sections, a jump also never goes into the middle of another section: it goes to a later question of its own section, to a later section or to the end of the form. The server removes other jumps in the same cases as backward jumps, and inserting or cloning a section at a position removes the jumps of other questions that would go into its middle, with the header `X-Forms-Adjusted` listing them.
296297
- Deleting an option removes the jumps of the question for that option; the response header `X-Forms-Adjusted` then holds `removed-jumps` with the id of the question.
297298
- Saving a question rejects jumps with an invalid structure with 400, as any invalid `extraSettings`. Copying or importing a form keeps the jumps whose target and option it can map, and drops the others and jumps per answer with an invalid structure. The path walks ignore malformed entries.
298299
- Only the deprecated `datetime` accepts neither `goTo` nor `next` (400).

‎lib/Controller/ApiController.php‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ public function getQuestion(int $formId, int $questionId): DataResponse {
540540
/**
541541
* Add a new question
542542
*
543-
* A clone loses its jumps to itself or to an earlier question, and the header `X-Forms-Adjusted` then holds `removed-jumps` with its id
543+
* A clone loses its jumps to itself, to an earlier question or into the middle of another section, and the header `X-Forms-Adjusted` then holds `removed-jumps` with its id.
544+
* A section inserted or cloned at a position removes the jumps of other questions that would go into its middle, and the header then holds `removed-jumps` with their ids.
544545
*
545546
* @param int $formId the form id
546547
* @param FormsQuestionType $type the new question type
@@ -694,7 +695,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
694695
$newQuestion->setExtraSettings(JumpHelper::mapJumps($extraSettings, null, $optionIdMap));
695696
$formQuestions = [...$allQuestions, $newQuestion];
696697
usort($formQuestions, static fn (Question $a, Question $b): int => $a->getOrder() <=> $b->getOrder());
697-
if (in_array($newQuestion, JumpHelper::removeBackwardJumps($formQuestions), true)) {
698+
if (in_array($newQuestion, JumpHelper::removeDisallowedJumps($formQuestions), true)) {
698699
$headers = self::getRemovedJumpsHeaders([$newQuestion->getId()]);
699700
}
700701
if ($newQuestion->getExtraSettings() !== $extraSettings) {
@@ -704,6 +705,16 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
704705
}
705706
}
706707

708+
if (($response['type'] ?? null) === Constants::ANSWER_TYPE_SECTION && $position !== null) {
709+
$changedQuestions = JumpHelper::removeDisallowedJumps($this->questionMapper->findByForm($formId));
710+
foreach ($changedQuestions as $changedQuestion) {
711+
$this->questionMapper->update($changedQuestion);
712+
}
713+
if ($changedQuestions !== []) {
714+
$headers = self::getRemovedJumpsHeaders(array_map(static fn (Question $changedQuestion): int => $changedQuestion->getId(), $changedQuestions));
715+
}
716+
}
717+
707718
$this->formMapper->update($form);
708719

709720
if (empty($response['extraSettings'])) {
@@ -717,7 +728,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
717728
* Writes the given key-value pairs into Database
718729
* Key `order` should only be changed by reorderQuestions() and is not allowed here
719730
*
720-
* Jumps to the question itself or to an earlier question, and jumps of a linear scale for values outside its bounds, are removed before saving, and the header `X-Forms-Adjusted` then holds `removed-jumps` with the id of the question.
731+
* Jumps to the question itself, to an earlier question or into the middle of another section, and jumps of a linear scale for values outside its bounds, are removed before saving, and the header `X-Forms-Adjusted` then holds `removed-jumps` with the id of the question.
721732
* Jumps for options that are not options of the question are removed as well, with the same header. Jumps to questions of other forms are rejected as invalid `extraSettings`.
722733
*
723734
* @param int $formId the form id
@@ -811,7 +822,7 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
811822
if (JumpHelper::removeJumpsOutsideScale($updatedQuestion)) {
812823
$removedJumps = true;
813824
}
814-
if (in_array($updatedQuestion, JumpHelper::removeBackwardJumps($formQuestions), true)) {
825+
if (in_array($updatedQuestion, JumpHelper::removeDisallowedJumps($formQuestions), true)) {
815826
$removedJumps = true;
816827
}
817828
$keyValuePairs['extraSettings'] = $updatedQuestion->getExtraSettings();
@@ -917,7 +928,7 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
917928
/**
918929
* Updates the Order of all Questions of a Form
919930
*
920-
* Jumps that would go to the question itself or to an earlier question are removed: the new extra settings of these questions are returned, and the header `X-Forms-Adjusted` holds `removed-jumps` with their ids, or alone for more than 100 questions
931+
* Jumps that would go to the question itself, to an earlier question or into the middle of another section are removed: the new extra settings of these questions are returned, and the header `X-Forms-Adjusted` holds `removed-jumps` with their ids, or alone for more than 100 questions
921932
*
922933
* @param int $formId Id of the form to reorder
923934
* @param list<int> $newOrder Array of Question-Ids in new order.
@@ -997,7 +1008,7 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
9971008

9981009
self::checkQuestionsStayInSections($typesBefore, array_map(static fn (Question $question) => $question->getType(), $questions));
9991010

1000-
$changedQuestions = JumpHelper::removeBackwardJumps($questions);
1011+
$changedQuestions = JumpHelper::removeDisallowedJumps($questions);
10011012

10021013
// Write to Database
10031014
foreach ($questions as $question) {
@@ -2374,7 +2385,7 @@ private function moveNextSectionUp(array $questions, int $order): array {
23742385
$moved[] = $question;
23752386
}
23762387
}
2377-
$changed = JumpHelper::removeBackwardJumps($questions);
2388+
$changed = JumpHelper::removeDisallowedJumps($questions);
23782389
foreach ($questions as $question) {
23792390
if (in_array($question, $moved, true) || in_array($question, $changed, true)) {
23802391
$this->questionMapper->update($question);

0 commit comments

Comments
 (0)