From 7f4d150747b783a087da8389551203e2a03cfdd7 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Sat, 21 Nov 2026 16:24:37 +0100 Subject: [PATCH 01/15] feat(linearscale): add a promoter score sub type A 0-10 scale is not always a "how likely are you to recommend" question, and switching a scale that already has answers to 0-10 would score those answers on the wrong scale. The promoter score is therefore a sub type of the linear scale, fixed when the question is created and stored in extraSettings.questionType next to "Numbers" and "Rating" in the add-question menu. Creating a promoter score question stores the range 0 to 10, the server rejects any other range for it, and the editor shows no range pickers for it. Its lowest and highest values are labelled "Not at all likely" and "Extremely likely" unless the author changes them, as "Strongly disagree" and "Strongly agree" do not fit a likelihood. Existing scales have no sub type and keep working as before. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- docs/API_v3.md | 3 +- docs/DataStructure.md | 10 +-- lib/Constants.php | 10 +++ lib/Controller/ApiController.php | 3 + lib/ResponseDefinitions.php | 2 +- lib/Service/FormsService.php | 6 ++ openapi.json | 3 +- playwright/support/sections/QuestionType.ts | 1 + .../Questions/QuestionLinearScale.vue | 15 +++-- src/components/Results/ResultsSummary.vue | 14 ++++- src/models/AnswerTypes.ts | 8 +++ src/models/Constants.ts | 24 +++++++ tests/Unit/Controller/ApiControllerTest.php | 15 ++++- tests/Unit/Service/FormsServiceTest.php | 62 +++++++++++++++++++ 14 files changed, 158 insertions(+), 18 deletions(-) diff --git a/docs/API_v3.md b/docs/API_v3.md index 7a1272103..604af503d 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -376,7 +376,7 @@ Returns the questions and options of the given form (without submissions). | Parameter | Type | Optional | Description | |-----------|---------|----------|-------------| | _type_ | [QuestionType](DataStructure.md#question-types) | | The question-type of the new question | - | _subtype_ | String | yes | _Optional_ The sub type, stored as `extraSettings.questionType`: the cell type for `grid` (`checkbox`, `number` or `radio`), `rating` for `linearscale` (a 1 to N scale drawn as icons). Any other value is rejected. | + | _subtype_ | String | yes | _Optional_ The sub type, stored as `extraSettings.questionType`: the cell type for `grid` (`checkbox`, `number` or `radio`), `rating` (a 1 to N scale drawn as icons) or `promoter` (a fixed 0 to 10 scale) for `linearscale`. Any other value is rejected. | | _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**. @@ -457,6 +457,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. ``` diff --git a/docs/DataStructure.md b/docs/DataStructure.md index e84c665ee..2aabc65ef 100644 --- a/docs/DataStructure.md +++ b/docs/DataStructure.md @@ -263,14 +263,14 @@ 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`; always `1` for the `rating` sub type | -| `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'`; not allowed for the `rating` sub type | -| `optionsLabelHighest` | `linearscale` | string | - | Set the label of the highest value, default: `'Strongly agree'`; not allowed for the `rating` sub type | +| `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` | Sub type, fixed at creation; `rating` is a 1 to N scale drawn as icons; absent for a plain scale | +| `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 diff --git a/lib/Constants.php b/lib/Constants.php index f74d2f3be..f56977d8e 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -119,6 +119,7 @@ class Constants { public const ANSWER_GRID_TYPE_RADIO = 'radio'; public const ANSWER_LINEARSCALE_TYPE_RATING = 'rating'; + public const ANSWER_LINEARSCALE_TYPE_PROMOTER = 'promoter'; // Option types public const OPTION_TYPE_CHOICE = 'choice'; @@ -267,6 +268,15 @@ class Constants { 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, ]; /** diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index de5e3c44f..9b0a7c2d9 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -581,6 +581,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'); } diff --git a/lib/ResponseDefinitions.php b/lib/ResponseDefinitions.php index c50e12f15..0db8b7bd4 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -46,7 +46,7 @@ * } * * @psalm-type FormsQuestionType = "dropdown"|"multiple"|"multiple_unique"|"date"|"time"|"short"|"long"|"file"|"datetime"|"grid"|"linearscale"|"color"|"ranking" - * @psalm-type FormsQuestionSubtype = "checkbox"|"number"|"radio"|"rating" + * @psalm-type FormsQuestionSubtype = "checkbox"|"number"|"radio"|"rating"|"promoter" * * @psalm-type FormsQuestion = array{ * id: int, diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index e6a8d110b..39c74a8ba 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -1061,6 +1061,12 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType 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; diff --git a/openapi.json b/openapi.json index 19baa5d42..d97ee439c 100644 --- a/openapi.json +++ b/openapi.json @@ -612,7 +612,8 @@ "checkbox", "number", "radio", - "rating" + "rating", + "promoter" ] }, "QuestionType": { diff --git a/playwright/support/sections/QuestionType.ts b/playwright/support/sections/QuestionType.ts index 5d8a57b74..4ac5094bd 100644 --- a/playwright/support/sections/QuestionType.ts +++ b/playwright/support/sections/QuestionType.ts @@ -33,4 +33,5 @@ export enum GridSubtype { export enum LinearScaleSubtype { Numbers = 'Numbers', Rating = 'Rating', + Promoter = 'Promoter score', } diff --git a/src/components/Questions/QuestionLinearScale.vue b/src/components/Questions/QuestionLinearScale.vue index 1deafe8d7..54cd53eeb 100644 --- a/src/components/Questions/QuestionLinearScale.vue +++ b/src/components/Questions/QuestionLinearScale.vue @@ -11,7 +11,7 @@ :warningInvalid="answerType.warningInvalid" :errorMessage="errorMessage" v-on="commonListeners"> -