From 91b976b55fa78bb5be5de98ee585545ef7134551 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Sat, 21 Nov 2026 16:35:22 +0100 Subject: [PATCH 1/2] feat(linearscale): add a rating sub type with stars, hearts or thumbs A rating ("how would you rate this") is answered with one whole number from 1 to N, which a linear scale already stores, validates, counts in the results and exports. Drawn as a row of radio buttons with labels, though, it is not the compact control people expect for it. The rating is therefore a sub type of the linear scale, fixed when the question is created and stored in extraSettings.questionType, the way grid questions store their cell type. The add-question menu lists "Numbers" and "Rating" under "Linear scale". A rating draws stars, hearts or thumbs up (extraSettings.ratingIcon) instead of the radio buttons and labels, still built from real radio inputs: each is named "3 of 5", has a hit area of --default-clickable-area, the icons wrap, and the hover effect is dropped under reduced motion. The editor offers the highest rating (2 to 10, default 5) and the icon. Answers, results and exports go through the existing linear scale paths; the results summary names a rating "Linear scale (Rating)" instead of listing scale labels it never shows. The server accepts no other sub type of a linear scale, keeps a rating starting at 1, and accepts ratingIcon only on a rating. Existing linear scale questions have no sub type and stay as they are. Closes #2608 Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- docs/API_v3.md | 1 + docs/DataStructure.md | 4 +- lib/Constants.php | 8 + lib/Controller/ApiController.php | 12 +- lib/ResponseDefinitions.php | 3 +- lib/Service/FormsService.php | 14 ++ openapi.json | 15 +- playwright/e2e/a11y-question-inputs.spec.ts | 8 +- playwright/e2e/question-editing.spec.ts | 12 +- playwright/support/sections/FormSection.ts | 8 +- playwright/support/sections/QuestionType.ts | 8 + src/components/AddQuestionMenu.vue | 17 +- .../Questions/QuestionLinearScale.vue | 206 +++++++++++++++++- src/components/Results/ResultsSummary.vue | 7 + src/models/AnswerTypes.ts | 18 +- tests/Unit/Controller/ApiControllerTest.php | 48 ++++ tests/Unit/Service/FormsServiceTest.php | 55 +++++ tests/Unit/Service/SubmissionServiceTest.php | 30 +++ 18 files changed, 448 insertions(+), 26 deletions(-) diff --git a/docs/API_v3.md b/docs/API_v3.md index 6e7654fe42..da2623dcbf 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -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` for `linearscale` (a 1 to N scale drawn as icons). 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. diff --git a/docs/DataStructure.md b/docs/DataStructure.md index d0e9acfa34..24c891721b 100644 --- a/docs/DataStructure.md +++ b/docs/DataStructure.md @@ -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` | +| `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'` | | `optionsLabelHighest` | `linearscale` | string | - | Set the label of the highest value, default: `'Strongly agree'` | | `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 | +| `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 3e465cd0cd..957080f6db 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -221,6 +221,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 = [ diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 416f263f1f..469f835be6 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -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 @@ -530,12 +530,13 @@ 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 * @return DataResponse * @throws OCSBadRequestException Invalid type + * @throws OCSBadRequestException Invalid subtype * @throws OCSBadRequestException Datetime question type no longer supported * @throws OCSForbiddenException User has no permissions to get this form * @throws OCSForbiddenException This form is archived and can not be modified @@ -575,6 +576,11 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype throw new OCSBadRequestException('Datetime question type no longer supported'); } + $extraSettings = $subtype ? ['questionType' => $subtype] : []; + if (!$this->formsService->areExtraSettingsValid($extraSettings, $type)) { + throw new OCSBadRequestException('Invalid subtype'); + } + // Retrieve all active questions sorted by Order. $questions = $this->questionMapper->findByForm($formId); @@ -599,7 +605,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype $question->setText($text); $question->setDescription(''); $question->setIsRequired(false); - $question->setExtraSettings($subtype ? ['questionType' => $subtype] : []); + $question->setExtraSettings($extraSettings); $question = $this->questionMapper->insert($question); diff --git a/lib/ResponseDefinitions.php b/lib/ResponseDefinitions.php index 17fac6599e..c50e12f150 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -35,6 +35,7 @@ * optionsLimitMax?: int, * optionsLimitMin?: int, * optionsLowest?: 0|1, + * ratingIcon?: "star"|"heart"|"thumb", * shuffleOptions?: bool, * timeMax?: string, * timeMin?: string, @@ -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" * * @psalm-type FormsQuestion = array{ * id: int, diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index 197ababbcb..3f3f7cff59 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -1051,6 +1051,20 @@ 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 && $subtype !== 'rating') { + return false; + } + + if ($subtype === 'rating' && ($extraSettings['optionsLowest'] ?? 1) !== 1) { + return false; + } + + if (isset($extraSettings['ratingIcon']) + && ($subtype !== 'rating' || !in_array($extraSettings['ratingIcon'], Constants::RATING_ICONS, true))) { + return false; + } } return true; } diff --git a/openapi.json b/openapi.json index 4e11633cea..f4919411a2 100644 --- a/openapi.json +++ b/openapi.json @@ -575,6 +575,14 @@ 1 ] }, + "ratingIcon": { + "type": "string", + "enum": [ + "star", + "heart", + "thumb" + ] + }, "shuffleOptions": { "type": "boolean" }, @@ -598,12 +606,13 @@ } } }, - "QuestionGridCellType": { + "QuestionSubtype": { "type": "string", "enum": [ "checkbox", "number", - "radio" + "radio", + "rating" ] }, "QuestionType": { @@ -1700,7 +1709,7 @@ "description": "the new question type" }, "subtype": { - "$ref": "#/components/schemas/QuestionGridCellType", + "$ref": "#/components/schemas/QuestionSubtype", "default": null, "description": "the new question subtype" }, diff --git a/playwright/e2e/a11y-question-inputs.spec.ts b/playwright/e2e/a11y-question-inputs.spec.ts index c32bc14f1c..4eeb6a9642 100644 --- a/playwright/e2e/a11y-question-inputs.spec.ts +++ b/playwright/e2e/a11y-question-inputs.spec.ts @@ -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) @@ -196,7 +200,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') diff --git a/playwright/e2e/question-editing.spec.ts b/playwright/e2e/question-editing.spec.ts index 6b37d0c003..ed8eec3241 100644 --- a/playwright/e2e/question-editing.spec.ts +++ b/playwright/e2e/question-editing.spec.ts @@ -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) @@ -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) diff --git a/playwright/support/sections/FormSection.ts b/playwright/support/sections/FormSection.ts index ab7d3e8d0b..b2c3b8a93a 100644 --- a/playwright/support/sections/FormSection.ts +++ b/playwright/support/sections/FormSection.ts @@ -4,7 +4,11 @@ */ import type { Locator, Page } from '@playwright/test' -import type { GridSubtype, QuestionType } from './QuestionType.ts' +import type { + GridSubtype, + LinearScaleSubtype, + QuestionType, +} from './QuestionType.ts' import { waitForApiResponse } from '../helpers.ts' import { QuestionSection } from './QuestionSection.ts' @@ -43,7 +47,7 @@ export class FormSection { public async addQuestion( type: QuestionType, - subtype?: GridSubtype, + subtype?: GridSubtype | LinearScaleSubtype, ): Promise { const created = waitForApiResponse(this.page, 'POST') await this.newQuestionButton.click() diff --git a/playwright/support/sections/QuestionType.ts b/playwright/support/sections/QuestionType.ts index 1f51c8f709..5d8a57b742 100644 --- a/playwright/support/sections/QuestionType.ts +++ b/playwright/support/sections/QuestionType.ts @@ -26,3 +26,11 @@ export enum GridSubtype { Number = 'Number', RadioButtons = 'Radio buttons', } + +/** + * Labels of the entries in the linear scale subtype menu. + */ +export enum LinearScaleSubtype { + Numbers = 'Numbers', + Rating = 'Rating', +} diff --git a/src/components/AddQuestionMenu.vue b/src/components/AddQuestionMenu.vue index 854d2ef58c..0b565da640 100644 --- a/src/components/AddQuestionMenu.vue +++ b/src/components/AddQuestionMenu.vue @@ -41,18 +41,23 @@ - {{ t('forms', 'Grid') }} + {{ answerTypesFilter[activeQuestionType].label }} + @click=" + onSubtypeClick( + activeQuestionType, + answer.extraSettings.questionType ?? null, + position, + ) + "> @@ -68,7 +73,6 @@ import type { AnswerTypeConfig } from '../models/AnswerTypes.ts' import IconPlus from '@material-symbols/svg-400/outlined/add.svg?raw' import IconChevronLeft from '@material-symbols/svg-400/outlined/chevron_left.svg?raw' -import { t } from '@nextcloud/l10n' import { defineComponent, ref, watch } from 'vue' import NcActionButton from '@nextcloud/vue/components/NcActionButton' import NcActions from '@nextcloud/vue/components/NcActions' @@ -145,7 +149,7 @@ export default defineComponent({ const onSubtypeClick = ( type: string, - subtype: string | number, + subtype: string | null, position: number | null, ) => { emit('addQuestion', type, subtype, position) @@ -157,7 +161,6 @@ export default defineComponent({ openLocal, onPrimaryClick, onSubtypeClick, - t, IconChevronLeft, IconPlus, } diff --git a/src/components/Questions/QuestionLinearScale.vue b/src/components/Questions/QuestionLinearScale.vue index b2b13b0f9b..14ff7a98ad 100644 --- a/src/components/Questions/QuestionLinearScale.vue +++ b/src/components/Questions/QuestionLinearScale.vue @@ -13,6 +13,7 @@ v-on="commonListeners"> +
+ +
import IconPencil from '@material-symbols/svg-400/outlined/edit.svg?raw' +import IconHeartFilled from '@material-symbols/svg-400/outlined/favorite-fill.svg?raw' +import IconHeart from '@material-symbols/svg-400/outlined/favorite.svg?raw' +import IconStarFilled from '@material-symbols/svg-400/outlined/star-fill.svg?raw' +import IconStar from '@material-symbols/svg-400/outlined/star.svg?raw' +import IconThumbFilled from '@material-symbols/svg-400/outlined/thumb_up-fill.svg?raw' +import IconThumb from '@material-symbols/svg-400/outlined/thumb_up.svg?raw' import { t } from '@nextcloud/l10n' import { computed, defineComponent, nextTick, onMounted, ref } from 'vue' +import NcActionCaption from '@nextcloud/vue/components/NcActionCaption' import NcActionInput from '@nextcloud/vue/components/NcActionInput' +import NcActionRadio from '@nextcloud/vue/components/NcActionRadio' import NcCheckboxRadioSwitch from '@nextcloud/vue/components/NcCheckboxRadioSwitch' import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper' import NcTextArea from '@nextcloud/vue/components/NcTextArea' @@ -154,14 +212,50 @@ type LinearScaleExtraSettings = { optionsHighest?: number | null optionsLabelLowest?: string | null optionsLabelHighest?: string | null + questionType?: string + ratingIcon?: string | null } +interface RatingIcon { + id: 'star' | 'heart' | 'thumb' + label: string + outline: string + filled: string + color: string +} + +const RATING_ICONS: RatingIcon[] = [ + { + id: 'star', + label: t('forms', 'Stars'), + outline: IconStar, + filled: IconStarFilled, + color: 'var(--color-element-warning)', + }, + { + id: 'heart', + label: t('forms', 'Hearts'), + outline: IconHeart, + filled: IconHeartFilled, + color: 'var(--color-element-error)', + }, + { + id: 'thumb', + label: t('forms', 'Thumbs up'), + outline: IconThumb, + filled: IconThumbFilled, + color: 'var(--color-element-info)', + }, +] + export default defineComponent({ name: 'QuestionLinearScale', components: { NcIconSvgWrapper, + NcActionCaption, NcActionInput, + NcActionRadio, NcCheckboxRadioSwitch, NcTextArea, Question, @@ -181,6 +275,23 @@ export default defineComponent({ (props.extraSettings as LinearScaleExtraSettings | undefined) ?? {} ) }) + const isRating = computed( + () => extraSettings.value.questionType === 'rating', + ) + const ratingIcon = computed( + () => + RATING_ICONS.find( + (icon) => icon.id === extraSettings.value.ratingIcon, + ) ?? RATING_ICONS[0], + ) + const ratingValue = computed( + () => parseInt(values.value?.[0] ?? '') || 0, + ) + const highestLabel = computed(() => + isRating.value + ? t('forms', 'Highest rating') + : t('forms', 'Highest value'), + ) const lowest = ref<{ $refs: { input: HTMLTextAreaElement } } | null>(null) const highest = ref<{ $refs: { input: HTMLTextAreaElement } } | null>(null) @@ -269,7 +380,7 @@ export default defineComponent({ } onMounted(() => { - if (!props.readOnly) { + if (!props.readOnly && !isRating.value) { resizeLabel('lowest') resizeLabel('highest') } @@ -290,6 +401,28 @@ export default defineComponent({ const onChange = (option: string): void => { emit('update:values', [option]) + if (isRating.value) { + question.errorMessage.value = null + } + } + + /** + * Accessible name of one option of a rating, e.g. "3 of 5" + * + * @param option the value of the option + */ + const ratingOptionLabel = (option: number): string => { + // TRANSLATORS Accessible name of one option of a rating, e.g. "3 of 5" + return t('forms', '{value} of {max}', { + value: option, + max: optionsHighest.value, + }) + } + + const onRatingIconChange = (icon: RatingIcon['id']): void => { + question.onExtraSettingsChange({ + ratingIcon: icon === RATING_ICONS[0].id ? null : icon, + }) } const onOptionsLowestChange = (value: number): void => { @@ -343,6 +476,13 @@ export default defineComponent({ optionsHighest, optionsLabelLowest, optionsLabelHighest, + isRating, + highestLabel, + ratingIcon, + ratingIcons: RATING_ICONS, + ratingValue, + ratingOptionLabel, + onRatingIconChange, validate, onChange, onOptionsLowestChange, @@ -357,6 +497,64 @@ export default defineComponent({