MDL-73233 user: Review Start page user preferences

The "Start page" user preferences page has been reviewed to
consider the new $CFG->enabledashboard setting.
The "Dashboard" won't be displayed in the list if it's disabled.
Besides, the default value is now calculated calling the new
get_default_home_page() method.
This commit is contained in:
Sara Arjona 2022-02-25 16:18:45 +01:00
parent 5349861e69
commit e3d2fa41d0
6 changed files with 51 additions and 11 deletions

View File

@ -74,6 +74,7 @@ if (get_home_page() != 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) {
// At this point, dashboard is enabled so we don't need to check for it (otherwise, get_home_page() won't return it).
redirect($CFG->wwwroot .'/my/');
} else if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MYCOURSES) && $redirect === 1) {
redirect($CFG->wwwroot .'/my/courses.php');

View File

@ -933,6 +933,8 @@ class core_user {
* @return void
*/
protected static function fill_preferences_cache() {
global $CFG;
if (self::$preferencescache !== null) {
return;
}
@ -966,13 +968,22 @@ class core_user {
global $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, HOMEPAGE_MYCOURSES),
$choices = [HOMEPAGE_SITE];
if (!empty($CFG->enabledashboard)) {
$choices[] = HOMEPAGE_MY;
}
$choices[] = HOMEPAGE_MYCOURSES;
$preferences['user_home_page_preference'] = [
'type' => PARAM_INT,
'null' => NULL_ALLOWED,
'default' => get_default_home_page(),
'choices' => $choices,
'permissioncallback' => function ($user, $preferencename) {
global $CFG;
return (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_USER));
}
);
];
// Core components that may want to define their preferences.
// List of core components implementing callback is hardcoded here for performance reasons.

View File

@ -0,0 +1,21 @@
@core
Feature: Enable dashboard setting
In order to hide/show dashboard in navigation
As an administrator
I can enable or disable it
Scenario: Hide setting when dashboard is disabled
Given the following config values are set as admin:
| enabledashboard | 0 |
# 2 = User preference.
| defaulthomepage | 2 |
When I log in as "admin"
And I navigate to "Appearance > Navigation" in site administration
Then the field "Enable dashboard" matches value "0"
And I should not see "Allow guest access to Dashboard"
And I should not see "Dashboard" in the "Start page for users" "select"
And I follow "Appearance"
And I should not see "Default Dashboard page"
And I follow "Preferences" in the user menu
And I follow "Start page"
And I should not see "Dashboard" in the "Start page" "select"

View File

@ -42,20 +42,22 @@ class defaulthomepage_form extends \moodleform {
* Define the form.
*/
public function definition () {
global $CFG;
$mform = $this->_form;
$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);
$options = [
HOMEPAGE_SITE => new lang_string('site'),
HOMEPAGE_MY => new lang_string('mymoodle', 'admin'),
HOMEPAGE_MYCOURSES => new lang_string('mycourses', 'admin'),
];
$options = [HOMEPAGE_SITE => new lang_string('home')];
if (!empty($CFG->enabledashboard)) {
$options[HOMEPAGE_MY] = new lang_string('mymoodle', 'admin');
}
$options[HOMEPAGE_MYCOURSES] = new lang_string('mycourses', 'admin');
$mform->addElement('select', 'defaulthomepage', get_string('defaulthomepageuser'), $options);
$mform->addHelpButton('defaulthomepage', 'defaulthomepageuser');
$mform->setDefault('defaulthomepage', HOMEPAGE_MY);
$mform->setDefault('defaulthomepage', get_default_home_page());
$this->add_action_buttons(true, get_string('savechanges'));
}

View File

@ -34,7 +34,12 @@ list($user, $course) = useredit_setup_preference_page($userid, SITEID);
$form = new core_user\form\defaulthomepage_form();
$user->defaulthomepage = get_user_preferences('user_home_page_preference', HOMEPAGE_MY, $user);
$defaulthomepage = get_default_home_page();
$user->defaulthomepage = get_user_preferences('user_home_page_preference', $defaulthomepage, $user);
if (empty($CFG->enabledashboard) && $user->defaulthomepage == HOMEPAGE_MY) {
// If the user was using the dashboard but it's disabled, return the default home page.
$user->defaulthomepage = $defaulthomepage;
}
$form->set_data($user);
$redirect = new moodle_url('/user/preferences.php', ['userid' => $user->id]);

View File

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