MDL-73784 usertours: Support multiple PIXICON placeholder

This commit is contained in:
Huong Nguyen
2022-02-07 10:49:52 +07:00
parent 681df03204
commit 4c097bc6c0
2 changed files with 26 additions and 13 deletions
+15 -13
View File
@@ -813,21 +813,23 @@ class step {
* @return string Processed tour content
*/
public static function get_step_image_from_input(string $content): string {
global $OUTPUT;
if (preg_match('/(?<=@@PIXICON::).*?(?=@@)/', $content, $matches)) {
$bits = explode('::', $matches[0]);
$identifier = $bits[0];
$component = $bits[1];
if ($component == 'moodle') {
$component = 'core';
}
$image = \html_writer::img($OUTPUT->image_url($identifier, $component)->out(false),
'', ['class' => 'img-fluid']);
$contenttoreplace = '@@PIXICON::' . $matches[0] . '@@';
$content = str_replace($contenttoreplace, $image, $content);
if (strpos($content, '@@PIXICON') === false) {
return $content;
}
$content = preg_replace_callback('%@@PIXICON::(?P<identifier>([^::]*))::(?P<component>([^@@]*))@@%',
function(array $matches) {
global $OUTPUT;
$component = $matches['component'];
if ($component == 'moodle') {
$component = 'core';
}
return \html_writer::img($OUTPUT->image_url($matches['identifier'], $component)->out(false), '',
['class' => 'img-fluid']);
},
$content
);
return $content;
}
}
+11
View File
@@ -828,6 +828,7 @@ class step_testcase extends advanced_testcase {
* Ensure that the get_step_image_from_input function replace PIXICON placeholder with the correct images correctly.
*/
public function test_get_step_image_from_input() {
// Test step content with single image.
$stepcontent = '@@PIXICON::tour/tour_mycourses::tool_usertours@@<br>Test';
$stepcontent = \tool_usertours\step::get_step_image_from_input($stepcontent);
@@ -836,6 +837,16 @@ class step_testcase extends advanced_testcase {
$this->assertStringEndsWith('Test', $stepcontent);
$this->assertStringNotContainsString('PIXICON', $stepcontent);
// Test step content with multiple images.
$stepcontent = '@@PIXICON::tour/tour_mycourses::tool_usertours@@<br>Test<br>@@PIXICON::tour/tour_myhomepage::tool_usertours@@';
$stepcontent = \tool_usertours\step::get_step_image_from_input($stepcontent);
// If the format is correct, PIXICON placeholder will be replaced with the img tag.
$this->assertStringStartsWith('<img', $stepcontent);
// We should have 2 img tags here.
$this->assertEquals(2, substr_count($stepcontent, '<img'));
$this->assertStringNotContainsString('PIXICON', $stepcontent);
// Test step content with incorrect format.
$stepcontent = '@@PIXICON::tour/tour_mycourses<br>Test';
$stepcontent = \tool_usertours\step::get_step_image_from_input($stepcontent);