From 5349861e693fefcb5f5bcf4fc2c6ff270f32586b Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 16:14:04 +0100 Subject: [PATCH 01/10] 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,17 +10367,40 @@ 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 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 | From e3d2fa41d0fcd43035b73554da3d7616b25f9b99 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 16:18:45 +0100 Subject: [PATCH 02/10] 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. --- index.php | 1 + lib/classes/user.php | 17 ++++++++++++--- lib/tests/behat/enabledashboard.feature | 21 +++++++++++++++++++ user/classes/form/defaulthomepage_form.php | 14 +++++++------ user/defaulthomepage.php | 7 ++++++- user/tests/behat/set_default_homepage.feature | 2 +- 6 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 lib/tests/behat/enabledashboard.feature diff --git a/index.php b/index.php index b2a3d4eec13..626dc504937 100644 --- a/index.php +++ b/index.php @@ -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'); diff --git a/lib/classes/user.php b/lib/classes/user.php index fba83aa0f2d..366165ee87a 100644 --- a/lib/classes/user.php +++ b/lib/classes/user.php @@ -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. diff --git a/lib/tests/behat/enabledashboard.feature b/lib/tests/behat/enabledashboard.feature new file mode 100644 index 00000000000..b21d531fb46 --- /dev/null +++ b/lib/tests/behat/enabledashboard.feature @@ -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" diff --git a/user/classes/form/defaulthomepage_form.php b/user/classes/form/defaulthomepage_form.php index f3c9f00bffc..ac67614d8db 100644 --- a/user/classes/form/defaulthomepage_form.php +++ b/user/classes/form/defaulthomepage_form.php @@ -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')); } diff --git a/user/defaulthomepage.php b/user/defaulthomepage.php index 91bcfe9503c..2cbe953eed9 100644 --- a/user/defaulthomepage.php +++ b/user/defaulthomepage.php @@ -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]); diff --git a/user/tests/behat/set_default_homepage.feature b/user/tests/behat/set_default_homepage.feature index eeb51b72c78..53c2ab4a757 100644 --- a/user/tests/behat/set_default_homepage.feature +++ b/user/tests/behat/set_default_homepage.feature @@ -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 | From c612f121a0c9b0d1e5ac8416a71e6794227f103f Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 16:25:00 +0100 Subject: [PATCH 03/10] MDL-73233 navigation: Hide Dashboard from primary nav When the $CFG->enabledashboard is disabled, the "Dashboard" menu option won't be displayed in the primary navigation. --- lib/classes/navigation/views/primary.php | 6 ++- theme/boost/tests/behat/primarynav.feature | 55 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/lib/classes/navigation/views/primary.php b/lib/classes/navigation/views/primary.php index c1b8108e3e9..d7d7e32a102 100644 --- a/lib/classes/navigation/views/primary.php +++ b/lib/classes/navigation/views/primary.php @@ -33,6 +33,8 @@ class primary extends view { * Initialise the primary navigation node */ public function initialise(): void { + global $CFG; + if (during_initial_install() || $this->initialised) { return; } @@ -56,8 +58,8 @@ class primary extends view { } // Add the dashboard link. - $showmyhomenode = empty($this->page->theme->removedprimarynavitems) || - !in_array('myhome', $this->page->theme->removedprimarynavitems); + $showmyhomenode = !empty($CFG->enabledashboard) && (empty($this->page->theme->removedprimarynavitems) || + !in_array('myhome', $this->page->theme->removedprimarynavitems)); if ($showmyhomenode) { $this->add(get_string('myhome'), new \moodle_url('/my/'), self::TYPE_SETTING, null, 'myhome', new \pix_icon('i/dashboard', '')); diff --git a/theme/boost/tests/behat/primarynav.feature b/theme/boost/tests/behat/primarynav.feature index e80fabbcc4e..8b4d2c12f6c 100644 --- a/theme/boost/tests/behat/primarynav.feature +++ b/theme/boost/tests/behat/primarynav.feature @@ -54,3 +54,58 @@ Feature: Primary navigation And "Side panel" "button" should exist And I click on "Side panel" "button" And I should see "Home" in the "theme_boost-drawers-primary" "region" + + Scenario: Dashboard is not displayed in the primary navigation when it is disabled + Given the following config values are set as admin: + | enabledashboard | 0 | + When I am on the "My courses" page logged in as "user1" + Then I should not see "Dashboard" + And the following config values are set as admin: + | enabledashboard | 1 | +# We need to reload the page to skip the "Welcome, xxxx!" and display the real page title. + And I reload the page + And I should see "Dashboard" + + Scenario: Start page when default home is dashboard but dashboard is disabled + Given the following config values are set as admin: + | enabledashboard | 0 | +# 1 = Dashboard. + | defaulthomepage | 1 | + When I log in as "admin" +# We need to reload the page to skip the "Welcome, xxxx!" and display the real page title. + And I reload the page + Then I should not see "Dashboard" in the "page-header" "region" + And I should see "My courses" in the "page-header" "region" + And I log out +# Check dashboard is displayed when it's re-enabled. + And the following config values are set as admin: + | enabledashboard | 1 | + And I log in as "admin" +# We need to reload the page to skip the "Welcome, xxxx!" and display the real page title. + And I reload the page + And I should see "Dashboard" in the "page-header" "region" + And I should not see "My courses" in the "page-header" "region" + + Scenario: Start page when default home is user preference set to dashboard but dashboard is disabled + Given the following config values are set as admin: + | enabledashboard | 0 | +# 2 = User preference. + | defaulthomepage | 2 | +# 1 = Dashboard. + And the following "user preferences" exist: + | user | preference | value | + | admin | user_home_page_preference | 1 | + When I log in as "admin" +# We need to reload the page to skip the "Welcome, xxxx!" and display the real page title. + And I reload the page + Then I should not see "Dashboard" + And I should see "My courses" in the "page-header" "region" + And I log out +# Check dashboard is displayed when it's re-enabled. + And the following config values are set as admin: + | enabledashboard | 1 | + And I log in as "admin" +# We need to reload the page to skip the "Welcome, xxxx!" and display the real page title. + And I reload the page + And I should see "Dashboard" in the "page-header" "region" + And I should not see "My courses" in the "page-header" "region" From 31ed9a6c2fbafcc5754806e0a84d30330ec5ddec Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 16:26:59 +0100 Subject: [PATCH 04/10] MDL-73233 my: Disable /my page depending on setting When $CFG->enabledashboard will be disabled, the /my page will redirect to a different page or display an error message. --- lang/en/my.php | 1 + my/index.php | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/lang/en/my.php b/lang/en/my.php index cc0b8f73567..2f3c77639da 100644 --- a/lang/en/my.php +++ b/lang/en/my.php @@ -23,6 +23,7 @@ */ $string['coursemanagementoptions'] = 'Course management options'; +$string['error:dashboardisdisabled'] = 'The dashboard has been disabled by the administrator.'; $string['mymoodle'] = 'Dashboard'; $string['nocourses'] = 'No course information to show.'; $string['noguest'] = 'The Dashboard page is not available to guest users'; diff --git a/my/index.php b/my/index.php index 500a010881d..9f18d628c14 100644 --- a/my/index.php +++ b/my/index.php @@ -52,6 +52,18 @@ if ($hassiteconfig && moodle_needs_upgrading()) { $strmymoodle = get_string('myhome'); +if (empty($CFG->enabledashboard)) { + // Dashboard is disabled, so the /my page shouldn't be displayed. + $defaultpage = get_default_home_page(); + if ($defaultpage == HOMEPAGE_MYCOURSES) { + // If default page is set to "My courses", redirect to it. + redirect(new moodle_url('/my/courses.php')); + } else { + // Otherwise, raise an exception to inform the dashboard is disabled. + throw new moodle_exception('error:dashboardisdisabled', 'my'); + } +} + if (isguestuser()) { // Force them to see system default, no editing allowed // If guests are not allowed my moodle, send them to front page. if (empty($CFG->allowguestmymoodle)) { From 40894d7c311086f1a2967afccdb543454ffd6d90 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 16:30:19 +0100 Subject: [PATCH 05/10] MDL-73233 frontpage: Display link to My courses Before MDL-70801, the "My courses" page didn't exist so the "More" button displayed bellow the "Enrolled courses" in frontpage pointed to Dashboard. I think this is a leftover from the issue when the "My courses" page was added and it makes more sense using the "My courses" page here always. --- course/renderer.php | 2 +- .../tests/behat/frontpage_display_modes.feature | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/course/renderer.php b/course/renderer.php index e0db4c9534c..9fac8ebf42d 100644 --- a/course/renderer.php +++ b/course/renderer.php @@ -1988,7 +1988,7 @@ class core_course_renderer extends plugin_renderer_base { // There are more enrolled courses than we can display, display link to 'My courses'. $courses = array_slice($courses, 0, $CFG->frontpagecourselimit, true); $chelper->set_courses_display_options(array( - 'viewmoreurl' => new moodle_url('/my/'), + 'viewmoreurl' => new moodle_url('/my/courses.php'), 'viewmoretext' => new lang_string('mycourses') )); } else if (core_course_category::top()->is_uservisible()) { diff --git a/course/tests/behat/frontpage_display_modes.feature b/course/tests/behat/frontpage_display_modes.feature index 7df3eff46a2..2b41a369c1c 100644 --- a/course/tests/behat/frontpage_display_modes.feature +++ b/course/tests/behat/frontpage_display_modes.feature @@ -60,3 +60,20 @@ Feature: Site home displays items in different modes And I should not see "Category A child" in the "region-main" "region" And I toggle "Category A" category children visibility in frontpage And I should see "Course 11 1" in the "region-main" "region" + + Scenario: Displays Enrolled users in frontpage + Given the following "users" exist: + | username | firstname | lastname | email | profile_field_frog | + | user1 | User | One | one@example.com | Kermit | + And the following "course enrolments" exist: + | user | course | role | + | admin | COURSE1_1 | student | + | admin | COURSE2_1 | student | + | admin | COURSE2_2 | student | + And I set the following administration settings values: + | Site home items when logged in | Enrolled courses | + | frontpagecourselimit | 2 | + And I log in as "admin" + And I am on site homepage + When I click on "My courses" "link" in the "frontpage-course-list" "region" + Then I should see "My courses" in the "page-header" "region" From 04d8b5999a89e6837da196cd22e07b87adcdf2f1 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 16:34:10 +0100 Subject: [PATCH 06/10] MDL-73233 moodlenet: Change cancel link to / The cancel link when content is imported from MoodleNet has been changed from /my to /, in order to get it working always, regardless the $CFG->enabledashboard setting is enabled or not. --- admin/tool/moodlenet/classes/output/select_page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/tool/moodlenet/classes/output/select_page.php b/admin/tool/moodlenet/classes/output/select_page.php index 60bbde16572..3a7a50013eb 100644 --- a/admin/tool/moodlenet/classes/output/select_page.php +++ b/admin/tool/moodlenet/classes/output/select_page.php @@ -70,7 +70,7 @@ class select_page implements \renderable, \templatable { return (object) [ 'name' => $this->importinfo->get_resource()->get_name(), 'type' => $this->importinfo->get_config()->type, - 'cancellink' => new \moodle_url('/my'), + 'cancellink' => new \moodle_url('/'), ]; } } From 599cb8bbb1f6955e273af4e9ce94dae66bfb29d3 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 16:36:28 +0100 Subject: [PATCH 07/10] MDL-73233 shibboleth: Change urltogo only when dashboard is enabled --- auth/shibboleth/index.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/auth/shibboleth/index.php b/auth/shibboleth/index.php index 030283ebcba..df0a7c97668 100644 --- a/auth/shibboleth/index.php +++ b/auth/shibboleth/index.php @@ -73,7 +73,7 @@ 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) { + if ($CFG->defaulthomepage == HOMEPAGE_MY && !empty($CFG->enabledashboard)) { $urltogo = $CFG->wwwroot.'/my/'; } else if ($CFG->defaulthomepage == HOMEPAGE_MYCOURSES) { $urltogo = $CFG->wwwroot.'/my/courses.php'; @@ -99,5 +99,3 @@ } else { print_error('shib_not_set_up_error', 'auth_shibboleth', '', $readmeurl); } - - From 42c96d1708821b7d6d35efc06c6f4043197b02ee Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 16:43:17 +0100 Subject: [PATCH 08/10] MDL-73233 navigation: Remove dashboard when it is disabled - Only add Dashboard to 'home' the root node if the $CFG->enabledashboard setting is enabled. - The "More..." link displayed in the "My courses" section from the navigation block was pointing to the dashboard. However, now that we have the "My courses" page, it makes more sense to change it and link to it. - For the User preferences, I've changed breadcrumbs from "Dashboard" to "Home" because they are more generic (and don't strictly belong to the dashboard). --- .../tests/behat/view_my_courses.feature | 21 ++++++++++ lib/navigationlib.php | 38 +++++++++++-------- user/tests/behat/set_default_homepage.feature | 1 + 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/blocks/navigation/tests/behat/view_my_courses.feature b/blocks/navigation/tests/behat/view_my_courses.feature index 04364afca12..632ae2453e1 100644 --- a/blocks/navigation/tests/behat/view_my_courses.feature +++ b/blocks/navigation/tests/behat/view_my_courses.feature @@ -104,3 +104,24 @@ Feature: View my courses in navigation block And I should not see "c2" in the "Navigation" "block" And I should see "c31" in the "Navigation" "block" And I should not see "c32" in the "Navigation" "block" + + @javascript + Scenario: The course limit setting is applied in the navigation block + Given the following config values are set as admin: + | navcourselimit | 2 | + And I log in as "student1" + And I should see "More..." in the "Navigation" "block" + When I click on "More..." "link" in the "Navigation" "block" + Then I should see "My courses" in the "page-header" "region" + + @javascript + Scenario: The dashboard node is not displayed in the navigation block when it is disabled + Given the following config values are set as admin: + | enabledashboard | 0 | + When I log in as "student1" + Then I should not see "Dashboard" in the "Navigation" "block" +# Re-enable dashboard and check then it's displayed in the navigation block. + And the following config values are set as admin: + | enabledashboard | 1 | + And I reload the page + And I should see "Dashboard" in the "Navigation" "block" diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 807878163f6..d8de09d3f5f 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -1407,9 +1407,12 @@ class global_navigation extends navigation_node { if ($defaulthomepage == HOMEPAGE_SITE) { // The home element should be my moodle because the root element is the site if (isloggedin() && !isguestuser()) { // Makes no sense if you aren't logged in - $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'), - self::TYPE_SETTING, null, 'myhome', new pix_icon('i/dashboard', '')); - $this->rootnodes['home']->showinflatnavigation = true; + if (!empty($CFG->enabledashboard)) { + // Only add dashboard to home if it's enabled. + $this->rootnodes['home'] = $this->add(get_string('myhome'), new moodle_url('/my/'), + self::TYPE_SETTING, null, 'myhome', new pix_icon('i/dashboard', '')); + $this->rootnodes['home']->showinflatnavigation = true; + } } } else { // The home element should be the site because the root node is my moodle @@ -3289,7 +3292,7 @@ class global_navigation extends navigation_node { // Show a link to the course page if there are more courses the user is enrolled in. if ($showmorelinkinnav || $showmorelinkinflatnav) { // Adding hash to URL so the link is not highlighted in the navigation when clicked. - $url = new moodle_url('/my/'); + $url = new moodle_url('/my/courses.php'); $parent = $this->rootnodes['mycourses']; $coursenode = $parent->add(get_string('morenavigationlinks'), $url, self::TYPE_CUSTOM, null, self::COURSE_INDEX_PAGE); @@ -3360,7 +3363,13 @@ class global_navigation_for_ajax extends global_navigation { $this->rootnodes = array(); $this->rootnodes['site'] = $this->add_course($SITE); - $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my'), self::TYPE_ROOTNODE, null, 'mycourses'); + $this->rootnodes['mycourses'] = $this->add( + get_string('mycourses'), + new moodle_url('/my/courses.php'), + self::TYPE_ROOTNODE, + null, + 'mycourses' + ); $this->rootnodes['courses'] = $this->add(get_string('courses'), null, self::TYPE_ROOTNODE, null, 'courses'); // The courses branch is always displayed, and is always expandable (although may be empty). // This mimicks what is done during {@link global_navigation::initialise()}. @@ -5015,14 +5024,11 @@ class settings_navigation extends navigation_node { // Add a user setting branch. if ($gstitle == 'usercurrentsettings') { - $dashboard = $this->add(get_string('myhome'), new moodle_url('/my/'), self::TYPE_CONTAINER, null, 'dashboard'); + $mainpage = $this->add(get_string('home'), new moodle_url('/'), self::TYPE_CONTAINER, null, 'site'); + // 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; - $homepage = get_home_page(); - if (($homepage == HOMEPAGE_MY || $homepage == HOMEPAGE_MYCOURSES)) { - $dashboard->mainnavonly = true; - } + $mainpage->display = false; $iscurrentuser = ($user->id == $USER->id); @@ -5036,7 +5042,7 @@ class settings_navigation extends navigation_node { } // Add the user profile to the dashboard. - $profilenode = $dashboard->add(get_string('profile'), new moodle_url('/user/profile.php', + $profilenode = $mainpage->add(get_string('profile'), new moodle_url('/user/profile.php', array('id' => $user->id)), self::TYPE_SETTING, null, 'myprofile'); if (!empty($CFG->navadduserpostslinks)) { @@ -5081,14 +5087,14 @@ class settings_navigation extends navigation_node { $messageargs['user2'] = $user->id; } $url = new moodle_url('/message/index.php', $messageargs); - $dashboard->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); + $mainpage->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); } // Add the "My private files" link. // This link doesn't have a unique display for course context so only display it under the user's profile. if ($issitecourse && $iscurrentuser && has_capability('moodle/user:manageownfiles', $usercontext)) { $url = new moodle_url('/user/files.php'); - $dashboard->add(get_string('privatefiles'), $url, self::TYPE_SETTING, null, 'privatefiles'); + $mainpage->add(get_string('privatefiles'), $url, self::TYPE_SETTING, null, 'privatefiles'); } // Add a node to view the users notes if permitted. @@ -5110,7 +5116,7 @@ class settings_navigation extends navigation_node { } else { // Otherwise we are in a course and should redirect to the user grade report (Activity report version). $url = new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $user->id)); } - $dashboard->add(get_string('grades', 'grades'), $url, self::TYPE_SETTING, null, 'mygrades'); + $mainpage->add(get_string('grades', 'grades'), $url, self::TYPE_SETTING, null, 'mygrades'); } // Let plugins hook into user navigation. @@ -5124,7 +5130,7 @@ class settings_navigation extends navigation_node { } $usersetting = navigation_node::create(get_string('preferences', 'moodle'), $prefurl, self::TYPE_CONTAINER, null, $key); - $dashboard->add_node($usersetting); + $mainpage->add_node($usersetting); } else { $usersetting = $this->add(get_string('preferences', 'moodle'), $prefurl, self::TYPE_CONTAINER, null, $key); $usersetting->display = false; diff --git a/user/tests/behat/set_default_homepage.feature b/user/tests/behat/set_default_homepage.feature index 53c2ab4a757..f07aa1fbac2 100644 --- a/user/tests/behat/set_default_homepage.feature +++ b/user/tests/behat/set_default_homepage.feature @@ -62,6 +62,7 @@ Feature: Set the site home page and dashboard as the default home page And I log out When I log in as "user1" And I follow "Preferences" in the user menu + And I should see "Home" in the ".breadcrumb" "css_element" And I follow "Start page" And I set the field "Start page" to "" And I press "Save changes" From 07f7da93cc813adea2018bb6c25d83f08da72eb0 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Fri, 25 Feb 2022 17:10:28 +0100 Subject: [PATCH 09/10] MDL-73233 tool_mobile: Return enabledashboard setting External function tool_mobile::get_config now returns the enabledashboard setting. --- admin/tool/mobile/classes/api.php | 4 ++++ admin/tool/mobile/tests/externallib_test.php | 2 ++ admin/tool/mobile/upgrade.txt | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/admin/tool/mobile/classes/api.php b/admin/tool/mobile/classes/api.php index e57242572d5..72e1151ab9c 100644 --- a/admin/tool/mobile/classes/api.php +++ b/admin/tool/mobile/classes/api.php @@ -340,6 +340,10 @@ class api { $settings->coursegraceperiodbefore = $CFG->coursegraceperiodbefore; } + if (empty($section) || $section === 'navigation') { + $settings->enabledashboard = $CFG->enabledashboard; + } + return $settings; } diff --git a/admin/tool/mobile/tests/externallib_test.php b/admin/tool/mobile/tests/externallib_test.php index 1e96562a5a9..a3af5eec152 100644 --- a/admin/tool/mobile/tests/externallib_test.php +++ b/admin/tool/mobile/tests/externallib_test.php @@ -233,6 +233,8 @@ class externallib_test extends externallib_advanced_testcase { $expected[] = ['name' => 'coursegraceperiodafter', 'value' => $CFG->coursegraceperiodafter]; $expected[] = ['name' => 'coursegraceperiodbefore', 'value' => $CFG->coursegraceperiodbefore]; + $expected[] = ['name' => 'enabledashboard', 'value' => $CFG->enabledashboard]; + $this->assertCount(0, $result['warnings']); $this->assertEquals($expected, $result['settings']); diff --git a/admin/tool/mobile/upgrade.txt b/admin/tool/mobile/upgrade.txt index 6d71d33530a..3328072493d 100644 --- a/admin/tool/mobile/upgrade.txt +++ b/admin/tool/mobile/upgrade.txt @@ -5,6 +5,7 @@ Information provided here is intended especially for developers. * The function tool_mobile\api::get_qrlogin_key() now requires as parameter an object with all the mobile plugin settings. * The tool_mobile_external::get_config external function now returns the tool_mobile_autologinmintimebetweenreq setting. +* External function tool_mobile::get_config now returns the enabledashboard setting. === 3.7 === @@ -28,4 +29,3 @@ Information provided here is intended especially for developers. * External function tool_mobile::get_public_config now returns the mobilecssurl field (Mobile custom CSS theme). * External function tool_mobile::get_public_config now returns the identityproviders field (list of external identity providers). - From 9261157cc30fb95f2c1b268d6dc55c87a1e8c7d8 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Mon, 28 Feb 2022 16:04:25 +0100 Subject: [PATCH 10/10] MDL-73233 course: Remove make_active() call When accessing to User grades and then to the grades from a specific course, this call was causing several behat errors because the dashboard node was not found. The code causing this issue can be safely removed because it isn't used (the current node is calculated a few lines below). --- course/user.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/course/user.php b/course/user.php index ee187c67b2f..7da10b8bd1a 100644 --- a/course/user.php +++ b/course/user.php @@ -138,8 +138,6 @@ switch ($mode) { // Change the navigation to point to the my grade node (If we are a student). if ($USER->id == $user->id) { require_once($CFG->dirroot . '/user/lib.php'); - // Make the dashboard active so that it shows up in the navbar correctly. - $gradenode = $PAGE->settingsnav->find('dashboard', null)->make_active(); // Get the correct 'Grades' url to point to. $activeurl = user_mygrades_url(); $navbar = $PAGE->navbar->add(get_string('grades', 'grades'), $activeurl, navigation_node::TYPE_SETTING);