diff --git a/question/type/random/backup/moodle2/restore_qtype_random_plugin.class.php b/question/type/random/backup/moodle2/restore_qtype_random_plugin.class.php index aaaec820389..1cda88b0aa3 100644 --- a/question/type/random/backup/moodle2/restore_qtype_random_plugin.class.php +++ b/question/type/random/backup/moodle2/restore_qtype_random_plugin.class.php @@ -47,9 +47,9 @@ class restore_qtype_random_plugin extends restore_qtype_plugin { $answer = $state->answer; $result = ''; // randomxx-yy answer format - if (preg_match('~^random([0-9]+)-(.*)$~', $answer, $matches)) { + if (preg_match('~^random([0-9]+)-~', $answer, $matches)) { $questionid = $matches[1]; - $subanswer = $matches[2]; + $subanswer = substr($answer, strlen('random' . $questionid . '-')); $newquestionid = $this->get_mappingid('question', $questionid); $questionqtype = $DB->get_field('question', 'qtype', array('id' => $newquestionid)); // Delegate subanswer recode to proper qtype, faking one question_states record diff --git a/question/type/random/questiontype.php b/question/type/random/questiontype.php index 6d9908337b8..72c5aaba309 100644 --- a/question/type/random/questiontype.php +++ b/question/type/random/questiontype.php @@ -232,7 +232,7 @@ class random_qtype extends default_questiontype { /// the other question types in that it now only needs one response /// record per question. global $QTYPES, $DB, $OUTPUT; - if (!preg_match('~^random([0-9]+)-(.*)$~', $state->responses[''], $answerregs)) { + if (!preg_match('~^random([0-9]+)-~', $state->responses[''], $matches)) { if (empty($state->responses[''])) { // This is the case if there weren't enough questions available in the category. $question->questiontext = ''. @@ -250,15 +250,16 @@ class random_qtype extends default_questiontype { echo $OUTPUT->notification("Wrapped state missing"); } } else { - if (!$wrappedquestion = $DB->get_record('question', array('id' => $answerregs[1]))) { + $questionid = $matches[1]; + if (!$wrappedquestion = $DB->get_record('question', array('id' => $questionid))) { // The teacher must have deleted this question by mistake // Convert it into a description type question with an explanation to the student $wrappedquestion = clone($question); - $wrappedquestion->id = $answerregs[1]; + $wrappedquestion->id = $questionid; $wrappedquestion->questiontext = get_string('questiondeleted', 'quiz'); $wrappedquestion->qtype = 'missingtype'; } - $state->responses[''] = (false === $answerregs[2]) ? '' : $answerregs[2]; + $state->responses[''] = substr($state->responses[''], strlen('random' . $questionid . '-')); } if (!$QTYPES[$wrappedquestion->qtype]