Skip to content

Commit bf45566

Browse files
feat(conditional): go to a question based on the value of a linear scale
Each value of a linear scale gets a row with the same select as on options, the values at the ends with their labels, e.g. "1 - Strongly disagree". The jumps are stored in extraSettings.goTo as {value, target}; as on radio and dropdown questions, there is no extraSettings.next, and a value without a jump leads to the following question. A value is an integer from 0 to 10, the range of all scales, and is matched as a whole number, so "04" counts as 4 as it does for the answer itself. Narrowing the scale removes the jumps of the values that are gone, reported in the response header X-Forms-Adjusted. Based on the linear scale support of #3123 by TimedIn. Co-Authored-By: TimedIn <git@timedin.net> Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent ddc17ec commit bf45566

16 files changed

Lines changed: 399 additions & 20 deletions

File tree

‎docs/API_v3.md‎

Lines changed: 3 additions & 3 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 | Jumps to the question itself or to an earlier question, or jumps of the deleted option or of options that are not options of the question, 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; 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) |
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 and radio 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, and deleting an option removes its jumps. 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 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.
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
@@ -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 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 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.
477477

478478
```
479479
"data": 1

‎docs/DataStructure.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,21 +270,21 @@ Optional extra settings for some [Question Types](#question-types)
270270
| `columns` | `grid` | Array | - | Array of column identifiers / labels for grid questions |
271271
| `rows` | `grid` | Array | - | Array of row identifiers / labels for grid questions |
272272
| `questionType` | `grid` | String | `checkbox`, `number`, `radio` | Type of cell for grid questions (checkbox, numeric input, or radio) |
273-
| `goTo` | `dropdown, multiple_unique` | Array | see [Jumps](#jumps) | Where to go next for an answer |
273+
| `goTo` | `dropdown, linearscale, multiple_unique` | Array | see [Jumps](#jumps) | Where to go next for an answer |
274274
| `next` | `color, date, file, grid, long, multiple, ranking, short, time` | Integer / string | question id, `'submit'` | Where to go after the question, see [Jumps](#jumps) |
275275

276276
### Jumps
277277

278278
An answer can lead to a later question or to the end of the form, as in Microsoft Forms. The form stays a flat list of questions.
279279

280-
- `goTo` is a list of jumps per answer: `{"optionId": 12, "target": 34}` for an option, `{"other": true, "target": "submit"}` for the answer "Other". There is at most one jump per answer.
280+
- `goTo` is a list of jumps per answer: `{"optionId": 12, "target": 34}` for an option, `{"other": true, "target": "submit"}` for the answer "Other", `{"value": 4, "target": 34}` for a value of a linear scale. There is at most one jump per answer. A `value` is an integer from 0 to 10, the range of all scales. When a question is updated, or its form copied or imported, the server removes the jumps for values outside the scale of the question; on update the response header `X-Forms-Adjusted` then holds `removed-jumps` with the id of the question.
281281
- `next` is one target after the whole question; on a checkbox question it applies whichever options are checked.
282-
- A question has either jumps per answer or one jump after the question, depending on its type: `goTo` only on `dropdown` 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.
282+
- 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.
283283
- 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.
284284
- 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).
285285
- 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.
286286
- 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.
287-
- Question types in neither list, such as `datetime`, accept neither `goTo` nor `next` (400).
287+
- Only the deprecated `datetime` accepts neither `goTo` nor `next` (400).
288288
- A jump of the answer "Other" is for `multiple_unique` questions, as dropdowns have no answer "Other", and only matches while `allowOtherAnswer` is on.
289289
- `next` also applies when the question is left unanswered.
290290

‎lib/Constants.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ class Constants {
255255
'optionsHighest' => ['integer', 'NULL'],
256256
'optionsLabelLowest' => ['string', 'NULL'],
257257
'optionsLabelHighest' => ['string', 'NULL'],
258+
'goTo' => ['array'],
258259
];
259260

260261
public const EXTRA_SETTINGS_GRID = [

‎lib/Controller/ApiController.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
713713
* Writes the given key-value pairs into Database
714714
* Key `order` should only be changed by reorderQuestions() and is not allowed here
715715
*
716-
* Jumps to the question itself or to an earlier question are removed before saving, and the header `X-Forms-Adjusted` then holds `removed-jumps` with the id of the question.
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.
717717
* 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`.
718718
*
719719
* @param int $formId the form id
@@ -808,6 +808,9 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
808808
$updatedQuestion->setExtraSettings($keyValuePairs['extraSettings']);
809809
$removedJumps = $this->formsService->removeJumpsOfMissingOptions($updatedQuestion);
810810
$formQuestions = array_values(array_map(static fn (Question $formQuestion): Question => $formQuestion->getId() === $questionId ? $updatedQuestion : $formQuestion, array_filter($allQuestions, static fn (Question $formQuestion): bool => $formQuestion->getOrder() !== 0)));
811+
if (FormsService::removeJumpsOutsideScale($updatedQuestion)) {
812+
$removedJumps = true;
813+
}
811814
if (in_array($updatedQuestion, FormsService::removeBackwardJumps($formQuestions), true)) {
812815
$removedJumps = true;
813816
}

‎lib/ResponseDefinitions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* dateMax?: string|int,
2828
* dateMin?: string|int,
2929
* dateRange?: bool,
30-
* goTo?: list<array{optionId?: int, other?: bool, target: int|string}>,
30+
* goTo?: list<array{optionId?: int, other?: bool, value?: int<0, 10>, target: int|string}>,
3131
* maxAllowedFilesCount?: int,
3232
* maxFileSize?: int,
3333
* next?: int|string,

‎lib/Service/FormsService.php‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ private static function isGoToValid(mixed $goTo, string $questionType): bool {
11131113
}
11141114

11151115
/**
1116-
* The answer of a jump per answer with one answer key and a target: `option:<id>` for an option, `other` for the answer "Other" of a radio question
1116+
* The answer of a jump per answer with one answer key and a target: `value:<value>` for a value from 0 to 10 of a linear scale, `option:<id>` for an option, `other` for the answer "Other" of a radio question
11171117
*
11181118
* @param mixed $jump the jump
11191119
* @param string $questionType the question type
@@ -1123,6 +1123,9 @@ private static function getJumpAnswer(mixed $jump, string $questionType): ?strin
11231123
if (!is_array($jump) || count($jump) !== 2 || !self::isJumpTarget($jump['target'] ?? null)) {
11241124
return null;
11251125
}
1126+
if ($questionType === Constants::ANSWER_TYPE_LINEARSCALE) {
1127+
return is_int($jump['value'] ?? null) && $jump['value'] >= 0 && $jump['value'] <= 10 ? 'value:' . $jump['value'] : null;
1128+
}
11261129
if (is_int($jump['optionId'] ?? null) && $jump['optionId'] > 0) {
11271130
return 'option:' . $jump['optionId'];
11281131
}
@@ -1254,6 +1257,7 @@ public static function mapJumpsOfCopies(array $questions, array $questionIdMap,
12541257
$extraSettings['goTo'] = array_values($goTo);
12551258
}
12561259
$question->setExtraSettings($extraSettings);
1260+
self::removeJumpsOutsideScale($question);
12571261
$copiesWithJumps[] = $question;
12581262
}
12591263
}
@@ -1300,6 +1304,30 @@ public static function removeBackwardJumps(array $questions): array {
13001304
return $changed;
13011305
}
13021306

1307+
/**
1308+
* Remove the jumps of a linear scale for values outside its bounds
1309+
*
1310+
* @param Question $question the question
1311+
* @return bool whether jumps were removed, with the new extra settings set
1312+
*/
1313+
public static function removeJumpsOutsideScale(Question $question): bool {
1314+
$extraSettings = $question->getExtraSettings();
1315+
if ($question->getType() !== Constants::ANSWER_TYPE_LINEARSCALE || !is_array($extraSettings['goTo'] ?? null)) {
1316+
return false;
1317+
}
1318+
1319+
$lowest = $extraSettings['optionsLowest'] ?? 1;
1320+
$highest = $extraSettings['optionsHighest'] ?? 5;
1321+
$goTo = array_values(array_filter($extraSettings['goTo'], static fn (mixed $jump): bool => !is_int(is_array($jump) ? ($jump['value'] ?? null) : null) || ($jump['value'] >= $lowest && $jump['value'] <= $highest)));
1322+
if (count($goTo) === count($extraSettings['goTo'])) {
1323+
return false;
1324+
}
1325+
1326+
$extraSettings['goTo'] = $goTo;
1327+
$question->setExtraSettings($extraSettings);
1328+
return true;
1329+
}
1330+
13031331
/**
13041332
* Get list of forms
13051333
*

‎lib/Service/SubmissionService.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,8 @@ private static function getJumpTarget(array $question, mixed $answer): string|in
930930
if (is_int($value) || is_float($value) || is_string($value)) {
931931
foreach (is_array($extraSettings['goTo'] ?? null) ? $extraSettings['goTo'] : [] as $jump) {
932932
if ((isset($jump['optionId']) && (string)$jump['optionId'] === (string)$value)
933-
|| (($jump['other'] ?? false) === true && self::isOtherAnswer($question, $value))) {
933+
|| (($jump['other'] ?? false) === true && self::isOtherAnswer($question, $value))
934+
|| (isset($jump['value']) && ctype_digit((string)$value) && (int)$value === $jump['value'])) {
934935
$target = $jump['target'] ?? null;
935936
return is_int($target) || is_string($target) ? $target : null;
936937
}

‎openapi.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,12 @@
545545
"other": {
546546
"type": "boolean"
547547
},
548+
"value": {
549+
"type": "integer",
550+
"format": "int64",
551+
"minimum": 0,
552+
"maximum": 10
553+
},
548554
"target": {
549555
"oneOf": [
550556
{
@@ -2381,7 +2387,7 @@
23812387
"patch": {
23822388
"operationId": "api-update-question",
23832389
"summary": "Writes the given key-value pairs into Database Key `order` should only be changed by reorderQuestions() and is not allowed here",
2384-
"description": "Jumps to the question itself or to an earlier question 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`.\nThis endpoint allows CORS requests",
2390+
"description": "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 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`.\nThis endpoint allows CORS requests",
23852391
"tags": [
23862392
"api"
23872393
],

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,28 @@ test('jump issues: a jump of the answer "Other" breaks while "Other" is off', ()
316316
),
317317
).toEqual([2])
318318
})
319+
320+
test('jump issues: a required scale whose values all jump past a question makes it unreachable', () => {
321+
const scale = (goTo: Array<{ value: number; target: number }>) =>
322+
({
323+
id: 1,
324+
text: 'Question 1',
325+
type: 'linearscale',
326+
isRequired: true,
327+
extraSettings: { optionsLowest: 1, optionsHighest: 2, goTo },
328+
}) as FormsQuestion
329+
expect(
330+
issues([scale([{ value: 1, target: 3 }]), short(2), short(3)]).unreachable,
331+
).toEqual([])
332+
expect(
333+
issues([
334+
scale([
335+
{ value: 1, target: 3 },
336+
{ value: 2, target: 3 },
337+
{ value: 3, target: 2 },
338+
]),
339+
short(2),
340+
short(3),
341+
]).unreachable,
342+
).toEqual([2])
343+
})

0 commit comments

Comments
 (0)