diff --git a/admin/tool/usertours/classes/cache.php b/admin/tool/usertours/classes/cache.php index cefc7794d65..71857e4eec8 100644 --- a/admin/tool/usertours/classes/cache.php +++ b/admin/tool/usertours/classes/cache.php @@ -76,15 +76,22 @@ EOF; /** * Fetch all enabled tours matching the specified target. * - * @param string $targetmatch The URL to match. + * @param moodle_url $targetmatch The URL to match. */ - public static function get_matching_tourdata($targetmatch) { + public static function get_matching_tourdata(\moodle_url $targetmatch) { $tours = self::get_enabled_tourdata(); - return array_filter($tours, function($tour) use ($targetmatch) { + // Attempt to determine whether this is the front page. + // This is a special case because the frontpage uses a shortened page path making it difficult to detect exactly. + $isfrontpage = $targetmatch->compare(new \moodle_url('/'), URL_MATCH_BASE); + $target = $targetmatch->out_as_local_url(); + return array_filter($tours, function($tour) use ($isfrontpage, $target) { + if ($isfrontpage && $tour->pathmatch === 'FRONTPAGE') { + return true; + } $pattern = preg_quote($tour->pathmatch, '@'); $pattern = str_replace('%', '.*', $pattern); - return !!preg_match("@{$pattern}@", $targetmatch); + return !!preg_match("@{$pattern}@", $target); }); } diff --git a/admin/tool/usertours/classes/manager.php b/admin/tool/usertours/classes/manager.php index af94d766628..f4ffa04beac 100644 --- a/admin/tool/usertours/classes/manager.php +++ b/admin/tool/usertours/classes/manager.php @@ -559,7 +559,7 @@ class manager { public static function get_matching_tours(\moodle_url $pageurl) { global $PAGE; - $tours = cache::get_matching_tourdata($pageurl->out_as_local_url()); + $tours = cache::get_matching_tourdata($pageurl); foreach ($tours as $record) { $tour = tour::load_from_record($record); diff --git a/admin/tool/usertours/lang/en/tool_usertours.php b/admin/tool/usertours/lang/en/tool_usertours.php index 32cd9b4269d..87508d59228 100644 --- a/admin/tool/usertours/lang/en/tool_usertours.php +++ b/admin/tool/usertours/lang/en/tool_usertours.php @@ -79,7 +79,10 @@ Some example values include: * /my/% - to match the Dashboard * /course/view.php?id=2 - to match a specific course * /mod/forum/view.php% - to match the forum discussion list -* /user/profile.php% - to match the user profile page'; +* /user/profile.php% - to match the user profile page + +If you wish to display a tour on the Site Home page, you can use the value: "FRONTPAGE". +'; $string['placement'] = 'Placement'; $string['pluginname'] = 'User tours'; $string['resettouronpage'] = 'Reset user tour on this page'; diff --git a/admin/tool/usertours/tests/cache_test.php b/admin/tool/usertours/tests/cache_test.php index fdbdde0b0aa..4e0c2b12911 100644 --- a/admin/tool/usertours/tests/cache_test.php +++ b/admin/tool/usertours/tests/cache_test.php @@ -150,6 +150,14 @@ class cache_testcase extends advanced_testcase { 'name' => 'my_glob_2', 'pathmatch' => '/my/%' ], + (object) [ + 'name' => 'frontpage_only', + 'pathmatch' => 'FRONTPAGE' + ], + (object) [ + 'name' => 'frontpage_match', + 'pathmatch' => '/?%' + ], ]; return [ @@ -163,6 +171,16 @@ class cache_testcase extends advanced_testcase { '/my/view.php', ['my_exact_1', 'my_glob_1', 'my_glob_2'], ], + 'Special constant FRONTPAGE must match front page only' => [ + $tourconfigs, + '/', + ['frontpage_only'], + ], + 'Standard frontpage URL matches both the special constant, and a correctly formed pathmatch' => [ + $tourconfigs, + '/?redirect=0', + ['frontpage_only', 'frontpage_match'], + ], ]; } @@ -181,7 +199,7 @@ class cache_testcase extends advanced_testcase { $this->helper_create_step((object) ['tourid' => $tour->get_id()]); } - $matches = \tool_usertours\cache::get_matching_tourdata((new moodle_url($targetmatch))->out_as_local_url()); + $matches = \tool_usertours\cache::get_matching_tourdata(new moodle_url($targetmatch)); $this->assertCount(count($expected), $matches); for ($i = 0; $i < count($matches); $i++) {