mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 16:32:18 +02:00
Merge branch 'MDL-82066' of https://github.com/paulholden/moodle
This commit is contained in:
commit
cf2073134a
7
.upgradenotes/MDL-82066-2024070911064670.yml
Normal file
7
.upgradenotes/MDL-82066-2024070911064670.yml
Normal file
@ -0,0 +1,7 @@
|
||||
issueNumber: MDL-82066
|
||||
notes:
|
||||
core_user:
|
||||
- message: >-
|
||||
New `\core_user\hook\extend_default_homepage` hook added to allow
|
||||
third-party plugins to extend the default homepage options for the site
|
||||
type: improved
|
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
|
@ -152,6 +152,12 @@ if ($hassiteconfig or has_any_capability($capabilities, $systemcontext)) { // sp
|
||||
}
|
||||
$choices[HOMEPAGE_MYCOURSES] = new lang_string('mycourses', 'admin');
|
||||
$choices[HOMEPAGE_USER] = new lang_string('userpreference', 'admin');
|
||||
|
||||
// Allow hook callbacks to extend options.
|
||||
$hook = new \core_user\hook\extend_default_homepage();
|
||||
\core\di::get(\core\hook\manager::class)->dispatch($hook);
|
||||
$choices += $hook->get_options();
|
||||
|
||||
$temp->add(new admin_setting_configselect('defaulthomepage', new lang_string('defaulthomepage', 'admin'),
|
||||
new lang_string('configdefaulthomepage', 'admin'), get_default_home_page(), $choices));
|
||||
if (!isset($CFG->enabledashboard) || $CFG->enabledashboard) {
|
||||
|
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) {
|
||||
|
@ -1044,8 +1044,13 @@ class core_user {
|
||||
$choices[] = HOMEPAGE_MY;
|
||||
}
|
||||
$choices[] = HOMEPAGE_MYCOURSES;
|
||||
|
||||
// Allow hook callbacks to extend options.
|
||||
$hook = new \core_user\hook\extend_default_homepage(true);
|
||||
\core\di::get(\core\hook\manager::class)->dispatch($hook);
|
||||
$choices = array_merge($choices, array_keys($hook->get_options()));
|
||||
|
||||
$preferences['user_home_page_preference'] = [
|
||||
'type' => PARAM_INT,
|
||||
'null' => NULL_ALLOWED,
|
||||
'default' => get_default_home_page(),
|
||||
'choices' => $choices,
|
||||
|
@ -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', ''),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -5164,18 +5164,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;
|
||||
@ -5212,6 +5212,8 @@ EOT;
|
||||
* @return array
|
||||
*/
|
||||
public static function get_home_page_provider(): array {
|
||||
global $CFG;
|
||||
|
||||
return [
|
||||
'No logged user' => [
|
||||
'user' => 'nologged',
|
||||
@ -5273,6 +5275,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,
|
||||
@ -5301,6 +5308,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",
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@ -5323,6 +5337,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;
|
||||
}
|
||||
|
@ -14,25 +14,21 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Form to allow user to set their default home page
|
||||
*
|
||||
* @package core_user
|
||||
* @copyright 2019 Paul Holden <paulh@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core_user\form;
|
||||
|
||||
use lang_string;
|
||||
use core\di;
|
||||
use core\hook\manager;
|
||||
use core\lang_string;
|
||||
use core_user\hook\extend_default_homepage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
require_once($CFG->dirroot . '/lib/formslib.php');
|
||||
|
||||
/**
|
||||
* Form class
|
||||
* Form to allow user to set their default home page
|
||||
*
|
||||
* @package core_user
|
||||
* @copyright 2019 Paul Holden <paulh@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
@ -55,6 +51,11 @@ class defaulthomepage_form extends \moodleform {
|
||||
}
|
||||
$options[HOMEPAGE_MYCOURSES] = new lang_string('mycourses', 'admin');
|
||||
|
||||
// Allow hook callbacks to extend options.
|
||||
$hook = new extend_default_homepage(true);
|
||||
di::get(manager::class)->dispatch($hook);
|
||||
$options += $hook->get_options();
|
||||
|
||||
$mform->addElement('select', 'defaulthomepage', get_string('defaulthomepageuser'), $options);
|
||||
$mform->addHelpButton('defaulthomepage', 'defaulthomepageuser');
|
||||
$mform->setDefault('defaulthomepage', get_default_home_page());
|
||||
|
68
user/classes/hook/extend_default_homepage.php
Normal file
68
user/classes/hook/extend_default_homepage.php
Normal file
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace core_user\hook;
|
||||
|
||||
use core\attribute\{label, tags};
|
||||
use core\lang_string;
|
||||
use core\url;
|
||||
|
||||
/**
|
||||
* Hook to allow callbacks to extend the default homepage options
|
||||
*
|
||||
* @package core_user
|
||||
* @copyright 2024 Paul Holden <paulh@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
#[label('Allows callbacks to extend the default homepage options')]
|
||||
#[tags('user')]
|
||||
final class extend_default_homepage {
|
||||
|
||||
/** @var array $options */
|
||||
private array $options = [];
|
||||
|
||||
/**
|
||||
* Hook constructor
|
||||
*
|
||||
* @param bool $userpreference Whether this is being selected as a user preference
|
||||
*/
|
||||
public function __construct(
|
||||
/** @var bool $userpreference Whether this is being selected as a user preference */
|
||||
public readonly bool $userpreference = false,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* To be called by callback to add an option
|
||||
*
|
||||
* @param url $url
|
||||
* @param lang_string|string $title
|
||||
*/
|
||||
public function add_option(url $url, lang_string|string $title): void {
|
||||
$this->options[(string) $url] = $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all added options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_options(): array {
|
||||
return $this->options;
|
||||
}
|
||||
}
|
@ -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