diff --git a/docs/DataStructure.md b/docs/DataStructure.md index b2e6169cc..ae005520a 100644 --- a/docs/DataStructure.md +++ b/docs/DataStructure.md @@ -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" } ``` diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 15d9e9d50..a14d6481b 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -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. @@ -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); diff --git a/lib/ResponseDefinitions.php b/lib/ResponseDefinitions.php index 2b0fcffdf..ede002d29 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -63,6 +63,7 @@ * submissionId: int, * fileId: ?int, * questionId: int, + * questionName?: string, * text: string * } *