diff --git a/DigitalLearningSolutions.Web/Controllers/TrackingSystem/CourseSetup/CourseSetupController.cs b/DigitalLearningSolutions.Web/Controllers/TrackingSystem/CourseSetup/CourseSetupController.cs index 775e52ecdb..5e0bd5afa7 100644 --- a/DigitalLearningSolutions.Web/Controllers/TrackingSystem/CourseSetup/CourseSetupController.cs +++ b/DigitalLearningSolutions.Web/Controllers/TrackingSystem/CourseSetup/CourseSetupController.cs @@ -651,11 +651,23 @@ private IActionResult SaveSectionAndRedirect(SetSectionContentViewModel model) { data.SectionContentData = new List(); } - if (data.EditCourseContent) + var sectionsToRemove = new List(); + foreach (var section in data.SectionContentData) { - return RedirectToNextSectionOrSummary( - model.Index, - new SetCourseContentViewModel(data.CourseContentData!)); + section.Tutorials = section.Tutorials + .Where(t => !model.Tutorials.Any(newT => newT.TutorialId == t.TutorialId)) + .ToList(); + if (!section.Tutorials.Any()) + { + sectionsToRemove.Add(section); + } + } + if (sectionsToRemove.Count > 0) + { + foreach (var section in sectionsToRemove) + { + data.SectionContentData.Remove(section); + } } data!.SectionContentData!.Add( new SectionContentTempData( @@ -732,18 +744,19 @@ private static IEnumerable UpdateC private IEnumerable GetTutorialsFromSectionContentData(List sectionContentData, List sectionTutorial) { if (sectionContentData == null || sectionTutorial == null) return new List(); - var updatedRecords = sectionContentData - .SelectMany(data => data.Tutorials) - .Join(sectionTutorial, - tempData => new { tempData.TutorialId, tempData.TutorialName }, // Match on both TutorialId and TutorialName - index => new { index.TutorialId, index.TutorialName }, - (tempData, index) => new Tutorial + var updatedRecords = sectionTutorial + .GroupJoin( + sectionContentData.SelectMany(data => data.Tutorials), + tutorial => new { tutorial.TutorialId, tutorial.TutorialName }, + tempData => new { tempData.TutorialId, tempData.TutorialName }, + (tutorial, matchingTempData) => new Tutorial { - TutorialId = index.TutorialId, - TutorialName = index.TutorialName, - Status = tempData.LearningEnabled, // Updated from sectionContentData - DiagStatus = tempData.DiagnosticEnabled // Updated from sectionContentData - }) + TutorialId = tutorial.TutorialId, + TutorialName = tutorial.TutorialName, + Status = matchingTempData.Any() ? matchingTempData.First().LearningEnabled : tutorial.Status, + DiagStatus = matchingTempData.Any() ? matchingTempData.First().DiagnosticEnabled : tutorial.DiagStatus + } + ) .ToList(); return updatedRecords;