From 6276672854b5e790b4ff3f67dddff8be65484271 Mon Sep 17 00:00:00 2001 From: Oleg Sychev Date: Fri, 24 Jan 2014 23:30:41 +0300 Subject: [PATCH] MDL-24408 extra_answer_fields : get answers even if they miss extra data Extra answer data may be optional, saving DB space - i.e. not every row in question_answers will have respective row in extra answers table. Current implementation of question_type::get_question_options() won't return such answers at all. This commit fix that, changing join type for db query. Also fixed coding style violation in the variable name in get_question_options. --- question/type/questiontypebase.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/question/type/questiontypebase.php b/question/type/questiontypebase.php index 67fbbe70488..4cde8b45f19 100644 --- a/question/type/questiontypebase.php +++ b/question/type/questiontypebase.php @@ -645,15 +645,17 @@ class question_type { $extraanswerfields = $this->extra_answer_fields(); if (is_array($extraanswerfields)) { - $answer_extension_table = array_shift($extraanswerfields); + $answerextensiontable = array_shift($extraanswerfields); + // Use LEFT JOIN in case not every answer has extra data. $question->options->answers = $DB->get_records_sql(" - SELECT qa.*, qax." . implode(', qax.', $extraanswerfields) . " - FROM {question_answers} qa, {{$answer_extension_table}} qax - WHERE qa.question = ? AND qax.answerid = qa.id + SELECT qa.*, qax." . implode(', qax.', $extraanswerfields) . ' + FROM {question_answers} qa ' . " + LEFT JOIN {{$answerextensiontable}} qax ON qa.id = qax.answerid + WHERE qa.question = ? ORDER BY qa.id", array($question->id)); if (!$question->options->answers) { echo $OUTPUT->notification('Failed to load question answers from the table ' . - $answer_extension_table . 'for questionid ' . $question->id); + $answerextensiontable . 'for questionid ' . $question->id); return false; } } else {