Skip to content

Commit 593625b

Browse files
committed
update backend code so that it can load all submissions of a user
Signed-off-by: Christian Hartmann <[email protected]>
1 parent 82d0374 commit 593625b

9 files changed

+41
-69
lines changed

lib/Constants.php

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class Constants {
191191
// Define Form Permissions
192192
public const PERMISSION_EDIT = 'edit';
193193
public const PERMISSION_RESULTS = 'results';
194+
public const PERMISSION_RESULTS_OWN = 'results_own';
194195
public const PERMISSION_RESULTS_DELETE = 'results_delete';
195196
public const PERMISSION_SUBMIT = 'submit';
196197
/** Special internal permissions to allow embedding a form (share) into external websites */

lib/Controller/ApiController.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,11 @@ public function getSubmissions(int $formId, ?string $fileFormat = null): DataRes
11591159
}
11601160

11611161
// Load submissions and currently active questions
1162-
$submissions = $this->submissionService->getSubmissions($formId);
1162+
if (in_array(Constants::PERMISSION_RESULTS, $this->formsService->getPermissions($form))) {
1163+
$submissions = $this->submissionService->getSubmissions($formId);
1164+
} else {
1165+
$submissions = $this->submissionService->getSubmissions($formId, $this->currentUser->getUID());
1166+
}
11631167
$questions = $this->formsService->getQuestions($formId);
11641168

11651169
// Append Display Names
@@ -1338,7 +1342,7 @@ public function updateSubmission(int $formId, int $submissionId, array $answers,
13381342
$form = $this->loadFormForSubmission($formId, $shareHash);
13391343

13401344
if (!$form->getAllowEdit()) {
1341-
throw new OCSBadRequestException('Can only update if AllowEdit is set');
1345+
throw new OCSBadRequestException('Can only update if allowEdit is set');
13421346
}
13431347

13441348
$questions = $this->formsService->getQuestions($formId);
@@ -1353,12 +1357,12 @@ public function updateSubmission(int $formId, int $submissionId, array $answers,
13531357

13541358
// get existing submission of this user
13551359
try {
1356-
$submission = $this->submissionMapper->findByFormAndUser($form->getId(), $this->currentUser->getUID());
1360+
$submission = $this->submissionMapper->findById($form->getId());
13571361
} catch (DoesNotExistException $e) {
13581362
throw new OCSBadRequestException('Cannot update a non existing submission');
13591363
}
13601364

1361-
if ($submissionId != $submission->getId()) {
1365+
if ($this->currentUser->getUID() != $submission->getUserId()) {
13621366
throw new OCSForbiddenException('Can only update your own submissions');
13631367
}
13641368

lib/Db/SubmissionMapper.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ public function findByForm(int $formId): array {
5151
* @param int $formId
5252
* @param string $userId
5353
*
54-
* @return Submission
55-
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
54+
* @return Submission[]
5655
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
5756
*/
58-
public function findByFormAndUser(int $formId, string $userId): Submission {
57+
public function findByFormAndUser(int $formId, string $userId): array {
5958
$qb = $this->db->getQueryBuilder();
6059

6160
$qb->select('*')
@@ -67,7 +66,7 @@ public function findByFormAndUser(int $formId, string $userId): Submission {
6766
//Newest submissions first
6867
->orderBy('timestamp', 'DESC');
6968

70-
return $this->findEntity($qb);
69+
return $this->findEntities($qb);
7170
}
7271

7372
/**
@@ -109,10 +108,11 @@ public function hasFormSubmissionsByUser(Form $form, string $userId): bool {
109108
/**
110109
* Count submissions by form
111110
* @param int $formId ID of the form to count submissions
111+
* @param null|string $userId (optional) ID of the current user, defaults to `null`
112112
* @throws \Exception
113113
*/
114-
public function countSubmissions(int $formId): int {
115-
return $this->countSubmissionsWithFilters($formId, null, -1);
114+
public function countSubmissions(int $formId, ?string $userId = null): int {
115+
return $this->countSubmissionsWithFilters($formId, $userId, -1);
116116
}
117117

118118
/**

lib/Migration/Version050000Date20250109201500.php renamed to lib/Migration/Version050200Date20250109201500.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use OCP\Migration\IOutput;
1616
use OCP\Migration\SimpleMigrationStep;
1717

18-
class Version050000Date20250109201500 extends SimpleMigrationStep {
18+
class Version050200Date20250109201500 extends SimpleMigrationStep {
1919

2020
/**
2121
* @param IOutput $output

lib/ResponseDefinitions.php

-3
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@
123123
* shares: list<FormsShare>,
124124
* submissionCount?: int,
125125
* submissionMessage: ?string,
126-
* answers?: array<string,mixed>,
127-
* newSubmission?: bool,
128-
* submissionId?: int,
129126
* }
130127
*
131128
* @psalm-type FormsUploadedFile = array{

lib/Service/FormsService.php

+12-51
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use OCA\Forms\Activity\ActivityManager;
1111
use OCA\Forms\Constants;
12-
use OCA\Forms\Db\AnswerMapper;
1312
use OCA\Forms\Db\Form;
1413
use OCA\Forms\Db\FormMapper;
1514
use OCA\Forms\Db\OptionMapper;
@@ -23,7 +22,6 @@
2322
use OCA\Forms\ResponseDefinitions;
2423
use OCP\AppFramework\Db\DoesNotExistException;
2524
use OCP\AppFramework\Db\IMapperException;
26-
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
2725
use OCP\EventDispatcher\IEventDispatcher;
2826
use OCP\Files\IRootFolder;
2927
use OCP\Files\NotFoundException;
@@ -56,7 +54,6 @@ public function __construct(
5654
private QuestionMapper $questionMapper,
5755
private ShareMapper $shareMapper,
5856
private SubmissionMapper $submissionMapper,
59-
private AnswerMapper $answerMapper,
6057
private ConfigService $configService,
6158
private IGroupManager $groupManager,
6259
private IUserManager $userManager,
@@ -99,32 +96,6 @@ private function getOptions(int $questionId): array {
9996
}
10097
}
10198

102-
private function getAnswers(int $formId, int $submissionId, string $userId): array {
103-
104-
$answerList = [];
105-
$answerEntities = $this->answerMapper->findBySubmission($submissionId);
106-
foreach ($answerEntities as $answerEntity) {
107-
$answer = $answerEntity->read();
108-
$questionId = $answer['questionId'];
109-
if (!array_key_exists($questionId, $answerList)) {
110-
$answerList[$questionId] = [];
111-
}
112-
$options = $this->getOptions($answer['questionId']);
113-
if (!empty($options)) {
114-
// match option text to option index
115-
foreach ($options as $option) {
116-
if ($option['text'] == $answer['text']) {
117-
$answerList[$questionId][] = strval($option['id']);
118-
}
119-
}
120-
} else {
121-
// copy the text
122-
$answerList[$questionId][] = $answer['text'];
123-
}
124-
}
125-
return $answerList;
126-
}
127-
12899
/**
129100
* Load questions corresponding to form
130101
*
@@ -222,25 +193,6 @@ public function getShares(int $formId): array {
222193
public function getForm(Form $form): array {
223194
$result = $form->read();
224195
$result['questions'] = $this->getQuestions($form->getId());
225-
226-
// add previous submission if there is one by this user for this form
227-
if ($this->currentUser->getUID() && $form->getAllowEdit()) {
228-
$submissionEntity = null;
229-
try {
230-
$submissionEntity = $this->submissionMapper->findByFormAndUser($form->getId(), $this->currentUser->getUID());
231-
$answers = $this->getAnswers($form->getId(), $submissionEntity->getId(), $this->currentUser->getUID());
232-
if (!empty($answers)) {
233-
$result['answers'] = $answers;
234-
$result['newSubmission'] = false;
235-
$result['submissionId'] = $submissionEntity->getId();
236-
}
237-
} catch (DoesNotExistException $e) {
238-
//handle silently
239-
} catch (MultipleObjectsReturnedException $e) {
240-
//handle silently
241-
}
242-
}
243-
244196
$result['shares'] = $this->getShares($form->getId());
245197

246198
// Append permissions for current user.
@@ -251,6 +203,8 @@ public function getForm(Form $form): array {
251203
// Append submissionCount if currentUser has permissions to see results
252204
if (in_array(Constants::PERMISSION_RESULTS, $result['permissions'])) {
253205
$result['submissionCount'] = $this->submissionMapper->countSubmissions($form->getId());
206+
} elseif (in_array(Constants::PERMISSION_RESULTS_OWN, $result['permissions'])) {
207+
$result['submissionCount'] = $this->submissionMapper->countSubmissions($form->getId(), $this->currentUser->getUID());
254208
}
255209

256210
if ($result['fileId']) {
@@ -287,6 +241,8 @@ public function getPartialFormArray(Form $form): array {
287241
// Append submissionCount if currentUser has permissions to see results
288242
if (in_array(Constants::PERMISSION_RESULTS, $result['permissions'])) {
289243
$result['submissionCount'] = $this->submissionMapper->countSubmissions($form->getId());
244+
} elseif (in_array(Constants::PERMISSION_RESULTS_OWN, $result['permissions'])) {
245+
$result['submissionCount'] = $this->submissionMapper->countSubmissions($form->getId(), $this->currentUser->getUID());
290246
}
291247

292248
return $result;
@@ -339,10 +295,15 @@ public function getPermissions(Form $form): array {
339295
if (count($permissions) === 0) {
340296
$access = $form->getAccess();
341297
if ($access['permitAllUsers'] && $this->configService->getAllowPermitAll()) {
342-
$permissions = [Constants::PERMISSION_SUBMIT];
298+
$permissions[] = Constants::PERMISSION_SUBMIT;
343299
}
344300
}
345301

302+
// If allowEdit is set on the form, add the permission for own results
303+
if ($form->getAllowEdit() === true) {
304+
$permissions[] = Constants::PERMISSION_RESULTS_OWN;
305+
}
306+
346307
return array_values(array_unique($permissions));
347308
}
348309

@@ -363,7 +324,7 @@ public function canEditForm(Form $form): bool {
363324
* @return boolean
364325
*/
365326
public function canSeeResults(Form $form): bool {
366-
return in_array(Constants::PERMISSION_RESULTS, $this->getPermissions($form));
327+
return !empty(array_intersect($this->getPermissions($form), [Constants::PERMISSION_RESULTS, Constants::PERMISSION_RESULTS_OWN]));
367328
}
368329

369330
/**
@@ -399,7 +360,7 @@ public function canSubmit(Form $form): bool {
399360
return true;
400361
}
401362

402-
// Refuse access, if SubmitMultiple is not set and AllowEdit is not set and user already has taken part.
363+
// Refuse access, if submitMultiple is not set and allowEdit is not set and user already has taken part.
403364
if (
404365
!$form->getSubmitMultiple() && !$form->getAllowEdit() &&
405366
$this->submissionMapper->hasFormSubmissionsByUser($form, $this->currentUser->getUID())

lib/Service/SubmissionService.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ private function getAnswers(int $submissionId): array {
9191
* Get all submissions of a form
9292
*
9393
* @param int $formId the form id
94+
* @param string|null $userId optional user id to filter submissions
9495
* @return list<array{
9596
* id: int,
9697
* formId: int,
@@ -99,10 +100,14 @@ private function getAnswers(int $submissionId): array {
99100
* answers: list<FormsAnswer>,
100101
* }>
101102
*/
102-
public function getSubmissions(int $formId): array {
103+
public function getSubmissions(int $formId, ?string $userId = null): array {
103104
$submissionList = [];
104105
try {
105-
$submissionEntities = $this->submissionMapper->findByForm($formId);
106+
if (is_null($userId)) {
107+
$submissionEntities = $this->submissionMapper->findByForm($formId);
108+
} else {
109+
$submissionEntities = $this->submissionMapper->findByFormAndUser($formId, $userId);
110+
}
106111
foreach ($submissionEntities as $submissionEntity) {
107112
$submission = $submissionEntity->read();
108113
$submission['answers'] = $this->getAnswers($submission['id']);

src/components/TopBar.vue

+5-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@ export default {
124124
)
125125
},
126126
canSeeResults() {
127-
return this.permissions.includes(
128-
this.PERMISSION_TYPES.PERMISSION_RESULTS,
127+
return (
128+
this.permissions.includes(this.PERMISSION_TYPES.PERMISSION_RESULTS)
129+
|| this.permissions.includes(
130+
this.PERMISSION_TYPES.PERMISSION_RESULTS_OWN,
131+
)
129132
)
130133
},
131134
canShare() {

src/mixins/PermissionTypes.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default {
1212
PERMISSION_TYPES: {
1313
PERMISSION_EDIT: 'edit',
1414
PERMISSION_RESULTS: 'results',
15+
PERMISSION_RESULTS_OWN: 'results_own',
1516
PERMISSION_RESULTS_DELETE: 'results_delete',
1617
PERMISSION_SUBMIT: 'submit',
1718
/** Internal permission to mark public link shares as embeddable */

0 commit comments

Comments
 (0)