Skip to content
Closed
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
2 changes: 2 additions & 0 deletions docs/API_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ Returns the questions and options of the given form (without submissions).
|-----------|---------|----------|-------------|
| _type_ | [QuestionType](DataStructure.md#question-types) | | The question-type of the new question |
| _text_ | String | yes | _Optional_ The text of the new question. |
| _subtype_ | String | yes | _Optional_ The sub type, stored as `extraSettings.questionType`: `checkbox`, `number` or `radio` for `grid`, `rating` (a 1 to N scale drawn as icons) or `promoter` (a fixed 0 to 10 scale) for `linearscale`. Any other value is rejected. |
| _position_ | Integer | yes | _(optional)_ 1-based position to insert the new question. When omitted the question is appended at the end. |
- Response: The new question object.

Expand Down Expand Up @@ -455,6 +456,7 @@ Update a single or multiple properties of a question-object.
- Restrictions:
- 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.

```
Expand Down
10 changes: 6 additions & 4 deletions docs/DataStructure.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,15 @@ Optional extra settings for some [Question Types](#question-types)
| `timeMax` | `time` | string | - | Maximum allowed time to be chosen (as `HH:mm` string) |
| `timeMin` | `time` | string | - | Minimum allowed time to be chosen (as `HH:mm` string) |
| `timeRange` | `time` | Boolean | `true/false` | The time picker should query a time range |
| `optionsLowest` | `linearscale` | Integer | `0, 1` | Set the lowest value of the scale, default: `1` |
| `optionsHighest` | `linearscale` | Integer | `2, 3, 4, 5, 6, 7, 8, 9, 10` | Set the highest value of the scale, default: `5` |
| `optionsLabelLowest` | `linearscale` | string | - | Set the label of the lowest value, default: `'Strongly disagree'` |
| `optionsLabelHighest` | `linearscale` | string | - | Set the label of the highest value, default: `'Strongly agree'` |
| `optionsLowest` | `linearscale` | Integer | `0, 1` | Set the lowest value of the scale, default: `1`; always `1` for the `rating` sub type, always `0` for the `promoter` sub type |
| `optionsHighest` | `linearscale` | Integer | `2, 3, 4, 5, 6, 7, 8, 9, 10` | Set the highest value of the scale, default: `5`; always `10` for the `promoter` sub type |
| `optionsLabelLowest` | `linearscale` | string | - | Set the label of the lowest value, default: `'Strongly disagree'`, `'Not at all likely'` for the `promoter` sub type; not allowed for the `rating` sub type |
| `optionsLabelHighest` | `linearscale` | string | - | Set the label of the highest value, default: `'Strongly agree'`, `'Extremely likely'` for the `promoter` sub type; not allowed for the `rating` sub type |
| `columns` | `grid` | Array | - | Array of column identifiers / labels for grid questions |
| `rows` | `grid` | Array | - | Array of row identifiers / labels for grid questions |
| `questionType` | `grid` | String | `checkbox`, `number`, `radio` | Type of cell for grid questions (checkbox, numeric input, or radio) |
| `questionType` | `linearscale` | String | `rating`, `promoter` | Sub type, fixed at creation; `rating` is a 1 to N scale drawn as icons, `promoter` is a 0 to 10 scale whose results show the promoter score; absent for a plain scale |
| `ratingIcon` | `linearscale` with `questionType` `rating` | string | `'star', 'heart', 'thumb'` | Set the icon of the rating, default: `'star'` |

### Option Types

Expand Down
24 changes: 24 additions & 0 deletions lib/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class Constants {
public const ANSWER_GRID_TYPE_NUMBER = 'number';
public const ANSWER_GRID_TYPE_RADIO = 'radio';

public const ANSWER_LINEARSCALE_TYPE_RATING = 'rating';
public const ANSWER_LINEARSCALE_TYPE_PROMOTER = 'promoter';

// All AnswerTypes
public const ANSWER_TYPES = [
self::ANSWER_TYPE_COLOR,
Expand Down Expand Up @@ -221,6 +224,14 @@ class Constants {
'optionsHighest' => ['integer', 'NULL'],
'optionsLabelLowest' => ['string', 'NULL'],
'optionsLabelHighest' => ['string', 'NULL'],
'questionType' => ['string'],
'ratingIcon' => ['string', 'NULL'],
];

public const RATING_ICONS = [
'star',
'heart',
'thumb',
];

public const EXTRA_SETTINGS_GRID = [
Expand All @@ -239,6 +250,19 @@ class Constants {
self::ANSWER_GRID_TYPE_RADIO,
];

public const EXTRA_SETTINGS_LINEARSCALE_QUESTION_TYPE = [
self::ANSWER_LINEARSCALE_TYPE_RATING,
self::ANSWER_LINEARSCALE_TYPE_PROMOTER,
];

/**
* The fixed range of a linear scale with the promoter score sub type
*/
public const LINEARSCALE_PROMOTER_RANGE = [
'optionsLowest' => 0,
'optionsHighest' => 10,
];

/**
* !! Keep in sync with src/mixins/ShareTypes.js !!
*/
Expand Down
7 changes: 5 additions & 2 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
* @psalm-import-type FormsPartialForm from ResponseDefinitions
* @psalm-import-type FormsQuestion from ResponseDefinitions
* @psalm-import-type FormsQuestionType from ResponseDefinitions
* @psalm-import-type FormsQuestionGridCellType from ResponseDefinitions
* @psalm-import-type FormsQuestionSubtype from ResponseDefinitions
* @psalm-import-type FormsSubmission from ResponseDefinitions
* @psalm-import-type FormsSubmissions from ResponseDefinitions
* @psalm-import-type FormsUploadedFile from ResponseDefinitions
Expand Down Expand Up @@ -530,7 +530,7 @@ public function getQuestion(int $formId, int $questionId): DataResponse {
*
* @param int $formId the form id
* @param FormsQuestionType $type the new question type
* @param FormsQuestionGridCellType $subtype the new question subtype
* @param FormsQuestionSubtype $subtype the new question subtype
* @param string $text the new question title
* @param ?int $fromId (optional) id of the question that should be cloned
* @param ?int $position (optional) the position of the new question
Expand Down Expand Up @@ -577,6 +577,9 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
}

$extraSettings = $subtype ? ['questionType' => $subtype] : [];
if ($type === Constants::ANSWER_TYPE_LINEARSCALE && $subtype === Constants::ANSWER_LINEARSCALE_TYPE_PROMOTER) {
$extraSettings += Constants::LINEARSCALE_PROMOTER_RANGE;
}
if (!$this->formsService->areExtraSettingsValid($extraSettings, $type)) {
throw new OCSBadRequestException('Invalid subtype');
}
Expand Down
3 changes: 2 additions & 1 deletion lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* optionsLimitMax?: int,
* optionsLimitMin?: int,
* optionsLowest?: 0|1,
* ratingIcon?: "star"|"heart"|"thumb",
* shuffleOptions?: bool,
* timeMax?: string,
* timeMin?: string,
Expand All @@ -45,7 +46,7 @@
* }
*
* @psalm-type FormsQuestionType = "dropdown"|"multiple"|"multiple_unique"|"date"|"time"|"short"|"long"|"file"|"datetime"|"grid"|"linearscale"|"color"|"ranking"
* @psalm-type FormsQuestionGridCellType = "checkbox"|"number"|"radio"
* @psalm-type FormsQuestionSubtype = "checkbox"|"number"|"radio"|"rating"|"promoter"
*
* @psalm-type FormsQuestion = array{
* id: int,
Expand Down
25 changes: 25 additions & 0 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,31 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType
|| isset($extraSettings['optionsHighest']) && ($extraSettings['optionsHighest'] < 2 || $extraSettings['optionsHighest'] > 10)) {
return false;
}

$subtype = $extraSettings['questionType'] ?? null;
if ($subtype !== null && !in_array($subtype, Constants::EXTRA_SETTINGS_LINEARSCALE_QUESTION_TYPE, true)) {
return false;
}

if ($subtype === Constants::ANSWER_LINEARSCALE_TYPE_RATING && ($extraSettings['optionsLowest'] ?? 1) !== 1) {
return false;
}

if ($subtype === Constants::ANSWER_LINEARSCALE_TYPE_PROMOTER
&& (($extraSettings['optionsLowest'] ?? null) !== Constants::LINEARSCALE_PROMOTER_RANGE['optionsLowest']
|| ($extraSettings['optionsHighest'] ?? null) !== Constants::LINEARSCALE_PROMOTER_RANGE['optionsHighest'])) {
return false;
}

if (isset($extraSettings['ratingIcon'])
&& ($subtype !== Constants::ANSWER_LINEARSCALE_TYPE_RATING || !in_array($extraSettings['ratingIcon'], Constants::RATING_ICONS, true))) {
return false;
}

if ($subtype === Constants::ANSWER_LINEARSCALE_TYPE_RATING
&& (isset($extraSettings['optionsLabelLowest']) || isset($extraSettings['optionsLabelHighest']))) {
return false;
}
} elseif ($questionType === Constants::ANSWER_TYPE_GRID) {
if (isset($extraSettings['questionType'])
&& !in_array($extraSettings['questionType'], Constants::EXTRA_SETTINGS_GRID_QUESTION_TYPE, true)) {
Expand Down
16 changes: 13 additions & 3 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@
1
]
},
"ratingIcon": {
"type": "string",
"enum": [
"star",
"heart",
"thumb"
]
},
"shuffleOptions": {
"type": "boolean"
},
Expand All @@ -598,12 +606,14 @@
}
}
},
"QuestionGridCellType": {
"QuestionSubtype": {
"type": "string",
"enum": [
"checkbox",
"number",
"radio"
"radio",
"rating",
"promoter"
]
},
"QuestionType": {
Expand Down Expand Up @@ -1700,7 +1710,7 @@
"description": "the new question type"
},
"subtype": {
"$ref": "#/components/schemas/QuestionGridCellType",
"$ref": "#/components/schemas/QuestionSubtype",
"default": null,
"description": "the new question subtype"
},
Expand Down
31 changes: 29 additions & 2 deletions playwright/e2e/a11y-question-inputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { test as formTest } from '../support/fixtures/form.ts'
import { test as appNavigationTest } from '../support/fixtures/navigation.ts'
import { test as randomUserTest } from '../support/fixtures/random-user.ts'
import { test as topBarTest } from '../support/fixtures/topBar.ts'
import { GridSubtype, QuestionType } from '../support/sections/QuestionType.ts'
import {
GridSubtype,
LinearScaleSubtype,
QuestionType,
} from '../support/sections/QuestionType.ts'
import { FormsView } from '../support/sections/TopBarSection.ts'

const test = mergeTests(randomUserTest, appNavigationTest, formTest, topBarTest)
Expand All @@ -18,6 +22,23 @@ test.beforeEach(async ({ page }) => {
await page.waitForURL(/apps\/forms\/$/)
})

test('The sub menu of a question type names its back entry', async ({
appNavigation,
form,
page,
}) => {
await appNavigation.clickNewForm()
await form.fillTitle('Test form')

await form.newQuestionButton.click()
await page
.getByRole('menuitem', { name: QuestionType.LinearScale, exact: true })
.click()
await expect(page.getByRole('menuitem').first()).toHaveAccessibleName(
'Linear scale, back to all question types',
)
})

test.describe('Accessibility: aria attributes on question inputs', () => {
test('Short answer with description has aria-labelledby and aria-describedby', async ({
appNavigation,
Expand Down Expand Up @@ -196,7 +217,7 @@ test.describe('Accessibility: aria attributes on question inputs', () => {
await appNavigation.clickNewForm()
await form.fillTitle('Test form')

await form.addQuestion(QuestionType.LinearScale)
await form.addQuestion(QuestionType.LinearScale, LinearScaleSubtype.Numbers)
const questions = await form.getQuestions()
await questions[0].fillTitle('Rate your experience')
await questions[0].fillDescription('From 1 to 5')
Expand All @@ -212,6 +233,12 @@ test.describe('Accessibility: aria attributes on question inputs', () => {
exact: true,
}),
).toBeVisible()
await expect(
questions[0].section.locator('fieldset.question-linear-scale__options'),
).not.toHaveAttribute('aria-labelledby')
await expect(
questions[0].section.getByRole('radio').first(),
).not.toHaveAttribute('aria-describedby')

await topBar.toggleView(FormsView.View)

Expand Down
12 changes: 10 additions & 2 deletions playwright/e2e/question-editing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { expect, mergeTests } from '@playwright/test'
import { test as formTest } from '../support/fixtures/form.ts'
import { test as appNavigationTest } from '../support/fixtures/navigation.ts'
import { test as randomUserTest } from '../support/fixtures/random-user.ts'
import { QuestionType } from '../support/sections/QuestionType.ts'
import {
LinearScaleSubtype,
QuestionType,
} from '../support/sections/QuestionType.ts'

const test = mergeTests(randomUserTest, appNavigationTest, formTest)

Expand All @@ -32,7 +35,12 @@ test.describe('Question editing lifecycle', () => {

for (const type of questionTypes) {
test(`Add a ${type} question`, async ({ form }) => {
await form.addQuestion(type)
await form.addQuestion(
type,
type === QuestionType.LinearScale
? LinearScaleSubtype.Numbers
: undefined,
)

const questions = await form.getQuestions()
expect(questions).toHaveLength(1)
Expand Down
Loading