Skip to content

Commit

Permalink
MDL-73784 usertours: Support multiple PIXICON placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Feb 7, 2022
1 parent 681df03 commit 4c097bc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
28 changes: 15 additions & 13 deletions admin/tool/usertours/classes/step.php
Expand Up @@ -813,21 +813,23 @@ public static function get_string_from_input($string) {
* @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 changes: 11 additions & 0 deletions admin/tool/usertours/tests/step_test.php
Expand Up @@ -828,6 +828,7 @@ public function test_getters($key, $value) {
* 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);

Expand All @@ -836,6 +837,16 @@ public function test_get_step_image_from_input() {
$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);

Expand Down

0 comments on commit 4c097bc

Please sign in to comment.