From 05b4ad9045f676a2d02bc0ffc0dd66d023be4fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Jencz?= Date: Tue, 15 Oct 2024 14:19:45 +0200 Subject: [PATCH 1/2] fix: do not allow saving other choice with answer has only spaces --- classes/question/question.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/classes/question/question.php b/classes/question/question.php index 7b4594b5..b8d5202f 100644 --- a/classes/question/question.php +++ b/classes/question/question.php @@ -567,6 +567,10 @@ public function response_complete($responsedata) { if (!empty($answer->choiceid) && isset($this->choices[$answer->choiceid]) && $this->choices[$answer->choiceid]->is_other_choice()) { $answered = !empty($answer->value); + $onlyspaces = preg_match("/^[\s]*$/", $answer->value); + if ($onlyspaces) { + $answered = false; + } } else { $answered = (!empty($answer->choiceid) || !empty($answer->value)); } From 36d16449f0d1d8f9aed507d708811deb18562bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Jencz?= Date: Tue, 15 Oct 2024 14:21:17 +0200 Subject: [PATCH 2/2] fix: Always save base choice --- classes/responsetype/single.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/classes/responsetype/single.php b/classes/responsetype/single.php index d8dc2f7c..33d7b43a 100644 --- a/classes/responsetype/single.php +++ b/classes/responsetype/single.php @@ -109,16 +109,15 @@ public function insert_response($responsedata) { foreach ($response->answers[$this->question->id] as $answer) { if (isset($this->question->choices[$answer->choiceid])) { if ($this->question->choices[$answer->choiceid]->is_other_choice()) { - // If no input specified, ignore this choice. - if (empty($answer->value) || preg_match("/^[\s]*$/", $answer->value)) { - continue; + // If no input specified, don't save other choice, but always save base response. + if (!empty($answer->value) && !preg_match("/^[\s]*$/", $answer->value)) { + $record = new \stdClass(); + $record->response_id = $response->id; + $record->question_id = $this->question->id; + $record->choice_id = $answer->choiceid; + $record->response = clean_text($answer->value); + $DB->insert_record('questionnaire_response_other', $record); } - $record = new \stdClass(); - $record->response_id = $response->id; - $record->question_id = $this->question->id; - $record->choice_id = $answer->choiceid; - $record->response = clean_text($answer->value); - $DB->insert_record('questionnaire_response_other', $record); } // Record the choice selection. $record = new \stdClass();