Skip to content

Commit 3c532ee

Browse files
committed
FORMS-984: Added rest webform endpoint for getting all submissions
1 parent f84e10f commit 3c532ee

File tree

3 files changed

+176
-1
lines changed

3 files changed

+176
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ about writing changes to this log.
88

99
## [Unreleased]
1010

11+
- Added endpoint for getting all submissions on a webform.
12+
1113
## [1.1.0]
1214

1315
### Added
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_rest_api\Plugin\rest\resource;
4+
5+
use Drupal\Core\Url;
6+
use Drupal\rest\ModifiedResourceResponse;
7+
use Drupal\rest\Plugin\ResourceBase;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
9+
use Symfony\Component\HttpFoundation\Request;
10+
11+
/**
12+
* Creates a resource for retrieving webform submission data and fields.
13+
*
14+
* @RestResource(
15+
* id = "webform_rest_all_form_submissions",
16+
* label = @Translation("Webform - All submissions for a form"),
17+
* uri_paths = {
18+
* "canonical" = "/webform_rest/{webform_id}/all"
19+
* }
20+
* )
21+
*/
22+
class WebformAllFormSubmissions extends ResourceBase {
23+
24+
/**
25+
* The current request.
26+
*
27+
* @var \Symfony\Component\HttpFoundation\Request
28+
*/
29+
private $currentRequest;
30+
31+
/**
32+
* The entity type manager object.
33+
*
34+
* @var \Drupal\Core\Entity\EntityTypeManager
35+
*/
36+
private $entityTypeManager;
37+
38+
/**
39+
* The webform helper.
40+
*
41+
* @var \Drupal\os2forms_rest_api\WebformHelper
42+
*/
43+
private $webformHelper;
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
49+
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
50+
51+
$instance->entityTypeManager = $container->get('entity_type.manager');
52+
$instance->setCurrentRequest($container->get('request_stack')->getCurrentRequest());
53+
$instance->webformHelper = $container->get('Drupal\os2forms_rest_api\WebformHelper');
54+
55+
return $instance;
56+
}
57+
58+
/**
59+
* Sets the current request.
60+
*
61+
* @param \Symfony\Component\HttpFoundation\Request $current_request
62+
* The current request.
63+
*
64+
* @return $this
65+
* Class.
66+
*/
67+
protected function setCurrentRequest(Request $current_request) {
68+
$this->currentRequest = $current_request;
69+
return $this;
70+
}
71+
72+
/**
73+
* Retrieve all submissions for a given webform id.
74+
*
75+
* @param string $webform_id
76+
* Webform ID.
77+
*
78+
* @return \Drupal\rest\ModifiedResourceResponse
79+
* HTTP response object containing webform submissions.
80+
*
81+
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
82+
* Throws HttpException in case of error.
83+
*/
84+
public function get(string $webform_id): ModifiedResourceResponse {
85+
if (empty($webform_id)) {
86+
$errors = [
87+
'error' => [
88+
'message' => 'Webform ID is required.',
89+
],
90+
];
91+
return new ModifiedResourceResponse($errors, 400);
92+
}
93+
94+
// Webform access check.
95+
$webform = $this->webformHelper->getWebform($webform_id);
96+
97+
if (NULL === $webform) {
98+
$errors = [
99+
'error' => [
100+
'message' => $this->t('Could not find webform with id :webform_id', [':webform_id' => $webform_id]),
101+
],
102+
];
103+
return new ModifiedResourceResponse($errors, 400);
104+
}
105+
106+
if (!$this->webformHelper->hasWebformAccess($webform, $this->webformHelper->getCurrentUser())) {
107+
$errors = [
108+
'error' => [
109+
'message' => $this->t('Access denied'),
110+
],
111+
];
112+
return new ModifiedResourceResponse($errors, 401);
113+
}
114+
115+
$submissionData = [];
116+
117+
$result = ['webform_id' => $webform_id];
118+
119+
// Query for webform submissions with this webform_id.
120+
$query = $this->entityTypeManager->getStorage('webform_submission')->getQuery()
121+
->condition('webform_id', $webform_id);
122+
123+
$startTimestamp = $this->currentRequest->query->get('starttime');
124+
if (is_numeric($startTimestamp)) {
125+
$query->condition('created', $startTimestamp, '>=');
126+
$result['starttime'] = $startTimestamp;
127+
}
128+
129+
$endTimestamp = $this->currentRequest->query->get('endtime');
130+
if (is_numeric($endTimestamp)) {
131+
$query->condition('created', $endTimestamp, '<=');
132+
$result['endtime'] = $endTimestamp;
133+
}
134+
135+
$query->accessCheck(FALSE);
136+
$sids = $query->execute();
137+
138+
foreach ($sids as $sid) {
139+
/** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
140+
$webform_submission = $this->entityTypeManager->getStorage('webform_submission')->load($sid);
141+
142+
$url = Url::fromRoute(
143+
'rest.webform_rest_submission.GET',
144+
[
145+
'webform_id' => $webform_id,
146+
'uuid' => $webform_submission->uuid(),
147+
],
148+
[
149+
'absolute' => TRUE,
150+
]
151+
)->toString();
152+
153+
$submissionData[$sid] = $url;
154+
}
155+
156+
$result['submissions'] = $submissionData;
157+
158+
return new ModifiedResourceResponse($result);
159+
}
160+
161+
}

src/WebformHelper.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterf
101101
'rest.webform_rest_elements.GET',
102102
'rest.webform_rest_fields.GET',
103103
'rest.webform_rest_submission.GET',
104+
'rest.webform_rest_all_form_submissions.GET',
104105
];
106+
105107
$requireUuid = static function ($route) {
106108
return in_array(
107109
$route,
@@ -278,7 +280,7 @@ private function loadUsers(array $spec): array {
278280
*
279281
* Note: This is only used to deny access to a file that is attached to a
280282
* webform (submission) that the user does not have permission to access.
281-
* Permission to access private files are handles elsewhere.
283+
* Permission to access private files are handled elsewhere.
282284
*
283285
* @phpstan-return int|array<string, string>|null
284286
*/
@@ -307,4 +309,14 @@ public function fileDownload(string $uri) {
307309
return NULL;
308310
}
309311

312+
/**
313+
* Return current user.
314+
*
315+
* @return \Drupal\Core\Session\AccountProxyInterface
316+
* The current user.
317+
*/
318+
public function getCurrentUser(): AccountProxyInterface {
319+
return $this->currentUser;
320+
}
321+
310322
}

0 commit comments

Comments
 (0)