bdaloukas / moodle-mod_game

moodle-mod_game
GNU General Public License v3.0
15 stars 40 forks source link

sudoku using glossary does not work with Moodle 4.x #60

Open ak4t0sh opened 9 months ago

ak4t0sh commented 9 months ago

Steps to reproduce

Result

Unknown column 'qv.questionbankentryid' in 'field list'
SELECT ge.id,qv.questionbankentryid,qv.version FROM mdl_glossary_entries ge WHERE (glossaryid='1' OR sourceglossaryid='1') ORDER BY qv.questionbankentryid,qv.version DESC
[array (
)]
Error code: dmlreadexception 

line 494 of /lib/dml/moodle_database.php: dml_read_exception thrown
line 293 of /lib/dml/moodle_read_slave_trait.php: call to moodle_database->query_end()
line 1282 of /lib/dml/mysqli_native_moodle_database.php: call to mysqli_native_moodle_database->query_end()
line 621 of /mod/game/locallib.php: call to mysqli_native_moodle_database->get_records_sql()
line 580 of /mod/game/locallib.php: call to game_questions_selectrandom_detail()
line 79 of /mod/game/sudoku/play.php: call to game_questions_selectrandom()
line 280 of /mod/game/attempt.php: call to game_sudoku_continue()
line 242 of /mod/game/attempt.php: call to game_create()
line 40 of /mod/game/attempt.php: call to game_do_attempt()

Analysis

In https://github.com/bdaloukas/moodle-mod_game/commit/9316457cfb5be3ffc17f6c36e4d3cd6252ec05e1 you introduced question/quiz specific fields in the query used in https://github.com/bdaloukas/moodle-mod_game/blob/e4109a7fb9af86d559fee1816f3cc40fe398978f/locallib.php#L620 making it no more compatible with glossary as source.

Solution / Workaround

Waiting a cleaner solution below an ugly patch I've done as workaround to prevent glossaries to crash.

---
 locallib.php | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/locallib.php b/locallib.php
index 87118bd..595bbd3 100644
--- a/locallib.php
+++ b/locallib.php
@@ -614,8 +614,9 @@ function game_questions_selectrandom_detail( $table, $select, $idfield="id", $co
     global $DB;

     $versions = game_get_moodle_version() >= '04.00';
-    $fields = $versions ? ',qv.questionbankentryid,qv.version' : '';
-    $order = $versions ? ' ORDER BY qv.questionbankentryid,qv.version DESC' : '';
+    $usequestionbank = !in_array($table, ["{glossary_entries} ge"]);
+    $fields = ($usequestionbank && $versions) ? ',qv.questionbankentryid,qv.version' : '';
+    $order = ($usequestionbank && $versions) ? ' ORDER BY qv.questionbankentryid,qv.version DESC' : '';

     $sql = "SELECT $idfield{$fields} FROM $table WHERE $select $order";
     if (($recs = $DB->get_records_sql( $sql)) == false) {
@@ -626,7 +627,7 @@ function game_questions_selectrandom_detail( $table, $select, $idfield="id", $co
     $map = array();
     $a = array();
     foreach ($recs as $rec) {
-        if( $versions) {
+        if( $usequestionback && $versions) {
             if( array_key_exists( $rec->questionbankentryid, $map)) {
                 continue;
             } else {
--