@@ -365,7 +365,8 @@ public IActionResult SetCourseOptions(EditCourseOptionsFormData model)
365365            data ! . CourseOptionsData  =  model . ToCourseOptionsTempData ( ) ; 
366366            multiPageFormService . SetMultiPageFormData ( data ,  MultiPageFormDataFeature . AddNewCourse ,  TempData ) ; 
367367
368-             return  RedirectToAction ( "SetCourseContent" ,  false ) ; 
368+             bool  editCourseContent  =  data ? . EditCourseContent  ??  false ; 
369+             return  RedirectToAction ( "SetCourseContent" ,  editCourseContent ) ; 
369370        } 
370371
371372        [ HttpGet ( "AddCourse/SetCourseContent" ) ] 
@@ -381,7 +382,7 @@ public IActionResult SetCourseContent(bool editCourseContent)
381382            { 
382383                return  RedirectToAction ( "Summary" ) ; 
383384            } 
384-             data . EditCourseContent  =  editCourseContent ; 
385+             data . EditCourseContent  =  data . EditCourseContent   ||   editCourseContent ; 
385386            multiPageFormService . SetMultiPageFormData ( data ,  MultiPageFormDataFeature . AddNewCourse ,  TempData ) ; 
386387
387388            var  model  =  data ! . CourseContentData  !=  null 
@@ -651,11 +652,23 @@ private IActionResult SaveSectionAndRedirect(SetSectionContentViewModel model)
651652            { 
652653                data . SectionContentData  =  new  List < SectionContentTempData > ( ) ; 
653654            } 
654-             if  ( data . EditCourseContent ) 
655+             var  sectionsToRemove  =  new  List < SectionContentTempData > ( ) ; 
656+             foreach  ( var  section  in  data . SectionContentData ) 
655657            { 
656-                 return  RedirectToNextSectionOrSummary ( 
657-                model . Index , 
658-                new  SetCourseContentViewModel ( data . CourseContentData ! ) ) ; 
658+                 section . Tutorials  =  section . Tutorials 
659+                     . Where ( t =>  ! model . Tutorials . Any ( newT =>  newT . TutorialId  ==  t . TutorialId ) ) 
660+                     . ToList ( ) ; 
661+                 if  ( ! section . Tutorials . Any ( ) ) 
662+                 { 
663+                     sectionsToRemove . Add ( section ) ; 
664+                 } 
665+             } 
666+             if  ( sectionsToRemove . Count  >  0 ) 
667+             {  
668+             foreach  ( var  section  in  sectionsToRemove ) 
669+             { 
670+                 data . SectionContentData . Remove ( section ) ; 
671+             } 
659672            } 
660673            data ! . SectionContentData ! . Add ( 
661674            new  SectionContentTempData ( 
@@ -732,18 +745,19 @@ private static IEnumerable<CourseStatisticsWithAdminFieldResponseCounts> UpdateC
732745        private  IEnumerable < Tutorial >  GetTutorialsFromSectionContentData ( List < SectionContentTempData >  sectionContentData ,  List < Tutorial >  sectionTutorial ) 
733746        { 
734747            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 
748+             var  updatedRecords  =  sectionTutorial 
749+         . GroupJoin ( 
750+              sectionContentData . SelectMany ( data  =>   data . Tutorials ) , 
751+             tutorial  =>  new  {  tutorial . TutorialId ,  tutorial . TutorialName  } , 
752+             tempData  =>  new  {  tempData . TutorialId ,  tempData . TutorialName  } , 
753+             ( tutorial ,   matchingTempData )  =>  new  Tutorial 
741754            { 
742-                 TutorialId  =  index . TutorialId , 
743-                 TutorialName  =  index . TutorialName , 
744-                 Status  =  tempData . LearningEnabled ,   // Updated from sectionContentData 
745-                 DiagStatus  =  tempData . DiagnosticEnabled  // Updated from sectionContentData 
746-             } ) 
755+                 TutorialId  =  tutorial . TutorialId , 
756+                 TutorialName  =  tutorial . TutorialName , 
757+                 Status  =  matchingTempData . Any ( )  ?  matchingTempData . First ( ) . LearningEnabled  :  tutorial . Status , 
758+                 DiagStatus  =  matchingTempData . Any ( )  ?  matchingTempData . First ( ) . DiagnosticEnabled  :  tutorial . DiagStatus 
759+             } 
760+         ) 
747761        . ToList ( ) ; 
748762
749763            return  updatedRecords ; 
0 commit comments