From c310b620ade681596dd4e9cefd18c86735ade9e5 Mon Sep 17 00:00:00 2001 From: Andi Permana Date: Tue, 10 Feb 2026 09:45:22 +0700 Subject: [PATCH] MDL-87544 core: route homepage selection with enablemycourses --- .../admin/tool/behat/tests/behat/tabs.feature | 4 +- .../block_myoverview_adminsettings.feature | 4 +- .../block_myoverview_customfield.feature | 4 +- .../behat/block_myoverview_dashboard.feature | 4 +- .../behat/block_myoverview_favourite.feature | 4 +- .../behat/block_myoverview_hidden.feature | 4 +- ...k_myoverview_pagelimit_persistence.feature | 4 +- .../behat/block_myoverview_pagination.feature | 4 +- .../behat/block_myoverview_progress.feature | 4 +- .../behat/block_myoverview_search.feature | 4 +- .../behat/block_myoverview_sorting.feature | 4 +- .../behat/block_myoverview_zerostate.feature | 4 +- .../behat/adminnistration_navigation.feature | 1 + .../tests/behat/expand_courses_node.feature | 1 + .../tests/behat/participants_link.feature | 1 + .../tests/behat/view_my_courses.feature | 3 +- .../tests/behat/starred_courses.feature | 4 +- public/blocks/tests/externallib_test.php | 27 ++++++++- ...ourse_completion_activity_criteria.feature | 4 +- .../behat/frontpage_display_modes.feature | 3 +- .../classes/navigation/global_navigation.php | 49 +++++++++++------ .../lib/classes/navigation/views/primary.php | 5 +- public/lib/moodlelib.php | 11 +++- .../lib/tests/behat/enabledashboard.feature | 1 + .../hook/navigation/primary_extend_test.php | 2 + public/lib/tests/moodlelib_test.php | 55 +++++++++++++++++++ .../tests/navigation/views/primary_test.php | 1 + public/my/courses.php | 15 +++++ public/my/index.php | 5 ++ public/my/tests/behat/my_courses.feature | 3 +- .../boost/tests/behat/mycoursesblocks.feature | 3 +- .../boost/tests/behat/primarynav.feature | 4 +- .../tests/behat/welcome_to_moodle.feature | 1 + .../tests/behat/mycoursesblocks.feature | 3 +- .../behat/welcome_to_classicmoodle.feature | 1 + .../tests/behat/set_default_homepage.feature | 4 +- public/user/tests/editlib_test.php | 3 + 37 files changed, 215 insertions(+), 43 deletions(-) diff --git a/public/admin/tool/behat/tests/behat/tabs.feature b/public/admin/tool/behat/tests/behat/tabs.feature index 210b916e526..0b0ffdb092b 100644 --- a/public/admin/tool/behat/tests/behat/tabs.feature +++ b/public/admin/tool/behat/tests/behat/tabs.feature @@ -6,7 +6,9 @@ Feature: Confirm that we can open multiple browser tabs @javascript @_switch_window Scenario: Open multiple browser tabs - Given the following "courses" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "courses" exist: | fullname | shortname | | Course 1 | C1 | | Course 2 | C2 | diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_adminsettings.feature b/public/blocks/myoverview/tests/behat/block_myoverview_adminsettings.feature index d962f0844bd..078ff3c6434 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_adminsettings.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_adminsettings.feature @@ -5,7 +5,9 @@ Feature: The my overview block allows admins to easily configure the students' c I can configure the appearance of the my overview block Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | student1 | Student | X | student1@example.com | S1 | And the following "categories" exist: diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_customfield.feature b/public/blocks/myoverview/tests/behat/block_myoverview_customfield.feature index 3a4afb4cd49..f145da55e84 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_customfield.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_customfield.feature @@ -2,7 +2,9 @@ Feature: The my overview block allows users to group courses by custom fields Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | student1 | Student | X | student1@example.com | S1 | And the following "custom field categories" exist: diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_dashboard.feature b/public/blocks/myoverview/tests/behat/block_myoverview_dashboard.feature index e79c016b5ac..e2984140832 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_dashboard.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_dashboard.feature @@ -5,7 +5,9 @@ Feature: The my overview block allows users to easily access their courses I can add the my overview block to my dashboard Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | student1 | Student | X | student1@example.com | S1 | And the following "categories" exist: diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_favourite.feature b/public/blocks/myoverview/tests/behat/block_myoverview_favourite.feature index c1e46257109..f763b2b2728 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_favourite.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_favourite.feature @@ -5,7 +5,9 @@ Feature: The my overview block allows users to favourite their courses I can add the my overview block to my dashboard Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | student1 | Student | X | student1@example.com | S1 | And the following "courses" exist: diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_hidden.feature b/public/blocks/myoverview/tests/behat/block_myoverview_hidden.feature index 2bb64fa3873..c4c997674e8 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_hidden.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_hidden.feature @@ -5,7 +5,9 @@ Feature: The my overview block allows users to hide their courses I can add the my overview block to my dashboard Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | student1 | Student | X | student1@example.com | S1 | And the following "courses" exist: diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_pagelimit_persistence.feature b/public/blocks/myoverview/tests/behat/block_myoverview_pagelimit_persistence.feature index 8df501404a8..019f299cc21 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_pagelimit_persistence.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_pagelimit_persistence.feature @@ -2,7 +2,9 @@ Feature: The my overview block allows users to persistence of their page limits Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | student1 | Student | X | student1@example.com | S1 | And the following "courses" exist: diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_pagination.feature b/public/blocks/myoverview/tests/behat/block_myoverview_pagination.feature index 54ba2af40f2..8506680d05d 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_pagination.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_pagination.feature @@ -2,7 +2,9 @@ Feature: My overview block pagination Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | student1 | Student | X | student1@example.com | S1 | And the following "courses" exist: diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_progress.feature b/public/blocks/myoverview/tests/behat/block_myoverview_progress.feature index 5de4debf1ff..e727a1a5408 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_progress.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_progress.feature @@ -5,7 +5,9 @@ Feature: Course overview block show users their progress on courses I can see the progress percentage of the courses I am enrolled in Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | teacher1 | Teacher | 1 | teacher1@example.com | T1 | | student1 | Student | 1 | student1@example.com | S1 | diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_search.feature b/public/blocks/myoverview/tests/behat/block_myoverview_search.feature index 5e210f277c0..f30e5330607 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_search.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_search.feature @@ -2,7 +2,9 @@ Feature: My overview block searching Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | student1 | Student | X | student1@example.com | S1 | | student2 | Student | Y | student2@example.com | S2 | diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_sorting.feature b/public/blocks/myoverview/tests/behat/block_myoverview_sorting.feature index 3d30d3553f9..d7177c99978 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_sorting.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_sorting.feature @@ -5,7 +5,9 @@ Feature: The my overview block allows users to easily sort their courses I can choose from a selection of sorting options Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | student1 | Student | X | student1@example.com | S1 | And the following "categories" exist: diff --git a/public/blocks/myoverview/tests/behat/block_myoverview_zerostate.feature b/public/blocks/myoverview/tests/behat/block_myoverview_zerostate.feature index bb8c8b72bb3..ebcabfdc96e 100644 --- a/public/blocks/myoverview/tests/behat/block_myoverview_zerostate.feature +++ b/public/blocks/myoverview/tests/behat/block_myoverview_zerostate.feature @@ -5,7 +5,9 @@ Feature: Zero state on my overview block I should see the proper information based on my capabilities Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | user | User | X | user@example.com | U1 | | manager | Manager | X | manager@example.com | M1 | diff --git a/public/blocks/navigation/tests/behat/adminnistration_navigation.feature b/public/blocks/navigation/tests/behat/adminnistration_navigation.feature index 4d2cae4c3c2..d014d059399 100644 --- a/public/blocks/navigation/tests/behat/adminnistration_navigation.feature +++ b/public/blocks/navigation/tests/behat/adminnistration_navigation.feature @@ -15,6 +15,7 @@ Feature: Test that admin can see related nodes in Administration block | Course 1 | c1 | cat1 | 1 | And the following config values are set as admin: | unaddableblocks | | theme_boost | + | enablemycourses | 1 | | And I log in as "admin" And I am on site homepage And I turn editing mode on diff --git a/public/blocks/navigation/tests/behat/expand_courses_node.feature b/public/blocks/navigation/tests/behat/expand_courses_node.feature index d8b046e7419..cc2456f9926 100644 --- a/public/blocks/navigation/tests/behat/expand_courses_node.feature +++ b/public/blocks/navigation/tests/behat/expand_courses_node.feature @@ -37,6 +37,7 @@ Feature: Expand the courses nodes within the navigation block And the following config values are set as admin: | enablemyhome | 1 | | navshowallcourses | 1 | + | enablemycourses | 1 | And I log in as "admin" And I am on site homepage And I turn editing mode on diff --git a/public/blocks/navigation/tests/behat/participants_link.feature b/public/blocks/navigation/tests/behat/participants_link.feature index 3dc76cca197..093815e9462 100644 --- a/public/blocks/navigation/tests/behat/participants_link.feature +++ b/public/blocks/navigation/tests/behat/participants_link.feature @@ -22,6 +22,7 @@ Feature: Displaying the link to the Participants page And I turn editing mode on And the following config values are set as admin: | unaddableblocks | | theme_boost| + | enablemycourses | 1 | | And I add the "Navigation" block if not present And I configure the "Navigation" block And I set the following fields to these values: diff --git a/public/blocks/navigation/tests/behat/view_my_courses.feature b/public/blocks/navigation/tests/behat/view_my_courses.feature index 3b9f199a9df..4f93fcb33d5 100644 --- a/public/blocks/navigation/tests/behat/view_my_courses.feature +++ b/public/blocks/navigation/tests/behat/view_my_courses.feature @@ -6,7 +6,8 @@ Feature: View my courses in navigation block Background: Given the following config values are set as admin: - | enablemyhome | 1 | + | enablemyhome | 1 | + | enablemycourses | 1 | And the following "users" exist: | username | firstname | lastname | email | | student1 | Student | 1 | student1@example.com | diff --git a/public/blocks/starredcourses/tests/behat/starred_courses.feature b/public/blocks/starredcourses/tests/behat/starred_courses.feature index 6bd79d414db..a7bb402df0c 100644 --- a/public/blocks/starredcourses/tests/behat/starred_courses.feature +++ b/public/blocks/starredcourses/tests/behat/starred_courses.feature @@ -5,7 +5,9 @@ Feature: Starred courses I must be able to add them to the Starred courses block Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | | student1 | Student | 1 | student1@example.com | And the following "courses" exist: diff --git a/public/blocks/tests/externallib_test.php b/public/blocks/tests/externallib_test.php index 2825b10a062..249652ba432 100644 --- a/public/blocks/tests/externallib_test.php +++ b/public/blocks/tests/externallib_test.php @@ -477,9 +477,12 @@ final class externallib_test extends \core_external\tests\externallib_testcase { * Test user get default dashboard blocks for my courses page. */ public function test_get_dashboard_blocks_my_courses(): void { - global $PAGE, $DB; + global $CFG, $PAGE, $DB; $this->resetAfterTest(true); + // Ensure My courses is enabled for this test. + $CFG->enablemycourses = 1; + $user = $this->getDataGenerator()->create_user(); $PAGE->set_url('/my/index.php'); // Need this because some internal API calls require the $PAGE url to be set. @@ -522,6 +525,28 @@ final class externallib_test extends \core_external\tests\externallib_testcase { $this->assertEquals(array_values($alldefaultblocksordered), $returnedblocks); } + /** + * Test that requesting My courses blocks when enablemycourses is disabled throws an exception. + * + * @covers \core_block_external::get_dashboard_blocks + */ + public function test_get_dashboard_blocks_my_courses_disabled(): void { + global $CFG, $PAGE; + $this->resetAfterTest(true); + + $user = $this->getDataGenerator()->create_user(); + $PAGE->set_url('/my/index.php'); + + $this->setUser($user); + + // Disable My courses. + $CFG->enablemycourses = 0; + + $this->expectException('moodle_exception'); + $this->expectExceptionMessage(get_string('error:mycoursesisdisabled', 'my')); + core_block_external::get_dashboard_blocks($user->id, false, MY_PAGE_COURSES); + } + /** * Test user passing the wrong page type and getting an exception. */ diff --git a/public/completion/tests/behat/course_completion_activity_criteria.feature b/public/completion/tests/behat/course_completion_activity_criteria.feature index c993cec93dd..a2b67dd3af0 100644 --- a/public/completion/tests/behat/course_completion_activity_criteria.feature +++ b/public/completion/tests/behat/course_completion_activity_criteria.feature @@ -5,7 +5,9 @@ Feature: Course completion state should match completion criteria I need to see the appropriate completion information on course and dashboard pages Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | idnumber | | teacher1 | Teacher | 1 | teacher1@example.com | T1 | | student1 | Student | 1 | student1@example.com | S1 | diff --git a/public/course/tests/behat/frontpage_display_modes.feature b/public/course/tests/behat/frontpage_display_modes.feature index feef9a27466..748b5e92f5a 100644 --- a/public/course/tests/behat/frontpage_display_modes.feature +++ b/public/course/tests/behat/frontpage_display_modes.feature @@ -6,7 +6,8 @@ Feature: Site home displays items in different modes Background: Given the following config values are set as admin: - | enablemyhome | 1 | + | enablemyhome | 1 | + | enablemycourses | 1 | And the following "categories" exist: | name | category | idnumber | | Category A | 0 | CATA | diff --git a/public/lib/classes/navigation/global_navigation.php b/public/lib/classes/navigation/global_navigation.php index 00ff10b2a50..7e146986d8f 100644 --- a/public/lib/classes/navigation/global_navigation.php +++ b/public/lib/classes/navigation/global_navigation.php @@ -228,18 +228,21 @@ class global_navigation extends navigation_node { null, 'currentcourse', ); - $this->rootnodes['mycourses'] = $this->add( - get_string('mycourses'), - new url('/my/courses.php'), - self::TYPE_ROOTNODE, - null, - 'mycourses', - new pix_icon('i/course', ''), - ); - // We do not need to show this node in the breadcrumbs if the default homepage is mycourses. - // It will be automatically handled by the breadcrumb generator. - if ($defaulthomepage == HOMEPAGE_MYCOURSES) { - $this->rootnodes['mycourses']->mainnavonly = true; + // Only add mycourses node if it's enabled. + if (!empty($CFG->enablemycourses)) { + $this->rootnodes['mycourses'] = $this->add( + get_string('mycourses'), + new url('/my/courses.php'), + self::TYPE_ROOTNODE, + null, + 'mycourses', + new pix_icon('i/course', ''), + ); + // We do not need to show this node in the breadcrumbs if the default homepage is mycourses. + // It will be automatically handled by the breadcrumb generator. + if ($defaulthomepage == HOMEPAGE_MYCOURSES) { + $this->rootnodes['mycourses']->mainnavonly = true; + } } $this->rootnodes['courses'] = $this->add( @@ -269,8 +272,10 @@ class global_navigation extends navigation_node { $this->rootnodes['currentcourse']->mainnavonly = true; if ($enrolledinanycourse) { - $this->rootnodes['mycourses']->isexpandable = true; - $this->rootnodes['mycourses']->showinflatnavigation = true; + if (!empty($CFG->enablemycourses)) { + $this->rootnodes['mycourses']->isexpandable = true; + $this->rootnodes['mycourses']->showinflatnavigation = true; + } if ($CFG->navshowallcourses) { // When we show all courses we need to show both the my courses and the regular courses branch. $this->rootnodes['courses']->isexpandable = true; @@ -278,7 +283,9 @@ class global_navigation extends navigation_node { } else { $this->rootnodes['courses']->isexpandable = true; } - $this->rootnodes['mycourses']->forceopen = true; + if (!empty($CFG->enablemycourses)) { + $this->rootnodes['mycourses']->forceopen = true; + } $canviewcourseprofile = true; @@ -1776,7 +1783,10 @@ class global_navigation extends navigation_node { $coursename = empty($CFG->navshowfullcoursenames) ? $shortname : $fullname; if ($coursetype == self::COURSE_CURRENT) { - if ($coursenode = $this->rootnodes['mycourses']->find($course->id, self::TYPE_COURSE)) { + if ( + !empty($CFG->enablemycourses) && + ($coursenode = $this->rootnodes['mycourses']->find($course->id, self::TYPE_COURSE)) + ) { return $coursenode; } else { $coursetype = self::COURSE_OTHER; @@ -1795,7 +1805,7 @@ class global_navigation extends navigation_node { $parent = $this->rootnodes['currentcourse']; $url = new url('/course/view.php', ['id' => $course->id]); $canexpandcourse = $this->can_expand_course($course); - } else if ($coursetype == self::COURSE_MY && !$forcegeneric) { + } else if ($coursetype == self::COURSE_MY && !$forcegeneric && !empty($CFG->enablemycourses)) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf if ( !empty($CFG->navshowmycoursecategories) @@ -2252,6 +2262,11 @@ class global_navigation extends navigation_node { protected function load_courses_enrolled() { global $CFG; + // Don't load courses if My Courses is disabled. + if (empty($CFG->enablemycourses)) { + return; + } + $limit = (int) $CFG->navcourselimit; $courses = enrol_get_my_courses('*'); diff --git a/public/lib/classes/navigation/views/primary.php b/public/lib/classes/navigation/views/primary.php index 9fb2f349054..ab73eacae83 100644 --- a/public/lib/classes/navigation/views/primary.php +++ b/public/lib/classes/navigation/views/primary.php @@ -66,8 +66,9 @@ class primary extends view { } // Add the mycourses link. - $showcoursesnode = empty($this->page->theme->removedprimarynavitems) || - !in_array('courses', $this->page->theme->removedprimarynavitems); + $showcoursesnode = !empty($CFG->enablemycourses) && + (empty($this->page->theme->removedprimarynavitems) || + !in_array('courses', $this->page->theme->removedprimarynavitems)); if ($showcoursesnode) { $this->add(get_string('mycourses'), new \moodle_url('/my/courses.php'), self::TYPE_ROOTNODE, null, 'mycourses'); } diff --git a/public/lib/moodlelib.php b/public/lib/moodlelib.php index 850a72838ad..ebdf5d5dc62 100644 --- a/public/lib/moodlelib.php +++ b/public/lib/moodlelib.php @@ -9919,7 +9919,7 @@ function get_home_page() { $homeenabled = !empty($CFG->enablemyhome); if (isloggedin() && isset($CFG->defaulthomepage) && $CFG->defaulthomepage !== '') { - // If dashboard is disabled, home will be set to default page. + // If dashboard, mycourses or home is disabled, home will be set to default page. $defaultpage = get_default_home_page(); if ($CFG->defaulthomepage == HOMEPAGE_SITE) { if ($homeenabled) { @@ -9934,7 +9934,11 @@ function get_home_page() { return $defaultpage; } } else if ($CFG->defaulthomepage == HOMEPAGE_MYCOURSES && !isguestuser()) { - return HOMEPAGE_MYCOURSES; + if (!empty($CFG->enablemycourses)) { + return HOMEPAGE_MYCOURSES; + } else { + return $defaultpage; + } } else if ($CFG->defaulthomepage == HOMEPAGE_USER && !isguestuser()) { $userhomepage = get_user_preferences('user_home_page_preference', $defaultpage); if (!$homeenabled && $userhomepage == HOMEPAGE_SITE) { @@ -9943,6 +9947,9 @@ function get_home_page() { 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; + } else if (empty($CFG->enablemycourses) && $userhomepage == HOMEPAGE_MYCOURSES) { + // If the user was using my courses but it's disabled, return the default home page. + $userhomepage = $defaultpage; } else if (get_default_home_page_url()) { return HOMEPAGE_URL; } diff --git a/public/lib/tests/behat/enabledashboard.feature b/public/lib/tests/behat/enabledashboard.feature index 83073403178..d7ad7d0ceb3 100644 --- a/public/lib/tests/behat/enabledashboard.feature +++ b/public/lib/tests/behat/enabledashboard.feature @@ -7,6 +7,7 @@ Feature: Enable dashboard setting Scenario: Hide setting when dashboard is disabled Given the following config values are set as admin: | enabledashboard | 0 | + | enablemycourses | 1 | # 2 = User preference. | defaulthomepage | 2 | When I log in as "admin" diff --git a/public/lib/tests/hook/navigation/primary_extend_test.php b/public/lib/tests/hook/navigation/primary_extend_test.php index 64476c7b027..fbd1fe222bf 100644 --- a/public/lib/tests/hook/navigation/primary_extend_test.php +++ b/public/lib/tests/hook/navigation/primary_extend_test.php @@ -85,6 +85,8 @@ final class primary_extend_test extends \advanced_testcase { set_config('enablemyhome', 0); set_config('enabledashboard', 1); + set_config('enablemycourses', 1); + $PAGE = new \moodle_page(); $PAGE->set_url('/'); diff --git a/public/lib/tests/moodlelib_test.php b/public/lib/tests/moodlelib_test.php index a17fc0d1573..210b84d9b3d 100644 --- a/public/lib/tests/moodlelib_test.php +++ b/public/lib/tests/moodlelib_test.php @@ -5250,6 +5250,7 @@ EOT; * @param int|null $enabledashboard Whether the dashboard should be enabled or not. * @param int|string|null $userpreference User preference for the home page setting. * $param int|null $allowguestmymoodle The $CFG->allowguestmymoodle setting value. + * @param int|null $enablemycourses Whether my courses should be enabled or not. * @param int|null $enablemyhome Whether the home page should be enabled or not. * @covers ::get_home_page */ @@ -5260,6 +5261,7 @@ EOT; ?int $enabledashboard = null, int|string|null $userpreference = null, ?int $allowguestmymoodle = null, + ?int $enablemycourses = null, ?int $enablemyhome = null, ): void { global $CFG, $USER; @@ -5281,6 +5283,10 @@ EOT; if (isset($allowguestmymoodle)) { $CFG->allowguestmymoodle = $allowguestmymoodle; } + if (!isset($enablemycourses)) { + $enablemycourses = 1; + } + $CFG->enablemycourses = $enablemycourses; if (!isset($enablemyhome)) { $enablemyhome = 1; } @@ -5424,6 +5430,28 @@ EOT; 'userpreference' => HOMEPAGE_SITE, 'enablemyhome' => 0, ], + 'Logged user. My courses set as default home page with my courses disabled' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MY, + 'defaulthomepage' => HOMEPAGE_MYCOURSES, + 'enabledashboard' => 1, + 'enablemycourses' => 0, + ], + 'Logged user. User preference set to my courses with my courses disabled' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_MY, + 'defaulthomepage' => HOMEPAGE_USER, + 'enabledashboard' => 1, + 'userpreference' => HOMEPAGE_MYCOURSES, + 'enablemycourses' => 0, + ], + 'Logged user. My courses disabled and dashboard disabled, fallback to site' => [ + 'user' => 'logged', + 'expected' => HOMEPAGE_SITE, + 'defaulthomepage' => HOMEPAGE_MYCOURSES, + 'enabledashboard' => 0, + 'enablemycourses' => 0, + ], ]; } @@ -5437,13 +5465,40 @@ EOT; $this->resetAfterTest(); + // Dashboard enabled takes priority. $CFG->enabledashboard = 1; + $CFG->enablemycourses = 1; + $CFG->enablemyhome = 1; $default = get_default_home_page(); $this->assertEquals(HOMEPAGE_MY, $default); + // Dashboard disabled, my courses enabled. $CFG->enabledashboard = 0; + $CFG->enablemycourses = 1; + $CFG->enablemyhome = 1; $default = get_default_home_page(); $this->assertEquals(HOMEPAGE_MYCOURSES, $default); + + // Dashboard and my courses disabled, home enabled. + $CFG->enabledashboard = 0; + $CFG->enablemycourses = 0; + $CFG->enablemyhome = 1; + $default = get_default_home_page(); + $this->assertEquals(HOMEPAGE_SITE, $default); + + // All three disabled, fallback to user preference. + $CFG->enabledashboard = 0; + $CFG->enablemycourses = 0; + $CFG->enablemyhome = 0; + $default = get_default_home_page(); + $this->assertEquals(HOMEPAGE_USER, $default); + + // Dashboard enabled, others disabled. + $CFG->enabledashboard = 1; + $CFG->enablemycourses = 0; + $CFG->enablemyhome = 0; + $default = get_default_home_page(); + $this->assertEquals(HOMEPAGE_MY, $default); } /** diff --git a/public/lib/tests/navigation/views/primary_test.php b/public/lib/tests/navigation/views/primary_test.php index a69470b3dd5..bdd8013e4e8 100644 --- a/public/lib/tests/navigation/views/primary_test.php +++ b/public/lib/tests/navigation/views/primary_test.php @@ -43,6 +43,7 @@ final class primary_test extends \advanced_testcase { $this->resetAfterTest(); set_config('enablemyhome', 0); set_config('enabledashboard', 1); + set_config('enablemycourses', 1); if ($usertype == 'admin') { $this->setAdminUser(); } else if ($usertype == 'guest') { diff --git a/public/my/courses.php b/public/my/courses.php index 5df27133a36..2e3e887fbaf 100644 --- a/public/my/courses.php +++ b/public/my/courses.php @@ -35,6 +35,21 @@ redirect_if_major_upgrade_required(); require_login(); +// Redirect if My Courses is disabled. +if (empty($CFG->enablemycourses)) { + $defaultpage = get_home_page(); + if ($defaultpage == HOMEPAGE_URL) { + redirect(get_default_home_page_url()); + } else if ($defaultpage == HOMEPAGE_MY) { + redirect(new moodle_url('/my/')); + } else if ($defaultpage == HOMEPAGE_USER) { + // All homepage options disabled - redirect to user preferences page. + redirect(new moodle_url('/user/preferences.php')); + } else { + redirect(new moodle_url('/')); + } +} + $hassiteconfig = has_capability('moodle/site:config', context_system::instance()); if ($hassiteconfig && moodle_needs_upgrading()) { redirect(new moodle_url('/admin/index.php')); diff --git a/public/my/index.php b/public/my/index.php index 6921ffccf53..6bb07d41089 100644 --- a/public/my/index.php +++ b/public/my/index.php @@ -58,6 +58,11 @@ if (empty($CFG->enabledashboard)) { if ($defaultpage == HOMEPAGE_MYCOURSES) { // If default page is set to "My courses", redirect to it. redirect(new moodle_url('/my/courses.php')); + } else if ($defaultpage == HOMEPAGE_SITE) { + redirect(new moodle_url('/')); + } else if ($defaultpage == HOMEPAGE_USER) { + // All homepage options disabled - redirect to user preferences page. + redirect(new moodle_url('/user/preferences.php')); } else { // Otherwise, raise an exception to inform the dashboard is disabled. throw new moodle_exception('error:dashboardisdisabled', 'my'); diff --git a/public/my/tests/behat/my_courses.feature b/public/my/tests/behat/my_courses.feature index 8f8e7b703af..b55e204f3f4 100644 --- a/public/my/tests/behat/my_courses.feature +++ b/public/my/tests/behat/my_courses.feature @@ -3,7 +3,8 @@ Feature: Run tests over my courses page Background: Given the following config values are set as admin: - | enablemyhome | 1 | + | enablemyhome | 1 | + | enablemycourses | 1 | And the following "users" exist: | username | firstname | lastname | email | | user1 | User | 1 | user1@example.com | diff --git a/public/theme/boost/tests/behat/mycoursesblocks.feature b/public/theme/boost/tests/behat/mycoursesblocks.feature index 85b7a1536dd..064ddc0ff61 100644 --- a/public/theme/boost/tests/behat/mycoursesblocks.feature +++ b/public/theme/boost/tests/behat/mycoursesblocks.feature @@ -6,7 +6,8 @@ Feature: My courses page block layout in Boost theme Background: Given the following config values are set as admin: - | enablemyhome | 1 | + | enablemyhome | 1 | + | enablemycourses | 1 | And the following "users" exist: | username | firstname | lastname | email | | student1 | Student | 1 | student@example.com | diff --git a/public/theme/boost/tests/behat/primarynav.feature b/public/theme/boost/tests/behat/primarynav.feature index cd824bc16e8..43dc084f9d1 100644 --- a/public/theme/boost/tests/behat/primarynav.feature +++ b/public/theme/boost/tests/behat/primarynav.feature @@ -5,7 +5,9 @@ Feature: Primary navigation I need to use the primary navigation Background: - Given the following "users" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "users" exist: | username | firstname | lastname | email | | user1 | User | One | user1@example.com | And the following config values are set as admin: diff --git a/public/theme/boost/tests/behat/welcome_to_moodle.feature b/public/theme/boost/tests/behat/welcome_to_moodle.feature index 02a42dc0745..1591b7d8e75 100644 --- a/public/theme/boost/tests/behat/welcome_to_moodle.feature +++ b/public/theme/boost/tests/behat/welcome_to_moodle.feature @@ -32,6 +32,7 @@ Feature: Welcome message on boost @accessibility Scenario: Login and be welcomed on the my courses page Given the following config values are set as admin: + | enablemycourses | 1 | | defaulthomepage | 3 | When I log in as "admin" Then I should see "My courses" in the "page-header" "region" diff --git a/public/theme/classic/tests/behat/mycoursesblocks.feature b/public/theme/classic/tests/behat/mycoursesblocks.feature index c0640e17e01..c8186327f14 100644 --- a/public/theme/classic/tests/behat/mycoursesblocks.feature +++ b/public/theme/classic/tests/behat/mycoursesblocks.feature @@ -6,7 +6,8 @@ Feature: My courses page block layout in Classic theme Background: Given the following config values are set as admin: - | enablemyhome | 1 | + | enablemyhome | 1 | + | enablemycourses | 1 | And the following "users" exist: | username | firstname | lastname | email | | student1 | Student | 1 | student@example.com | diff --git a/public/theme/classic/tests/behat/welcome_to_classicmoodle.feature b/public/theme/classic/tests/behat/welcome_to_classicmoodle.feature index b58177dd43b..012b3426897 100644 --- a/public/theme/classic/tests/behat/welcome_to_classicmoodle.feature +++ b/public/theme/classic/tests/behat/welcome_to_classicmoodle.feature @@ -28,6 +28,7 @@ Feature: Welcome message on classic Scenario: Login and be welcomed on the my courses page Given the following config values are set as admin: + | enablemycourses | 1 | | defaulthomepage | 3 | When I log in as "admin" Then I should see "My courses" in the "page-header" "region" diff --git a/public/user/tests/behat/set_default_homepage.feature b/public/user/tests/behat/set_default_homepage.feature index a0badad3ab9..c856a7ab550 100644 --- a/public/user/tests/behat/set_default_homepage.feature +++ b/public/user/tests/behat/set_default_homepage.feature @@ -5,7 +5,9 @@ Feature: Set the site home page and dashboard as the default home page I need to choose which page I want and set it as my home page Background: - Given the following "courses" exist: + Given the following config values are set as admin: + | enablemycourses | 1 | + And the following "courses" exist: | fullname | shortname | category | groupmode | | Course 1 | C1 | 0 | 1 | And the following "users" exist: diff --git a/public/user/tests/editlib_test.php b/public/user/tests/editlib_test.php index eb8dc773f43..b4fbfedbfae 100644 --- a/public/user/tests/editlib_test.php +++ b/public/user/tests/editlib_test.php @@ -141,6 +141,9 @@ final class editlib_test extends \advanced_testcase { $user = $this->getDataGenerator()->create_user(); $this->setUser($user); + // Enable My Courses so HOMEPAGE_MYCOURSES is a valid option. + set_config('enablemycourses', 1); + if ($extraoptions) { // Pretend we have hook callbacks adding extra allowed options. $testcallback = function(\core_user\hook\extend_default_homepage $hook) use ($extraoptions) {