MOBILE-3930 course: Change section if needed when changing module

main
Dani Palou 2022-03-11 11:40:15 +01:00
parent b72e247f81
commit 3f82998ae2
3 changed files with 61 additions and 19 deletions

View File

@ -148,6 +148,14 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
this.viewedModules[data.cmId] = true; this.viewedModules[data.cmId] = true;
if (!this.lastModuleViewed || data.timeaccess > this.lastModuleViewed.timeaccess) { if (!this.lastModuleViewed || data.timeaccess > this.lastModuleViewed.timeaccess) {
this.lastModuleViewed = data; 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) { if (!currentSectionData.forceSelected && lastModuleViewed) {
// Search the section with the last module viewed. // Search the section with the last module viewed.
let lastModuleSection: CoreCourseSection | undefined; const lastModuleSection = this.getViewedModuleSection(sections, lastModuleViewed);
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),
);
}
section = lastModuleSection || section; section = lastModuleSection || section;
moduleId = lastModuleSection ? lastModuleViewed?.cmId : undefined; 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. * 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. // Scroll to module if needed. Give more priority to the input.
moduleId = this.moduleId && previousValue === undefined ? this.moduleId : moduleId; const moduleIdToScroll = this.moduleId && previousValue === undefined ? this.moduleId : moduleId;
if (moduleId) { if (moduleIdToScroll) {
setTimeout(() => { setTimeout(() => {
CoreDomUtils.scrollToElementBySelector( this.scrollToModule(moduleIdToScroll);
this.elementRef.nativeElement,
this.content,
'#core-course-module-' + moduleId,
);
}, 200); }, 200);
} else { } else {
this.content.scrollToTop(0); this.content.scrollToTop(0);
@ -466,6 +485,19 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
this.invalidateSectionButtons(); 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. * Compare if two sections are equal.
* *

View File

@ -271,6 +271,10 @@ export class CoreCourseModuleSummaryComponent implements OnInit, OnDestroy {
{ {
replace: true, replace: true,
animationDirection: 'back', animationDirection: 'back',
params: {
module: this.module,
openModule: false,
},
}, },
); );
} }

View File

@ -57,6 +57,7 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy {
protected module?: CoreCourseModuleData; protected module?: CoreCourseModuleData;
protected modNavOptions?: CoreNavigationOptions; protected modNavOptions?: CoreNavigationOptions;
protected isGuest = false; protected isGuest = false;
protected openModule = true;
protected contentsTab: CoreTabsOutletTab & { pageParams: Params } = { protected contentsTab: CoreTabsOutletTab & { pageParams: Params } = {
page: CONTENTS_PAGE_NAME, page: CONTENTS_PAGE_NAME,
title: 'core.course', title: 'core.course',
@ -136,6 +137,7 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy {
this.module = CoreNavigator.getRouteParam<CoreCourseModuleData>('module'); this.module = CoreNavigator.getRouteParam<CoreCourseModuleData>('module');
this.isGuest = !!CoreNavigator.getRouteBooleanParam('isGuest'); this.isGuest = !!CoreNavigator.getRouteBooleanParam('isGuest');
this.modNavOptions = CoreNavigator.getRouteParam<CoreNavigationOptions>('modNavOptions'); this.modNavOptions = CoreNavigator.getRouteParam<CoreNavigationOptions>('modNavOptions');
this.openModule = CoreNavigator.getRouteBooleanParam('openModule') ?? true; // If false, just scroll to module.
if (!this.modNavOptions) { if (!this.modNavOptions) {
// Fallback to old way of passing params. @deprecated since 4.0. // Fallback to old way of passing params. @deprecated since 4.0.
const modParams = CoreNavigator.getRouteParam<Params>('modParams'); const modParams = CoreNavigator.getRouteParam<Params>('modParams');
@ -155,6 +157,10 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy {
if (this.module) { if (this.module) {
this.contentsTab.pageParams.moduleId = this.module.id; 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); this.tabs.push(this.contentsTab);
@ -170,7 +176,7 @@ export class CoreCourseIndexPage implements OnInit, OnDestroy {
* A tab was selected. * A tab was selected.
*/ */
tabSelected(): void { tabSelected(): void {
if (!this.module || !this.course) { if (!this.module || !this.course || !this.openModule) {
return; return;
} }
// Now that the first tab has been selected we can load the module. // Now that the first tab has been selected we can load the module.