Skip to content

Commit

Permalink
MDL-81458 question: Cast the fraction value to a float
Browse files Browse the repository at this point in the history
When retrieving the answer data from the database, it's essential to
cast the value of the answer's fraction to a float before storing it
into the question object. This step is crucial for ensuring consistency,
as some database engines return floats as strings like '1.0000000'.
  • Loading branch information
Mihail Geshoski committed Apr 9, 2024
1 parent b621a7e commit 72e9a1b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions question/type/questiontypebase.php
Expand Up @@ -931,23 +931,29 @@ public function get_question_options($question) {
if (is_array($extraanswerfields)) {
$answerextensiontable = array_shift($extraanswerfields);
// Use LEFT JOIN in case not every answer has extra data.
$question->options->answers = $DB->get_records_sql("
$answers = $DB->get_records_sql("
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) {
if (!$answers) {
echo $OUTPUT->notification('Failed to load question answers from the table ' .
$answerextensiontable . 'for questionid ' . $question->id);
return false;
}
} else {
// Don't check for success or failure because some question types do
// not use the answers table.
$question->options->answers = $DB->get_records('question_answers',
$answers = $DB->get_records('question_answers',
array('question' => $question->id), 'id ASC');
}
// Store the answers into the question object.
$question->options->answers = array_map(function($answer) {
// Some database engines return floats as strings like '1.0000000'. Cast to float for consistency.
$answer->fraction = (float) $answer->fraction;
return $answer;
}, $answers);

$question->hints = $DB->get_records('question_hints',
array('questionid' => $question->id), 'id ASC');
Expand Down

0 comments on commit 72e9a1b

Please sign in to comment.