Skip to content

feat: add question name to form answer output #2723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions docs/DataStructure.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,20 @@ A submission-object describes a single submission by a user to a form.

The actual answers of users on submission.

| Property | Type | Restrictions | Description |
| ------------ | ------- | ------------- | ----------------------------------------------- |
| id | Integer | unique | An instance-wide unique id of the submission |
| submissionId | Integer | | The id of the submission, the answer belongs to |
| questionId | Integer | | The id of the question, the answer belongs to |
| text | String | max. 4096 ch. | The actual answer text, the user submitted |

```
| Property | Type | Restrictions | Description |
| ------------ | ------- | ------------- | ---------------------------------------------------- |
| id | Integer | unique | An instance-wide unique id of the submission |
| submissionId | Integer | | The id of the submission, the answer belongs to |
| questionId | Integer | | The id of the question, the answer belongs to |
| questionName | String | | The technical name that was assigned to the question |
| text | String | max. 4096 ch. | The actual answer text, the user submitted |

```json
{
"id": 5,
"submissionId": 5,
"questionId": 1,
"questionName": "preference",
"text": "Option 2"
}
```
Expand Down
20 changes: 17 additions & 3 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,10 +1159,24 @@ public function getSubmissions(int $formId, ?string $fileFormat = null): DataRes

// Load submissions and currently active questions
$submissions = $this->submissionService->getSubmissions($formId);
$questions = $this->formsService->getQuestions($formId);
$questions = [];
foreach($this->formsService->getQuestions($formId) as $question) {
$questions[$question['id']] = $question;
}


// Append Display Names
$submissions = array_map(function (array $submission) {
$submissions = array_map(function (array $submission) use ($questions) {
if (!empty($submission['answers'])) {
$submission['answers'] = array_map(function (array $answer) use ($questions) {
$name = $questions[$answer['questionId']]['name'];
if ($name) {
$answer['questionName'] = $name;
}
return $answer;
}, $submission['answers']);
}

if (substr($submission['userId'], 0, 10) === 'anon-user-') {
// Anonymous User
// TRANSLATORS On Results when listing the single Responses to the form, this text is shown as heading of the Response.
Expand All @@ -1189,7 +1203,7 @@ public function getSubmissions(int $formId, ?string $fileFormat = null): DataRes

$response = [
'submissions' => $submissions,
'questions' => $questions,
'questions' => array_values($questions),
];

return new DataResponse($response);
Expand Down
1 change: 1 addition & 0 deletions lib/ResponseDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
* submissionId: int,
* fileId: ?int,
* questionId: int,
* questionName?: string,
* text: string
* }
*
Expand Down
Loading