@@ -1346,7 +1346,7 @@ public function newSubmission(int $formId, array $answers, string $shareHash = '
1346
1346
continue ;
1347
1347
}
1348
1348
1349
- $ this ->storeAnswersForQuestion ($ form , $ submission ->getId (), $ questions [$ questionIndex ], $ answerArray, false );
1349
+ $ this ->storeAnswersForQuestion ($ form , $ submission ->getId (), $ questions [$ questionIndex ], $ answerArray );
1350
1350
}
1351
1351
1352
1352
$ this ->formMapper ->update ($ form );
@@ -1393,47 +1393,45 @@ public function updateSubmission(int $formId, int $submissionId, array $answers,
1393
1393
}
1394
1394
1395
1395
$ 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 ());
1403
1401
}
1404
1402
1405
1403
// get existing submission of this user
1406
1404
try {
1407
- $ submission = $ this ->submissionMapper ->findById ($ form -> getId () );
1405
+ $ submission = $ this ->submissionMapper ->findById ($ submissionId );
1408
1406
} 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 ' );
1410
1412
}
1411
1413
1412
- if ($ this ->currentUser ->getUID () != $ submission ->getUserId ()) {
1414
+ if ($ this ->currentUser ->getUID () !== $ submission ->getUserId ()) {
1413
1415
throw new OCSForbiddenException ('Can only update your own submissions ' );
1414
1416
}
1415
1417
1416
1418
$ submission ->setTimestamp (time ());
1417
1419
$ this ->submissionMapper ->update ($ submission );
1418
1420
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 ;
1423
1430
}
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
- }
1432
1431
1433
- $ question = $ questions [$ questionIndex ];
1432
+ $ question = $ questions [$ questionIndex ];
1434
1433
1435
- $ this ->storeAnswersForQuestion ($ form , $ submission ->getId (), $ question , $ answerArray , true );
1436
- }
1434
+ $ this ->storeAnswersForQuestion ($ form , $ submission ->getId (), $ question , $ answerArray );
1437
1435
}
1438
1436
1439
1437
//Create Activity
@@ -1655,23 +1653,14 @@ public function uploadFiles(int $formId, int $questionId, string $shareHash = ''
1655
1653
// private functions
1656
1654
1657
1655
/**
1658
- * Insert or update answers for a question
1656
+ * Insert answers for a question
1659
1657
*
1660
1658
* @param Form $form
1661
1659
* @param int $submissionId
1662
1660
* @param array $question
1663
1661
* @param string[]|array<array{uploadedFileId: string, uploadedFileName: string}> $answerArray
1664
- * @param bool $update
1665
1662
*/
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 {
1675
1664
foreach ($ answerArray as $ answer ) {
1676
1665
$ answerEntity = new Answer ();
1677
1666
$ answerEntity ->setSubmissionId ($ submissionId );
@@ -1688,33 +1677,6 @@ private function storeAnswersForQuestion(Form $form, int $submissionId, array $q
1688
1677
} elseif (!empty ($ question ['extraSettings ' ]['allowOtherAnswer ' ]) && strpos ($ answer , Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX ) === 0 ) {
1689
1678
$ answerText = str_replace (Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX , '' , $ answer );
1690
1679
}
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
-
1718
1680
} elseif ($ question ['type ' ] === Constants::ANSWER_TYPE_FILE ) {
1719
1681
$ uploadedFile = $ this ->uploadedFileMapper ->getByUploadedFileId ($ answer ['uploadedFileId ' ]);
1720
1682
$ answerEntity ->setFileId ($ uploadedFile ->getFileId ());
@@ -1734,43 +1696,20 @@ private function storeAnswersForQuestion(Form $form, int $submissionId, array $q
1734
1696
$ file ->move ($ folder ->getPath () . '/ ' . $ name );
1735
1697
1736
1698
$ answerText = $ name ;
1737
-
1738
- $ answerEntity ->setText ($ answerText );
1739
- $ this ->answerMapper ->insert ($ answerEntity );
1740
1699
} else {
1741
1700
$ answerText = $ answer ; // Not a multiple-question, answerText is given answer
1701
+ }
1742
1702
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 ;
1757
1705
}
1758
1706
1707
+ $ answerEntity ->setText ($ answerText );
1708
+ $ this ->answerMapper ->insert ($ answerEntity );
1759
1709
if ($ uploadedFile ) {
1760
1710
$ this ->uploadedFileMapper ->delete ($ uploadedFile );
1761
1711
}
1762
1712
}
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
- }
1774
1713
}
1775
1714
1776
1715
private function loadFormForSubmission (int $ formId , string $ shareHash ): Form {
0 commit comments