MDL-36571 qtype multichoice: don't corrupt unicode characters.
authorTim Hunt <T.J.Hunt@open.ac.uk>
Mon, 19 Nov 2012 17:21:00 +0000 (17:21 +0000)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Mon, 19 Nov 2012 18:20:38 +0000 (18:20 +0000)
It seems that \s includes non-breaking space, and the typical one-byte
representation of that is the same as some parts of multibyte unicode
characters. Therefore, you need to include the u modifer on the regular
expressions.

Also, remove any number of <br /> at the end of the answer.

Thanks to Joseph Rézeau and Jean-Michel Vedrine for working out what the
problem was, and how to fix it.

question/type/multichoice/question.php
question/type/multichoice/tests/question_test.php

index afca6a4..8dd7086 100644 (file)
@@ -126,10 +126,10 @@ abstract class qtype_multichoice_base extends question_graded_automatically {
     }
 
     public function make_html_inline($html) {
-        $html = preg_replace('~\s*<p>\s*~', '', $html);
-        $html = preg_replace('~\s*</p>\s*~', '<br />', $html);
-        $html = preg_replace('~<br />$~', '', $html);
-        return $html;
+        $html = preg_replace('~\s*<p>\s*~u', '', $html);
+        $html = preg_replace('~\s*</p>\s*~u', '<br />', $html);
+        $html = preg_replace('~(<br\s*/?>)+$~u', '', $html);
+        return trim($html);
     }
 }
 
index fe5ba51..cbd3827 100644 (file)
@@ -147,6 +147,8 @@ class qtype_multichoice_single_question_test extends advanced_testcase {
         $this->assertEquals("Frog<br />XXX <img src='http://example.com/pic.png' alt='Graph' />",
                 $mc->make_html_inline(" <p> Frog </p> \n\r
                     <p> XXX <img src='http://example.com/pic.png' alt='Graph' /> </p> "));
+        $this->assertEquals('Frog', $mc->make_html_inline('<p>Frog</p><p></p>'));
+        $this->assertEquals('Frog<br />†', $mc->make_html_inline('<p>Frog</p><p>†</p>'));
     }
 }