Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-81317

Moodle 4.1 upgrade results in broken random questions

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Blocker Blocker
    • None
    • 4.1 regressions, 4.1.9
    • Questions, Quiz

      After upgrading Moodle 3.9.22 to 4.1.9, when attempting to do anything related to a quiz containing random questions, there are errors.

      • Users of all roles are presented with the error “There are not enough questions in category to create the question”.
      • An editingteacher, viewing the questions for a quiz is presented with the error “Faulty random question! Please delete this question.” for each question." (quiz/notenoughrandomquestions).

      I've discovered that the 2022020200.04 upgrade step produces the below query.

       

          INSERT 
          INTO
              mdl_question_set_references
              (usingcontextid, component, questionarea, itemid, questionscontextid, filtercondition) SELECT
                  c.id,
                  'mod_quiz',
                  'slot',
                  qs.id,
                  qc.contextid,
                  CONCAT('
      {
          "questioncategoryid": "',
                  q.category,
                  '",
          "includingsubcategories": "',
                  qs.includingsubcategories,
                  '"
      }
      ') 
              FROM
                  mdl_question q 
              INNER JOIN
                  mdl_quiz_slots qs 
                      on q.id = qs.questionid 
              INNER JOIN
                  mdl_course_modules cm 
                      ON cm.instance = qs.quizid 
                      AND cm.module = '16' 
              INNER JOIN
                  mdl_context c 
                      ON cm.id = c.instanceid 
                      AND c.contextlevel = '70' 
              INNER JOIN
                  mdl_question_categories qc 
                      ON qc.id = q.category 
              WHERE
                  q.qtype = 'random';

       

      The CONCAT fails to consider that quiz_slots.includingsubcategories is nullable. If quiz_slots.includingsubcategories is null the entire CONCAT returns NULL. The next step uses the result of this concat to populate the question_set_references.filtercondition value and sets it to null, breaking the random questions.

      The solution is to use COALESCE to return 0 string if qs.includingsubcategories is null.

      CONCAT('
      {
          "questioncategoryid": "', q.category, '",
          "includingsubcategories": "', COALESCE(qs.includingsubcategories, '0'), '"
      }
      ') 

      This creates the expected JSON

      {"questioncategoryid":"56154","includingsubcategories":"0"} 

            Unassigned Unassigned
            geeksmith Chris Megahan
            Votes:
            12 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0 minutes
                0m
                Logged:
                Time Spent - 5 minutes
                5m

                  Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.