From 3f82998ae25863e7213db0522651167867c0dec7 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 11 Mar 2022 11:40:15 +0100 Subject: [PATCH] MOBILE-3930 course: Change section if needed when changing module --- .../components/course-format/course-format.ts | 68 ++++++++++++++----- .../module-summary/module-summary.ts | 4 ++ src/core/features/course/pages/index/index.ts | 8 ++- 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/core/features/course/components/course-format/course-format.ts b/src/core/features/course/components/course-format/course-format.ts index 32c742a53..207d2dc8e 100644 --- a/src/core/features/course/components/course-format/course-format.ts +++ b/src/core/features/course/components/course-format/course-format.ts @@ -148,6 +148,14 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy { this.viewedModules[data.cmId] = true; if (!this.lastModuleViewed || data.timeaccess > this.lastModuleViewed.timeaccess) { this.lastModuleViewed = data; + + if (this.selectedSection) { + // Change section to display the one with the last viewed module + const lastViewedSection = this.getViewedModuleSection(this.sections, data); + if (lastViewedSection && lastViewedSection.id !== this.selectedSection?.id) { + this.sectionChanged(lastViewedSection, data.cmId); + } + } } }); } @@ -298,17 +306,7 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy { if (!currentSectionData.forceSelected && lastModuleViewed) { // Search the section with the last module viewed. - let lastModuleSection: CoreCourseSection | undefined; - - if (lastModuleViewed.sectionId) { - lastModuleSection = sections.find(section => section.id === lastModuleViewed.sectionId); - } - if (!lastModuleSection) { - // No sectionId or section not found. Search the module. - lastModuleSection = sections.find( - section => section.modules.some(module => module.id === lastModuleViewed.cmId), - ); - } + const lastModuleSection = this.getViewedModuleSection(sections, lastModuleViewed); section = lastModuleSection || section; moduleId = lastModuleSection ? lastModuleViewed?.cmId : undefined; @@ -343,6 +341,31 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy { }); } + /** + * Get the section of a viewed module. + * + * @param sections List of sections. + * @param viewedModule Viewed module. + * @return Section, undefined if not found. + */ + protected getViewedModuleSection( + sections: CoreCourseSection[], + viewedModule: CoreCourseViewedModulesDBRecord, + ): CoreCourseSection | undefined { + if (viewedModule.sectionId) { + const lastModuleSection = sections.find(section => section.id === viewedModule.sectionId); + + if (lastModuleSection) { + return lastModuleSection; + } + } + + // No sectionId or section not found. Search the module. + return sections.find( + section => section.modules.some(module => module.id === viewedModule.cmId), + ); + } + /** * Display the course index modal. */ @@ -443,14 +466,10 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy { } // Scroll to module if needed. Give more priority to the input. - moduleId = this.moduleId && previousValue === undefined ? this.moduleId : moduleId; - if (moduleId) { + const moduleIdToScroll = this.moduleId && previousValue === undefined ? this.moduleId : moduleId; + if (moduleIdToScroll) { setTimeout(() => { - CoreDomUtils.scrollToElementBySelector( - this.elementRef.nativeElement, - this.content, - '#core-course-module-' + moduleId, - ); + this.scrollToModule(moduleIdToScroll); }, 200); } else { this.content.scrollToTop(0); @@ -466,6 +485,19 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy { this.invalidateSectionButtons(); } + /** + * Scroll to a certain module. + * + * @param moduleId Module ID. + */ + protected scrollToModule(moduleId: number): void { + CoreDomUtils.scrollToElementBySelector( + this.elementRef.nativeElement, + this.content, + '#core-course-module-' + moduleId, + ); + } + /** * Compare if two sections are equal. * diff --git a/src/core/features/course/components/module-summary/module-summary.ts b/src/core/features/course/components/module-summary/module-summary.ts index f480c8469..61e5695cf 100644 --- a/src/core/features/course/components/module-summary/module-summary.ts +++ b/src/core/features/course/components/module-summary/module-summary.ts @@ -271,6 +271,10 @@ export class CoreCourseModuleSummaryComponent implements OnInit, OnDestroy { { replace: true, animationDirection: 'back', + params: { + module: this.module, + openModule: false, + }, }, ); } diff --git a/src/core/features/course/pages/index/index.ts b/src/core/features/course/pages/index/index.ts index bb2537e83..e6258e4e6 100644 --- a/src/core/features/course/pages/index/index.ts +++ b/src/core/features/course/pages/index/index.ts @@ -57,6 +57,7 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy { protected module?: CoreCourseModuleData; protected modNavOptions?: CoreNavigationOptions; protected isGuest = false; + protected openModule = true; protected contentsTab: CoreTabsOutletTab & { pageParams: Params } = { page: CONTENTS_PAGE_NAME, title: 'core.course', @@ -136,6 +137,7 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy { this.module = CoreNavigator.getRouteParam('module'); this.isGuest = !!CoreNavigator.getRouteBooleanParam('isGuest'); this.modNavOptions = CoreNavigator.getRouteParam('modNavOptions'); + this.openModule = CoreNavigator.getRouteBooleanParam('openModule') ?? true; // If false, just scroll to module. if (!this.modNavOptions) { // Fallback to old way of passing params. @deprecated since 4.0. const modParams = CoreNavigator.getRouteParam('modParams'); @@ -155,6 +157,10 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy { if (this.module) { this.contentsTab.pageParams.moduleId = this.module.id; + if (!this.contentsTab.pageParams.sectionId && !this.contentsTab.pageParams.sectionNumber) { + // No section specified, use module section. + this.contentsTab.pageParams.sectionId = this.module.section; + } } this.tabs.push(this.contentsTab); @@ -170,7 +176,7 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy { * A tab was selected. */ tabSelected(): void { - if (!this.module || !this.course) { + if (!this.module || !this.course || !this.openModule) { return; } // Now that the first tab has been selected we can load the module.