@@ -1346,7 +1346,7 @@ public function newSubmission(int $formId, array $answers, string $shareHash = '
13461346 continue ;
13471347 }
13481348
1349- $ this ->storeAnswersForQuestion ($ form , $ submission ->getId (), $ questions [$ questionIndex ], $ answerArray, false );
1349+ $ this ->storeAnswersForQuestion ($ form , $ submission ->getId (), $ questions [$ questionIndex ], $ answerArray );
13501350 }
13511351
13521352 $ this ->formMapper ->update ($ form );
@@ -1393,47 +1393,45 @@ public function updateSubmission(int $formId, int $submissionId, array $answers,
13931393 }
13941394
13951395 $ questions = $ this ->formsService ->getQuestions ($ formId );
1396- // Is the submission valid
1397- $ isSubmissionValid = $ this ->submissionService ->validateSubmission ($ questions , $ answers , $ form ->getOwnerId ());
1398- if (is_string ($ isSubmissionValid )) {
1399- throw new OCSBadRequestException ($ isSubmissionValid );
1400- }
1401- if ($ isSubmissionValid === false ) {
1402- throw new OCSBadRequestException ('At least one submitted answer is not valid ' );
1396+ try {
1397+ // Is the submission valid
1398+ $ this ->submissionService ->validateSubmission ($ questions , $ answers , $ form ->getOwnerId ());
1399+ } catch (\InvalidArgumentException $ e ) {
1400+ throw new OCSBadRequestException ($ e ->getMessage ());
14031401 }
14041402
14051403 // get existing submission of this user
14061404 try {
1407- $ submission = $ this ->submissionMapper ->findById ($ form -> getId () );
1405+ $ submission = $ this ->submissionMapper ->findById ($ submissionId );
14081406 } catch (DoesNotExistException $ e ) {
1409- throw new OCSBadRequestException ('Cannot update a non existing submission ' );
1407+ throw new OCSBadRequestException ('Submission doesn \'t exist ' );
1408+ }
1409+
1410+ if ($ formId !== $ submission ->getFormId ()) {
1411+ throw new OCSBadRequestException ('Submission doesn \'t belong to given form ' );
14101412 }
14111413
1412- if ($ this ->currentUser ->getUID () != $ submission ->getUserId ()) {
1414+ if ($ this ->currentUser ->getUID () !== $ submission ->getUserId ()) {
14131415 throw new OCSForbiddenException ('Can only update your own submissions ' );
14141416 }
14151417
14161418 $ submission ->setTimestamp (time ());
14171419 $ this ->submissionMapper ->update ($ submission );
14181420
1419- if (empty ($ answers )) {
1420- // Clear Answers
1421- foreach ($ questions as $ question ) {
1422- $ this ->storeAnswersForQuestion ($ form , $ submission ->getId (), $ question , ['' ], true );
1421+ // Delete current answers
1422+ $ this ->answerMapper ->deleteBySubmission ($ submissionId );
1423+
1424+ // Process Answers
1425+ foreach ($ answers as $ questionId => $ answerArray ) {
1426+ // Search corresponding Question, skip processing if not found
1427+ $ questionIndex = array_search ($ questionId , array_column ($ questions , 'id ' ));
1428+ if ($ questionIndex === false ) {
1429+ continue ;
14231430 }
1424- } else {
1425- // Process Answers
1426- foreach ($ answers as $ questionId => $ answerArray ) {
1427- // Search corresponding Question, skip processing if not found
1428- $ questionIndex = array_search ($ questionId , array_column ($ questions , 'id ' ));
1429- if ($ questionIndex === false ) {
1430- continue ;
1431- }
14321431
1433- $ question = $ questions [$ questionIndex ];
1432+ $ question = $ questions [$ questionIndex ];
14341433
1435- $ this ->storeAnswersForQuestion ($ form , $ submission ->getId (), $ question , $ answerArray , true );
1436- }
1434+ $ this ->storeAnswersForQuestion ($ form , $ submission ->getId (), $ question , $ answerArray );
14371435 }
14381436
14391437 //Create Activity
@@ -1655,23 +1653,14 @@ public function uploadFiles(int $formId, int $questionId, string $shareHash = ''
16551653 // private functions
16561654
16571655 /**
1658- * Insert or update answers for a question
1656+ * Insert answers for a question
16591657 *
16601658 * @param Form $form
16611659 * @param int $submissionId
16621660 * @param array $question
16631661 * @param string[]|array<array{uploadedFileId: string, uploadedFileName: string}> $answerArray
1664- * @param bool $update
16651662 */
1666- private function storeAnswersForQuestion (Form $ form , int $ submissionId , array $ question , array $ answerArray , bool $ update ): void {
1667- // get stored answers for this question
1668- $ storedAnswers = [];
1669- if ($ update ) {
1670- $ storedAnswers = $ this ->answerMapper ->findBySubmissionAndQuestion ($ submissionId , $ question ['id ' ]);
1671- }
1672-
1673- $ newAnswerTexts = [];
1674-
1663+ private function storeAnswersForQuestion (Form $ form , $ submissionId , array $ question , array $ answerArray ): void {
16751664 foreach ($ answerArray as $ answer ) {
16761665 $ answerEntity = new Answer ();
16771666 $ answerEntity ->setSubmissionId ($ submissionId );
@@ -1688,33 +1677,6 @@ private function storeAnswersForQuestion(Form $form, int $submissionId, array $q
16881677 } elseif (!empty ($ question ['extraSettings ' ]['allowOtherAnswer ' ]) && strpos ($ answer , Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX ) === 0 ) {
16891678 $ answerText = str_replace (Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX , '' , $ answer );
16901679 }
1691-
1692- if (!array_key_exists ($ question ['id ' ], $ newAnswerTexts )) {
1693- $ newAnswerTexts [$ question ['id ' ]] = [];
1694- }
1695- $ newAnswerTexts [$ question ['id ' ]][] = $ answerText ;
1696-
1697- // has this answer already been stored?
1698- $ foundAnswer = false ;
1699- foreach ($ storedAnswers as $ storedAnswer ) {
1700- if ($ storedAnswer ->getText () == $ answerText ) {
1701- // nothing to be changed
1702- $ foundAnswer = true ;
1703- break ;
1704- }
1705- }
1706- if (!$ foundAnswer ) {
1707- if ($ answerText === '' ) {
1708- continue ;
1709- }
1710- // need to add answer
1711- $ answerEntity = new Answer ();
1712- $ answerEntity ->setSubmissionId ($ submissionId );
1713- $ answerEntity ->setQuestionId ($ question ['id ' ]);
1714- $ answerEntity ->setText ($ answerText );
1715- $ this ->answerMapper ->insert ($ answerEntity );
1716- }
1717-
17181680 } elseif ($ question ['type ' ] === Constants::ANSWER_TYPE_FILE ) {
17191681 $ uploadedFile = $ this ->uploadedFileMapper ->getByUploadedFileId ($ answer ['uploadedFileId ' ]);
17201682 $ answerEntity ->setFileId ($ uploadedFile ->getFileId ());
@@ -1734,43 +1696,20 @@ private function storeAnswersForQuestion(Form $form, int $submissionId, array $q
17341696 $ file ->move ($ folder ->getPath () . '/ ' . $ name );
17351697
17361698 $ answerText = $ name ;
1737-
1738- $ answerEntity ->setText ($ answerText );
1739- $ this ->answerMapper ->insert ($ answerEntity );
17401699 } else {
17411700 $ answerText = $ answer ; // Not a multiple-question, answerText is given answer
1701+ }
17421702
1743- if (!empty ($ storedAnswers )) {
1744- $ answerEntity = $ storedAnswers [0 ];
1745- $ answerEntity ->setText ($ answerText );
1746- $ this ->answerMapper ->update ($ answerEntity );
1747- } else {
1748- if ($ answerText === '' ) {
1749- continue ;
1750- }
1751- $ answerEntity = new Answer ();
1752- $ answerEntity ->setSubmissionId ($ submissionId );
1753- $ answerEntity ->setQuestionId ($ question ['id ' ]);
1754- $ answerEntity ->setText ($ answerText );
1755- $ this ->answerMapper ->insert ($ answerEntity );
1756- }
1703+ if ($ answerText === '' ) {
1704+ continue ;
17571705 }
17581706
1707+ $ answerEntity ->setText ($ answerText );
1708+ $ this ->answerMapper ->insert ($ answerEntity );
17591709 if ($ uploadedFile ) {
17601710 $ this ->uploadedFileMapper ->delete ($ uploadedFile );
17611711 }
17621712 }
1763-
1764- if (in_array ($ question ['type ' ], Constants::ANSWER_TYPES_PREDEFINED )) {
1765- // drop all answers that are not in new set of answers
1766- foreach ($ storedAnswers as $ storedAnswer ) {
1767- $ questionId = $ storedAnswer ->getQuestionId ();
1768-
1769- if (empty ($ newAnswerTexts [$ questionId ]) || !in_array ($ storedAnswer ->getText (), $ newAnswerTexts [$ questionId ])) {
1770- $ this ->answerMapper ->delete ($ storedAnswer );
1771- }
1772- }
1773- }
17741713 }
17751714
17761715 private function loadFormForSubmission (int $ formId , string $ shareHash ): Form {
0 commit comments