MDL-87544 core: route homepage selection with enablemycourses

This commit is contained in:
Andi Permana
2026-03-06 16:50:32 +07:00
parent f810c5e4fd
commit c310b620ad
37 changed files with 215 additions and 43 deletions
@@ -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 |
@@ -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:
@@ -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:
@@ -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:
@@ -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:
@@ -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:
@@ -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:
@@ -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:
@@ -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 |
@@ -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 |
@@ -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:
@@ -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 |
@@ -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
@@ -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
@@ -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:
@@ -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 |
@@ -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:
+26 -1
View File
@@ -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.
*/
@@ -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 |
@@ -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 |
@@ -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('*');
@@ -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');
}
+9 -2
View File
@@ -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;
}
@@ -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"
@@ -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('/');
+55
View File
@@ -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);
}
/**
@@ -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') {
+15
View File
@@ -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'));
+5
View File
@@ -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');
+2 -1
View File
@@ -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 |
@@ -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 |
@@ -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:
@@ -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"
@@ -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 |
@@ -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"
@@ -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:
+3
View File
@@ -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) {