From e4b76feaf2c3bd8e2c3486260bcd5b9fd5cce69d Mon Sep 17 00:00:00 2001 From: Kostiantyn Miakshyn Date: Tue, 15 Sep 2026 12:51:25 +0200 Subject: [PATCH 1/7] feat: introduce Section as a new question type Signed-off-by: Kostiantyn Miakshyn --- docs/DataStructure.md | 1 + lib/Constants.php | 4 +- lib/Controller/ApiController.php | 5 + lib/ResponseDefinitions.php | 2 +- lib/Service/SubmissionService.php | 13 +- openapi.json | 3 +- src/components/Questions/Question.vue | 39 +++++- src/components/Questions/QuestionSection.vue | 128 +++++++++++++++++++ src/composables/useQuestion.ts | 3 + src/models/AnswerTypes.ts | 12 ++ src/views/Submit.vue | 47 +++++++ tests/Unit/Controller/ApiControllerTest.php | 32 ++++- tests/Unit/Service/SubmissionServiceTest.php | 35 +++++ 13 files changed, 317 insertions(+), 7 deletions(-) create mode 100644 src/components/Questions/QuestionSection.vue diff --git a/docs/DataStructure.md b/docs/DataStructure.md index 78c19a958..321c8d824 100644 --- a/docs/DataStructure.md +++ b/docs/DataStructure.md @@ -240,6 +240,7 @@ Currently supported Question-Types are: | `linearscale` | A linear or Likert scale question where you choose an option that best fits your opinion | | `color` | A color answer, hex string representation (e. g. `#123456`) | | `ranking` | Using pre-defined options, the user ranks them from most to least preferred. Needs at least one option available. Answers are stored in ranked order (one answer row per option). | +| `section` | A structural element to group questions into sections. It cannot be answered and has no options. | ## Extra Settings diff --git a/lib/Constants.php b/lib/Constants.php index 5debb548b..a2cfbf46c 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -96,7 +96,7 @@ class Constants { ]; /** - * !! Keep in sync with src/models/AnswerTypes.js !! + * !! Keep in sync with src/models/AnswerTypes.ts !! */ // Available AnswerTypes @@ -111,6 +111,7 @@ class Constants { public const ANSWER_TYPE_MULTIPLE = 'multiple'; public const ANSWER_TYPE_MULTIPLEUNIQUE = 'multiple_unique'; public const ANSWER_TYPE_RANKING = 'ranking'; + public const ANSWER_TYPE_SECTION = 'section'; public const ANSWER_TYPE_SHORT = 'short'; public const ANSWER_TYPE_TIME = 'time'; @@ -150,6 +151,7 @@ class Constants { self::ANSWER_TYPE_MULTIPLE, self::ANSWER_TYPE_MULTIPLEUNIQUE, self::ANSWER_TYPE_RANKING, + self::ANSWER_TYPE_SECTION, self::ANSWER_TYPE_SHORT, self::ANSWER_TYPE_TIME, ]; diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 9797d1825..d612d30d2 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -1408,6 +1408,11 @@ public function getSubmissions(int $formId, ?string $query = null, ?int $limit = } $questions = []; foreach ($this->formsService->getQuestions($formId) as $question) { + // Sections are structural elements and cannot be answered + if ($question['type'] === Constants::ANSWER_TYPE_SECTION) { + continue; + } + $questions[$question['id']] = $question; } diff --git a/lib/ResponseDefinitions.php b/lib/ResponseDefinitions.php index a51b0a14b..0cf4c7b96 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -52,7 +52,7 @@ * extraSettings?: FormsQuestionExtraSettings|\stdClass, * } * - * @psalm-type FormsQuestionType = "dropdown"|"multiple"|"multiple_unique"|"date"|"time"|"short"|"long"|"file"|"datetime"|"grid"|"linearscale"|"color"|"ranking" + * @psalm-type FormsQuestionType = "dropdown"|"multiple"|"multiple_unique"|"date"|"time"|"short"|"long"|"file"|"datetime"|"grid"|"linearscale"|"color"|"ranking"|"section" * @psalm-type FormsQuestionSubtype = "checkbox"|"number"|"radio"|"rating"|"promoter" * * @psalm-type FormsQuestion = array{ diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 850e964f2..d85e09861 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -264,7 +264,10 @@ public function getSubmissionsData(Form $form, string $fileFormat, ?File $file = // Oldest first $submissionEntities = array_reverse($submissionEntities); - $questions = $this->questionMapper->findByForm($form->getId()); + $questions = array_filter( + $this->questionMapper->findByForm($form->getId()), + static fn (Question $question): bool => $question->getType() !== Constants::ANSWER_TYPE_SECTION, + ); $activeQuestionIds = $this->getActiveQuestionIds($questions); @@ -691,6 +694,14 @@ public function validateSubmission(array $questions, array $answers, string $for throw new \InvalidArgumentException(sprintf('Invalid answer for question "%s".', $question['text'])); } + // Sections are structural elements and cannot have answers + if ($question['type'] === Constants::ANSWER_TYPE_SECTION) { + if ($questionAnswered && array_filter($answers[$questionId])) { + throw new \InvalidArgumentException(sprintf('Section "%s" cannot have answers.', $question['text'])); + } + continue; + } + // Check if all required questions have an answer if (!empty($question['isRequired']) && self::isShownToRespondents($question) diff --git a/openapi.json b/openapi.json index 9ee22c000..b0a6c2023 100644 --- a/openapi.json +++ b/openapi.json @@ -700,7 +700,8 @@ "grid", "linearscale", "color", - "ranking" + "ranking", + "section" ] }, "Share": { diff --git a/src/components/Questions/Question.vue b/src/components/Questions/Question.vue index fa95bd6dc..22fd6b94e 100644 --- a/src/components/Questions/Question.vue +++ b/src/components/Questions/Question.vue @@ -8,6 +8,7 @@ class="question" :class="{ 'question--editable': !readOnly, + 'question--section': readOnly && isSection, }" :aria-label="t('forms', 'Question number {index}', { index })"> @@ -92,6 +93,7 @@ @@ -109,6 +111,7 @@ , + default: '', + }, + contentValid: { type: Boolean, // eslint-disable-next-line vue/no-boolean-default @@ -355,11 +363,13 @@ export default defineComponent({ const buttonUp = ref<{ $el?: HTMLElement } | undefined>(undefined) const buttonDown = ref<{ $el?: HTMLElement } | undefined>(undefined) + const isSection = computed(() => props.type === 'section') + /** * Extend text with asterisk if question is required */ const computedText = computed(() => { - if (props.isRequired) { + if (props.isRequired && !isSection.value) { return props.text + ' *' } return props.text @@ -514,6 +524,7 @@ export default defineComponent({ titleId, descriptionId, hasDescription, + isSection, hasError, hasInfo, errorId, @@ -693,5 +704,31 @@ export default defineComponent({ box-sizing: border-box; } } + + &--section { + margin-block-end: 16px; + position: sticky; + top: 0; + z-index: 2; + + h3 { + font-size: 24px !important; + border-block-end: 1px solid var(--color-border); + } + } + + // Limit the description to two lines while the section is stuck to the top + &--section-stuck .question__header__description { + // two lines at 1.5em line-height plus the output padding + max-height: calc(2 * 1.5em + 12px); + overflow: hidden; + } +} + +// In views with a sticky top bar, sections must stick below it +.app-content:not(.app-content--public) .question--section { + top: calc( + var(--default-clickable-area) + 2 * var(--app-navigation-padding, 0px) + ); } diff --git a/src/components/Questions/QuestionSection.vue b/src/components/Questions/QuestionSection.vue new file mode 100644 index 000000000..3f5c5c3e7 --- /dev/null +++ b/src/components/Questions/QuestionSection.vue @@ -0,0 +1,128 @@ + + + + + + + diff --git a/src/composables/useQuestion.ts b/src/composables/useQuestion.ts index 9945b319a..43b661b50 100644 --- a/src/composables/useQuestion.ts +++ b/src/composables/useQuestion.ts @@ -171,6 +171,7 @@ interface QuestionPropsLike { isRequired: boolean readOnly: boolean name: string + type: string | null maxStringLengths: MaxStringLengths canMoveUp: boolean canMoveDown: boolean @@ -188,6 +189,7 @@ interface QuestionForwardedProps { readOnly: boolean maxStringLengths: MaxStringLengths name: string + type: string | null canMoveUp: boolean canMoveDown: boolean jumps: Jumps @@ -253,6 +255,7 @@ export function useQuestion(props: QuestionPropsLike, options: UseQuestionOption readOnly: props.readOnly, maxStringLengths: props.maxStringLengths, name: props.name, + type: props.type, canMoveUp: props.canMoveUp, canMoveDown: props.canMoveDown, jumps, diff --git a/src/models/AnswerTypes.ts b/src/models/AnswerTypes.ts index d0cebc0eb..055ca444d 100644 --- a/src/models/AnswerTypes.ts +++ b/src/models/AnswerTypes.ts @@ -21,6 +21,7 @@ import IconTextShort from '@material-symbols/svg-400/outlined/short_text.svg?raw import IconStar from '@material-symbols/svg-400/outlined/star.svg?raw' import IconTextLong from '@material-symbols/svg-400/outlined/subject.svg?raw' import IconSwapVertical from '@material-symbols/svg-400/outlined/swap_vert.svg?raw' +import IconViewAgenda from '@material-symbols/svg-400/outlined/view_agenda.svg?raw' import { t } from '@nextcloud/l10n' import { markRaw } from 'vue' import QuestionColor from '../components/Questions/QuestionColor.vue' @@ -32,6 +33,7 @@ import QuestionLinearScale from '../components/Questions/QuestionLinearScale.vue import QuestionLong from '../components/Questions/QuestionLong.vue' import QuestionMultiple from '../components/Questions/QuestionMultiple.vue' import QuestionRanking from '../components/Questions/QuestionRanking.vue' +import QuestionSection from '../components/Questions/QuestionSection.vue' import QuestionShort from '../components/Questions/QuestionShort.vue' import { LinearScaleType, OptionType } from './Constants.ts' @@ -321,6 +323,16 @@ const answerTypes: Record = { 'This question needs a title and at least one answer!', ), }, + + section: { + component: markRaw(QuestionSection), + icon: IconViewAgenda, + label: t('forms', 'Section'), + predefined: false, + + titlePlaceholder: t('forms', 'Section title'), + warningInvalid: t('forms', 'This section needs a title!'), + }, } export default answerTypes diff --git a/src/views/Submit.vue b/src/views/Submit.vue index b8d491a1d..95df5bb87 100644 --- a/src/views/Submit.vue +++ b/src/views/Submit.vue @@ -390,6 +390,16 @@ interface QuestionComponentRef { validate: () => Promise } +interface IndexedQuestion { + question: SubmitQuestion + displayIndex: number +} + +interface QuestionGroup { + section: IndexedQuestion | null + questions: IndexedQuestion[] +} + interface DialogButton { label: string icon: string @@ -508,6 +518,42 @@ export default defineComponent({ }) as SubmitQuestion[] }) + /** + * Group questions by sections. + * Each section contains its questions and the section itself. + * This is needed for position:sticky to work when there are several + * sections and allows to display groups on separate pages later on. + */ + const groupedQuestions = computed(() => { + const groups: QuestionGroup[] = [] + let currentGroup: QuestionGroup = { section: null, questions: [] } + let questionIndex = 1 + + for (const question of validQuestions.value) { + if (question.type === 'section') { + if (currentGroup.section || currentGroup.questions.length > 0) { + groups.push(currentGroup) + } + currentGroup = { + section: { question, displayIndex: questionIndex }, + questions: [], + } + } else { + currentGroup.questions.push({ + question, + displayIndex: questionIndex, + }) + } + questionIndex++ + } + + if (currentGroup.section || currentGroup.questions.length > 0) { + groups.push(currentGroup) + } + + return groups + }) + const validQuestionsIds = computed>(() => { return new Set(validQuestions.value.map((question) => question.id)) }) @@ -1674,6 +1720,7 @@ export default defineComponent({ expirationMessage, openingMessage, formElement, + groupedQuestions, hasAnswers, infoMessage, isArchived, diff --git a/tests/Unit/Controller/ApiControllerTest.php b/tests/Unit/Controller/ApiControllerTest.php index 212366b89..e89c3d07a 100644 --- a/tests/Unit/Controller/ApiControllerTest.php +++ b/tests/Unit/Controller/ApiControllerTest.php @@ -244,7 +244,7 @@ public static function dataGetSubmissions() { 'submissions' => [ ['userId' => 'anon-user-1'] ], - 'questions' => [['id' => 1, 'name' => 'questions']], + 'questions' => [['id' => 1, 'name' => 'questions', 'type' => Constants::ANSWER_TYPE_SHORT]], 'expected' => [ 'submissions' => [ [ @@ -256,6 +256,7 @@ public static function dataGetSubmissions() { [ 'id' => 1, 'name' => 'questions', + 'type' => Constants::ANSWER_TYPE_SHORT, 'extraSettings' => new \stdClass(), ], ], @@ -266,7 +267,7 @@ public static function dataGetSubmissions() { 'submissions' => [ ['userId' => 'jdoe'] ], - 'questions' => [['id' => 1, 'name' => 'questions']], + 'questions' => [['id' => 1, 'name' => 'questions', 'type' => Constants::ANSWER_TYPE_SHORT]], 'expected' => [ 'submissions' => [ [ @@ -278,6 +279,33 @@ public static function dataGetSubmissions() { [ 'id' => 1, 'name' => 'questions', + 'type' => Constants::ANSWER_TYPE_SHORT, + 'extraSettings' => new \stdClass(), + ], + ], + 'filteredSubmissionsCount' => 1, + ] + ], + 'sections are filtered out' => [ + 'submissions' => [ + ['userId' => 'jdoe'] + ], + 'questions' => [ + ['id' => 1, 'name' => 'questions', 'type' => Constants::ANSWER_TYPE_SHORT], + ['id' => 2, 'name' => 'section', 'type' => Constants::ANSWER_TYPE_SECTION], + ], + 'expected' => [ + 'submissions' => [ + [ + 'userId' => 'jdoe', + 'userDisplayName' => 'jdoe', + ] + ], + 'questions' => [ + [ + 'id' => 1, + 'name' => 'questions', + 'type' => Constants::ANSWER_TYPE_SHORT, 'extraSettings' => new \stdClass(), ], ], diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index 29780a9ad..75e98695f 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -1952,6 +1952,41 @@ public static function dataValidateSubmission() { // Expected Result – required question must be answered 'Question "Rank these" is required.', ], + 'invalid-section-with-answer' => [ + // Questions + [ + ['id' => 1, 'type' => 'section', 'text' => 'My section', 'isRequired' => false] + ], + // Answers – sections cannot have answers + [ + '1' => ['some answer'] + ], + // Expected Result + 'Section "My section" cannot have answers.', + ], + 'valid-section-without-answer' => [ + // Questions + [ + ['id' => 1, 'type' => 'section', 'text' => 'My section', 'isRequired' => false], + ['id' => 2, 'type' => 'short', 'text' => 'A question', 'isRequired' => false] + ], + // Answers + [ + '2' => ['an answer'] + ], + // Expected Result – no error + null, + ], + 'valid-section-required-unanswered' => [ + // Questions – a required section still cannot be answered and must not block submission + [ + ['id' => 1, 'type' => 'section', 'text' => 'My section', 'isRequired' => true] + ], + // Answers + [], + // Expected Result – no error + null, + ], ]; } From 66f4bd2dbf71923292ff12399220c5d23510ed61 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Mon, 23 Nov 2026 19:33:16 +0100 Subject: [PATCH 2/7] refactor(section): show a section as a plain heading A section no longer sticks to the top of the page while the respondent scrolls, so the observer, the sentinel element and the grouping of the questions by section are gone. Sections become pages of their own in a later step, where a sticking heading has no purpose. The questions that follow a section get a heading one level below the section's, so the heading outline of the form follows its sections. Forms without sections keep their headings as before. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- playwright/support/sections/SubmitSection.ts | 2 +- src/components/Questions/Question.vue | 27 ++---- src/components/Questions/QuestionSection.vue | 91 ++------------------ src/composables/useQuestion.ts | 7 ++ src/views/Submit.vue | 65 ++++---------- 5 files changed, 41 insertions(+), 151 deletions(-) diff --git a/playwright/support/sections/SubmitSection.ts b/playwright/support/sections/SubmitSection.ts index 01518cd15..e8f685dd1 100644 --- a/playwright/support/sections/SubmitSection.ts +++ b/playwright/support/sections/SubmitSection.ts @@ -22,7 +22,7 @@ export class SubmitSection { /** * Get a question's list item by its title text. * Questions render as
  • , - * and each contains an

    with the title text. + * and each contains a heading with the title text. * * @param name the title of the question */ diff --git a/src/components/Questions/Question.vue b/src/components/Questions/Question.vue index 22fd6b94e..c02db46af 100644 --- a/src/components/Questions/Question.vue +++ b/src/components/Questions/Question.vue @@ -63,13 +63,14 @@ :maxlength="maxStringLengths.questionText" required @input="onTitleChange" /> -

    {{ computedText }} -

    +
    diff --git a/src/components/Questions/QuestionSection.vue b/src/components/Questions/QuestionSection.vue index 3f5c5c3e7..cc7290b5f 100644 --- a/src/components/Questions/QuestionSection.vue +++ b/src/components/Questions/QuestionSection.vue @@ -4,28 +4,17 @@ --> - - diff --git a/src/composables/useQuestion.ts b/src/composables/useQuestion.ts index 43b661b50..3dbe1fdcf 100644 --- a/src/composables/useQuestion.ts +++ b/src/composables/useQuestion.ts @@ -63,6 +63,10 @@ export const QUESTION_PROPS = { type: Number, required: true, }, + headingLevel: { + type: Number, + default: 3, + }, name: { type: String, default: '', @@ -166,6 +170,7 @@ interface QuestionPropsLike { id: number formId: number | null index: number + headingLevel: number text: string description: string isRequired: boolean @@ -183,6 +188,7 @@ interface QuestionPropsLike { interface QuestionForwardedProps { index: number + headingLevel: number text: string description: string isRequired: boolean @@ -249,6 +255,7 @@ export function useQuestion(props: QuestionPropsLike, options: UseQuestionOption const questionProps = computed(() => ({ index: props.index, + headingLevel: props.headingLevel, text: props.text, description: props.description, isRequired: props.isRequired, diff --git a/src/views/Submit.vue b/src/views/Submit.vue index 95df5bb87..bfbd5d9cb 100644 --- a/src/views/Submit.vue +++ b/src/views/Submit.vue @@ -166,6 +166,9 @@ readOnly :answerType="answerTypes[question.type]" :index="shownQuestions.indexOf(question) + 1" + :headingLevel=" + questionIdsInSection.has(question.id) ? 4 : 3 + " :maxStringLengths="maxStringLengths" :values="answers[question.id]" @update:values=" @@ -390,16 +393,6 @@ interface QuestionComponentRef { validate: () => Promise } -interface IndexedQuestion { - question: SubmitQuestion - displayIndex: number -} - -interface QuestionGroup { - section: IndexedQuestion | null - questions: IndexedQuestion[] -} - interface DialogButton { label: string icon: string @@ -518,42 +511,6 @@ export default defineComponent({ }) as SubmitQuestion[] }) - /** - * Group questions by sections. - * Each section contains its questions and the section itself. - * This is needed for position:sticky to work when there are several - * sections and allows to display groups on separate pages later on. - */ - const groupedQuestions = computed(() => { - const groups: QuestionGroup[] = [] - let currentGroup: QuestionGroup = { section: null, questions: [] } - let questionIndex = 1 - - for (const question of validQuestions.value) { - if (question.type === 'section') { - if (currentGroup.section || currentGroup.questions.length > 0) { - groups.push(currentGroup) - } - currentGroup = { - section: { question, displayIndex: questionIndex }, - questions: [], - } - } else { - currentGroup.questions.push({ - question, - displayIndex: questionIndex, - }) - } - questionIndex++ - } - - if (currentGroup.section || currentGroup.questions.length > 0) { - groups.push(currentGroup) - } - - return groups - }) - const validQuestionsIds = computed>(() => { return new Set(validQuestions.value.map((question) => question.id)) }) @@ -583,6 +540,20 @@ export default defineComponent({ ) }) + const questionIdsInSection = computed>(() => { + const firstSection = shownQuestions.value.findIndex( + (question) => question.type === 'section', + ) + return new Set( + firstSection === -1 + ? [] + : shownQuestions.value + .slice(firstSection) + .filter((question) => question.type !== 'section') + .map((question) => question.id), + ) + }) + const submittedAnswers = computed(() => { return Object.fromEntries( Object.entries(answers.value).filter(([questionId]) => @@ -1714,13 +1685,13 @@ export default defineComponent({ getAnswerError, jumpAnnouncement, shownQuestions, + questionIdsInSection, confirmClearFormButtons, confirmEmptyModalButtons, confirmLeaveFormButtons, expirationMessage, openingMessage, formElement, - groupedQuestions, hasAnswers, infoMessage, isArchived, From 7501210ec45b259fa6ce4268abf757108a9c0dfc Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Mon, 23 Nov 2026 21:18:09 +0100 Subject: [PATCH 3/7] feat(section): number sections apart from the questions Sections count on their own and no longer take a question number, so the questions keep the numbers respondents expect. The submit view numbers the questions along the respondent's path, jump targets list a later section as "Section N" and the questions by their own number, and copying a section is left out until duplicating sections is designed. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- src/components/Questions/Question.vue | 58 +++++++++++----- src/composables/useJumps.ts | 65 ++++++++++++------ src/composables/useQuestion.ts | 19 ++---- src/utils/Sections.ts | 89 ++++++++++++++++++++++++ src/views/Create.vue | 97 ++++++++++++++++++++------- src/views/Submit.vue | 58 ++++++++++------ 6 files changed, 288 insertions(+), 98 deletions(-) create mode 100644 src/utils/Sections.ts diff --git a/src/components/Questions/Question.vue b/src/components/Questions/Question.vue index c02db46af..ba824feb0 100644 --- a/src/components/Questions/Question.vue +++ b/src/components/Questions/Question.vue @@ -10,7 +10,14 @@ 'question--editable': !readOnly, 'question--section': readOnly && isSection, }" - :aria-label="t('forms', 'Question number {index}', { index })"> + :aria-label=" + isSection + ? readOnly + ? undefined + : t('forms', 'Section number {index}', { index }) + : t('forms', 'Question number {index}', { index }) + " + :aria-labelledby="isSection && readOnly && titled ? titleId : undefined">
    @@ -85,7 +96,7 @@ forceMenu placement="bottom-end" class="question__header__title__menu"> -