From 8f7626c27d5c52ce1982d39857f74f21ebbd4e71 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Wed, 19 Feb 2025 23:21:44 +0800 Subject: [PATCH] MDL-84532 qtype_ordering: Fix failing query in mssql on upgrade The SELECT statement can only include columns that are part of the GROUP BY clause. To resolve this issue, instead of including all columns ('*'), only the 'id' column is now included in the SELECT statement, while still preserving the existing GROUP BY statement. This column is the only one needed to identify the relevant qtype_ordering_options record that requires updating in the next step. --- question/type/ordering/db/upgrade.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/question/type/ordering/db/upgrade.php b/question/type/ordering/db/upgrade.php index 6e82aaa9a01..9d01b143820 100644 --- a/question/type/ordering/db/upgrade.php +++ b/question/type/ordering/db/upgrade.php @@ -300,7 +300,7 @@ function xmldb_qtype_ordering_upgrade($oldversion) { // We need to find all ordering question configurations that currently store the unsupported "0" (all) option // for the 'selectcount' setting and the total number of answers that are related to each of these ordering // questions. - $sql = "SELECT qoo.*, COUNT(DISTINCT(qa.id)) AS answerscount + $sql = "SELECT qoo.id, COUNT(DISTINCT(qa.id)) AS answerscount FROM {qtype_ordering_options} qoo JOIN {question_answers} qa ON qa.question = qoo.questionid WHERE selectcount = :selectcount @@ -311,9 +311,8 @@ function xmldb_qtype_ordering_upgrade($oldversion) { // to the total number of answers related to this question. This way, we are making sure that the original // behavior is preserved and all existing items (answers) related to the question will be included in the // subset. - $questionoption->selectcount = $questionoption->answerscount; - unset($questionoption->answerscount); - $DB->update_record('qtype_ordering_options', $questionoption); + $DB->set_field('qtype_ordering_options', 'selectcount', $questionoption->answerscount, + ['id' => $questionoption->id]); } $questionoptions->close(); @@ -341,7 +340,7 @@ function xmldb_qtype_ordering_upgrade($oldversion) { // We need to find all ordering question configurations that currently store the unsupported "0" (all) option // for the 'selectcount' setting and the total number of answers that are related to each of these ordering // questions. - $sql = "SELECT qoo.*, COUNT(DISTINCT(qa.id)) AS answerscount + $sql = "SELECT qoo.id, COUNT(DISTINCT(qa.id)) AS answerscount FROM {qtype_ordering_options} qoo JOIN {question_answers} qa ON qa.question = qoo.questionid WHERE selectcount = :selectcount @@ -352,9 +351,8 @@ function xmldb_qtype_ordering_upgrade($oldversion) { // to the total number of answers related to this question. This way, we are making sure that the original // behavior is preserved and all existing items (answers) related to the question will be included in the // subset. - $questionoption->selectcount = $questionoption->answerscount; - unset($questionoption->answerscount); - $DB->update_record('qtype_ordering_options', $questionoption); + $DB->set_field('qtype_ordering_options', 'selectcount', $questionoption->answerscount, + ['id' => $questionoption->id]); } $questionoptions->close();