From f88bd546eb2fac4408caf4e63c4409999d03cb6e Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Mon, 19 Nov 2012 17:46:25 +0000 Subject: [PATCH] MDL-36683 qtype match restore: better subq indentification. This is very similar to what MDL-30018 did for question_answers. --- .../restore_qtype_match_plugin.class.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/question/type/match/backup/moodle2/restore_qtype_match_plugin.class.php b/question/type/match/backup/moodle2/restore_qtype_match_plugin.class.php index 85e248f5359..b58c47acd10 100644 --- a/question/type/match/backup/moodle2/restore_qtype_match_plugin.class.php +++ b/question/type/match/backup/moodle2/restore_qtype_match_plugin.class.php @@ -137,6 +137,25 @@ class restore_qtype_match_plugin extends restore_qtype_plugin { array($newquestionid, $data->questiontext, $data->answertext), 'id', IGNORE_MULTIPLE); + // Not able to find the answer, let's try cleaning the answertext + // of all the question answers in DB as slower fallback. MDL-36683 / MDL-30018. + if (!$sub) { + $params = array('question' => $newquestionid); + $potentialsubs = $DB->get_records('question_match_sub', array('question' => $newquestionid), '', 'id, questiontext, answertext'); + foreach ($potentialsubs as $potentialsub) { + // Clean in the same way than {@link xml_writer::xml_safe_utf8()}. + $cleanquestion = preg_replace('/[\x-\x8\xb-\xc\xe-\x1f\x7f]/is', '', $potentialsub->questiontext); // Clean CTRL chars. + $cleanquestion = preg_replace("/\r\n|\r/", "\n", $cleanquestion); // Normalize line ending. + + $cleananswer = preg_replace('/[\x-\x8\xb-\xc\xe-\x1f\x7f]/is', '', $potentialsub->answertext); // Clean CTRL chars. + $cleananswer = preg_replace("/\r\n|\r/", "\n", $cleananswer); // Normalize line ending. + + if ($cleanquestion === $data->questiontext && $cleananswer == $data->answertext) { + $sub = $potentialsub; + } + } + } + // Found, let's create the mapping if ($sub) { $this->set_mapping('question_match_sub', $oldid, $sub->id);