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

Course backup not restored due to Quiz Multianswer not mapping correctly (Postgres)

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Duplicate
    • Affects Version/s: 3.7.1
    • Fix Version/s: None
    • Component/s: Backup, Questions
    • Labels:
      None
    • Affected Branches:
      MOODLE_37_STABLE

      Description

      With a recent migration to Moodle 3.7.1 we were not able to restore  a course due to a dml exception when processing a quiz activity with some multianswer questions that did not map  the answers correctly, returning an array of empty strings that were then imploded, producing at the end one unique string of separators (i.e. ',,,,,,,,,,,,'). 

      Since we use a Postgres database, the following error raised (with several references on the tracker but still showing up for our scenario):

      Debug info: ERROR: invalid input syntax for integer: ""
      SELECT * FROM mdl_question WHERE id IN ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16) ORDER BY id ASC
      [array (
      0 => '',
      1 => '',
      2 => '',
      3 => '',
      4 => '',
      5 => '',
      6 => '',
      7 => '',
      8 => '',
      9 => '',
      10 => '',
      11 => '',
      12 => '',
      13 => '',
      14 => '',
      15 => '',
      )]
      Error code: dmlreadexception

       

      We also successfully restored the same backup on demo.moodle.net, it worked since there's a Mysql DB on use.

       

      The actual behavior details are:

      File: question/type/multianswer/backup/moodle2/restore_qtype_multianswer_plugin.class.php

      Class: restore_qtype_multianswer_pluginMethod: after_execute_question

       

      Since the call to the method get_mappingid('question', $question) might return an empty string (answers were not created because they were lacking on the backup's 'questions.xml' file):

      foreach ($sequencearr as $key => $question){     $sequencearr[$key] = $this->get_mappingid('question', $question);} $sequence = implode(',', $sequencearr);
      

      The variable $sequence might return ',,,,,,,,,'

      Thus, the following line produced the dlm error (Postgres):

      $DB->set_field('question_multianswer', 'sequence', $sequence, array('id' => $rec->id));
      

       

      Proposed change:

      $sequence = implode(',', array_unique($sequencearr));
      

      Performing the array_unique on $sequencearr will ensure it gets reduced to one empty value, therefore the imploded $sequence will not end being a string containing only commas.

      We found this procedure is a fail-safe that works for Mysql/MariaDB and Postgres.

       

      Can someone validate it please?

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              timhunt Tim Hunt
              Reporter:
              felicemcc Felice Candilio
              Participants:
              Component watchers:
              Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Peter Dias, Tim Hunt, Andrew Nicols, Jun Pataleta, Michael Hawkins, Shamim Rezaie, Simey Lameze
              Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: