From 8d4e4364c0ef2bd61a9f5cc19a4568f7a3647331 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 5 Sep 2024 11:02:56 +0200 Subject: [PATCH] MOBILE-4641 core: Support custom default home pages --- .../classes/sites/unauthenticated-site.ts | 2 + .../services/contentlinks-helper.ts | 8 ++-- .../services/handlers/my-courses-mainmenu.ts | 5 ++- src/core/features/mainmenu/pages/menu/menu.ts | 44 +++++++++++++++---- .../mainmenu/tests/behat/mainmenu.feature | 28 ++++++++++++ 5 files changed, 72 insertions(+), 15 deletions(-) diff --git a/src/core/classes/sites/unauthenticated-site.ts b/src/core/classes/sites/unauthenticated-site.ts index 8ce6df346..fc958b0e3 100644 --- a/src/core/classes/sites/unauthenticated-site.ts +++ b/src/core/classes/sites/unauthenticated-site.ts @@ -445,6 +445,7 @@ export type CoreSiteInfoResponse = { userquota?: number; // User quota (bytes). 0 means user can ignore the quota. usermaxuploadfilesize?: number; // User max upload file size (bytes). -1 means the user can ignore the upload file size. userhomepage?: CoreSiteInfoUserHomepage; // The default home page for the user. + userhomepageurl?: string; // @since 4.5. The URL of the custom user home page when using HOMEPAGE_URL. userprivateaccesskey?: string; // Private user access key for fetching files. siteid?: number; // Site course ID. sitecalendartype?: string; // Calendar type set in the site. @@ -475,6 +476,7 @@ export enum CoreSiteInfoUserHomepage { HOMEPAGE_SITE = 0, // Site home. HOMEPAGE_MY = 1, // Dashboard. HOMEPAGE_MYCOURSES = 3, // My courses. + HOMEPAGE_URL = 4, // A custom URL. } /** diff --git a/src/core/features/contentlinks/services/contentlinks-helper.ts b/src/core/features/contentlinks/services/contentlinks-helper.ts index 4f63b6770..979eb29ef 100644 --- a/src/core/features/contentlinks/services/contentlinks-helper.ts +++ b/src/core/features/contentlinks/services/contentlinks-helper.ts @@ -136,7 +136,7 @@ export class CoreContentLinksHelperProvider { if (data.site) { // URL is the root of the site. - this.handleRootURL(data.site, openBrowserRoot); + await this.handleRootURL(data.site, openBrowserRoot); return true; } @@ -150,19 +150,19 @@ export class CoreContentLinksHelperProvider { if (!CoreSites.isLoggedIn()) { // No current site. Perform the action if only 1 site found, choose the site otherwise. if (action.sites?.length == 1) { - action.action(action.sites[0]); + await action.action(action.sites[0]); } else { this.goToChooseSite(url); } } else if (action.sites?.length == 1 && action.sites[0] == CoreSites.getCurrentSiteId()) { // Current site. - action.action(action.sites[0]); + await action.action(action.sites[0]); } else { try { // Not current site or more than one site. Ask for confirmation. await CoreDomUtils.showConfirm(Translate.instant('core.contentlinks.confirmurlothersite')); if (action.sites?.length == 1) { - action.action(action.sites[0]); + await action.action(action.sites[0]); } else { this.goToChooseSite(url); } diff --git a/src/core/features/courses/services/handlers/my-courses-mainmenu.ts b/src/core/features/courses/services/handlers/my-courses-mainmenu.ts index 110d8f1e7..52dbb47c0 100644 --- a/src/core/features/courses/services/handlers/my-courses-mainmenu.ts +++ b/src/core/features/courses/services/handlers/my-courses-mainmenu.ts @@ -60,9 +60,10 @@ export class CoreCoursesMyCoursesMainMenuHandlerService implements CoreMainMenuH * @inheritdoc */ getDisplayData(): CoreMainMenuHandlerData { - const site = CoreSites.getCurrentSite(); + const userHomePage = CoreSites.getCurrentSite()?.getInfo()?.userhomepage; - const displayMyCourses = site?.getInfo() && site?.getInfo()?.userhomepage === CoreSiteInfoUserHomepage.HOMEPAGE_MYCOURSES; + const displayMyCourses = userHomePage === CoreSiteInfoUserHomepage.HOMEPAGE_MYCOURSES || + userHomePage === CoreSiteInfoUserHomepage.HOMEPAGE_URL; return { title: 'core.courses.mycourses', diff --git a/src/core/features/mainmenu/pages/menu/menu.ts b/src/core/features/mainmenu/pages/menu/menu.ts index d2c43a7f0..ab32457f4 100644 --- a/src/core/features/mainmenu/pages/menu/menu.ts +++ b/src/core/features/mainmenu/pages/menu/menu.ts @@ -33,6 +33,8 @@ import { CoreLogger } from '@singletons/logger'; import { CorePlatform } from '@services/platform'; import { CoreWait } from '@singletons/wait'; import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager'; +import { CoreSiteInfoUserHomepage } from '@classes/sites/unauthenticated-site'; +import { CoreContentLinksHelper } from '@features/contentlinks/services/contentlinks-helper'; const ANIMATION_DURATION = 500; @@ -110,15 +112,7 @@ export class CoreMainMenuPage implements OnInit, OnDestroy { async ngOnInit(): Promise { this.showTabs = true; - const deepLinkManager = new CoreMainMenuDeepLinkManager(); - - // Treat the deep link (if any) when the login navigation finishes. - CoreSites.runAfterLoginNavigation({ - priority: 800, - callback: async () => { - await deepLinkManager.treatLink(); - }, - }); + this.initAfterLoginNavigations(); this.isMainScreen = !this.mainTabs?.outlet.canGoBack(); this.updateVisibility(); @@ -223,6 +217,38 @@ export class CoreMainMenuPage implements OnInit, OnDestroy { } } + /** + * Set up the code to run after the login navigation finishes. + */ + protected initAfterLoginNavigations(): void { + // Treat custom home page and deep link (if any) when the login navigation finishes. + const deepLinkManager = new CoreMainMenuDeepLinkManager(); + + CoreSites.runAfterLoginNavigation({ + priority: 800, + callback: async () => { + await deepLinkManager.treatLink(); + }, + }); + + CoreSites.runAfterLoginNavigation({ + priority: 1000, + callback: async () => { + const userHomePage = CoreSites.getCurrentSite()?.getInfo()?.userhomepage; + if (userHomePage !== CoreSiteInfoUserHomepage.HOMEPAGE_URL) { + return; + } + + const url = CoreSites.getCurrentSite()?.getInfo()?.userhomepageurl; + if (!url) { + return; + } + + await CoreContentLinksHelper.handleLink(url); + }, + }); + } + /** * Check all non visible tab handlers for any badge text or number. */ diff --git a/src/core/features/mainmenu/tests/behat/mainmenu.feature b/src/core/features/mainmenu/tests/behat/mainmenu.feature index 2a281f2ce..9c5fc73cb 100644 --- a/src/core/features/mainmenu/tests/behat/mainmenu.feature +++ b/src/core/features/mainmenu/tests/behat/mainmenu.feature @@ -38,6 +38,34 @@ Feature: Main Menu opens the right page And I should find "Course 1" in the app And "My courses" "text" should appear before "Home" "text" in the ".mainmenu-tabs" "css_element" + @lms_from4.5 + Scenario: Opens right main menu tab when defaulthomepage is set to a custom URL that belongs to a tab + Given the following config values are set as admin: + | defaulthomepage | #wwwroot#/message/index.php | + And I entered the app as "student" + Then "Messages" "ion-tab-button" should be selected in the app + And I should find "Contacts" in the app + + @lms_from4.5 + Scenario: Opens new page when defaulthomepage is set to a custom URL + Given the following config values are set as admin: + | defaulthomepage | #wwwroot#/badges/mybadges.php | + And I entered the app as "student" + Then I should find "Badges" in the app + And I should find "There are currently no badges" in the app + + When I press the back button in the app + Then "My courses" "ion-tab-button" should be selected in the app + And I should find "Course 1" in the app + + @lms_from4.5 + Scenario: defaulthomepage ignored if it's set to a custom URL not supported by the app + Given the following config values are set as admin: + | defaulthomepage | #wwwroot#/foo/bar.php | + And I entered the app as "student" + Then "My courses" "ion-tab-button" should be selected in the app + And I should find "Course 1" in the app + # @todo MOBILE-4119: This test is too flaky to run in CI until the race condition is fixed. # Scenario: Opens first tab after Site Home, Dashboard, and My Courses are disabled # Given I entered the app as "student"