Skip to content

Commit 62a3000

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 30aff16 commit 62a3000

6 files changed

Lines changed: 43 additions & 17 deletions

File tree

docs/DataStructure.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,19 +164,21 @@ A submission-object describes a single submission by a user to a form.
164164

165165
The actual answers of users on submission.
166166

167-
| Property | Type | Restrictions | Description |
168-
| ------------ | ------- | ------------- | ----------------------------------------------- |
169-
| id | Integer | unique | An instance-wide unique id of the submission |
170-
| submissionId | Integer | | The id of the submission, the answer belongs to |
171-
| questionId | Integer | | The id of the question, the answer belongs to |
172-
| text | String | max. 4096 ch. | The actual answer text, the user submitted |
173-
174-
```
167+
| Property | Type | Restrictions | Description |
168+
| ------------ | ------- | ------------- | ---------------------------------------------------- |
169+
| id | Integer | unique | An instance-wide unique id of the submission |
170+
| submissionId | Integer | | The id of the submission, the answer belongs to |
171+
| questionId | Integer | | The id of the question, the answer belongs to |
172+
| questionName | String | | The technical name that was assigned to the question |
173+
| text | String | max. 4096 ch. | The actual answer text, the user submitted |
174+
175+
```json
175176
{
176-
"id": 5,
177-
"submissionId": 5,
178-
"questionId": 1,
179-
"text": "Option 2"
177+
"id": 5,
178+
"submissionId": 5,
179+
"questionId": 1,
180+
"questionName": "preference",
181+
"text": "Option 2"
180182
}
181183
```
182184

lib/Controller/ApiController.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,24 @@ public function getSubmissions(int $formId, ?string $query = null, ?int $limit =
11721172
$submissions = $this->submissionService->getSubmissions($formId, $userId, $query, $limit, $offset);
11731173
$filteredSubmissionsCount = $this->submissionMapper->countSubmissions($formId, $userId, $query);
11741174
}
1175-
$questions = $this->formsService->getQuestions($formId);
1175+
$questions = [];
1176+
foreach ($this->formsService->getQuestions($formId) as $question) {
1177+
$questions[$question['id']] = $question;
1178+
}
1179+
11761180

11771181
// Append Display Names
1178-
$submissions = array_map(function (array $submission) {
1182+
$submissions = array_map(function (array $submission) use ($questions) {
1183+
if (!empty($submission['answers'])) {
1184+
$submission['answers'] = array_map(function (array $answer) use ($questions) {
1185+
$name = $questions[$answer['questionId']]['name'];
1186+
if ($name) {
1187+
$answer['questionName'] = $name;
1188+
}
1189+
return $answer;
1190+
}, $submission['answers']);
1191+
}
1192+
11791193
if (substr($submission['userId'], 0, 10) === 'anon-user-') {
11801194
// Anonymous User
11811195
// TRANSLATORS On Results when listing the single Responses to the form, this text is shown as heading of the Response.
@@ -1202,7 +1216,7 @@ public function getSubmissions(int $formId, ?string $query = null, ?int $limit =
12021216

12031217
$response = [
12041218
'submissions' => $submissions,
1205-
'questions' => $questions,
1219+
'questions' => array_values($questions),
12061220
'filteredSubmissionsCount' => $filteredSubmissionsCount,
12071221
];
12081222

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
*

openapi.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
"type": "integer",
5959
"format": "int64"
6060
},
61+
"questionName": {
62+
"type": "string"
63+
},
6164
"text": {
6265
"type": "string"
6366
}

tests/Integration/Api/ApiV3Test.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,7 @@ public function dataGetSubmissions() {
11171117
// 'questionId' => Checked dynamically
11181118
'text' => 'Option 1',
11191119
'fileId' => null,
1120+
'questionName' => 'city',
11201121
]
11211122
]
11221123
],
@@ -1137,6 +1138,7 @@ public function dataGetSubmissions() {
11371138
// 'questionId' => Checked dynamically
11381139
'text' => 'Option 2',
11391140
'fileId' => null,
1141+
'questionName' => 'city'
11401142
]
11411143
]
11421144
],
@@ -1384,10 +1386,12 @@ public function testNewSubmission() {
13841386
'questionId' => $this->testForms[0]['questions'][1]['id'],
13851387
'text' => 'Option 1',
13861388
'fileId' => null,
1389+
'questionName' => 'city',
13871390
],
13881391
[
13891392
'questionId' => $this->testForms[0]['questions'][2]['id'],
13901393
'text' => 'test.txt',
1394+
'questionName' => 'file',
13911395
],
13921396
]
13931397
], $data['submissions'][0]);

tests/Unit/Controller/ApiControllerTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public function dataGetSubmissions() {
223223
'submissions' => [
224224
['userId' => 'anon-user-1']
225225
],
226-
'questions' => [['name' => 'questions']],
226+
'questions' => [['id' => 1, 'name' => 'questions']],
227227
'expected' => [
228228
'submissions' => [
229229
[
@@ -233,6 +233,7 @@ public function dataGetSubmissions() {
233233
],
234234
'questions' => [
235235
[
236+
'id' => 1,
236237
'name' => 'questions',
237238
'extraSettings' => new \stdClass(),
238239
],
@@ -244,7 +245,7 @@ public function dataGetSubmissions() {
244245
'submissions' => [
245246
['userId' => 'jdoe']
246247
],
247-
'questions' => [['name' => 'questions']],
248+
'questions' => [['id' => 1, 'name' => 'questions']],
248249
'expected' => [
249250
'submissions' => [
250251
[
@@ -254,6 +255,7 @@ public function dataGetSubmissions() {
254255
],
255256
'questions' => [
256257
[
258+
'id' => 1,
257259
'name' => 'questions',
258260
'extraSettings' => new \stdClass(),
259261
],

0 commit comments

Comments
 (0)