99
1010use OCA \Forms \Activity \ActivityManager ;
1111use OCA \Forms \Constants ;
12- use OCA \Forms \Db \AnswerMapper ;
1312use OCA \Forms \Db \Form ;
1413use OCA \Forms \Db \FormMapper ;
1514use OCA \Forms \Db \OptionMapper ;
2322use OCA \Forms \ResponseDefinitions ;
2423use OCP \AppFramework \Db \DoesNotExistException ;
2524use OCP \AppFramework \Db \IMapperException ;
26- use OCP \AppFramework \Db \MultipleObjectsReturnedException ;
2725use OCP \EventDispatcher \IEventDispatcher ;
2826use OCP \Files \IRootFolder ;
2927use 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 ())
0 commit comments