Merge branch 'MDL-57017-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
David Monllao 2016-11-21 12:40:26 +08:00
commit 7d432be4e9
4 changed files with 35 additions and 7 deletions

View File

@ -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);
});
}

View File

@ -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);

View File

@ -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';

View File

@ -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++) {