@@ -651,11 +651,23 @@ private IActionResult SaveSectionAndRedirect(SetSectionContentViewModel model)
651651 {
652652 data . SectionContentData = new List < SectionContentTempData > ( ) ;
653653 }
654- if ( data . EditCourseContent )
654+ var sectionsToRemove = new List < SectionContentTempData > ( ) ;
655+ foreach ( var section in data . SectionContentData )
655656 {
656- return RedirectToNextSectionOrSummary (
657- model . Index ,
658- new SetCourseContentViewModel ( data . CourseContentData ! ) ) ;
657+ section . Tutorials = section . Tutorials
658+ . Where ( t => ! model . Tutorials . Any ( newT => newT . TutorialId == t . TutorialId ) )
659+ . ToList ( ) ;
660+ if ( ! section . Tutorials . Any ( ) )
661+ {
662+ sectionsToRemove . Add ( section ) ;
663+ }
664+ }
665+ if ( sectionsToRemove . Count > 0 )
666+ {
667+ foreach ( var section in sectionsToRemove )
668+ {
669+ data . SectionContentData . Remove ( section ) ;
670+ }
659671 }
660672 data ! . SectionContentData ! . Add (
661673 new SectionContentTempData (
@@ -732,18 +744,19 @@ private static IEnumerable<CourseStatisticsWithAdminFieldResponseCounts> UpdateC
732744 private IEnumerable < Tutorial > GetTutorialsFromSectionContentData ( List < SectionContentTempData > sectionContentData , List < Tutorial > sectionTutorial )
733745 {
734746 if ( sectionContentData == null || sectionTutorial == null ) return new List < Tutorial > ( ) ;
735- var updatedRecords = sectionContentData
736- . SelectMany ( data => data . Tutorials )
737- . Join ( sectionTutorial ,
738- tempData => new { tempData . TutorialId , tempData . TutorialName } , // Match on both TutorialId and TutorialName
739- index => new { index . TutorialId , index . TutorialName } ,
740- ( tempData , index ) => new Tutorial
747+ var updatedRecords = sectionTutorial
748+ . GroupJoin (
749+ sectionContentData . SelectMany ( data => data . Tutorials ) ,
750+ tutorial => new { tutorial . TutorialId , tutorial . TutorialName } ,
751+ tempData => new { tempData . TutorialId , tempData . TutorialName } ,
752+ ( tutorial , matchingTempData ) => new Tutorial
741753 {
742- TutorialId = index . TutorialId ,
743- TutorialName = index . TutorialName ,
744- Status = tempData . LearningEnabled , // Updated from sectionContentData
745- DiagStatus = tempData . DiagnosticEnabled // Updated from sectionContentData
746- } )
754+ TutorialId = tutorial . TutorialId ,
755+ TutorialName = tutorial . TutorialName ,
756+ Status = matchingTempData . Any ( ) ? matchingTempData . First ( ) . LearningEnabled : tutorial . Status ,
757+ DiagStatus = matchingTempData . Any ( ) ? matchingTempData . First ( ) . DiagnosticEnabled : tutorial . DiagStatus
758+ }
759+ )
747760 . ToList ( ) ;
748761
749762 return updatedRecords ;
0 commit comments