Skip to content

Commit c4ec966

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}. A value is matched as a whole number, so "04" counts as 4 as it does for the answer itself. Values outside the scale never match, so changing the bounds needs no cleanup. 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 643e52e commit c4ec966

13 files changed

Lines changed: 187 additions & 14 deletions

File tree

‎docs/DataStructure.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,14 @@ 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 |
274-
| `next` | `color, date, dropdown, file, grid, long, multiple, multiple_unique, ranking, short, time` | Integer / string | question id, `'submit'` | Where to go after the question when its answer has no `goTo` entry, see [Jumps](#jumps) |
273+
| `goTo` | `dropdown, linearscale, multiple_unique` | Array | see [Jumps](#jumps) | Where to go next for an answer |
274+
| `next` | `color, date, dropdown, file, grid, linearscale, long, multiple, multiple_unique, ranking, short, time` | Integer / string | question id, `'submit'` | Where to go after the question when its answer has no `goTo` entry, 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 and Google 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.
281281
- `next` is the target after the question when its answer has no jump, or when the question has no answers to choose from. A checkbox question has only `next`: one target for the whole question, whichever options are checked.
282282
- A target is the id of a later question of the same form or `"submit"` for the end of the form. Without a jump the respondent goes to the following question.
283283
- 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.

‎lib/Constants.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class Constants {
237237

238238
public const QUESTION_TYPES_WITH_GOTO = [
239239
self::ANSWER_TYPE_DROPDOWN,
240+
self::ANSWER_TYPE_LINEARSCALE,
240241
self::ANSWER_TYPE_MULTIPLEUNIQUE,
241242
];
242243

@@ -246,6 +247,7 @@ class Constants {
246247
self::ANSWER_TYPE_DROPDOWN,
247248
self::ANSWER_TYPE_FILE,
248249
self::ANSWER_TYPE_GRID,
250+
self::ANSWER_TYPE_LINEARSCALE,
249251
self::ANSWER_TYPE_LONG,
250252
self::ANSWER_TYPE_MULTIPLE,
251253
self::ANSWER_TYPE_MULTIPLEUNIQUE,

‎lib/ResponseDefinitions.php‎

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

‎lib/Service/FormsService.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ private static function isJumpTarget(mixed $target): bool {
10781078
}
10791079

10801080
/**
1081-
* Whether the value is a list of jumps per answer, each with one answer key and a target, at most one per answer
1081+
* Whether the value is a list of jumps per answer, each with one answer key (`value` on linear scales, else `optionId` or `other`) and a target, at most one per answer
10821082
*
10831083
* @param mixed $goTo the value of `goTo`
10841084
* @param string $questionType the question type
@@ -1094,7 +1094,12 @@ private static function isGoToValid(mixed $goTo, string $questionType): bool {
10941094
return false;
10951095
}
10961096

1097-
if (is_int($jump['optionId'] ?? null) && $jump['optionId'] > 0) {
1097+
if ($questionType === Constants::ANSWER_TYPE_LINEARSCALE) {
1098+
if (!is_int($jump['value'] ?? null)) {
1099+
return false;
1100+
}
1101+
$answer = 'value:' . $jump['value'];
1102+
} elseif (is_int($jump['optionId'] ?? null) && $jump['optionId'] > 0) {
10981103
$answer = 'option:' . $jump['optionId'];
10991104
} elseif (($jump['other'] ?? null) === true) {
11001105
$answer = 'other';

‎lib/Service/SubmissionService.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,8 @@ private function getJumpTarget(array $question, mixed $answer): string|int|null
815815
continue;
816816
}
817817
if ((isset($jump['optionId']) && (string)$jump['optionId'] === (string)$value)
818-
|| (($jump['other'] ?? false) === true && $this->isOtherAnswer($question, $value))) {
818+
|| (($jump['other'] ?? false) === true && $this->isOtherAnswer($question, $value))
819+
|| (isset($jump['value']) && ctype_digit((string)$value) && (int)$value === $jump['value'])) {
819820
return $jump['target'] ?? null;
820821
}
821822
}

‎openapi.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,10 @@
556556
"other": {
557557
"type": "boolean"
558558
},
559+
"value": {
560+
"type": "integer",
561+
"format": "int64"
562+
},
559563
"target": {
560564
"oneOf": [
561565
{

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,28 @@ test('jump issues: with "Other" allowed, the answer "Other" needs a jump as well
172172
]).unreachable,
173173
).toEqual([2])
174174
})
175+
176+
test('jump issues: a required scale whose values all jump past a question makes it unreachable', () => {
177+
const scale = (goTo: Array<{ value: number; target: number }>) =>
178+
({
179+
id: 1,
180+
text: 'Question 1',
181+
type: 'linearscale',
182+
isRequired: true,
183+
extraSettings: { optionsLowest: 1, optionsHighest: 2, goTo },
184+
}) as FormsQuestion
185+
expect(
186+
issues([scale([{ value: 1, target: 3 }]), short(2), short(3)]).unreachable,
187+
).toEqual([])
188+
expect(
189+
issues([
190+
scale([
191+
{ value: 1, target: 3 },
192+
{ value: 2, target: 3 },
193+
{ value: 3, target: 2 },
194+
]),
195+
short(2),
196+
short(3),
197+
]).unreachable,
198+
).toEqual([2])
199+
})

‎src/components/Questions/QuestionLinearScale.vue‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@
128128
{{ optionsLabelHighest }}
129129
</div>
130130
</div>
131+
<ul v-if="showTargets" class="question-linear-scale__targets">
132+
<li
133+
v-for="option in scaleOptions"
134+
:key="option"
135+
class="question-linear-scale__target">
136+
<span>{{ getValueText(option) }}</span>
137+
<JumpTargetSelect
138+
:modelValue="jumps.getTarget({ value: option })"
139+
:options="jumps.targetOptions"
140+
:ariaLabel="jumps.getTargetLabel(getValueText(option))"
141+
@update:modelValue="
142+
jumps.setTarget({ value: option }, $event)
143+
" />
144+
</li>
145+
</ul>
131146
<template #insert>
132147
<slot name="insert" />
133148
</template>
@@ -142,6 +157,7 @@ import NcActionInput from '@nextcloud/vue/components/NcActionInput'
142157
import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch'
143158
import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper'
144159
import NcTextArea from '@nextcloud/vue/components/NcTextArea'
160+
import JumpTargetSelect from './JumpTargetSelect.vue'
145161
import Question from './Question.vue'
146162
import {
147163
QUESTION_EMITS,
@@ -164,6 +180,7 @@ export default defineComponent({
164180
NcActionInput,
165181
NcCheckboxRadioSwitch,
166182
NcTextArea,
183+
JumpTargetSelect,
167184
Question,
168185
},
169186
@@ -241,6 +258,23 @@ export default defineComponent({
241258
242259
const questionValues = computed(() => values.value)
243260
261+
const showTargets = computed(() => !props.readOnly && question.jumps.enabled)
262+
263+
/**
264+
* A scale value with the label of its end of the scale, e.g. "1 - Strongly disagree"
265+
*
266+
* @param option the value
267+
*/
268+
const getValueText = (option: number): string => {
269+
if (option === optionsLowest.value && optionsLabelLowest.value) {
270+
return `${option} - ${optionsLabelLowest.value}`
271+
}
272+
if (option === optionsHighest.value && optionsLabelHighest.value) {
273+
return `${option} - ${optionsLabelHighest.value}`
274+
}
275+
return String(option)
276+
}
277+
244278
/**
245279
* ID for the label for the lowest option
246280
*/
@@ -336,6 +370,8 @@ export default defineComponent({
336370
lowest,
337371
highest,
338372
scaleOptions,
373+
showTargets,
374+
getValueText,
339375
isUnique,
340376
questionValues,
341377
labelId,
@@ -357,6 +393,23 @@ export default defineComponent({
357393
</script>
358394

359395
<style lang="scss" scoped>
396+
.question-linear-scale__targets {
397+
display: flex;
398+
flex-direction: column;
399+
gap: var(--default-grid-baseline);
400+
margin-block-start: calc(2 * var(--default-grid-baseline));
401+
padding-inline-end: calc(var(--default-clickable-area) + 12px);
402+
}
403+
404+
.question-linear-scale__target {
405+
display: flex;
406+
flex-wrap: wrap;
407+
align-items: center;
408+
justify-content: space-between;
409+
gap: var(--default-grid-baseline);
410+
container-type: inline-size;
411+
}
412+
360413
.question__content {
361414
display: flex;
362415

‎src/composables/useJumps.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@ export const FORM_JUMP_ISSUES_KEY: InjectionKey<ComputedRef<JumpIssues>> =
2323
export const FORM_QUESTIONS_KEY: InjectionKey<ComputedRef<FormsQuestion[]>> =
2424
Symbol('formQuestions')
2525

26-
export const QUESTION_TYPES_WITH_GOTO = ['dropdown', 'multiple_unique']
26+
export const QUESTION_TYPES_WITH_GOTO = [
27+
'dropdown',
28+
'linearscale',
29+
'multiple_unique',
30+
]
2731

2832
export const QUESTION_TYPES_WITH_NEXT = [
2933
'color',
3034
'date',
3135
'dropdown',
3236
'file',
3337
'grid',
38+
'linearscale',
3439
'long',
3540
'multiple',
3641
'multiple_unique',

‎src/types/Entities.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ export interface FormsQuestionExtraSettings {
6969
/** A later question by id, or the end of the form */
7070
export type FormsJumpTarget = number | 'submit'
7171

72-
/** Where an answer leads: an option or the answer "Other" */
72+
/** Where an answer leads: an option, the answer "Other", or a value of a linear scale */
7373
export interface FormsJump {
7474
optionId?: number
7575
other?: true
76+
value?: number
7677
target: FormsJumpTarget
7778
}
7879

0 commit comments

Comments
 (0)