9
9
10
10
use OCA \Forms \Activity \ActivityManager ;
11
11
use OCA \Forms \Constants ;
12
- use OCA \Forms \Db \AnswerMapper ;
13
12
use OCA \Forms \Db \Form ;
14
13
use OCA \Forms \Db \FormMapper ;
15
14
use OCA \Forms \Db \OptionMapper ;
23
22
use OCA \Forms \ResponseDefinitions ;
24
23
use OCP \AppFramework \Db \DoesNotExistException ;
25
24
use OCP \AppFramework \Db \IMapperException ;
26
- use OCP \AppFramework \Db \MultipleObjectsReturnedException ;
27
25
use OCP \EventDispatcher \IEventDispatcher ;
28
26
use OCP \Files \IRootFolder ;
29
27
use OCP \Files \NotFoundException ;
@@ -56,7 +54,6 @@ public function __construct(
56
54
private QuestionMapper $ questionMapper ,
57
55
private ShareMapper $ shareMapper ,
58
56
private SubmissionMapper $ submissionMapper ,
59
- private AnswerMapper $ answerMapper ,
60
57
private ConfigService $ configService ,
61
58
private IGroupManager $ groupManager ,
62
59
private IUserManager $ userManager ,
@@ -99,32 +96,6 @@ private function getOptions(int $questionId): array {
99
96
}
100
97
}
101
98
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
-
128
99
/**
129
100
* Load questions corresponding to form
130
101
*
@@ -222,25 +193,6 @@ public function getShares(int $formId): array {
222
193
public function getForm (Form $ form ): array {
223
194
$ result = $ form ->read ();
224
195
$ 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
-
244
196
$ result ['shares ' ] = $ this ->getShares ($ form ->getId ());
245
197
246
198
// Append permissions for current user.
@@ -251,6 +203,8 @@ public function getForm(Form $form): array {
251
203
// Append submissionCount if currentUser has permissions to see results
252
204
if (in_array (Constants::PERMISSION_RESULTS , $ result ['permissions ' ])) {
253
205
$ 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 ());
254
208
}
255
209
256
210
if ($ result ['fileId ' ]) {
@@ -287,6 +241,8 @@ public function getPartialFormArray(Form $form): array {
287
241
// Append submissionCount if currentUser has permissions to see results
288
242
if (in_array (Constants::PERMISSION_RESULTS , $ result ['permissions ' ])) {
289
243
$ 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 ());
290
246
}
291
247
292
248
return $ result ;
@@ -339,10 +295,15 @@ public function getPermissions(Form $form): array {
339
295
if (count ($ permissions ) === 0 ) {
340
296
$ access = $ form ->getAccess ();
341
297
if ($ access ['permitAllUsers ' ] && $ this ->configService ->getAllowPermitAll ()) {
342
- $ permissions = [ Constants::PERMISSION_SUBMIT ] ;
298
+ $ permissions[] = Constants::PERMISSION_SUBMIT ;
343
299
}
344
300
}
345
301
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
+
346
307
return array_values (array_unique ($ permissions ));
347
308
}
348
309
@@ -363,7 +324,7 @@ public function canEditForm(Form $form): bool {
363
324
* @return boolean
364
325
*/
365
326
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 ] ));
367
328
}
368
329
369
330
/**
@@ -399,7 +360,7 @@ public function canSubmit(Form $form): bool {
399
360
return true ;
400
361
}
401
362
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.
403
364
if (
404
365
!$ form ->getSubmitMultiple () && !$ form ->getAllowEdit () &&
405
366
$ this ->submissionMapper ->hasFormSubmissionsByUser ($ form , $ this ->currentUser ->getUID ())
0 commit comments