MOBILE-3930 course: Change section if needed when changing module
parent
b72e247f81
commit
3f82998ae2
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -271,6 +271,10 @@ export class CoreCourseModuleSummaryComponent implements OnInit, OnDestroy {
|
|||
{
|
||||
replace: true,
|
||||
animationDirection: 'back',
|
||||
params: {
|
||||
module: this.module,
|
||||
openModule: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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<CoreCourseModuleData>('module');
|
||||
this.isGuest = !!CoreNavigator.getRouteBooleanParam('isGuest');
|
||||
this.modNavOptions = CoreNavigator.getRouteParam<CoreNavigationOptions>('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<Params>('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.
|
||||
|
|
Loading…
Reference in New Issue