Skip to content

Commit

Permalink
fix: Make sure "other" answers are correctly handled
Browse files Browse the repository at this point in the history
In the backend it handling the "other" answer setting was not changed
after switching from object to array for extra settings.

In the frontend the value handling of the checkboxes or radio switches
was not correct as the initial values is always an empty array.
This lead to issues with radio switch answers.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux authored and Chartman123 committed Oct 26, 2023
1 parent 6d74165 commit 8303476
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ public function insertSubmission(int $formId, array $answers, string $shareHash
$optionIndex = array_search($answer, array_column($question['options'], 'id'));
if ($optionIndex !== false) {
$answerText = $question['options'][$optionIndex]['text'];
} elseif (!empty($question['extraSettings']->allowOtherAnswer) && strpos($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX) === 0) {
} elseif (!empty($question['extraSettings']['allowOtherAnswer']) && strpos($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX) === 0) {
$answerText = str_replace(Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX, "", $answer);
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private function array2csv(array $header, array $records): string {
* @return boolean If the submission is valid
*/
public function validateSubmission(array $questions, array $answers): bool {

// Check by questions
foreach ($questions as $question) {
$questionId = $question['id'];
Expand All @@ -312,7 +312,7 @@ public function validateSubmission(array $questions, array $answers): bool {
if ($question['isRequired'] &&
(!$questionAnswered ||
!array_filter($answers[$questionId], 'strlen') ||
(!empty($question['extraSettings']->allowOtherAnswer) && !array_filter($answers[$questionId], fn ($value) => $value !== Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX)))
(!empty($question['extraSettings']['allowOtherAnswer']) && !array_filter($answers[$questionId], fn ($value) => $value !== Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX)))
) {
return false;
}
Expand All @@ -334,7 +334,7 @@ public function validateSubmission(array $questions, array $answers): bool {
}

// Check if all answers are within the possible options
if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED) && empty($question['extraSettings']->allowOtherAnswer)) {
if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED) && empty($question['extraSettings']['allowOtherAnswer'])) {
foreach ($answers[$questionId] as $answer) {
// Search corresponding option, return false if non-existent
if (array_search($answer, array_column($question['options'], 'id')) === false) {
Expand Down
39 changes: 20 additions & 19 deletions src/components/Questions/QuestionMultiple.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<fieldset :name="name || undefined" :aria-labelledby="titleId">
<NcCheckboxRadioSwitch v-for="(answer) in sortedOptions"
:key="answer.id"
:checked.sync="questionValues"
:checked="questionValues"
:value="answer.id.toString()"
:name="`${id}-answer`"
:type="isUnique ? 'radio' : 'checkbox'"
Expand All @@ -58,7 +58,7 @@
{{ answer.text }}
</NcCheckboxRadioSwitch>
<div v-if="allowOtherAnswer" class="question__other-answer">
<NcCheckboxRadioSwitch :checked.sync="questionValues"
<NcCheckboxRadioSwitch :checked="questionValues"
:value="valueOtherAnswer"
:name="`${id}-answer`"
:type="isUnique ? 'radio' : 'checkbox'"
Expand Down Expand Up @@ -149,20 +149,12 @@ export default {
data() {
return {
questionValues: this.values,
inputOtherAnswer: this.valueToInputOtherAnswer(),
QUESTION_EXTRASETTINGS_OTHER_PREFIX: 'system-other-answer:',
}
},
computed: {
placeholderOtherAnswer() {
if (this.readOnly) {
return this.answerType.submitPlaceholder
}
return this.answerType.createPlaceholder
},
contentValid() {
return this.answerType.validate(this)
},
Expand Down Expand Up @@ -192,6 +184,17 @@ export default {
return this.isUnique ? IconRadioboxBlank : IconCheckboxBlankOutline
},
placeholderOtherAnswer() {
if (this.readOnly) {
return this.answerType.submitPlaceholder
}
return this.answerType.createPlaceholder
},
questionValues() {
return this.isUnique ? this.values?.[0] : this.values
},
titleId() {
return `q${this.$attrs.index}_title`
},
Expand All @@ -205,7 +208,7 @@ export default {
},
hasRequiredOtherAnswerInput() {
const checkedOtherAnswer = this.questionValues.filter(item => item.startsWith(this.QUESTION_EXTRASETTINGS_OTHER_PREFIX))
const checkedOtherAnswer = this.values.filter(item => item.startsWith(this.QUESTION_EXTRASETTINGS_OTHER_PREFIX))
return checkedOtherAnswer[0] !== undefined
},
},
Expand All @@ -229,24 +232,22 @@ export default {
inputOtherAnswer() {
if (this.isUnique) {
this.questionValues = this.valueOtherAnswer
this.onChange()
this.onChange(this.valueOtherAnswer)
return
}
this.questionValues = this.questionValues.filter(item => !item.startsWith(this.QUESTION_EXTRASETTINGS_OTHER_PREFIX))
const values = this.values.filter(item => !item.startsWith(this.QUESTION_EXTRASETTINGS_OTHER_PREFIX))
if (this.inputOtherAnswer !== '') {
this.questionValues.push(this.valueOtherAnswer)
values.push(this.valueOtherAnswer)
}
this.onChange()
this.onChange(values)
},
},
methods: {
onChange() {
this.$emit('update:values', this.isUnique ? [this.questionValues] : this.questionValues)
onChange(value) {
this.$emit('update:values', this.isUnique ? [value] : value)
},
/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Controller/ApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public function testInsertSubmission_answers() {
[
'id' => 2,
'type' => Constants::ANSWER_TYPE_MULTIPLE,
'extraSettings' => (object)['allowOtherAnswer' => true],
'extraSettings' => ['allowOtherAnswer' => true],
'options' => [
['id' => 1, 'text' => 'test id 1'],
['id' => 2, 'text' => 'test id 2'],
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Service/SubmissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ public function dataValidateSubmission() {
'required-empty-other-answer' => [
// Questions
[
['id' => 1, 'type' => 'multiple_unique', 'isRequired' => true, 'extraSettings' => (object)['allowOtherAnswer' => true], 'options' => [
['id' => 1, 'type' => 'multiple_unique', 'isRequired' => true, 'extraSettings' => ['allowOtherAnswer' => true], 'options' => [
['id' => 3]
]]
],
Expand Down Expand Up @@ -664,7 +664,7 @@ public function dataValidateSubmission() {
['id' => 6]
]],
['id' => 8, 'type' => 'time', 'isRequired' => false],
['id' => 9, 'type' => 'multiple_unique', 'isRequired' => true, 'extraSettings' => (object)['allowOtherAnswer' => true], 'options' => [
['id' => 9, 'type' => 'multiple_unique', 'isRequired' => true, 'extraSettings' => ['allowOtherAnswer' => true], 'options' => [
['id' => 3]
]],
],
Expand Down

0 comments on commit 8303476

Please sign in to comment.