Skip to content

Commit

Permalink
MDL-73233 user: Review Start page user preferences
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sarjona committed Mar 16, 2022
1 parent 5349861 commit e3d2fa4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
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');
Expand Down
17 changes: 14 additions & 3 deletions lib/classes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,8 @@ public static function get_property_default($property) {
* @return void
*/
protected static function fill_preferences_cache() {
global $CFG;

if (self::$preferencescache !== null) {
return;
}
Expand Down Expand Up @@ -966,13 +968,22 @@ protected static function fill_preferences_cache() {
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.
Expand Down
21 changes: 21 additions & 0 deletions lib/tests/behat/enabledashboard.feature
Original file line number Diff line number Diff line change
@@ -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"
14 changes: 8 additions & 6 deletions user/classes/form/defaulthomepage_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
Expand Down
7 changes: 6 additions & 1 deletion user/defaulthomepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@

$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]);
Expand Down
2 changes: 1 addition & 1 deletion user/tests/behat/set_default_homepage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

0 comments on commit e3d2fa4

Please sign in to comment.