Skip to content

Commit 3c26309

Browse files
committed
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 32e1fec commit 3c26309

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
@@ -154,18 +154,20 @@ A submission-object describes a single submission by a user to a form.
154154

155155
The actual answers of users on submission.
156156

157-
| Property | Type | Restrictions | Description |
158-
| ------------ | ------- | ------------- | ----------------------------------------------- |
159-
| id | Integer | unique | An instance-wide unique id of the submission |
160-
| submissionId | Integer | | The id of the submission, the answer belongs to |
161-
| questionId | Integer | | The id of the question, the answer belongs to |
162-
| text | String | max. 4096 ch. | The actual answer text, the user submitted |
163-
164-
```
157+
| Property | Type | Restrictions | Description |
158+
| ------------ | ------- | ------------- | ---------------------------------------------------- |
159+
| id | Integer | unique | An instance-wide unique id of the submission |
160+
| submissionId | Integer | | The id of the submission, the answer belongs to |
161+
| questionId | Integer | | The id of the question, the answer belongs to |
162+
| questionName | String | | The technical name that was assigned to the question |
163+
| text | String | max. 4096 ch. | The actual answer text, the user submitted |
164+
165+
```json
165166
{
166167
"id": 5,
167168
"submissionId": 5,
168169
"questionId": 1,
170+
"questionName": "preference",
169171
"text": "Option 2"
170172
}
171173
```

lib/Controller/ApiController.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,10 +1159,24 @@ public function getSubmissions(int $formId, ?string $fileFormat = null): DataRes
11591159

11601160
// Load submissions and currently active questions
11611161
$submissions = $this->submissionService->getSubmissions($formId);
1162-
$questions = $this->formsService->getQuestions($formId);
1162+
$questions = [];
1163+
foreach($this->formsService->getQuestions($formId) as $question) {
1164+
$questions[$question['id']] = $question;
1165+
}
1166+
11631167

11641168
// Append Display Names
1165-
$submissions = array_map(function (array $submission) {
1169+
$submissions = array_map(function (array $submission) use ($questions) {
1170+
if (!empty($submission['answers'])) {
1171+
$submission['answers'] = array_map(function (array $answer) use ($questions) {
1172+
$name = $questions[$answer['questionId']]['name'];
1173+
if ($name) {
1174+
$answer['questionName'] = $name;
1175+
}
1176+
return $answer;
1177+
}, $submission['answers']);
1178+
}
1179+
11661180
if (substr($submission['userId'], 0, 10) === 'anon-user-') {
11671181
// Anonymous User
11681182
// TRANSLATORS On Results when listing the single Responses to the form, this text is shown as heading of the Response.
@@ -1189,7 +1203,7 @@ public function getSubmissions(int $formId, ?string $fileFormat = null): DataRes
11891203

11901204
$response = [
11911205
'submissions' => $submissions,
1192-
'questions' => $questions,
1206+
'questions' => array_values($questions),
11931207
];
11941208

11951209
return new DataResponse($response);

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)