diff -uNr moodle.orig/question/restorelib.php moodle.5482_restore_quiz/question/restorelib.php
--- moodle.orig/question/restorelib.php	2006-08-19 03:09:28.000000000 +0100
+++ moodle.5482_restore_quiz/question/restorelib.php	2006-10-05 19:01:09.000000000 +0100
@@ -215,11 +215,23 @@
             if ($question_exists) {
                 $newid = $question_exists->id;
                 $creatingnewquestion = false;
+                // Fixing bug #5482: random questions have parent field set to its own id,
+                //                   see: $QTYPES['random']->get_question_options()
+                if ($question->qtype == "4") $question->parent=$newid;
             //Else, create a new question
             } else {
                 //The structure is equal to the db, so insert the question
                 $newid = insert_record ("question",$question);
                 $creatingnewquestion = true;
+                // Fixing bug #5482: random questions have parent field set to its own id.
+                if ($question->qtype == "4") {
+                    $question->parent=$newid;
+                    //we have to update the random question if the question has been inserted
+                    if ($newid) {
+                        $randomq->id=$newid; $randomq->parent=$question->parent;
+                        $status = update_record ("question",$randomq);
+                    }
+                }
             }
 
             //Save newid to backup tables
diff -uNr moodle.orig/question/type/match/questiontype.php moodle.5482_restore_quiz/question/type/match/questiontype.php
--- moodle.orig/question/type/match/questiontype.php	2006-04-06 21:35:49.000000000 +0100
+++ moodle.5482_restore_quiz/question/type/match/questiontype.php	2006-11-22 01:30:26.000000000 +0000
@@ -513,16 +513,28 @@
             //Extract the match_sub for the question and the answer
             $exploded = explode("-",$tok);
             $match_question_id = $exploded[0];
-            $match_answer_code = $exploded[1];
+            $match_answer_id = $exploded[1];
             //Get the match_sub from backup_ids (for the question)
             if (!$match_que = backup_getid($restore->backup_unique_code,"question_match_sub",$match_question_id)) {
-                echo 'Could not recode question_match_sub '.$match_question_id.'<br />';
+                echo 'Could not recode question in question_match_sub '.$match_question_id.'<br />';
             }
-            if ($in_first) {
-                $answer_field .= $match_que->new_id."-".$match_answer_code;
-                $in_first = false;
-            } else {
-                $answer_field .= ",".$match_que->new_id."-".$match_answer_code;
+            //Get the match_sub from backup_ids (for the answer)
+            if (!$match_ans = backup_getid($restore->backup_unique_code,"question_match_sub",$match_answer_id)) {
+                echo 'Could not recode answer in question_match_sub '.$match_answer_id.'<br />';
+            }
+
+            if ($match_que) {
+                //If the question hasn't response, it must be 0
+                if (!$match_ans and $match_answer_id == 0) {
+                    $match_ans->new_id = 0;
+                }
+
+                if ($in_first) {
+                    $answer_field .= $match_que->new_id."-".$match_ans->new_id;
+                    $in_first = false;
+                } else {
+                    $answer_field .= ",".$match_que->new_id."-".$match_ans->new_id;
+                }
             }
             //check for next
             $tok = strtok(",");
diff -uNr moodle.orig/question/type/random/questiontype.php moodle.5482_restore_quiz/question/type/random/questiontype.php
--- moodle.orig/question/type/random/questiontype.php	2006-04-30 18:31:33.000000000 +0100
+++ moodle.5482_restore_quiz/question/type/random/questiontype.php	2006-10-05 21:25:45.000000000 +0100
@@ -236,6 +236,43 @@
          ->compare_responses($wrappedquestion, $state, $teststate);
     }
 
+    function restore_recode_answer($state, $restore) {
+        // The answer looks like 'randomXX-ANSWER', where XX is
+        // the id of the used question and ANSWER the actual
+        // response to that question.
+        // However, there may still be old-style states around,
+        // which store the id of the wrapped quetsion in the
+        // state of the random question and store the response
+        // in a separate state for the wrapped question
+
+        global $QTYPES;
+        $answer_field = "";
+
+        if (ereg('^random([0-9]+)-(.*)$', $state->answer, $answerregs)) {
+            // Recode the question id in $answerregs[1]
+           //Get the question from backup_ids
+            $wrapped = backup_getid($restore->backup_unique_code,"question",$answerregs[1]);
+            // Fixing bug #5482: $state->question (assuming this is question->id) needed by multianswer
+            $wrappedquestion->qtype = get_field('quiz_questions', 'qtype', 'id', $wrapped->new_id);
+            $newstate = $state;
+            $newstate->question = $wrapped->new_id;
+            $newstate->answer = $answerregs[2];
+            $answer_field = 'random'.$wrapped->new_id.'-';
+
+            // Recode the answer field in $answerregs[2] depending on
+            // the qtype of question with id $answerregs[1]
+            $answer_field .= $QTYPES[$wrappedquestion->qtype]->restore_recode_answer($newstate, $restore);
+        } else {
+            // Handle old-style states
+            $answer_link = backup_getid($restore->backup_unique_code,"question",$state->answer);
+            if ($answer_link) {
+                $answer_field = $answer_link->new_id;
+            }
+        }
+
+        return $answer_field;
+    }
+
 }
 //// END OF CLASS ////
 
