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,11 @@ 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+ } else {
207+ $ userSubmissionCount = $ this ->submissionMapper ->countSubmissions ($ form ->getId (), $ this ->currentUser ->getUID ());
208+ if ($ userSubmissionCount > 0 ) {
209+ $ result ['submissionCount ' ] = $ userSubmissionCount ;
210+ }
254211 }
255212
256213 if ($ result ['fileId ' ]) {
@@ -287,6 +244,11 @@ public function getPartialFormArray(Form $form): array {
287244 // Append submissionCount if currentUser has permissions to see results
288245 if (in_array (Constants::PERMISSION_RESULTS , $ result ['permissions ' ])) {
289246 $ result ['submissionCount ' ] = $ this ->submissionMapper ->countSubmissions ($ form ->getId ());
247+ } else {
248+ $ userSubmissionCount = $ this ->submissionMapper ->countSubmissions ($ form ->getId (), $ this ->currentUser ->getUID ());
249+ if ($ userSubmissionCount > 0 ) {
250+ $ result ['submissionCount ' ] = $ userSubmissionCount ;
251+ }
290252 }
291253
292254 return $ result ;
@@ -356,14 +318,24 @@ public function canEditForm(Form $form): bool {
356318 return in_array (Constants::PERMISSION_EDIT , $ this ->getPermissions ($ form ));
357319 }
358320
321+ /**
322+ * Determines if the current user has permission to view the results of a given form.
323+ *
324+ * A user can see the results of a form if they have made at least one submission
325+ * to the form or possess the required permission to view results.
326+ *
327+ * @param Form $form The form for which the results visibility is being checked.
328+ * @return bool True if the user can see the results, false otherwise.
329+ */
359330 /**
360331 * Can the current user see results of a form
361332 *
362333 * @param Form $form
363334 * @return boolean
364335 */
365336 public function canSeeResults (Form $ form ): bool {
366- return in_array (Constants::PERMISSION_RESULTS , $ this ->getPermissions ($ form ));
337+ return $ this ->submissionMapper ->countSubmissions ($ form ->getId (), $ this ->currentUser ->getUID ()) > 0
338+ || in_array (Constants::PERMISSION_RESULTS , $ this ->getPermissions ($ form ));
367339 }
368340
369341 /**
@@ -399,7 +371,7 @@ public function canSubmit(Form $form): bool {
399371 return true ;
400372 }
401373
402- // Refuse access, if SubmitMultiple is not set and AllowEdit is not set and user already has taken part.
374+ // Refuse access, if submitMultiple is not set and allowEdit is not set and user already has taken part.
403375 if (
404376 !$ form ->getSubmitMultiple () && !$ form ->getAllowEdit () &&
405377 $ this ->submissionMapper ->hasFormSubmissionsByUser ($ form , $ this ->currentUser ->getUID ())
@@ -493,7 +465,7 @@ private function isSharedFormShown(Form $form): bool {
493465 return false ;
494466 }
495467
496- // Shown if permitall and showntoall are both set.
468+ // Shown if permitAll and showToAll are both set.
497469 if ($ form ->getAccess ()['permitAllUsers ' ] &&
498470 $ form ->getAccess ()['showToAllUsers ' ] &&
499471 $ this ->configService ->getAllowPermitAll () &&
0 commit comments