From 299d77dd5af9e6d82b0054e3bcd30fd756748e1e Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 28 Apr 2015 13:01:20 +0100 Subject: [PATCH] MDL-50028 qtype_match: fix correct answer display Atto's stupid tendency to create HTML like

You don't need a br at the end of a paragraph!

was breaking it. --- question/type/match/renderer.php | 2 +- question/type/match/styles.css | 8 ---- question/type/multichoice/question.php | 7 ---- question/type/questionbase.php | 14 +++++++ question/type/tests/question_definition_test.php | 53 ++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 question/type/tests/question_definition_test.php diff --git a/question/type/match/renderer.php b/question/type/match/renderer.php index 876ee889d79..3ee066c7436 100644 --- a/question/type/match/renderer.php +++ b/question/type/match/renderer.php @@ -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)]; } diff --git a/question/type/match/styles.css b/question/type/match/styles.css index 834a8be4eae..3fa9bdcc11e 100644 --- a/question/type/match/styles.css +++ b/question/type/match/styles.css @@ -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; diff --git a/question/type/multichoice/question.php b/question/type/multichoice/question.php index 0542f5c27cf..e04656d5b4a 100644 --- a/question/type/multichoice/question.php +++ b/question/type/multichoice/question.php @@ -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*

\s*~u', '', $html); - $html = preg_replace('~\s*

\s*~u', '
', $html); - $html = preg_replace('~()+$~u', '', $html); - return trim($html); - } } diff --git a/question/type/questionbase.php b/question/type/questionbase.php index 8c5ecfaf2a6..31c36f0b96d 100644 --- a/question/type/questionbase.php +++ b/question/type/questionbase.php @@ -388,6 +388,20 @@ abstract class question_definition { } /** + * 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

tag. + * @param string $html to HTML to fix up. + * @return string the fixed HTML. + */ + public function make_html_inline($html) { + $html = preg_replace('~\s*

\s*~u', '', $html); + $html = preg_replace('~\s*

\s*~u', '
', $html); + $html = preg_replace('~()+$~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. * @param question_display_options $options the options that control display of the question. diff --git a/question/type/tests/question_definition_test.php b/question/type/tests/question_definition_test.php new file mode 100644 index 00000000000..3bcd50f531c --- /dev/null +++ b/question/type/tests/question_definition_test.php @@ -0,0 +1,53 @@ +. + +/** + * 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('

Frog

')); + $this->assertEquals('Frog', $mc->make_html_inline('

Frog

')); + $this->assertEquals('Frog
Toad', $mc->make_html_inline("

Frog

\n

Toad

")); + $this->assertEquals('Graph', + $mc->make_html_inline( + '

Graph

')); + $this->assertEquals("Frog
XXX Graph", + $mc->make_html_inline("

Frog

\n\r +

XXX Graph

")); + $this->assertEquals('Frog', $mc->make_html_inline('

Frog

')); + $this->assertEquals('Frog
†', $mc->make_html_inline('

Frog

')); + } +} -- 2.11.4.GIT