MDL-50028 qtype_match: fix correct answer display
authorTim Hunt <T.J.Hunt@open.ac.uk>
Tue, 28 Apr 2015 12:01:20 +0000 (13:01 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Tue, 28 Apr 2015 12:07:26 +0000 (13:07 +0100)
Atto's stupid tendency to create HTML like
<p>You don't need a br at the end of a paragraph!</br></p>
was breaking it.

question/type/match/renderer.php
question/type/match/styles.css
question/type/multichoice/question.php
question/type/questionbase.php
question/type/tests/question_definition_test.php [new file with mode: 0644]

index 876ee88..3ee066c 100644 (file)
@@ -137,7 +137,7 @@ class qtype_match_renderer extends qtype_with_combined_feedback_renderer {
         $choices = $this->format_choices($question);
         $right = array();
         foreach ($stemorder as $key => $stemid) {
-            $right[] = $this->format_stem_text($qa, $stemid) . ' – ' .
+            $right[] = $question->make_html_inline($this->format_stem_text($qa, $stemid)) . ' – ' .
                     $choices[$question->get_right_choice_for($stemid)];
         }
 
index 834a8be..3fa9bdc 100644 (file)
@@ -1,11 +1,3 @@
-.que.match .feedback .rightanswer * {
-    display: inline;
-}
-.que.match .feedback .rightanswer script {
-    display: none;
-}
-
-
 /* Editing form. */
 body#page-question-type-match div[id^=fitem_id_][id*=subquestions_] {
     background: #EEE;
index 0542f5c..e04656d 100644 (file)
@@ -124,13 +124,6 @@ abstract class qtype_multichoice_base extends question_graded_automatically {
                     $args, $forcedownload);
         }
     }
-
-    public function make_html_inline($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 8c5ecfa..31c36f0 100644 (file)
@@ -387,6 +387,20 @@ abstract class question_definition {
                 $qa, 'question', 'generalfeedback', $this->id);
     }
 
+    /**
+     * Take some HTML that should probably already be a single line, like a
+     * multiple choice choice, or the corresponding feedback, and make it so that
+     * it is suitable to go in a place where the HTML must be inline, like inside a <p> tag.
+     * @param string $html to HTML to fix up.
+     * @return string the fixed HTML.
+     */
+    public function make_html_inline($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);
+    }
+
     /**
      * Checks whether the users is allow to be served a particular file.
      * @param question_attempt $qa the question attempt being displayed.
diff --git a/question/type/tests/question_definition_test.php b/question/type/tests/question_definition_test.php
new file mode 100644 (file)
index 0000000..3bcd50f
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Unit tests for the question_definition base classes.
+ *
+ * @package   core_question
+ * @copyright 2015 The Open University
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+global $CFG;
+require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
+
+
+/**
+ * Test for question_definition base classes.
+ *
+ * @copyright  2015 The Open University
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class question_definition_testcase extends advanced_testcase {
+    public function test_make_html_inline() {
+        // Base class is abstract, so we need to pick one qusetion type to test this method.
+        $mc = test_question_maker::make_a_multichoice_single_question();
+        $this->assertEquals('Frog', $mc->make_html_inline('<p>Frog</p>'));
+        $this->assertEquals('Frog', $mc->make_html_inline('<p>Frog<br /></p>'));
+        $this->assertEquals('Frog<br />Toad', $mc->make_html_inline("<p>Frog</p>\n<p>Toad</p>"));
+        $this->assertEquals('<img src="http://example.com/pic.png" alt="Graph" />',
+                $mc->make_html_inline(
+                    '<p><img src="http://example.com/pic.png" alt="Graph" /></p>'));
+        $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>'));
+    }
+}