diff --git a/DigitalLearningSolutions.Data/DataServices/FrameworkDataService.cs b/DigitalLearningSolutions.Data/DataServices/FrameworkDataService.cs index d9b4b25a88..b3176c1bec 100644 --- a/DigitalLearningSolutions.Data/DataServices/FrameworkDataService.cs +++ b/DigitalLearningSolutions.Data/DataServices/FrameworkDataService.cs @@ -266,6 +266,7 @@ bool deleteFromExisting void DeleteCompetencyAssessmentQuestion(int frameworkCompetencyId, int assessmentQuestionId, int adminId); void DeleteCompetencyLearningResource(int competencyLearningResourceId, int adminId); + void UpdateFrameworkCompetencyFrameworkCompetencyGroup(int? competencyGroupId, int frameworkCompetencyGroupId, int adminId); } public class FrameworkDataService : IFrameworkDataService @@ -2460,5 +2461,22 @@ public int GetFrameworkCompetencyGroupId(int frameworkId, int competencyGroupId) new { frameworkId, competencyGroupId } ).Single(); } + + public void UpdateFrameworkCompetencyFrameworkCompetencyGroup(int? competencyGroupId, int frameworkCompetencyGroupId, int adminId) + { + var numberOfAffectedRows = connection.Execute( + @"UPDATE FrameworkCompetencies + SET FrameworkCompetencyGroupId = @frameworkCompetencyGroupId, UpdatedByAdminID = @adminId + WHERE ID = @competencyGroupId AND FrameworkCompetencyGroupId <> @frameworkCompetencyGroupId", + new { frameworkCompetencyGroupId, competencyGroupId, adminId } + ); + if (numberOfAffectedRows < 1) + { + logger.LogWarning( + "Not updating framework competencies framework competency group id as db update failed. " + + $"frameworkCompetencyGroupId: {frameworkCompetencyGroupId}, competencyGroupId: {competencyGroupId}." + ); + } + } } } diff --git a/DigitalLearningSolutions.Web/Services/FrameworkService.cs b/DigitalLearningSolutions.Web/Services/FrameworkService.cs index 2293df9c18..b407be417f 100644 --- a/DigitalLearningSolutions.Web/Services/FrameworkService.cs +++ b/DigitalLearningSolutions.Web/Services/FrameworkService.cs @@ -261,6 +261,7 @@ bool deleteFromExisting void DeleteCompetencyAssessmentQuestion(int frameworkCompetencyId, int assessmentQuestionId, int adminId); void DeleteCompetencyLearningResource(int competencyLearningResourceId, int adminId); + void UpdateFrameworkCompetencyFrameworkCompetencyGroup(int? competencyGroupId, int frameworkCompetencyGroupId, int adminId); } public class FrameworkService : IFrameworkService { @@ -729,5 +730,10 @@ public int GetFrameworkCompetencyGroupId(int frameworkId, int competencyGroupId) { return frameworkDataService.GetFrameworkCompetencyGroupId(frameworkId, competencyGroupId); } + + public void UpdateFrameworkCompetencyFrameworkCompetencyGroup(int? competencyGroupId, int frameworkCompetencyGroupId, int adminId) + { + frameworkDataService.UpdateFrameworkCompetencyFrameworkCompetencyGroup(competencyGroupId, frameworkCompetencyGroupId, adminId); + } } } diff --git a/DigitalLearningSolutions.Web/Services/ImportCompetenciesFromFileService.cs b/DigitalLearningSolutions.Web/Services/ImportCompetenciesFromFileService.cs index d7aa831d6c..b3bf4b9c48 100644 --- a/DigitalLearningSolutions.Web/Services/ImportCompetenciesFromFileService.cs +++ b/DigitalLearningSolutions.Web/Services/ImportCompetenciesFromFileService.cs @@ -36,14 +36,21 @@ public ImportCompetenciesResult PreProcessCompetenciesTable(IXLWorkbook workbook var competencyRows = table.Rows().Skip(1).Select(row => new CompetencyTableRow(table, row)).ToList(); var newCompetencyIds = competencyRows.Select(row => row.ID ?? 0).ToList(); var existingIds = frameworkService.GetFrameworkCompetencyOrder(frameworkId, newCompetencyIds); + var existingGroups = frameworkService + .GetFrameworkCompetencyGroups(frameworkId) + .Select(row => row.Name) + .Distinct() + .ToList(); + var newGroups = competencyRows.Select(row => row.CompetencyGroup ?? "").ToList(); foreach (var competencyRow in competencyRows) { - PreProcessCompetencyRow(competencyRow, newCompetencyIds, existingIds); + PreProcessCompetencyRow(competencyRow, newCompetencyIds, existingIds, existingGroups, newGroups); } return new ImportCompetenciesResult(competencyRows); } - private void PreProcessCompetencyRow(CompetencyTableRow competencyRow, List newIds, List existingIds) + private void PreProcessCompetencyRow(CompetencyTableRow competencyRow, List newIds, List existingIds, List existingGroups, List newGroups) { + if (competencyRow.ID == null) { competencyRow.RowStatus = RowStatus.CompetencyInserted; @@ -64,6 +71,16 @@ private void PreProcessCompetencyRow(CompetencyTableRow competencyRow, List { competencyRow.Reordered = true; } + else + { + var groupName = (string)(competencyRow?.CompetencyGroup); + originalIndex = existingGroups.IndexOf(groupName); + newIndex = newGroups.IndexOf(groupName); + if (originalIndex != newIndex) + { + competencyRow.Reordered = true; + } + } } } competencyRow.Validate(); @@ -124,10 +141,10 @@ internal ImportCompetenciesResult ProcessCompetenciesTable(IXLTable table, int a if (reorderCompetenciesOption == 2) { var distinctCompetencyGroups = competenciesRows - .Where(row => !string.IsNullOrWhiteSpace(row.CompetencyGroup)) - .Select(row => row.CompetencyGroup) - .Distinct() - .ToList(); + .Where(row => !string.IsNullOrWhiteSpace(row.CompetencyGroup)) + .Select(row => row.CompetencyGroup) + .Distinct() + .ToList(); for (int i = 0; i < competencyGroupCount; i++) { var existingGroups = frameworkService.GetFrameworkCompetencyGroups(frameworkId).Select(row => new { row.ID, row.Name }) @@ -177,6 +194,7 @@ CompetencyTableRow competencyRow if (newCompetencyGroupId > 0) { frameworkCompetencyGroupId = frameworkService.InsertFrameworkCompetencyGroup(newCompetencyGroupId, frameworkId, adminId); + frameworkService.UpdateFrameworkCompetencyFrameworkCompetencyGroup(competencyRow.ID, (int)frameworkCompetencyGroupId, adminId); if (frameworkCompetencyGroupId > maxFrameworkCompetencyGroupId) { maxFrameworkCompetencyGroupId = (int)frameworkCompetencyGroupId; @@ -185,6 +203,7 @@ CompetencyTableRow competencyRow else { frameworkCompetencyGroupId = frameworkService.GetFrameworkCompetencyGroupId(frameworkId, newCompetencyGroupId); + var isUpdated = frameworkService.UpdateFrameworkCompetencyGroup((int)frameworkCompetencyGroupId, newCompetencyGroupId, competencyRow.CompetencyGroup, competencyRow.GroupDescription, adminId); competencyRow.RowStatus = RowStatus.CompetencyGroupUpdated; }