MDL-73784 usertours: Support multiple PIXICON placeholder
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user