4646use OCP \AppFramework \OCS \OCSNotFoundException ;
4747use OCP \AppFramework \OCSController ;
4848use OCP \BackgroundJob \IJobList ;
49+ use OCP \Files \Folder ;
4950use OCP \Files \IMimeTypeDetector ;
5051use OCP \Files \IRootFolder ;
52+ use OCP \Files \NotFoundException ;
5153use OCP \IL10N ;
5254use OCP \IRequest ;
5355use OCP \IUser ;
@@ -742,6 +744,10 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
742744 $ question ->setOrder (0 );
743745 $ this ->questionMapper ->update ($ question );
744746
747+ if ($ question ->getType () === Constants::ANSWER_TYPE_FILE ) {
748+ $ this ->deleteQuestionFolder ($ form , $ question );
749+ }
750+
745751 // Update all question-order > deleted order.
746752 $ formQuestions = $ this ->questionMapper ->findByForm ($ formId );
747753 foreach ($ formQuestions as $ question ) {
@@ -761,6 +767,43 @@ public function deleteQuestion(int $formId, int $questionId): DataResponse {
761767 return new DataResponse ($ questionId );
762768 }
763769
770+ /**
771+ * Delete question folders from all submissions
772+ * @param Form $form The form
773+ * @param Question $question The question
774+ */
775+ private function deleteQuestionFolder (Form $ form , Question $ question ): void {
776+ try {
777+ $ userFolder = $ this ->rootFolder ->getUserFolder ($ form ->getOwnerId ());
778+ $ formFolderPath = $ this ->formsService ->getFormUploadedFilesFolderPath ($ form );
779+
780+ $ formFolder = $ userFolder ->get ($ formFolderPath );
781+ if (!$ formFolder instanceof Folder) {
782+ return ;
783+ }
784+ $ questionFolderPrefix = $ question ->getId () . ' - ' ;
785+
786+ // Iterate through submission folders and delete matching question folders
787+ foreach ($ formFolder ->getDirectoryListing () as $ submissionFolder ) {
788+ if (!$ submissionFolder instanceof Folder) {
789+ continue ;
790+ }
791+ foreach ($ submissionFolder ->getDirectoryListing () as $ node ) {
792+ if (str_starts_with ($ node ->getName (), $ questionFolderPrefix )) {
793+ $ node ->delete ();
794+ }
795+ }
796+ }
797+ } catch (NotFoundException ) {
798+ // Do nothing
799+ } catch (\Throwable $ e ) {
800+ $ this ->logger ->warning ('Failed to delete question folders: {error} ' , [
801+ 'error ' => $ e ->getMessage (),
802+ 'questionId ' => $ question ->getId (),
803+ ]);
804+ }
805+ }
806+
764807 /**
765808 * Updates the Order of all Questions of a Form
766809 *
@@ -1589,7 +1632,7 @@ public function deleteSubmission(int $formId, int $submissionId): DataResponse {
15891632 }
15901633
15911634 // Delete submission (incl. Answers)
1592- $ this ->submissionMapper ->deleteById ($ submissionId );
1635+ $ this ->submissionMapper ->deleteById ($ form , $ submissionId );
15931636 $ this ->formMapper ->update ($ form );
15941637
15951638 return new DataResponse ($ submissionId );
@@ -1743,7 +1786,7 @@ public function uploadFiles(int $formId, int $questionId, string $shareHash = ''
17431786 } else {
17441787 $ folder = $ userFolder ->newFolder ($ path );
17451788 }
1746- /** @var \OCP\Files\ Folder $folder */
1789+ /** @var Folder $folder */
17471790
17481791 $ fileName = $ folder ->getNonExistingName ($ uploadedFile ['name ' ]);
17491792 $ file = $ folder ->newFile ($ fileName , file_get_contents ($ uploadedFile ['tmp_name ' ]));
@@ -1819,7 +1862,7 @@ private function storeAnswersForQuestion(Form $form, $submissionId, array $quest
18191862 } else {
18201863 $ folder = $ userFolder ->newFolder ($ path );
18211864 }
1822- /** @var \OCP\Files\ Folder $folder */
1865+ /** @var Folder $folder */
18231866
18241867 $ file = $ userFolder ->getById ($ uploadedFile ->getFileId ())[0 ];
18251868 $ name = $ folder ->getNonExistingName ($ file ->getName ());
0 commit comments