Skip to content

Commit f4c9085

Browse files
susnuxChartman123
authored andcommitted
feat: add question name to form answer output
* Resolves #2700 This allows to identify question from within the UI, as the user can see the name assigned to a question to identify it when using the submissions in external places like workflows. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 2914a2e commit f4c9085

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

docs/DataStructure.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,20 @@ A submission-object describes a single submission by a user to a form.
160160

161161
The actual answers of users on submission.
162162

163-
| Property | Type | Restrictions | Description |
164-
| ------------ | ------- | ------------- | ----------------------------------------------- |
165-
| id | Integer | unique | An instance-wide unique id of the submission |
166-
| submissionId | Integer | | The id of the submission, the answer belongs to |
167-
| questionId | Integer | | The id of the question, the answer belongs to |
168-
| text | String | max. 4096 ch. | The actual answer text, the user submitted |
169-
170-
```
163+
| Property | Type | Restrictions | Description |
164+
| ------------ | ------- | ------------- | ---------------------------------------------------- |
165+
| id | Integer | unique | An instance-wide unique id of the submission |
166+
| submissionId | Integer | | The id of the submission, the answer belongs to |
167+
| questionId | Integer | | The id of the question, the answer belongs to |
168+
| questionName | String | | The technical name that was assigned to the question |
169+
| text | String | max. 4096 ch. | The actual answer text, the user submitted |
170+
171+
```json
171172
{
172173
"id": 5,
173174
"submissionId": 5,
174175
"questionId": 1,
176+
"questionName": "preference",
175177
"text": "Option 2"
176178
}
177179
```

lib/Controller/ApiController.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,10 +1182,24 @@ public function getSubmissions(int $formId, ?string $query = null, ?int $limit =
11821182
$submissions = $this->submissionService->getSubmissions($formId, $userId, $query, $limit, $offset);
11831183
$filteredSubmissionsCount = $this->submissionMapper->countSubmissions($formId, $userId, $query);
11841184
}
1185-
$questions = $this->formsService->getQuestions($formId);
1185+
$questions = [];
1186+
foreach($this->formsService->getQuestions($formId) as $question) {
1187+
$questions[$question['id']] = $question;
1188+
}
1189+
11861190

11871191
// Append Display Names
1188-
$submissions = array_map(function (array $submission) {
1192+
$submissions = array_map(function (array $submission) use ($questions) {
1193+
if (!empty($submission['answers'])) {
1194+
$submission['answers'] = array_map(function (array $answer) use ($questions) {
1195+
$name = $questions[$answer['questionId']]['name'];
1196+
if ($name) {
1197+
$answer['questionName'] = $name;
1198+
}
1199+
return $answer;
1200+
}, $submission['answers']);
1201+
}
1202+
11891203
if (substr($submission['userId'], 0, 10) === 'anon-user-') {
11901204
// Anonymous User
11911205
// TRANSLATORS On Results when listing the single Responses to the form, this text is shown as heading of the Response.
@@ -1212,7 +1226,7 @@ public function getSubmissions(int $formId, ?string $query = null, ?int $limit =
12121226

12131227
$response = [
12141228
'submissions' => $submissions,
1215-
'questions' => $questions,
1229+
'questions' => array_values($questions),
12161230
'filteredSubmissionsCount' => $filteredSubmissionsCount,
12171231
];
12181232

lib/ResponseDefinitions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
* submissionId: int,
6464
* fileId: ?int,
6565
* questionId: int,
66+
* questionName?: string,
6667
* text: string
6768
* }
6869
*

0 commit comments

Comments
 (0)