From a306ceb1ecda39cbd7f8da8346c0ae9dcb84dcb1 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 27 Aug 2012 03:24:10 +0200 Subject: [PATCH] MDL-30018 restore: better answer matching. Credit goes to Tyler Bannister, thanks! On backup, contents are cleaned to be safe utf8. That was leading to some matching problems on restore against not cleaned DB contents (for already existing questions). This commit adds one fallback method to perform the match against cleaned DB contents. --- backup/moodle2/restore_qtype_plugin.class.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backup/moodle2/restore_qtype_plugin.class.php b/backup/moodle2/restore_qtype_plugin.class.php index 40b06ac51a2..99365bad2f2 100644 --- a/backup/moodle2/restore_qtype_plugin.class.php +++ b/backup/moodle2/restore_qtype_plugin.class.php @@ -154,6 +154,22 @@ abstract class restore_qtype_plugin extends restore_plugin { AND ' . $DB->sql_compare_text('answer', 255) . ' = ' . $DB->sql_compare_text('?', 255); $params = array($newquestionid, $data->answertext); $newitemid = $DB->get_field_sql($sql, $params); + + // Not able to find the answer, let's try cleaning the answertext + // of all the question answers in DB as slower fallback. MDL-30018. + if (!$newitemid) { + $params = array('question' => $newquestionid); + $answers = $DB->get_records('question_answers', $params, '', 'id, answer'); + foreach ($answers as $answer) { + // Clean in the same way than {@link xml_writer::xml_safe_utf8()}. + $clean = preg_replace('/[\x-\x8\xb-\xc\xe-\x1f\x7f]/is','', $answer->answer); // Clean CTRL chars. + $clean = preg_replace("/\r\n|\r/", "\n", $clean); // Normalize line ending. + if ($clean === $data->answertext) { + $newitemid = $data->id; + } + } + } + // If we haven't found the newitemid, something has gone really wrong, question in DB // is missing answers, exception if (!$newitemid) {