Skip to content

Commit

Permalink
drop DeleteSubmission, and make Clear button work for AllowEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
tpokorra committed Dec 31, 2024
1 parent 7bb4a7d commit acacf30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 68 deletions.
39 changes: 23 additions & 16 deletions lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1219,17 +1219,24 @@ public function updateSubmission(int $formId, int $submissionId, array $answers,
$submission->setTimestamp(time());
$this->submissionMapper->update($submission);

// Process Answers
foreach ($answers as $questionId => $answerArray) {
// Search corresponding Question, skip processing if not found
$questionIndex = array_search($questionId, array_column($questions, 'id'));
if ($questionIndex === false) {
continue;
if (empty($answers)) {
// Clear Answers
foreach ($questions as $question) {
$this->storeAnswersForQuestion($form, $submission->getId(), $question, array(''), true);
}
} else {
// Process Answers
foreach ($answers as $questionId => $answerArray) {
// Search corresponding Question, skip processing if not found
$questionIndex = array_search($questionId, array_column($questions, 'id'));
if ($questionIndex === false) {
continue;
}

$question = $questions[$questionIndex];
$question = $questions[$questionIndex];

$this->storeAnswersForQuestion($form, $submission->getId(), $question, $answerArray, true);
$this->storeAnswersForQuestion($form, $submission->getId(), $question, $answerArray, true);
}
}

//Create Activity
Expand Down Expand Up @@ -1558,15 +1565,15 @@ private function storeAnswersForQuestion(Form $form, $submissionId, array $quest
$this->answerMapper->insert($answerEntity);
}
}
}

if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED)) {
// drop all answers that are not in new set of answers
foreach($storedAnswers as $storedAnswer) {
$questionId = $storedAnswer->getQuestionId();

if (empty($newAnswerTexts[$questionId]) || !in_array($storedAnswer->getText(), $newAnswerTexts[$questionId])) {
$this->answerMapper->delete($storedAnswer);
}
if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED)) {
// drop all answers that are not in new set of answers
foreach($storedAnswers as $storedAnswer) {
$questionId = $storedAnswer->getQuestionId();

if (empty($newAnswerTexts[$questionId]) || !in_array($storedAnswer->getText(), $newAnswerTexts[$questionId])) {
$this->answerMapper->delete($storedAnswer);
}
}
}
Expand Down
53 changes: 1 addition & 52 deletions src/views/Submit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,6 @@
</template>
{{ t('forms', 'Submit') }}
</NcButton>
<NcButton
v-if="!newSubmission"
class="delete-button"
:disabled="loading"
type="button"
@click="onDeleteSubmission">
<template #icon>
<NcIconSvgWrapper :svg="IconDeleteSvg" />
</template>
{{ t('forms', 'Delete') }}
</NcButton>
</div>
</form>

Expand Down Expand Up @@ -678,7 +667,7 @@ export default {
this.loading = true

try {
if (this.newSubmission === false) {
if (this.submissionId) {
await axios.post(
generateOcsUrl('apps/forms/api/v3/forms/{id}/submissions/{submissionId}', {
id: this.form.id,
Expand Down Expand Up @@ -718,44 +707,11 @@ export default {
this.resetData()
},

/**
* Delete the submission
*/
async onDeleteSubmission() {
if (!confirm(t('forms', 'Are you sure you want to delete your response?'))) {
return
}

this.loading = true

try {
if (this.newSubmission === false) {
await axios.delete(generateOcsUrl('apps/forms/api/v3/forms/{id}/submissions/{submissionId}', {
id: this.form.id,
submissionId: this.submissionId
}))
} else {
throw new Error('cannot delete new submission')
}
this.success = true
this.submitForm = true
this.success = true
this.deleteFormFieldFromLocalStorage()
emit('forms:last-updated:set', this.form.id)
} catch (error) {
logger.error('Error while deleting the form submission', { error })
showError(t('forms', 'There was an error deleting the form submission'))
} finally {
this.loading = false
}
},

/**
* Reset View-Data
*/
resetData() {
this.answers = {}
this.newSubmission = true
this.loading = false
this.showConfirmLeaveDialog = false
this.showClearFormDialog = false
Expand Down Expand Up @@ -860,13 +816,6 @@ export default {
margin-block-end: 160px;
padding-inline-start: 20px;
}

.delete-button {
float:right;
background-color: red;
margin: 5px;
padding-inline-start: 20px;
}
}
}
</style>

0 comments on commit acacf30

Please sign in to comment.