MDL-78662 core_question: Create utility function to format question.

This commit is contained in:
hieuvu 2024-03-11 11:03:07 +07:00
parent 67b2c43225
commit 6d34386ca9
3 changed files with 45 additions and 0 deletions

View File

@ -1134,6 +1134,20 @@ abstract class question_utils {
return $editoroptions;
}
/**
* Format question fragment string and apply filtering,
*
* @param string $text current text that we want to be apply filters.
* @param context $context of the page question are in.
* @return string result has been modified by filters.
*/
public static function format_question_fragment(string $text, context $context): string {
global $PAGE;
$filtermanager = \filter_manager::instance();
$filtermanager->setup_page_for_filters($PAGE, $context);
return $filtermanager->filter_string($text, $context);
}
}

View File

@ -226,4 +226,32 @@ class questionutils_test extends \advanced_testcase {
$this->assertSame(-1.5, question_utils::clean_param_mark('-1.5'));
$this->assertSame(-1.5, question_utils::clean_param_mark('-1,5'));
}
/**
* Test the content is being filtered by filters.
*
* @covers ::format_question_fragment
*/
public function test_format_question_fragment(): void {
global $CFG;
require_once($CFG->libdir . '/filterlib.php');
$this->resetAfterTest();
// Set few filters on.
filter_set_global_state('multilang', TEXTFILTER_ON);
filter_set_global_state('mathjaxloader', TEXTFILTER_ON);
filter_set_applies_to_strings('multilang', 1);
filter_set_applies_to_strings('mathjaxloader', 1);
$systemcontext = \context_system::instance();
$input = 'Some inline math \\( y = x^2 \\) and multi lang with html tag
<span lang="en" class="multilang"><b>English</b></span><span lang="fr" class="multilang">Français</span>';
$expected = question_utils::format_question_fragment($input, $systemcontext);
// The data should only be filtered by mathjax and multi lang filter. HTML tags should not be affeacted.
$this->assertStringContainsString('<span class="filter_mathjaxloader_equation">Some inline math', $expected);
$this->assertStringContainsString('<span class="nolink">\( y = x^2 \)</span>', $expected);
$this->assertStringNotContainsString('<span lang="en" class="multilang">', $expected);
$this->assertStringContainsString('<b>English</b>', $expected);
}
}

View File

@ -83,6 +83,9 @@ This files describes API changes for code that uses the question API.
.question-bank-table. This applies to the same styles to preview on the qbank_columnsortorder admin screen. It is important
that the styles match on these pages so that the defaults have the expected result in the question bank.
10) A new utility function format_question_fragment is created.
format_question_fragment is added so that question content can filter base on filters.
=== 4.2 ===
1) The question/qengine.js has been deprecated. We create core_question/question_engine