Skip to content

Commit e684174

Browse files
feat(section): keep every question in a section
Once a form has a section, every question belongs to one. Questions before the first section were an untitled lead-in that the editor could neither frame nor handle like a section. The editor creates the first section, with "Untitled section" filled in as its title, when a section is added below existing questions: a section inserted between questions splits them into two sections, one appended at the end follows a section holding all questions before it. The server rejects new, copied and reordered questions that would add to the questions before the first section, so API clients cannot bring them back. Deleting the first section moves the next section up to its place, so its questions join that section; jumps that become backward by this are removed and reported in X-Forms-Adjusted. Forms stored with questions before the first section keep them, and requests that do not add to them keep working. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent c31989f commit e684174

10 files changed

Lines changed: 545 additions & 17 deletions

File tree

‎docs/API_v3.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Some requests succeed with their usual status, and their payload keeps its shape
3737

3838
| Key | Value | Set by | Meaning |
3939
|-----|-------|--------|---------|
40-
| `removed-jumps` | Inner list of question ids, e.g. `(3 7)` | Updating, reordering and copying questions; deleting options; changing the bounds of a linear scale | Jumps to the question itself or to an earlier question, jumps of the deleted option or of options that are not options of the question, or jumps for values outside the scale, were removed from the `extraSettings` of these questions, see [Jumps](DataStructure.md#jumps) |
40+
| `removed-jumps` | Inner list of question ids, e.g. `(3 7)` | Updating, reordering and copying questions; deleting the first section of a form; deleting options; changing the bounds of a linear scale | Jumps to the question itself or to an earlier question, jumps of the deleted option or of options that are not options of the question, or jumps for values outside the scale, were removed from the `extraSettings` of these questions, see [Jumps](DataStructure.md#jumps) |
4141

4242
```
4343
X-Forms-Adjusted: removed-jumps=(3 7)
@@ -395,7 +395,7 @@ Returns the questions and options of the given form (without submissions).
395395
| _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. |
396396
| _text_ | String | yes | _Optional_ The text of the new question. |
397397
| _position_ | Integer | yes | _(optional)_ 1-based position to insert the new question. When omitted the question is appended at the end. |
398-
- 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**.
398+
- 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**. In a form with sections every question is in a section (see [Sections](DataStructure.md#sections)): a question or section that would add to the questions before the first section, for example a section appended below the questions of a form without sections, is rejected with **400 Bad Request** "Every question must be in a section".
399399
- Response: The new question object.
400400

401401
```
@@ -494,7 +494,7 @@ Reorders all Questions of a single form
494494
| Parameter | Type | Description |
495495
|-----------|---------|-------------|
496496
| _newOrder_ | Array | Array of **all** Question-IDs, ordered in the desired order |
497-
- Restrictions: The Array **must** contain all Question-IDs corresponding to the specified form and **must not** contain any duplicates.
497+
- Restrictions: The Array **must** contain all Question-IDs corresponding to the specified form and **must not** contain any duplicates. An order that puts more questions before the first section than before is rejected with **400 Bad Request** "Every question must be in a section" (see [Sections](DataStructure.md#sections)).
498498
- Response: Array of questionIDs and their corresponding order. Jumps that would go to the question itself or to an earlier question are removed (see [Jumps](DataStructure.md#jumps)): the questions concerned also carry their new `extraSettings`, and the response header `X-Forms-Adjusted` then lists their ids, e.g. `X-Forms-Adjusted: removed-jumps=(3 7)`, or holds `removed-jumps` alone for more than 100 questions (see [Adjustments by the server](#adjustments-by-the-server)).
499499
An id of a deleted question gets a **Not Found** response.
500500

@@ -521,7 +521,7 @@ Reorders all Questions of a single form
521521
|-----------|---------|-------------|
522522
| _formId_ | Integer | ID of the form containing the question |
523523
| _questionId_ | Integer | ID of the question to delete |
524-
- Response: **Status-Code OK**, as well as the id of the deleted question.
524+
- Response: **Status-Code OK**, as well as the id of the deleted question. Deleting the first section moves the next section up to its place, so its questions stay in a section (see [Sections](DataStructure.md#sections)); jumps that become backward by this are removed, and the response header `X-Forms-Adjusted` then lists their questions, e.g. `X-Forms-Adjusted: removed-jumps=(3 7)` (see [Adjustments by the server](#adjustments-by-the-server)).
525525

526526
```
527527
"data": 4

‎docs/DataStructure.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ Currently supported Question-Types are:
242242
| `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). |
243243
| `section` | A structural element to group questions into sections. It cannot be answered and has no options. |
244244

245+
### Sections
246+
247+
Once a form has a section, every question is in a section: the form starts with a section, and each question belongs to the section before it.
248+
249+
- A request that would put more questions before the first section is rejected, see [Create a new question](API_v3.md#create-a-new-question) and [Reorder questions](API_v3.md#reorder-questions). To add the first section below existing questions, first insert a section at position 1, so that it holds them. The editor does this by itself, with the title "Untitled section" filled in; the title can be changed or cleared like any other.
250+
- Deleting the first section moves the next section up to its place, so the questions of the deleted section join the next section. Deleting any other section lets its questions join the section before it. Deleting the only section leaves a form without sections.
251+
- A form whose questions were stored before the first section by an earlier version keeps them there, and requests that do not add to them keep working.
252+
245253
## Extra Settings
246254

247255
Optional extra settings for some [Question Types](#question-types)

‎lib/Controller/ApiController.php‎

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ public function getQuestion(int $formId, int $questionId): DataResponse {
551551
* @return DataResponse<Http::STATUS_CREATED, FormsQuestion, array{X-Forms-Adjusted?: string, Access-Control-Expose-Headers?: string}>
552552
* @throws OCSBadRequestException Invalid type
553553
* @throws OCSBadRequestException Invalid subtype
554+
* @throws OCSBadRequestException Every question must be in a section
554555
* @throws OCSBadRequestException Datetime question type no longer supported
555556
* @throws OCSForbiddenException User has no permissions to get this form
556557
* @throws OCSForbiddenException This form is archived and can not be modified
@@ -605,6 +606,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
605606

606607
// Retrieve all active questions sorted by Order.
607608
$questions = $this->questionMapper->findByForm($formId);
609+
self::checkInsertKeepsSections($questions, (string)$type, $position);
608610

609611
if ($position !== null) {
610612
$position = $this->shiftQuestionsForInsert($questions, $position);
@@ -655,6 +657,7 @@ public function newQuestion(int $formId, ?string $type = null, ?string $subtype
655657
}
656658

657659
$allQuestions = $this->questionMapper->findByForm($formId);
660+
self::checkInsertKeepsSections($allQuestions, $sourceQuestion->getType(), $position);
658661

659662
$questionData = $sourceQuestion->read();
660663
unset($questionData['id']);
@@ -833,9 +836,11 @@ public function updateQuestion(int $formId, int $questionId, array $keyValuePair
833836
/**
834837
* Delete a question
835838
*
839+
* Deleting the first section moves the next section up to its place, so its questions stay in a section; jumps that become backward are removed, and the header `X-Forms-Adjusted` then holds `removed-jumps` with the ids of their questions
840+
*
836841
* @param int $formId the form id
837842
* @param int $questionId the question id
838-
* @return DataResponse<Http::STATUS_OK, int, array{}>
843+
* @return DataResponse<Http::STATUS_OK, int, array{X-Forms-Adjusted?: string, Access-Control-Expose-Headers?: string}>
839844
* @throws OCSBadRequestException Question doesn\'t belong to given Form
840845
* @throws OCSForbiddenException This form is archived and can not be modified
841846
* @throws OCSForbiddenException User has no permissions to get this form
@@ -874,6 +879,7 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
874879

875880
// Store Order of deleted Question
876881
$deletedOrder = $question->getOrder();
882+
$deletedType = $question->getType();
877883

878884
// Mark question as deleted
879885
$question->setOrder(0);
@@ -897,13 +903,15 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
897903
}
898904
}
899905

906+
$headers = $deletedType === Constants::ANSWER_TYPE_SECTION ? $this->moveNextSectionUp($formQuestions, $deletedOrder) : [];
907+
900908
if ($form->getConfirmationEmailQuestionId() === $questionId) {
901909
$form->setConfirmationEmailQuestionId(null);
902910
}
903911

904912
$this->formMapper->update($form);
905913

906-
return new DataResponse($questionId);
914+
return new DataResponse($questionId, Http::STATUS_OK, $headers);
907915
}
908916

909917
/**
@@ -916,6 +924,7 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
916924
* @return DataResponse<Http::STATUS_OK, array<string, FormsQuestionOrder>, array{X-Forms-Adjusted?: string, Access-Control-Expose-Headers?: string}>
917925
* @throws OCSBadRequestException The given array contains duplicates
918926
* @throws OCSBadRequestException The length of the given array does not match the number of stored questions
927+
* @throws OCSBadRequestException Every question must be in a section
919928
* @throws OCSBadRequestException Question doesn't belong to given Form
920929
* @throws OCSForbiddenException This form is archived and can not be modified
921930
* @throws OCSForbiddenException User has no permissions to get this form
@@ -954,6 +963,7 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
954963
$this->logger->debug('The length of the given array does not match the number of stored questions');
955964
throw new OCSBadRequestException('The length of the given array does not match the number of stored questions');
956965
}
966+
$typesBefore = array_map(static fn (Question $question) => $question->getType(), $questions);
957967

958968
$questions = []; // Clear Array of Entities
959969
$response = []; // Array of ['questionId' => ['order' => newOrder]]
@@ -985,6 +995,8 @@ public function reorderQuestions(int $formId, array $newOrder): DataResponse {
985995
}
986996
}
987997

998+
self::checkQuestionsStayInSections($typesBefore, array_map(static fn (Question $question) => $question->getType(), $questions));
999+
9881000
$changedQuestions = JumpHelper::removeBackwardJumps($questions);
9891001

9901002
// Write to Database
@@ -2342,6 +2354,63 @@ private function logFormUpdate(Form $form, array $previousAccess, ?int $previous
23422354
}
23432355
}
23442356

2357+
/**
2358+
* Move the next section up to the place of a deleted section that was the first section, so the questions of the deleted section join the next section; jumps that become backward are removed
2359+
*
2360+
* @param list<Question> $questions the remaining questions of the form in their order
2361+
* @param int $order the order of the deleted section
2362+
* @return array{X-Forms-Adjusted?: string, Access-Control-Expose-Headers?: string} the header listing the questions whose jumps were removed
2363+
*/
2364+
private function moveNextSectionUp(array $questions, int $order): array {
2365+
$first = FormsService::countQuestionsOutsideSections(array_map(static fn (Question $question) => $question->getType(), $questions));
2366+
if ($first <= $order - 1) {
2367+
return [];
2368+
}
2369+
array_splice($questions, $order - 1, 0, array_splice($questions, $first, 1));
2370+
$moved = [];
2371+
foreach ($questions as $index => $question) {
2372+
if ($question->getOrder() !== $index + 1) {
2373+
$question->setOrder($index + 1);
2374+
$moved[] = $question;
2375+
}
2376+
}
2377+
$changed = JumpHelper::removeBackwardJumps($questions);
2378+
foreach ($questions as $question) {
2379+
if (in_array($question, $moved, true) || in_array($question, $changed, true)) {
2380+
$this->questionMapper->update($question);
2381+
}
2382+
}
2383+
return self::getRemovedJumpsHeaders(array_map(static fn (Question $question): int => $question->getId(), $changed));
2384+
}
2385+
2386+
/**
2387+
* Throw when inserting a question would put more questions outside any section
2388+
*
2389+
* @param list<Question> $questions the questions of the form in their order
2390+
* @param string $type the type of the new question
2391+
* @param ?int $position the 1-based position of the new question, null to append it
2392+
* @throws OCSBadRequestException Every question must be in a section
2393+
*/
2394+
private static function checkInsertKeepsSections(array $questions, string $type, ?int $position): void {
2395+
$types = array_map(static fn (Question $question) => $question->getType(), $questions);
2396+
$after = $types;
2397+
array_splice($after, $position === null ? count($types) : max(0, min($position - 1, count($types))), 0, [$type]);
2398+
self::checkQuestionsStayInSections($types, $after);
2399+
}
2400+
2401+
/**
2402+
* Throw when a change puts more questions before the first section than before the change
2403+
*
2404+
* @param list<string> $before the question types of the form in their order before the change
2405+
* @param list<string> $after the question types of the form in their order after the change
2406+
* @throws OCSBadRequestException Every question must be in a section
2407+
*/
2408+
private static function checkQuestionsStayInSections(array $before, array $after): void {
2409+
if (FormsService::countQuestionsOutsideSections($after) > FormsService::countQuestionsOutsideSections($before)) {
2410+
throw new OCSBadRequestException('Every question must be in a section');
2411+
}
2412+
}
2413+
23452414
/**
23462415
* Shift existing question orders to make room for an insertion at $position.
23472416
* Normalizes the given $position to the valid range and updates all questions

‎lib/Service/FormsService.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,17 @@ public function removeJumpsOfMissingOptions(Question $question): bool {
11471147
return true;
11481148
}
11491149

1150+
/**
1151+
* The number of questions before the first section, 0 for a form without sections
1152+
*
1153+
* @param list<string> $types the question types of the form in their order
1154+
* @return int the number of questions outside any section
1155+
*/
1156+
public static function countQuestionsOutsideSections(array $types): int {
1157+
$first = array_search(Constants::ANSWER_TYPE_SECTION, $types, true);
1158+
return $first === false ? 0 : $first;
1159+
}
1160+
11501161
/**
11511162
* Get list of forms
11521163
*

‎openapi.json‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,7 @@
26222622
"delete": {
26232623
"operationId": "api-delete-question",
26242624
"summary": "Delete a question",
2625-
"description": "This endpoint allows CORS requests",
2625+
"description": "Deleting the first section moves the next section up to its place, so its questions stay in a section; jumps that become backward are removed, and the header `X-Forms-Adjusted` then holds `removed-jumps` with the ids of their questions\nThis endpoint allows CORS requests",
26262626
"tags": [
26272627
"api"
26282628
],
@@ -2666,6 +2666,18 @@
26662666
"responses": {
26672667
"200": {
26682668
"description": "the id of the deleted question",
2669+
"headers": {
2670+
"X-Forms-Adjusted": {
2671+
"schema": {
2672+
"type": "string"
2673+
}
2674+
},
2675+
"Access-Control-Expose-Headers": {
2676+
"schema": {
2677+
"type": "string"
2678+
}
2679+
}
2680+
},
26692681
"content": {
26702682
"application/json": {
26712683
"schema": {

0 commit comments

Comments
 (0)