Skip to content

Commit 6e7653b

Browse files
committed
TD-5163 Commits competency order changes if required
1 parent 8a9c022 commit 6e7653b

File tree

7 files changed

+68
-19
lines changed

7 files changed

+68
-19
lines changed

DigitalLearningSolutions.Data/DataServices/FrameworkDataService.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ int adminId
206206
);
207207

208208
void UpdateFrameworkCompetency(int frameworkCompetencyId, string name, string? description, int adminId, bool? alwaysShowDescription);
209-
void UpdateCompetencyFlags(int frameworkId, int competencyId, int[] selectedFlagIds);
209+
int UpdateCompetencyFlags(int frameworkId, int competencyId, int[] selectedFlagIds);
210210

211211
void MoveFrameworkCompetencyGroup(int frameworkCompetencyGroupId, bool singleStep, string direction);
212212

@@ -1025,7 +1025,7 @@ int adminId
10251025
);
10261026
if (usedElsewhere > 0)
10271027
{
1028-
var newCompetencyGroupId = InsertCompetencyGroup(name, description, adminId);
1028+
var newCompetencyGroupId = InsertCompetencyGroup(name, description, adminId, null);
10291029
if (newCompetencyGroupId > 0)
10301030
{
10311031
var numberOfAffectedRows = connection.Execute(
@@ -1086,13 +1086,14 @@ public void UpdateFrameworkCompetency(int frameworkCompetencyId, string name, st
10861086
}
10871087
}
10881088

1089-
public void UpdateCompetencyFlags(int frameworkId, int competencyId, int[] selectedFlagIds)
1089+
public int UpdateCompetencyFlags(int frameworkId, int competencyId, int[] selectedFlagIds)
10901090
{
1091+
int totalRowsAffected = 0;
10911092
string? commaSeparatedSelectedFlagIds = null;
10921093
if (selectedFlagIds?.Length > 0)
10931094
{
10941095
commaSeparatedSelectedFlagIds = String.Join(',', selectedFlagIds);
1095-
connection.Execute(
1096+
totalRowsAffected += connection.Execute(
10961097
@$"INSERT INTO CompetencyFlags(CompetencyID, FlagID, Selected)
10971098
SELECT @competencyId, f.ID, 1
10981099
FROM Flags f
@@ -1102,11 +1103,12 @@ SELECT FlagID FROM CompetencyFlags
11021103
)",
11031104
new { competencyId, selectedFlagIds });
11041105
}
1105-
connection.Execute(
1106+
totalRowsAffected += connection.Execute(
11061107
@$"UPDATE CompetencyFlags
11071108
SET Selected = (CASE WHEN FlagID IN ({commaSeparatedSelectedFlagIds ?? "null"}) THEN 1 ELSE 0 END)
1108-
WHERE CompetencyID = @competencyId",
1109+
WHERE CompetencyID = @competencyId AND Selected <> (CASE WHEN FlagID IN ({commaSeparatedSelectedFlagIds ?? "null"}) THEN 1 ELSE 0 END)",
11091110
new { competencyId, frameworkId });
1111+
return totalRowsAffected;
11101112
}
11111113

11121114
public int AddCustomFlagToFramework(int frameworkId, string flagName, string flagGroup, string flagTagClass)
@@ -1127,7 +1129,7 @@ public void UpdateFrameworkCustomFlag(int frameworkId, int id, string flagName,
11271129
new { frameworkId, id, flagName, flagGroup, flagTagClass });
11281130
}
11291131

1130-
public void MoveFrameworkCompetencyGroup(int frameworkCompetencyGroupId, bool singleStep, string direction)
1132+
public void MoveFrameworkCompetencyGroup(int frameworkCompetencyGroupId, bool singleStep, string direction) // Valid directions are 'UP' and 'DOWN'
11311133
{
11321134
connection.Execute(
11331135
"ReorderFrameworkCompetencyGroup",
@@ -1136,7 +1138,7 @@ public void MoveFrameworkCompetencyGroup(int frameworkCompetencyGroupId, bool si
11361138
);
11371139
}
11381140

1139-
public void MoveFrameworkCompetency(int frameworkCompetencyId, bool singleStep, string direction)
1141+
public void MoveFrameworkCompetency(int frameworkCompetencyId, bool singleStep, string direction) // Valid directions are 'UP' and 'DOWN'
11401142
{
11411143
connection.Execute(
11421144
"ReorderFrameworkCompetency",

DigitalLearningSolutions.Data/Models/Frameworks/Import/CompetencyTableRow.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public CompetencyTableRow(IXLTable table, IXLRangeRow row)
3838
RowStatus = RowStatus.NotYetProcessed;
3939
}
4040
public int RowNumber { get; set; }
41+
public int CompetencyOrderNumber { get; set; }
4142
public string? AlwaysShowDescriptionRaw { get; set; }
4243
public ImportCompetenciesResult.ErrorReason? Error { get; set; }
4344
public RowStatus RowStatus { get; set; }

DigitalLearningSolutions.Web/Services/FrameworkService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ int adminId
200200
);
201201

202202
void UpdateFrameworkCompetency(int frameworkCompetencyId, string name, string? description, int adminId, bool? alwaysShowDescription = false);
203-
void UpdateCompetencyFlags(int frameworkId, int competencyId, int[] selectedFlagIds);
203+
int UpdateCompetencyFlags(int frameworkId, int competencyId, int[] selectedFlagIds);
204204

205205
void MoveFrameworkCompetencyGroup(int frameworkCompetencyGroupId, bool singleStep, string direction);
206206

@@ -662,9 +662,9 @@ public void UpdateAssessmentQuestion(int id, string question, int assessmentQues
662662
frameworkDataService.UpdateAssessmentQuestion(id, question, assessmentQuestionInputTypeId, maxValueDescription, minValueDescription, scoringInstructions, minValue, maxValue, includeComments, adminId, commentsPrompt, commentsHint);
663663
}
664664

665-
public void UpdateCompetencyFlags(int frameworkId, int competencyId, int[] selectedFlagIds)
665+
public int UpdateCompetencyFlags(int frameworkId, int competencyId, int[] selectedFlagIds)
666666
{
667-
frameworkDataService.UpdateCompetencyFlags(frameworkId, competencyId, selectedFlagIds);
667+
return frameworkDataService.UpdateCompetencyFlags(frameworkId, competencyId, selectedFlagIds);
668668
}
669669

670670
public BrandedFramework? UpdateFrameworkBranding(int frameworkId, int brandId, int categoryId, int topicId, int adminId)

DigitalLearningSolutions.Web/Services/ImportCompetenciesFromFileService.cs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public ImportCompetenciesResult ProcessCompetenciesFromFile(IXLWorkbook workbook
7878
int maxFrameworkCompetencyId = frameworkService.GetMaxFrameworkCompetencyID();
7979
int maxFrameworkCompetencyGroupId = frameworkService.GetMaxFrameworkCompetencyGroupID();
8080
var table = OpenCompetenciesTable(workbook, vocabulary);
81-
return ProcessCompetenciesTable(table, adminUserId, frameworkId, maxFrameworkCompetencyId, maxFrameworkCompetencyGroupId, addAssessmentQuestionsOption, customAssessmentQuestionID, defaultQuestionIds);
81+
return ProcessCompetenciesTable(table, adminUserId, frameworkId, maxFrameworkCompetencyId, maxFrameworkCompetencyGroupId, addAssessmentQuestionsOption, reorderCompetenciesOption, customAssessmentQuestionID, defaultQuestionIds);
8282
}
8383
internal IXLTable OpenCompetenciesTable(IXLWorkbook workbook, string vocabulary)
8484
{
@@ -95,18 +95,36 @@ internal IXLTable OpenCompetenciesTable(IXLWorkbook workbook, string vocabulary)
9595
}
9696
return table;
9797
}
98-
internal ImportCompetenciesResult ProcessCompetenciesTable(IXLTable table, int adminUserId, int frameworkId, int maxFrameworkCompetencyId, int maxFrameworkCompetencyGroupId, int addAssessmentQuestionsOption, int customAssessmentQuestionID, List<int> defaultQuestionIds)
98+
internal ImportCompetenciesResult ProcessCompetenciesTable(IXLTable table, int adminUserId, int frameworkId, int maxFrameworkCompetencyId, int maxFrameworkCompetencyGroupId, int addAssessmentQuestionsOption, int reorderCompetenciesOption, int customAssessmentQuestionID, List<int> defaultQuestionIds)
9999
{
100100
var competenciesRows = table.Rows().Skip(1).Select(row => new CompetencyTableRow(table, row)).ToList();
101-
101+
int rowCount = 0;
102+
string currentGroup = null;
103+
competenciesRows = competenciesRows
104+
.OrderBy(row => row.CompetencyGroup)
105+
.Select(row =>
106+
{
107+
if (row.CompetencyGroup != currentGroup)
108+
{
109+
currentGroup = row.CompetencyGroup;
110+
rowCount = 1;
111+
}
112+
else
113+
{
114+
rowCount++;
115+
}
116+
row.CompetencyOrderNumber = rowCount;
117+
return row;
118+
})
119+
.ToList();
102120
var competencyGroupCount = competenciesRows
103121
.Where(row => !string.IsNullOrWhiteSpace(row.CompetencyGroup))
104122
.Select(row => row.CompetencyGroup)
105123
.Distinct()
106124
.Count();
107125
foreach (var competencyRow in competenciesRows)
108126
{
109-
maxFrameworkCompetencyGroupId = ProcessCompetencyRow(adminUserId, frameworkId, maxFrameworkCompetencyId, maxFrameworkCompetencyGroupId, addAssessmentQuestionsOption, customAssessmentQuestionID, defaultQuestionIds, competencyRow);
127+
maxFrameworkCompetencyGroupId = ProcessCompetencyRow(adminUserId, frameworkId, maxFrameworkCompetencyId, maxFrameworkCompetencyGroupId, addAssessmentQuestionsOption, reorderCompetenciesOption, customAssessmentQuestionID, defaultQuestionIds, competencyRow);
110128
}
111129

112130
return new ImportCompetenciesResult(competenciesRows);
@@ -117,6 +135,7 @@ private int ProcessCompetencyRow(
117135
int maxFrameworkCompetencyId,
118136
int maxFrameworkCompetencyGroupId,
119137
int addAssessmentQuestionsOption,
138+
int reorderCompetenciesOption,
120139
int customAssessmentQuestionID,
121140
List<int> defaultQuestionIds,
122141
CompetencyTableRow competencyRow
@@ -206,8 +225,8 @@ CompetencyTableRow competencyRow
206225
flagIds.Add(flagId);
207226
}
208227
if (flagIds.Count > 0) {
209-
frameworkService.UpdateCompetencyFlags(frameworkId, newCompetencyId, [.. flagIds]);
210-
if (competencyRow.RowStatus == RowStatus.Skipped)
228+
var updated = frameworkService.UpdateCompetencyFlags(frameworkId, newCompetencyId, [.. flagIds]);
229+
if (updated > 0 && competencyRow.RowStatus == RowStatus.Skipped)
211230
{
212231
competencyRow.RowStatus = RowStatus.CompetencyUpdated;
213232
}
@@ -231,6 +250,29 @@ CompetencyTableRow competencyRow
231250
}
232251
}
233252

253+
// Reorder competencies if required:
254+
if (reorderCompetenciesOption == 2)
255+
{
256+
var frameworkCompetencyId = (int)competencyRow.ID;
257+
var frameworkCompetency = frameworkService.GetFrameworkCompetencyById(frameworkCompetencyId);
258+
var placesToMove = Math.Abs(frameworkCompetency.Ordering - competencyRow.CompetencyOrderNumber);
259+
260+
if (placesToMove > 0)
261+
{
262+
var direction = frameworkCompetency.Ordering > competencyRow.CompetencyOrderNumber ? "UP" : "DOWN";
263+
264+
for (int i = 0; i < placesToMove; i++)
265+
{
266+
frameworkService.MoveFrameworkCompetency(frameworkCompetencyId, true, direction);
267+
}
268+
269+
if (competencyRow.RowStatus == RowStatus.Skipped)
270+
{
271+
competencyRow.RowStatus = RowStatus.CompetencyUpdated;
272+
}
273+
}
274+
}
275+
234276
return maxFrameworkCompetencyGroupId;
235277
}
236278

DigitalLearningSolutions.Web/Views/Frameworks/Developer/Import/ApplyCompetencyOrdering.cshtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
Apply @Model.FrameworkVocabularySingular.ToLower() sequence changes?
3737
</h1>
3838
</legend>
39+
<div class="nhsuk-inset-text">
40+
<span class="nhsuk-u-visually-hidden">Information: </span>
41+
<p>We strongly recommend including all framework competencies in your uploaded sheet if reordering competencies to ensure that the correct sequence is applied.</p>
42+
</div>
3943
<div class="nhsuk-hint nhsuk-u-margin-bottom-2">
4044
Your uploaded file includes changes to the sequence of existing @Model.FrameworkVocabularyPlural.ToLower(). Choose whether to store the changes to @Model.FrameworkVocabularySingular.ToLower() sequence during update.
4145
</div>

DigitalLearningSolutions.Web/Views/Frameworks/Developer/Import/ImportCompleted.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
{
4848
<text>
4949
<li>
50-
@Model.CompetenciesToReorderCount existing @Model.FrameworkVocabularySingular.ToLower() @(Model.CompetenciesToReorderCount == 1 ? "record" : "records") have changed sequence in your uploaded sheet. You can choose whether to fix them in the new order next.
50+
Some existing @Model.FrameworkVocabularySingular.ToLower() @(Model.CompetenciesToReorderCount == 1 ? "record" : "records") have changed sequence in your uploaded sheet. You can choose whether to fix them in the new order next.
5151
</li></text>
5252
}
5353
<li>No errors</li>

DigitalLearningSolutions.Web/Views/Frameworks/Developer/Import/UploadResults.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<li>@Model.CompetencyGroupsInsertedCount new @(Model.CompetencyGroupsInsertedCount == 1 ? $"{Model.FrameworkVocabularySingular.ToLower()} group" : $"{Model.FrameworkVocabularySingular.ToLower()} groups") inserted</li>
3838
<li>@Model.CompetenciesInsertedCount new @(Model.CompetenciesInsertedCount == 1 ? Model.FrameworkVocabularySingular.ToLower() : Model.FrameworkVocabularyPlural.ToLower()) inserted</li>
3939
<li>@Model.CompetenciesUpdatedCount existing @(Model.CompetenciesUpdatedCount == 1 ? Model.FrameworkVocabularySingular.ToLower() : Model.FrameworkVocabularyPlural.ToLower()) updated</li>
40-
<li>@Model.SkippedCount rows @(Model.SkippedCount == 1 ? "line" : "lines") skipped (nothing inserted but no errors)</li>
40+
<li>@Model.SkippedCount rows @(Model.SkippedCount == 1 ? "line" : "lines") skipped (nothing inserted or updated but no errors)</li>
4141
<li>@Model.ErrorCount @(Model.ErrorCount == 1 ? "line" : "lines") skipped due to errors</li>
4242
</ul>
4343

0 commit comments

Comments
 (0)