MOBILE-4660 course: Fix open last viewed section

main
Pau Ferrer Ocaña 2024-10-01 11:59:38 +02:00 committed by Dani Palou
parent d3c3c56296
commit d384752113
2 changed files with 39 additions and 37 deletions

View File

@ -145,12 +145,16 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy {
if (initialSectionId !== undefined && initialSectionId > 0) { if (initialSectionId !== undefined && initialSectionId > 0) {
this.accordionMultipleValue.push(initialSectionId.toString()); this.accordionMultipleValue.push(initialSectionId.toString());
this.accordionGroupChange();
CoreDom.scrollToElement( CoreDom.scrollToElement(
this.elementRef.nativeElement, this.elementRef.nativeElement,
`#addons-course-storage-${initialSectionId}`, `#addons-course-storage-${initialSectionId}`,
{ addYAxis: -10 }, { addYAxis: -10 },
); );
} else {
this.accordionMultipleValue.push(this.sections[0].id.toString());
this.accordionGroupChange();
} }
await Promise.all([ await Promise.all([
@ -762,10 +766,10 @@ export class AddonStorageManagerCourseStoragePage implements OnInit, OnDestroy {
/** /**
* Toggle expand status. * Toggle expand status.
* *
* @param event Event object. * @param event Event object. If not defined, use the current value.
*/ */
accordionGroupChange(event: AccordionGroupChangeEventDetail): void { accordionGroupChange(event?: AccordionGroupChangeEventDetail): void {
const sectionIds = event.value as string[] | []; const sectionIds = event?.value as string[] ?? this.accordionMultipleValue;
this.sections.forEach((section) => { this.sections.forEach((section) => {
section.expanded = false; section.expanded = false;
section.modules.forEach((section) => { section.modules.forEach((section) => {

View File

@ -185,27 +185,25 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
} }
}); });
this.modViewedObserver = CoreEvents.on(CoreEvents.COURSE_MODULE_VIEWED, (data) => { this.modViewedObserver = CoreEvents.on(CoreEvents.COURSE_MODULE_VIEWED, (lastModuleViewed) => {
if (data.courseId !== this.course.id) { if (lastModuleViewed.courseId !== this.course.id) {
return; return;
} }
this.viewedModules[data.cmId] = true; this.viewedModules[lastModuleViewed.cmId] = true;
if (!this.lastModuleViewed || data.timeaccess > this.lastModuleViewed.timeaccess) { if (!this.lastModuleViewed || lastModuleViewed.timeaccess > this.lastModuleViewed.timeaccess) {
this.lastModuleViewed = data; this.lastModuleViewed = lastModuleViewed;
if (this.selectedSection && this.selectedSection.id !== this.allSectionsId) { if (this.selectedSection && this.selectedSection.id !== this.allSectionsId) {
// Change section to display the one with the last viewed module // Change section to display the one with the last viewed module
const lastViewedSection = this.getViewedModuleSection(this.sections, data); const lastViewedSection = this.getViewedModuleSection();
if (lastViewedSection && lastViewedSection.id !== this.selectedSection?.id) { if (lastViewedSection && lastViewedSection.id !== this.selectedSection?.id) {
this.sectionChanged(lastViewedSection, data.cmId); this.sectionChanged(lastViewedSection, this.lastModuleViewed.cmId);
} }
} }
} }
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
}); });
this.initializeExpandedSections();
} }
/** /**
@ -227,8 +225,11 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
} }
if (changes.sections && this.sections) { if (changes.sections && this.sections) {
this.treatSections(this.sections); await this.initializeExpandedSections();
await this.treatSections(this.sections);
} }
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
} }
@ -247,7 +248,7 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
* Get the components classes. * Get the components classes.
*/ */
protected async getComponents(): Promise<void> { protected async getComponents(): Promise<void> {
if (!this.course || this.course.format == this.lastCourseFormat) { if (!this.course || this.course.format === this.lastCourseFormat) {
return; return;
} }
@ -362,23 +363,21 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
// No section specified, not found or not visible, load current section or the section with last module viewed. // No section specified, not found or not visible, load current section or the section with last module viewed.
const currentSectionData = await CoreCourseFormatDelegate.getCurrentSection(this.course, sections); const currentSectionData = await CoreCourseFormatDelegate.getCurrentSection(this.course, sections);
const lastModuleViewed = this.lastModuleViewed;
let section = currentSectionData.section; let section = currentSectionData.section;
let moduleId: number | undefined; let moduleId: number | undefined;
// If all sections is not preferred, load the last viewed module section. // If all sections is not preferred, load the last viewed module section.
if (!allSectionsPreferred && lastModuleViewed) { if (!allSectionsPreferred && this.lastModuleViewed) {
if (!currentSectionData.forceSelected) { if (!currentSectionData.forceSelected) {
// Search the section with the last module viewed. // Search the section with the last module viewed.
const lastModuleSection = this.getViewedModuleSection(sections, lastModuleViewed); const lastModuleSection = this.getViewedModuleSection();
section = lastModuleSection || section; section = lastModuleSection || section;
moduleId = lastModuleSection ? lastModuleViewed?.cmId : undefined; moduleId = lastModuleSection ? this.lastModuleViewed.cmId : undefined;
} else { } else {
const modules = CoreCourseHelper.getSectionsModules([currentSectionData.section]); const modules = CoreCourseHelper.getSectionsModules([currentSectionData.section]);
if (modules.some(module => module.id === lastModuleViewed.cmId)) { if (modules.some(module => module.id === this.lastModuleViewed?.cmId)) {
// Last module viewed is inside the highlighted section. // Last module viewed is inside the highlighted section.
moduleId = lastModuleViewed.cmId; moduleId = this.lastModuleViewed.cmId;
} }
} }
} }
@ -404,32 +403,29 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
this.viewedModules[entry.cmId] = true; this.viewedModules[entry.cmId] = true;
}); });
if (this.lastModuleViewed) { const lastViewedSection = this.getViewedModuleSection();
const section = this.getViewedModuleSection(this.sections, this.lastModuleViewed); if (lastViewedSection) {
if (section) { this.setSectionExpanded(lastViewedSection);
this.setSectionExpanded(section);
}
} }
} }
/** /**
* Get the section of a viewed module. If the module is in a subsection, returns the root section. * Get the section of a viewed module. If the module is in a subsection, returns the root section.
* *
* @param sections List of sections.
* @param viewedModule Viewed module.
* @returns Section, undefined if not found. * @returns Section, undefined if not found.
*/ */
protected getViewedModuleSection( protected getViewedModuleSection(): CoreCourseSection | undefined {
sections: CoreCourseSection[], if (!this.lastModuleViewed) {
viewedModule: CoreCourseViewedModulesDBRecord, return;
): CoreCourseSection | undefined { }
const { section, parents } = CoreCourseHelper.findSection(sections, {
id: viewedModule.sectionId, const { section, parents } = CoreCourseHelper.findSection(this.sections, {
moduleId: viewedModule.cmId, id: this.lastModuleViewed.sectionId,
moduleId: this.lastModuleViewed.cmId,
}); });
const lastModuleSection: CoreCourseSection | undefined = parents[0] ?? section; const lastModuleSection: CoreCourseSection | undefined = parents[0] ?? section;
return lastModuleSection && lastModuleSection.id !== this.stealthModulesSectionId ? lastModuleSection : undefined; return lastModuleSection?.id !== this.stealthModulesSectionId ? lastModuleSection : undefined;
} }
/** /**
@ -789,8 +785,10 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
this.currentSite?.getLocalSiteConfig<string>(`${COURSE_EXPANDED_SECTIONS_PREFIX}${this.course.id}`), this.currentSite?.getLocalSiteConfig<string>(`${COURSE_EXPANDED_SECTIONS_PREFIX}${this.course.id}`),
); );
// Expand all sections if not defined.
if (expandedSections === undefined) { if (expandedSections === undefined) {
this.accordionMultipleValue = [];
// Expand all sections if not defined.
CoreCourseHelper.flattenSections(this.sections).forEach((section) => { CoreCourseHelper.flattenSections(this.sections).forEach((section) => {
section.expanded = true; section.expanded = true;
this.accordionMultipleValue.push(section.id.toString()); this.accordionMultipleValue.push(section.id.toString());