mirror of
https://github.com/moodle/moodle.git
synced 2025-01-17 21:49:15 +01:00
MDL-82066 user: define methods for handling default homepage URLs.
Per the previous commit, default homepage configuration (set either for the site or as a user preference) can now be extended by third party hook callbacks, in which case a URL is stored.
This commit is contained in:
parent
a22ccfc7d0
commit
9d621f5da1
12
.upgradenotes/MDL-82066-2024070915322412.yml
Normal file
12
.upgradenotes/MDL-82066-2024070915322412.yml
Normal file
@ -0,0 +1,12 @@
|
||||
issueNumber: MDL-82066
|
||||
notes:
|
||||
core:
|
||||
- message: >-
|
||||
The `get_home_page()` method can now return new constant `HOMEPAGE_URL`,
|
||||
applicable when a third-party hook has extended the default homepage
|
||||
options for the site
|
||||
|
||||
|
||||
A new method, `get_default_home_page_url()` has been added which will
|
||||
return the correct URL when this constant is returned
|
||||
type: changed
|
12
index.php
12
index.php
@ -33,10 +33,13 @@ require_once($CFG->libdir .'/filelib.php');
|
||||
|
||||
redirect_if_major_upgrade_required();
|
||||
|
||||
// Redirect logged-in users to homepage if required.
|
||||
$redirect = optional_param('redirect', 1, PARAM_BOOL);
|
||||
|
||||
$urlparams = array();
|
||||
if (!empty($CFG->defaulthomepage) &&
|
||||
($CFG->defaulthomepage == HOMEPAGE_MY || $CFG->defaulthomepage == HOMEPAGE_MYCOURSES) &&
|
||||
optional_param('redirect', 1, PARAM_BOOL) === 0
|
||||
$redirect === 0
|
||||
) {
|
||||
$urlparams['redirect'] = 0;
|
||||
}
|
||||
@ -68,9 +71,8 @@ if ($hassiteconfig && moodle_needs_upgrading()) {
|
||||
// If site registration needs updating, redirect.
|
||||
\core\hub\registration::registration_reminder('/index.php');
|
||||
|
||||
if (get_home_page() != HOMEPAGE_SITE) {
|
||||
// Redirect logged-in users to My Moodle overview if required.
|
||||
$redirect = optional_param('redirect', 1, PARAM_BOOL);
|
||||
$homepage = get_home_page();
|
||||
if ($homepage != HOMEPAGE_SITE) {
|
||||
if (optional_param('setdefaulthome', false, PARAM_BOOL)) {
|
||||
set_user_preference('user_home_page_preference', HOMEPAGE_SITE);
|
||||
} else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && $redirect === 1) {
|
||||
@ -78,6 +80,8 @@ if (get_home_page() != HOMEPAGE_SITE) {
|
||||
redirect($CFG->wwwroot .'/my/');
|
||||
} else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MYCOURSES) && $redirect === 1) {
|
||||
redirect($CFG->wwwroot .'/my/courses.php');
|
||||
} else if ($homepage == HOMEPAGE_URL) {
|
||||
redirect(get_default_home_page_url());
|
||||
} else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER)) {
|
||||
$frontpagenode = $PAGE->settingsnav->find('frontpage', null);
|
||||
if ($frontpagenode) {
|
||||
|
@ -533,6 +533,10 @@ define('HOMEPAGE_USER', 2);
|
||||
* The home page should be the users my courses page
|
||||
*/
|
||||
define('HOMEPAGE_MYCOURSES', 3);
|
||||
/**
|
||||
* The home page is defined as a URL
|
||||
*/
|
||||
define('HOMEPAGE_URL', 4);
|
||||
|
||||
/**
|
||||
* URL of the Moodle sites registration portal.
|
||||
@ -9910,12 +9914,16 @@ function get_home_page() {
|
||||
} else if ($CFG->defaulthomepage == HOMEPAGE_MYCOURSES && !isguestuser()) {
|
||||
return HOMEPAGE_MYCOURSES;
|
||||
} else if ($CFG->defaulthomepage == HOMEPAGE_USER && !isguestuser()) {
|
||||
$userhomepage = (int) get_user_preferences('user_home_page_preference', $defaultpage);
|
||||
$userhomepage = get_user_preferences('user_home_page_preference', $defaultpage);
|
||||
if (empty($CFG->enabledashboard) && $userhomepage == HOMEPAGE_MY) {
|
||||
// If the user was using the dashboard but it's disabled, return the default home page.
|
||||
$userhomepage = $defaultpage;
|
||||
} else if (clean_param($userhomepage, PARAM_LOCALURL)) {
|
||||
return HOMEPAGE_URL;
|
||||
}
|
||||
return $userhomepage;
|
||||
return (int) $userhomepage;
|
||||
} else if (clean_param($CFG->defaulthomepage, PARAM_LOCALURL)) {
|
||||
return HOMEPAGE_URL;
|
||||
}
|
||||
}
|
||||
return HOMEPAGE_SITE;
|
||||
@ -9933,6 +9941,31 @@ function get_default_home_page(): int {
|
||||
return (!isset($CFG->enabledashboard) || $CFG->enabledashboard) ? HOMEPAGE_MY : HOMEPAGE_MYCOURSES;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default home page as a URL where it has been configured as one via site configuration or user preference
|
||||
*
|
||||
* It is assumed that callers have already checked that {@see get_home_page} returns {@see HOMEPAGE_URL} prior to
|
||||
* calling this method
|
||||
*
|
||||
* @return \core\url|null
|
||||
*/
|
||||
function get_default_home_page_url(): ?\core\url {
|
||||
global $CFG;
|
||||
|
||||
if ($defaulthomepage = clean_param($CFG->defaulthomepage, PARAM_LOCALURL)) {
|
||||
return new \core\url($defaulthomepage);
|
||||
}
|
||||
|
||||
if ($CFG->defaulthomepage == HOMEPAGE_USER) {
|
||||
$userhomepage = get_user_preferences('user_home_page_preference');
|
||||
if ($userhomepage = clean_param($userhomepage, PARAM_LOCALURL)) {
|
||||
return new \core\url($userhomepage);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of a course to be displayed when showing a list of courses.
|
||||
* By default this is just $course->fullname but user can configure it. The
|
||||
|
@ -1352,14 +1352,14 @@ class global_navigation extends navigation_node {
|
||||
}
|
||||
|
||||
$homepage = get_home_page();
|
||||
if ($homepage == HOMEPAGE_SITE) {
|
||||
// We are using the site home for the root element.
|
||||
if ($homepage == HOMEPAGE_MY) {
|
||||
// We are using the users my moodle for the root element.
|
||||
$properties = array(
|
||||
'key' => 'home',
|
||||
'key' => 'myhome',
|
||||
'type' => navigation_node::TYPE_SYSTEM,
|
||||
'text' => get_string('home'),
|
||||
'action' => new moodle_url('/'),
|
||||
'icon' => new pix_icon('i/home', '')
|
||||
'text' => get_string('myhome'),
|
||||
'action' => new moodle_url('/my/'),
|
||||
'icon' => new pix_icon('i/dashboard', ''),
|
||||
);
|
||||
} else if ($homepage == HOMEPAGE_MYCOURSES) {
|
||||
// We are using the user's course summary page for the root element.
|
||||
@ -1371,13 +1371,13 @@ class global_navigation extends navigation_node {
|
||||
'icon' => new pix_icon('i/course', '')
|
||||
);
|
||||
} else {
|
||||
// We are using the users my moodle for the root element.
|
||||
// We are using the site home for the root element.
|
||||
$properties = array(
|
||||
'key' => 'myhome',
|
||||
'key' => 'home',
|
||||
'type' => navigation_node::TYPE_SYSTEM,
|
||||
'text' => get_string('myhome'),
|
||||
'action' => new moodle_url('/my/'),
|
||||
'icon' => new pix_icon('i/dashboard', '')
|
||||
'text' => get_string('home'),
|
||||
'action' => new moodle_url('/'),
|
||||
'icon' => new pix_icon('i/home', ''),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -5162,18 +5162,18 @@ EOT;
|
||||
* @dataProvider get_home_page_provider
|
||||
* @param string $user Whether the user is logged, guest or not logged.
|
||||
* @param int $expected Expected value after calling the get_home_page method.
|
||||
* @param int|null $defaulthomepage The $CFG->defaulthomepage setting value.
|
||||
* @param int|string|null $defaulthomepage The $CFG->defaulthomepage setting value.
|
||||
* @param int|null $enabledashboard Whether the dashboard should be enabled or not.
|
||||
* @param int|null $userpreference User preference for the home page setting.
|
||||
* @param int|string|null $userpreference User preference for the home page setting.
|
||||
* $param int|null $allowguestmymoodle The $CFG->allowguestmymoodle setting value.
|
||||
* @covers ::get_home_page
|
||||
*/
|
||||
public function test_get_home_page(
|
||||
string $user,
|
||||
int $expected,
|
||||
?int $defaulthomepage = null,
|
||||
int|string|null $defaulthomepage = null,
|
||||
?int $enabledashboard = null,
|
||||
?int $userpreference = null,
|
||||
int|string|null $userpreference = null,
|
||||
?int $allowguestmymoodle = null,
|
||||
): void {
|
||||
global $CFG, $USER;
|
||||
@ -5210,6 +5210,8 @@ EOT;
|
||||
* @return array
|
||||
*/
|
||||
public static function get_home_page_provider(): array {
|
||||
global $CFG;
|
||||
|
||||
return [
|
||||
'No logged user' => [
|
||||
'user' => 'nologged',
|
||||
@ -5271,6 +5273,11 @@ EOT;
|
||||
'defaulthomepage' => HOMEPAGE_SITE,
|
||||
'enabledashboard' => 0,
|
||||
],
|
||||
'Logged user. URL set as default home page.' => [
|
||||
'user' => 'logged',
|
||||
'expected' => HOMEPAGE_URL,
|
||||
'defaulthomepage' => "{$CFG->wwwroot}/home",
|
||||
],
|
||||
'Logged user. User preference set as default page with dashboard enabled and user preference set to dashboard' => [
|
||||
'user' => 'logged',
|
||||
'expected' => HOMEPAGE_MY,
|
||||
@ -5299,6 +5306,13 @@ EOT;
|
||||
'enabledashboard' => 0,
|
||||
'userpreference' => HOMEPAGE_MYCOURSES,
|
||||
],
|
||||
'Logged user. User preference set as default page with user preference set to URL.' => [
|
||||
'user' => 'logged',
|
||||
'expected' => HOMEPAGE_URL,
|
||||
'defaulthomepage' => HOMEPAGE_USER,
|
||||
'enabledashboard' => null,
|
||||
'userpreference' => "{$CFG->wwwroot}/home",
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@ -5321,6 +5335,31 @@ EOT;
|
||||
$this->assertEquals(HOMEPAGE_MYCOURSES, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting default home page for {@see HOMEPAGE_URL}
|
||||
*
|
||||
* @covers ::get_default_home_page_url
|
||||
*/
|
||||
public function test_get_default_home_page_url(): void {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$this->assertNull(get_default_home_page_url());
|
||||
|
||||
// Site configuration.
|
||||
$CFG->defaulthomepage = "{$CFG->wwwroot}/home";
|
||||
$this->assertEquals($CFG->defaulthomepage, get_default_home_page_url());
|
||||
|
||||
// User preference.
|
||||
$CFG->defaulthomepage = HOMEPAGE_USER;
|
||||
|
||||
$userpreference = "{$CFG->wwwroot}/about";
|
||||
set_user_preference('user_home_page_preference', $userpreference);
|
||||
$this->assertEquals($userpreference, get_default_home_page_url());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the get_performance_info function with regard to locks.
|
||||
*
|
||||
|
@ -362,6 +362,9 @@ function core_login_get_return_url() {
|
||||
$urltogo = $CFG->wwwroot.'/my/courses.php';
|
||||
}
|
||||
}
|
||||
if ($homepage === HOMEPAGE_URL) {
|
||||
$urltogo = (string) get_default_home_page_url();
|
||||
}
|
||||
}
|
||||
return $urltogo;
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ Feature: Contact site support method and availability can be customised
|
||||
Scenario: Contact site support can be disabled
|
||||
Given the following config values are set as admin:
|
||||
| supportavailability | 0 |
|
||||
| defaulthomepage | home |
|
||||
| defaulthomepage | 0 |
|
||||
# Confirm unauthenticated visitor cannot see the option.
|
||||
When I am on site homepage
|
||||
Then I should not see "Contact site support" in the "page-footer" "region"
|
||||
@ -98,7 +98,7 @@ Feature: Contact site support method and availability can be customised
|
||||
Given the following config values are set as admin:
|
||||
| supportavailability | 0 |
|
||||
| supportpage | profile.php |
|
||||
| defaulthomepage | home |
|
||||
| defaulthomepage | 0 |
|
||||
When I log in as "user1"
|
||||
And I am on the "user > Contact Site Support" page
|
||||
Then I should see "Acceptance test site" in the "page-header" "region"
|
||||
|
Loading…
x
Reference in New Issue
Block a user