Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/API_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Some requests succeed with their usual status, and their payload keeps its shape

| Key | Value | Set by | Meaning |
|-----|-------|--------|---------|
| `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) |
| `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) |

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

### Other API changes

- 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.
- 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.
- In API version 3.0 the following endpoints were introduced/changed:
- `GET /api/v3/forms/{formId}/questions` to get all questions of a form
- `GET /api/v3/forms/{formId}/questions/{questionId}` to get a single question
Expand Down Expand Up @@ -276,7 +276,7 @@ Creates a clone of a form (without submissions).
| Parameter | Type | Description |
|-----------|---------|-------------|
| _formId_ | Integer | ID of the form to clone |
- 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)).
- 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)).

```
See section 'Request full data of a form'.
Expand Down Expand Up @@ -396,7 +396,7 @@ Returns the questions and options of the given form (without submissions).
| _text_ | String | yes | _Optional_ The text of the new question. |
| _position_ | Integer | yes | _(optional)_ 1-based position to insert the new question. When omitted the question is appended at the end. |
- 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**.
- Response: The new question object.
- 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)).

```
"data": {
Expand Down Expand Up @@ -474,7 +474,7 @@ Update a single or multiple properties of a question-object.
- It is **not allowed** to update one of the following key-value pairs: _id, formId, order, type_.
- 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.
- 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**.
- 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.
- 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.

```
"data": 1
Expand All @@ -495,7 +495,7 @@ Reorders all Questions of a single form
|-----------|---------|-------------|
| _newOrder_ | Array | Array of **all** Question-IDs, ordered in the desired order |
- Restrictions: The Array **must** contain all Question-IDs corresponding to the specified form and **must not** contain any duplicates.
- 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)).
- 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)).
An id of a deleted question gets a **Not Found** response.

```
Expand Down Expand Up @@ -542,7 +542,7 @@ Creates a clone of a question with all its options.
| Parameter | Type | Optional | Description |
|-----------|---------|----------|-------------|
| _position_ | Integer | yes | _(optional)_ 1-based position to insert the cloned question. When omitted the clone is appended at the end. |
- 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)).
- 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)).

```
See section 'Create a new question'.
Expand Down
1 change: 1 addition & 0 deletions docs/DataStructure.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ An answer can lead to a later question or to the end of the form, as in Microsof
- 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.
- 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.
- 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).
- 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.
- 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.
- 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.
- Only the deprecated `datetime` accepts neither `goTo` nor `next` (400).
Expand Down
23 changes: 17 additions & 6 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ public function getQuestion(int $formId, int $questionId): DataResponse {
/**
* Add a new question
*
* 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
* 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.
* 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.
*
* @param int $formId the form id
* @param FormsQuestionType $type the new question type
Expand Down Expand Up @@ -690,7 +691,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
$newQuestion->setExtraSettings(FormsService::mapJumps($extraSettings, null, $optionIdMap));
$formQuestions = [...$allQuestions, $newQuestion];
usort($formQuestions, static fn (Question $a, Question $b): int => $a->getOrder() <=> $b->getOrder());
if (in_array($newQuestion, FormsService::removeBackwardJumps($formQuestions), true)) {
if (in_array($newQuestion, FormsService::removeDisallowedJumps($formQuestions), true)) {
$headers = self::getRemovedJumpsHeaders([$newQuestion->getId()]);
}
if ($newQuestion->getExtraSettings() !== $extraSettings) {
Expand All @@ -700,6 +701,16 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
}
}

if (($response['type'] ?? null) === Constants::ANSWER_TYPE_SECTION && $position !== null) {
$changedQuestions = FormsService::removeDisallowedJumps($this->questionMapper->findByForm($formId));
foreach ($changedQuestions as $changedQuestion) {
$this->questionMapper->update($changedQuestion);
}
if ($changedQuestions !== []) {
$headers = self::getRemovedJumpsHeaders(array_map(static fn (Question $changedQuestion): int => $changedQuestion->getId(), $changedQuestions));
}
}

$this->formMapper->update($form);

if (empty($response['extraSettings'])) {
Expand All @@ -713,7 +724,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
* Writes the given key-value pairs into Database
* Key `order` should only be changed by reorderQuestions() and is not allowed here
*
* 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.
* 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.
* 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`.
*
* @param int $formId the form id
Expand Down Expand Up @@ -807,7 +818,7 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
if (FormsService::removeJumpsOutsideScale($updatedQuestion)) {
$removedJumps = true;
}
if (in_array($updatedQuestion, FormsService::removeBackwardJumps($formQuestions), true)) {
if (in_array($updatedQuestion, FormsService::removeDisallowedJumps($formQuestions), true)) {
$removedJumps = true;
}
$keyValuePairs['extraSettings'] = $updatedQuestion->getExtraSettings();
Expand Down Expand Up @@ -908,7 +919,7 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
/**
* Updates the Order of all Questions of a Form
*
* 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
* 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
*
* @param int $formId Id of the form to reorder
* @param list<int> $newOrder Array of Question-Ids in new order.
Expand Down Expand Up @@ -984,7 +995,7 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
}
}

$changedQuestions = FormsService::removeBackwardJumps($questions);
$changedQuestions = FormsService::removeDisallowedJumps($questions);

// Write to Database
foreach ($questions as $question) {
Expand Down
Loading
Loading