MDL-73231 my: Allow my courses to be a homepage

This commit is contained in:
Mathew May 2021-12-02 13:43:15 +08:00
parent 222c8cc77c
commit 54e098c5a5
11 changed files with 38 additions and 9 deletions

View File

@ -188,6 +188,7 @@ preferences,moodle|/user/preferences.php|t/preferences',
$choices = array(
HOMEPAGE_SITE => new lang_string('site'),
HOMEPAGE_MY => new lang_string('mymoodle', 'admin'),
HOMEPAGE_MYCOURSES => new lang_string('mycourses', 'admin'),
HOMEPAGE_USER => new lang_string('userpreference', 'admin')
);
$temp->add(new admin_setting_configselect('defaulthomepage', new lang_string('defaulthomepage', 'admin'),

View File

@ -70,11 +70,16 @@
}
/// Go to my-moodle page instead of homepage if defaulthomepage enabled
if (!has_capability('moodle/site:config',context_system::instance()) and !empty($CFG->defaulthomepage) && $CFG->defaulthomepage == HOMEPAGE_MY and !isguestuser()) {
if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') {
if (!has_capability('moodle/site:config',
context_system::instance()) and !empty($CFG->defaulthomepage) and !isguestuser()) {
if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') {
if ($CFG->defaulthomepage == HOMEPAGE_MY) {
$urltogo = $CFG->wwwroot.'/my/';
} else if ($CFG->defaulthomepage == HOMEPAGE_MYCOURSES) {
$urltogo = $CFG->wwwroot.'/my/courses.php';
}
}
}
redirect($urltogo);

View File

@ -34,7 +34,10 @@ require_once($CFG->libdir .'/filelib.php');
redirect_if_major_upgrade_required();
$urlparams = array();
if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && optional_param('redirect', 1, PARAM_BOOL) === 0) {
if (!empty($CFG->defaulthomepage) &&
($CFG->defaulthomepage == HOMEPAGE_MY || $CFG->defaulthomepage == HOMEPAGE_MYCOURSES) &&
optional_param('redirect', 1, PARAM_BOOL) === 0
) {
$urlparams['redirect'] = 0;
}
$PAGE->set_url('/', $urlparams);
@ -71,6 +74,8 @@ if (get_home_page() != HOMEPAGE_SITE) {
set_user_preference('user_home_page_preference', HOMEPAGE_SITE);
} else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY) && $redirect === 1) {
redirect($CFG->wwwroot .'/my/');
} else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MYCOURSES) && $redirect === 1) {
redirect($CFG->wwwroot .'/my/courses.php');
} else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER)) {
$frontpagenode = $PAGE->settingsnav->find('frontpage', null);
if ($frontpagenode) {

View File

@ -43,7 +43,8 @@ class primary extends view {
null, 'home', new \pix_icon('i/home', ''));
if (isloggedin() ) {
if (!isguestuser()) {
if (get_home_page() == HOMEPAGE_MY) {
$homepage = get_home_page();
if ($homepage == HOMEPAGE_MY || $homepage == HOMEPAGE_MYCOURSES) {
// We need to stop automatic redirection.
$sitehome->action->param('redirect', '0');
}

View File

@ -967,7 +967,7 @@ class core_user {
return $USER->id == $user->id && has_capability('moodle/blog:view', context_system::instance());
});
$preferences['user_home_page_preference'] = array('type' => PARAM_INT, 'null' => NULL_ALLOWED, 'default' => HOMEPAGE_MY,
'choices' => array(HOMEPAGE_SITE, HOMEPAGE_MY),
'choices' => array(HOMEPAGE_SITE, HOMEPAGE_MY, HOMEPAGE_MYCOURSES),
'permissioncallback' => function ($user, $preferencename) {
global $CFG;
return (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER));

View File

@ -489,6 +489,10 @@ define('HOMEPAGE_MY', 1);
* The home page can be chosen by the user
*/
define('HOMEPAGE_USER', 2);
/**
* The home page should be the users my courses page
*/
define('HOMEPAGE_MYCOURSES', 3);
/**
* URL of the Moodle sites registration portal.
@ -10348,6 +10352,8 @@ function get_home_page() {
if (isloggedin() && !isguestuser() && !empty($CFG->defaulthomepage)) {
if ($CFG->defaulthomepage == HOMEPAGE_MY) {
return HOMEPAGE_MY;
} else if ($CFG->defaulthomepage == HOMEPAGE_MYCOURSES) {
return HOMEPAGE_MYCOURSES;
} else {
return (int)get_user_preferences('user_home_page_preference', HOMEPAGE_MY);
}

View File

@ -1338,7 +1338,8 @@ class global_navigation extends navigation_node {
$this->rootnodes['home'] = $this->add(get_string('sitehome'), new moodle_url('/'),
self::TYPE_SETTING, null, 'home', new pix_icon('i/home', ''));
$this->rootnodes['home']->showinflatnavigation = true;
if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY)) {
if (!empty($CFG->defaulthomepage) &&
($CFG->defaulthomepage == HOMEPAGE_MY || $CFG->defaulthomepage == HOMEPAGE_MYCOURSES)) {
// We need to stop automatic redirection
$this->rootnodes['home']->action->param('redirect', '0');
}
@ -4916,7 +4917,8 @@ class settings_navigation extends navigation_node {
// This should be set to false as we don't want to show this to the user. It's only for generating the correct
// breadcrumb.
$dashboard->display = false;
if (get_home_page() == HOMEPAGE_MY) {
$homepage = get_home_page();
if (($homepage == HOMEPAGE_MY || $homepage == HOMEPAGE_MYCOURSES)) {
$dashboard->mainnavonly = true;
}

View File

@ -357,6 +357,11 @@ function core_login_get_return_url() {
$urltogo = $CFG->wwwroot.'/my/';
}
}
if ($homepage === HOMEPAGE_MYCOURSES && !isguestuser()) {
if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') {
$urltogo = $CFG->wwwroot.'/my/courses.php';
}
}
}
return $urltogo;
}

View File

@ -10,7 +10,7 @@ Feature: Primary navigation
| user1 | User | One | user1@example.com |
@javascript @theme_boost
Scenario Outline: Admin sets defaulthomepage to 0,1 and verify the landing page and site home link
Scenario Outline: Admin sets defaulthomepage and verify the landing page and site home link
Given I log in as "admin"
And the following config values are set as admin:
| defaulthomepage | <defaulthomepageset> |
@ -22,9 +22,10 @@ Feature: Primary navigation
| defaulthomepageset | homepage | sitehome | linkelement |
| 0 | Home | Home | //a[contains(@class, 'nav-link active') and contains(@tabindex, 0) and not(contains(@href, 'redirect=0'))] |
| 1 | Dashboard | Home | //a[contains(@class, 'nav-link') and contains(@tabindex, 0) and (contains(@href, 'redirect=0'))] |
| 3 | My courses | Home | //a[contains(@class, 'nav-link') and contains(@tabindex, 0) and (contains(@href, 'redirect=0'))] |
@javascript @theme_boost
Scenario Outline: Admin sets defaulthomepage to 2 and verify the landing page based on user preference set
Scenario Outline: Admin sets defaulthomepage to user preference and verifies the landing page based on it
Given I log in as "admin"
And I navigate to "Appearance > Navigation" in site administration
And I set the field "Home page for users" to "User preference"
@ -43,3 +44,4 @@ Feature: Primary navigation
| userpreference | homepage |
| Site | Home |
| Dashboard | Dashboard |
| My courses | My courses |

View File

@ -50,6 +50,7 @@ class defaulthomepage_form extends \moodleform {
$options = [
HOMEPAGE_SITE => new lang_string('site'),
HOMEPAGE_MY => new lang_string('mymoodle', 'admin'),
HOMEPAGE_MYCOURSES => new lang_string('mycourses', 'admin'),
];
$mform->addElement('select', 'defaulthomepage', get_string('defaulthomepageuser'), $options);

View File

@ -71,3 +71,4 @@ Feature: Set the site home page and dashboard as the default home page
| preference | breadcrumb |
| Site | Home |
| Dashboard | Dashboard |
| My courses | My courses |