From 5349861e693fefcb5f5bcf4fc2c6ff270f32586b Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 16:14:04 +0100 Subject: [PATCH] MDL-73233 admin: Add enabledashboard setting The $CFG->enabledashboard setting has been added to Appearance > Navigation, to let admins disable the "Dashboard" option from the primary navigation. This commit also changes the behaviour of get_home_page(), to take into account this setting and adds a new method, get_default_home_page(), to return the expected default home page (that wil be used when current default page is not defined or valid). --- admin/settings/appearance.php | 38 +++++--- lang/en/admin.php | 2 + lib/moodlelib.php | 27 +++++- lib/tests/moodlelib_test.php | 139 +++++++++++++++++++++++++++++ lib/upgrade.txt | 2 + theme/boost/tests/behat/primarynav.feature | 2 +- 6 files changed, 196 insertions(+), 14 deletions(-) diff --git a/admin/settings/appearance.php b/admin/settings/appearance.php index 04350e3642c..b0547e7aaa0 100644 --- a/admin/settings/appearance.php +++ b/admin/settings/appearance.php @@ -186,15 +186,29 @@ reports,core_reportbuilder|/reportbuilder/index.php', // Navigation settings $temp = new admin_settingpage('navigation', new lang_string('navigation')); - $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_configcheckbox( + 'enabledashboard', + new lang_string('enabledashboard', 'admin'), + new lang_string('enabledashboard_help', 'admin'), + 1 + )); + + $choices = [HOMEPAGE_SITE => new lang_string('home')]; + if (!empty($CFG->enabledashboard)) { + $choices[HOMEPAGE_MY] = new lang_string('mymoodle', 'admin'); + } + $choices[HOMEPAGE_MYCOURSES] = new lang_string('mycourses', 'admin'); + $choices[HOMEPAGE_USER] = new lang_string('userpreference', 'admin'); $temp->add(new admin_setting_configselect('defaulthomepage', new lang_string('defaulthomepage', 'admin'), - new lang_string('configdefaulthomepage', 'admin'), HOMEPAGE_MY, $choices)); - $temp->add(new admin_setting_configcheckbox('allowguestmymoodle', new lang_string('allowguestmymoodle', 'admin'), new lang_string('configallowguestmymoodle', 'admin'), 1)); + new lang_string('configdefaulthomepage', 'admin'), get_default_home_page(), $choices)); + if (!empty($CFG->enabledashboard)) { + $temp->add(new admin_setting_configcheckbox( + 'allowguestmymoodle', + new lang_string('allowguestmymoodle', 'admin'), + new lang_string('configallowguestmymoodle', 'admin'), + 1 + )); + } $temp->add(new admin_setting_configcheckbox('navshowfullcoursenames', new lang_string('navshowfullcoursenames', 'admin'), new lang_string('navshowfullcoursenames_help', 'admin'), 0)); $temp->add(new admin_setting_configcheckbox('navshowcategories', new lang_string('navshowcategories', 'admin'), new lang_string('confignavshowcategories', 'admin'), 1)); $temp->add(new admin_setting_configcheckbox('navshowmycoursecategories', new lang_string('navshowmycoursecategories', 'admin'), new lang_string('navshowmycoursecategories_help', 'admin'), 0)); @@ -237,9 +251,11 @@ reports,core_reportbuilder|/reportbuilder/index.php', $temp->add(new admin_setting_configcheckbox('doctonewwindow', new lang_string('doctonewwindow', 'admin'), new lang_string('configdoctonewwindow', 'admin'), 0)); $ADMIN->add('appearance', $temp); - $temp = new admin_externalpage('mypage', new lang_string('mypage', 'admin'), $CFG->wwwroot . '/my/indexsys.php', - 'moodle/my:configsyspages'); - $ADMIN->add('appearance', $temp); + if (!empty($CFG->enabledashboard)) { + $temp = new admin_externalpage('mypage', new lang_string('mypage', 'admin'), $CFG->wwwroot . '/my/indexsys.php', + 'moodle/my:configsyspages'); + $ADMIN->add('appearance', $temp); + } $temp = new admin_externalpage('profilepage', new lang_string('myprofile', 'admin'), $CFG->wwwroot . '/user/profilesys.php', 'moodle/my:configsyspages'); diff --git a/lang/en/admin.php b/lang/en/admin.php index 06029e8e969..4b0591e3d00 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -562,6 +562,8 @@ $string['enablecourserelativedates'] = 'Enable course relative dates'; $string['enablecourserelativedates_desc'] = 'Allow courses to be set up to display dates relative to the user\'s start date in the course.'; $string['enablecourserequests'] = 'Enable course requests'; $string['enabled'] = 'Enabled'; +$string['enabledashboard'] = 'Enable dashboard'; +$string['enabledashboard_help'] = 'The Dashboard shows Timeline, Calendar and Recently accessed items by default. You can set a different default Dashboard for everyone and allow users to customise their own Dashboard.'; $string['enabledevicedetection'] = 'Enable device detection'; $string['enableglobalsearch'] = 'Enable global search'; $string['enableglobalsearch_desc'] = 'If enabled, data will be indexed and synchronised by a scheduled task.'; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index ee60a46c6e3..cae910f8f56 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -10367,18 +10367,41 @@ function get_home_page() { global $CFG; if (isloggedin() && !isguestuser() && !empty($CFG->defaulthomepage)) { + // If dashboard is disabled, home will be set to default page. + $defaultpage = get_default_home_page(); if ($CFG->defaulthomepage == HOMEPAGE_MY) { - return HOMEPAGE_MY; + if (!empty($CFG->enabledashboard)) { + return HOMEPAGE_MY; + } else { + return $defaultpage; + } } else if ($CFG->defaulthomepage == HOMEPAGE_MYCOURSES) { return HOMEPAGE_MYCOURSES; } else { - return (int)get_user_preferences('user_home_page_preference', HOMEPAGE_MY); + $userhomepage = (int) 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; + } + return $userhomepage; } } return HOMEPAGE_SITE; } /** + * Returns the default home page to display if current one is not defined or can't be applied. + * The default behaviour is to return Dashboard if it's enabled or My courses page if it isn't. + * + * @return int The default home page. + */ +function get_default_home_page(): int { + global $CFG; + + return !empty($CFG->enabledashboard) ? HOMEPAGE_MY : HOMEPAGE_MYCOURSES; +} + +/** * 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 * result of this function should be passed through print_string. diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index 3a7a42cb774..13a70526d0c 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -5196,4 +5196,143 @@ EOF; ], ]; } + + /** + * Test get_home_page() method. + * + * @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 $defaulthomepage The $CFG->defaulthomepage setting value. + * @param int $enabledashboard Whether the dashboard should be enabled or not. + * @param int $userpreference User preference for the home page setting. + * @covers ::get_home_page + */ + public function test_get_home_page(string $user, int $expected, ?int $defaulthomepage = null, ?int $enabledashboard = null, + ?int $userpreference = null) { + global $CFG, $USER; + + $this->resetAfterTest(); + + if ($user == 'guest') { + $this->setGuestUser(); + } else if ($user == 'logged') { + $this->setUser($this->getDataGenerator()->create_user()); + } + + if (isset($defaulthomepage)) { + $CFG->defaulthomepage = $defaulthomepage; + } + if (isset($enabledashboard)) { + $CFG->enabledashboard = $enabledashboard; + } + + if ($USER) { + set_user_preferences(['user_home_page_preference' => $userpreference], $USER->id); + } + + $homepage = get_home_page(); + $this->assertEquals($expected, $homepage); + } + + /** + * Data provider for get_home_page checks. + * + * @return array + */ + public function get_home_page_provider(): array { + return [ + 'No logged user' => [ + 'user' => 'nologged', + 'expected' => HOMEPAGE_SITE, + ], + 'Guest user' => [ + 'user' => 'guest', + 'expected' => HOMEPAGE_SITE, + ], + 'Logged user. Dashboard set as default home page and enabled' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MY, + 'defaulthomepage' => HOMEPAGE_MY, + 'enabledashboard' => 1, + ], + 'Logged user. Dashboard set as default home page but disabled' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MYCOURSES, + 'defaulthomepage' => HOMEPAGE_MY, + 'enabledashboard' => 0, + ], + 'Logged user. My courses set as default home page with dashboard enabled' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MYCOURSES, + 'defaulthomepage' => HOMEPAGE_MYCOURSES, + 'enabledashboard' => 1, + ], + 'Logged user. My courses set as default home page with dashboard disabled' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MYCOURSES, + 'defaulthomepage' => HOMEPAGE_MYCOURSES, + 'enabledashboard' => 0, + ], + 'Logged user. Site set as default home page with dashboard enabled' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_SITE, + 'defaulthomepage' => HOMEPAGE_SITE, + 'enabledashboard' => 1, + ], + 'Logged user. Site set as default home page with dashboard disabled' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_SITE, + 'defaulthomepage' => HOMEPAGE_SITE, + 'enabledashboard' => 0, + ], + 'Logged user. User preference set as default page with dashboard enabled and user preference set to dashboard' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MY, + 'defaulthomepage' => HOMEPAGE_USER, + 'enabledashboard' => 1, + 'userpreference' => HOMEPAGE_MY, + ], + 'Logged user. User preference set as default page with dashboard disabled and user preference set to dashboard' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MYCOURSES, + 'defaulthomepage' => HOMEPAGE_USER, + 'enabledashboard' => 0, + 'userpreference' => HOMEPAGE_MY, + ], + 'Logged user. User preference set as default page with dashboard enabled and user preference set to my courses' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MYCOURSES, + 'defaulthomepage' => HOMEPAGE_USER, + 'enabledashboard' => 1, + 'userpreference' => HOMEPAGE_MYCOURSES, + ], + 'Logged user. User preference set as default page with dashboard disabled and user preference set to my courses' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MYCOURSES, + 'defaulthomepage' => HOMEPAGE_USER, + 'enabledashboard' => 0, + 'userpreference' => HOMEPAGE_MYCOURSES, + ], + ]; + } + + /** + * Test get_default_home_page() method. + * + * @covers ::get_default_home_page + */ + public function test_get_default_home_page() { + global $CFG; + + $this->resetAfterTest(); + + $CFG->enabledashboard = 1; + $default = get_default_home_page(); + $this->assertEquals(HOMEPAGE_MY, $default); + + $CFG->enabledashboard = 0; + $default = get_default_home_page(); + $this->assertEquals(HOMEPAGE_MYCOURSES, $default); + } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 70050362a18..d060d941659 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -247,6 +247,8 @@ value to get the list of blocks that won't be displayed for a theme. `get_template_name(\renderer_base): string` function which will inform the default render() function with a template name. * The parameter $modinfo of the get_data method in completion_info class has been deprecated and is not used anymore. +* A new method, get_default_home_page(), has been added to moodlelib to get the default home page to display if current one is not +defined or can't be applied. === 3.11.4 === * A new option dontforcesvgdownload has been added to the $options parameter of the send_file() function. diff --git a/theme/boost/tests/behat/primarynav.feature b/theme/boost/tests/behat/primarynav.feature index ce2c9653e4e..e80fabbcc4e 100644 --- a/theme/boost/tests/behat/primarynav.feature +++ b/theme/boost/tests/behat/primarynav.feature @@ -42,7 +42,7 @@ Feature: Primary navigation Examples: | userpreference | homepage | - | Site | Home | + | Home | Home | | Dashboard | Dashboard | | My courses | My courses | -- 2.11.4.GIT