From 31d6befed6d674a84651b2518588eee6648d03e5 Mon Sep 17 00:00:00 2001
From: Tyler Bannister <tyler.bannister@remote-learner.net>
Date: Wed, 1 Feb 2012 11:19:15 -0500
Subject: [PATCH] Updated fix for extra space problem.

---
 backup/moodle2/restore_qtype_plugin.class.php |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/backup/moodle2/restore_qtype_plugin.class.php b/backup/moodle2/restore_qtype_plugin.class.php
index 42c6c1d..ca70c3c 100644
--- a/backup/moodle2/restore_qtype_plugin.class.php
+++ b/backup/moodle2/restore_qtype_plugin.class.php
@@ -143,12 +143,18 @@ abstract class restore_qtype_plugin extends restore_plugin {
         // The question existed, we need to map the existing question_answers
         } else {
             // Look in question_answers by answertext matching
-            $sql = 'SELECT id
-                      FROM {question_answers}
-                     WHERE question = ?
-                       AND ' . $DB->sql_compare_text('answer', 255) . ' = ' . $DB->sql_compare_text('?', 255);
-            $params = array($newquestionid, $data->answertext);
-            $newitemid = $DB->get_field_sql($sql, $params);
+            $params = array('question' => $newquestionid);
+            $answers = $DB->get_records('question_answers', $params, '', 'id, answer');
+            $newitemid = 0;
+
+            foreach ($answers as $answer) {
+                $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) {
-- 
1.7.5.4

