@@ -185,27 +185,25 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
185185 }
186186 } ) ;
187187
188- this . modViewedObserver = CoreEvents . on ( CoreEvents . COURSE_MODULE_VIEWED , ( data ) => {
189- if ( data . courseId !== this . course . id ) {
188+ this . modViewedObserver = CoreEvents . on ( CoreEvents . COURSE_MODULE_VIEWED , ( lastModuleViewed ) => {
189+ if ( lastModuleViewed . courseId !== this . course . id ) {
190190 return ;
191191 }
192192
193- this . viewedModules [ data . cmId ] = true ;
194- if ( ! this . lastModuleViewed || data . timeaccess > this . lastModuleViewed . timeaccess ) {
195- this . lastModuleViewed = data ;
193+ this . viewedModules [ lastModuleViewed . cmId ] = true ;
194+ if ( ! this . lastModuleViewed || lastModuleViewed . timeaccess > this . lastModuleViewed . timeaccess ) {
195+ this . lastModuleViewed = lastModuleViewed ;
196196
197197 if ( this . selectedSection && this . selectedSection . id !== this . allSectionsId ) {
198198 // Change section to display the one with the last viewed module
199- const lastViewedSection = this . getViewedModuleSection ( this . sections , data ) ;
199+ const lastViewedSection = this . getViewedModuleSection ( ) ;
200200 if ( lastViewedSection && lastViewedSection . id !== this . selectedSection ?. id ) {
201- this . sectionChanged ( lastViewedSection , data . cmId ) ;
201+ this . sectionChanged ( lastViewedSection , this . lastModuleViewed . cmId ) ;
202202 }
203203 }
204204 }
205205 this . changeDetectorRef . markForCheck ( ) ;
206206 } ) ;
207-
208- this . initializeExpandedSections ( ) ;
209207 }
210208
211209 /**
@@ -227,8 +225,11 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
227225 }
228226
229227 if ( changes . sections && this . sections ) {
230- this . treatSections ( this . sections ) ;
228+ await this . initializeExpandedSections ( ) ;
229+
230+ await this . treatSections ( this . sections ) ;
231231 }
232+
232233 this . changeDetectorRef . markForCheck ( ) ;
233234 }
234235
@@ -247,7 +248,7 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
247248 * Get the components classes.
248249 */
249250 protected async getComponents ( ) : Promise < void > {
250- if ( ! this . course || this . course . format == this . lastCourseFormat ) {
251+ if ( ! this . course || this . course . format === this . lastCourseFormat ) {
251252 return ;
252253 }
253254
@@ -362,23 +363,21 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
362363 // No section specified, not found or not visible, load current section or the section with last module viewed.
363364 const currentSectionData = await CoreCourseFormatDelegate . getCurrentSection ( this . course , sections ) ;
364365
365- const lastModuleViewed = this . lastModuleViewed ;
366366 let section = currentSectionData . section ;
367367 let moduleId : number | undefined ;
368368
369369 // If all sections is not preferred, load the last viewed module section.
370- if ( ! allSectionsPreferred && lastModuleViewed ) {
370+ if ( ! allSectionsPreferred && this . lastModuleViewed ) {
371371 if ( ! currentSectionData . forceSelected ) {
372372 // Search the section with the last module viewed.
373- const lastModuleSection = this . getViewedModuleSection ( sections , lastModuleViewed ) ;
374-
373+ const lastModuleSection = this . getViewedModuleSection ( ) ;
375374 section = lastModuleSection || section ;
376- moduleId = lastModuleSection ? lastModuleViewed ? .cmId : undefined ;
375+ moduleId = lastModuleSection ? this . lastModuleViewed . cmId : undefined ;
377376 } else {
378377 const modules = CoreCourseHelper . getSectionsModules ( [ currentSectionData . section ] ) ;
379- if ( modules . some ( module => module . id === lastModuleViewed . cmId ) ) {
378+ if ( modules . some ( module => module . id === this . lastModuleViewed ? .cmId ) ) {
380379 // Last module viewed is inside the highlighted section.
381- moduleId = lastModuleViewed . cmId ;
380+ moduleId = this . lastModuleViewed . cmId ;
382381 }
383382 }
384383 }
@@ -404,32 +403,29 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
404403 this . viewedModules [ entry . cmId ] = true ;
405404 } ) ;
406405
407- if ( this . lastModuleViewed ) {
408- const section = this . getViewedModuleSection ( this . sections , this . lastModuleViewed ) ;
409- if ( section ) {
410- this . setSectionExpanded ( section ) ;
411- }
406+ const lastViewedSection = this . getViewedModuleSection ( ) ;
407+ if ( lastViewedSection ) {
408+ this . setSectionExpanded ( lastViewedSection ) ;
412409 }
413410 }
414411
415412 /**
416413 * Get the section of a viewed module. If the module is in a subsection, returns the root section.
417414 *
418- * @param sections List of sections.
419- * @param viewedModule Viewed module.
420415 * @returns Section, undefined if not found.
421416 */
422- protected getViewedModuleSection (
423- sections : CoreCourseSection [ ] ,
424- viewedModule : CoreCourseViewedModulesDBRecord ,
425- ) : CoreCourseSection | undefined {
426- const { section, parents } = CoreCourseHelper . findSection ( sections , {
427- id : viewedModule . sectionId ,
428- moduleId : viewedModule . cmId ,
417+ protected getViewedModuleSection ( ) : CoreCourseSection | undefined {
418+ if ( ! this . lastModuleViewed ) {
419+ return ;
420+ }
421+
422+ const { section, parents } = CoreCourseHelper . findSection ( this . sections , {
423+ id : this . lastModuleViewed . sectionId ,
424+ moduleId : this . lastModuleViewed . cmId ,
429425 } ) ;
430426 const lastModuleSection : CoreCourseSection | undefined = parents [ 0 ] ?? section ;
431427
432- return lastModuleSection && lastModuleSection . id !== this . stealthModulesSectionId ? lastModuleSection : undefined ;
428+ return lastModuleSection ? .id !== this . stealthModulesSectionId ? lastModuleSection : undefined ;
433429 }
434430
435431 /**
@@ -789,8 +785,10 @@ export class CoreCourseFormatComponent implements OnInit, OnChanges, OnDestroy {
789785 this . currentSite ?. getLocalSiteConfig < string > ( `${ COURSE_EXPANDED_SECTIONS_PREFIX } ${ this . course . id } ` ) ,
790786 ) ;
791787
792- // Expand all sections if not defined.
793788 if ( expandedSections === undefined ) {
789+ this . accordionMultipleValue = [ ] ;
790+
791+ // Expand all sections if not defined.
794792 CoreCourseHelper . flattenSections ( this . sections ) . forEach ( ( section ) => {
795793 section . expanded = true ;
796794 this . accordionMultipleValue . push ( section . id . toString ( ) ) ;
0 commit comments