Skip to content

Commit ce8a60a

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 4875f13 commit ce8a60a

12 files changed

Lines changed: 544 additions & 87 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 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 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. 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. 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**.
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": {
@@ -473,7 +473,7 @@ Update a single or multiple properties of a question-object.
473473
- Restrictions:
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.
476-
- 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.
476+
- 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.
477477

478478
```
479479
"data": 1
@@ -494,7 +494,7 @@ Reorders all Questions of a single form
494494
|-----------|---------|-------------|
495495
| _newOrder_ | Array | Array of **all** Question-IDs, ordered in the desired order |
496496
- Restrictions: The Array **must** contain all Question-IDs corresponding to the specified form and **must not** contain any duplicates.
497-
- 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)).
497+
- 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)).
498498

499499
```
500500
"data": {
@@ -540,7 +540,7 @@ Creates a clone of a question with all its options.
540540
| Parameter | Type | Optional | Description |
541541
|-----------|---------|----------|-------------|
542542
| _position_ | Integer | yes | _(optional)_ 1-based position to insert the cloned question. When omitted the clone is appended at the end. |
543-
- 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)).
543+
- 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)).
544544

545545
```
546546
See section 'Create a new question'.

‎docs/DataStructure.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ An answer can lead to a later question or to the end of the form, as in Microsof
283283
- 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.
284284
- 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.
285285
- 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).
286+
- 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.
286287
- 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.
287288
- 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.
288289
- Only the deprecated `datetime` accepts neither `goTo` nor `next` (400).

‎lib/Controller/ApiController.php‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ public function getQuestion(int $formId, int $questionId): DataResponse {
539539
/**
540540
* Add a new question
541541
*
542-
* 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
542+
* 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.
543+
* 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.
543544
*
544545
* @param int $formId the form id
545546
* @param FormsQuestionType $type the new question type
@@ -690,7 +691,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
690691
$newQuestion->setExtraSettings(FormsService::mapJumps($extraSettings, null, $optionIdMap));
691692
$formQuestions = [...$allQuestions, $newQuestion];
692693
usort($formQuestions, static fn (Question $a, Question $b): int => $a->getOrder() <=> $b->getOrder());
693-
if (in_array($newQuestion, FormsService::removeBackwardJumps($formQuestions), true)) {
694+
if (in_array($newQuestion, FormsService::removeDisallowedJumps($formQuestions), true)) {
694695
$headers = self::getRemovedJumpsHeaders([$newQuestion->getId()]);
695696
}
696697
if ($newQuestion->getExtraSettings() !== $extraSettings) {
@@ -700,6 +701,16 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
700701
}
701702
}
702703

704+
if (($response['type'] ?? null) === Constants::ANSWER_TYPE_SECTION && $position !== null) {
705+
$changedQuestions = FormsService::removeDisallowedJumps($this->questionMapper->findByForm($formId));
706+
foreach ($changedQuestions as $changedQuestion) {
707+
$this->questionMapper->update($changedQuestion);
708+
}
709+
if ($changedQuestions !== []) {
710+
$headers = self::getRemovedJumpsHeaders(array_map(static fn (Question $changedQuestion): int => $changedQuestion->getId(), $changedQuestions));
711+
}
712+
}
713+
703714
$this->formMapper->update($form);
704715

705716
if (empty($response['extraSettings'])) {
@@ -713,7 +724,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
713724
* Writes the given key-value pairs into Database
714725
* Key `order` should only be changed by reorderQuestions() and is not allowed here
715726
*
716-
* 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.
727+
* 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.
717728
* 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`.
718729
*
719730
* @param int $formId the form id
@@ -811,7 +822,7 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
811822
if (FormsService::removeJumpsOutsideScale($updatedQuestion)) {
812823
$removedJumps = true;
813824
}
814-
if (in_array($updatedQuestion, FormsService::removeBackwardJumps($formQuestions), true)) {
825+
if (in_array($updatedQuestion, FormsService::removeDisallowedJumps($formQuestions), true)) {
815826
$removedJumps = true;
816827
}
817828
$keyValuePairs['extraSettings'] = $updatedQuestion->getExtraSettings();
@@ -916,7 +927,7 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
916927
/**
917928
* Updates the Order of all Questions of a Form
918929
*
919-
* 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
930+
* 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
920931
*
921932
* @param int $formId Id of the form to reorder
922933
* @param list<int> $newOrder Array of Question-Ids in new order.
@@ -1001,7 +1012,7 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
10011012
}
10021013
}
10031014

1004-
$changedQuestions = FormsService::removeBackwardJumps($questions);
1015+
$changedQuestions = FormsService::removeDisallowedJumps($questions);
10051016

10061017
// Write to Database
10071018
foreach ($questions as $question) {

0 commit comments

Comments
 (0)