MDL-50028 qtype_match: fix correct answer display

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.
This commit is contained in:
Tim Hunt
2015-04-28 13:08:23 +01:00
parent f7db06401e
commit be18b7c593
5 changed files with 68 additions and 16 deletions
+1 -1
View 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)];
}
-8
View 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;
-7
View 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);
}
}
+14
View 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.
@@ -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>'));
}
}