Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save base response when radio button with other is used #613

Open
wants to merge 2 commits into
base: MOODLE_404_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions classes/question/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above code could be simplified to:
$answered = !empty($answer->value) || !preg_match("/^[\s]*$/", $answer->value);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case for answering "other" with no content?

$answered = (!empty($answer->choiceid) || !empty($answer->value));
}
Expand Down
17 changes: 8 additions & 9 deletions classes/responsetype/single.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure this is what is desired. If the user didn't select a response, why would a response be saved? Please provide a test case

$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();
Expand Down