Skip to content

Commit 5a1ce68

Browse files
committed
TD-5163 Reorders competency groups if needed
1 parent 6e7653b commit 5a1ce68

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

DigitalLearningSolutions.Web/Services/ImportCompetenciesFromFileService.cs

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private void PreProcessCompetencyRow(CompetencyTableRow competencyRow, List<int>
6161
{
6262
int originalIndex = existingIds.IndexOf(id);
6363
int newIndex = newIds.IndexOf(id);
64-
if(originalIndex == newIndex)
64+
if (originalIndex == newIndex)
6565
{
6666
competencyRow.RowStatus = RowStatus.CompetencyUpdated;
6767
}
@@ -101,7 +101,6 @@ internal ImportCompetenciesResult ProcessCompetenciesTable(IXLTable table, int a
101101
int rowCount = 0;
102102
string currentGroup = null;
103103
competenciesRows = competenciesRows
104-
.OrderBy(row => row.CompetencyGroup)
105104
.Select(row =>
106105
{
107106
if (row.CompetencyGroup != currentGroup)
@@ -126,7 +125,32 @@ internal ImportCompetenciesResult ProcessCompetenciesTable(IXLTable table, int a
126125
{
127126
maxFrameworkCompetencyGroupId = ProcessCompetencyRow(adminUserId, frameworkId, maxFrameworkCompetencyId, maxFrameworkCompetencyGroupId, addAssessmentQuestionsOption, reorderCompetenciesOption, customAssessmentQuestionID, defaultQuestionIds, competencyRow);
128127
}
128+
// TO DO: Check for changes to competency group order and apply them if appropriate:
129+
if (reorderCompetenciesOption == 2)
130+
{
131+
var distinctCompetencyGroups = competenciesRows
132+
.Where(row => !string.IsNullOrWhiteSpace(row.CompetencyGroup))
133+
.Select(row => row.CompetencyGroup)
134+
.Distinct()
135+
.ToList();
129136

137+
var existingGroups = frameworkService.GetFrameworkCompetencyGroups(frameworkId).Select(row => new { row.ID, row.Name })
138+
.Distinct()
139+
.ToList();
140+
for (int i = 0; i < competencyGroupCount; i++)
141+
{
142+
var placesToMove = Math.Abs(existingGroups.FindIndex(group => group.Name == distinctCompetencyGroups[i])-i);
143+
if (placesToMove > 0)
144+
{
145+
var thisGroup = existingGroups.FirstOrDefault(group => group.Name == distinctCompetencyGroups[i]);
146+
var direction = existingGroups.FindIndex(group => group.Name == distinctCompetencyGroups[i]) > i ? "UP" : "DOWN";
147+
for (int p = 0; p < placesToMove; p++)
148+
{
149+
frameworkService.MoveFrameworkCompetencyGroup(thisGroup.ID, true, direction);
150+
}
151+
}
152+
}
153+
}
130154
return new ImportCompetenciesResult(competenciesRows);
131155
}
132156
private int ProcessCompetencyRow(
@@ -148,7 +172,7 @@ CompetencyTableRow competencyRow
148172
int newCompetencyId = 0;
149173
int newFrameworkCompetencyId = 0;
150174
//If competency group is set, check if competency group exists within framework and add if not and get the Framework Competency Group ID
151-
int ? frameworkCompetencyGroupId = null;
175+
int? frameworkCompetencyGroupId = null;
152176
if (competencyRow.CompetencyGroup != null)
153177
{
154178
int newCompetencyGroupId = frameworkService.InsertCompetencyGroup(competencyRow.CompetencyGroup, competencyRow.GroupDescription, adminUserId, frameworkId);
@@ -169,10 +193,10 @@ CompetencyTableRow competencyRow
169193
if (frameworkCompetency != null)
170194
{
171195
newCompetencyId = frameworkCompetency.CompetencyID;
172-
if (frameworkCompetency.Name != competencyRow.Competency || frameworkCompetency.Description != competencyRow.CompetencyDescription || frameworkCompetency.AlwaysShowDescription != competencyRow.AlwaysShowDescription )
196+
if (frameworkCompetency.Name != competencyRow.Competency || frameworkCompetency.Description != competencyRow.CompetencyDescription || frameworkCompetency.AlwaysShowDescription != competencyRow.AlwaysShowDescription)
173197
{
174198
frameworkService.UpdateFrameworkCompetency((int)competencyRow.ID, competencyRow.Competency, competencyRow.CompetencyDescription, adminUserId, competencyRow.AlwaysShowDescription ?? false);
175-
competencyRow.RowStatus = (competencyRow.RowStatus == RowStatus.CompetencyGroupInserted ? RowStatus.CompetencyGroupAndCompetencyUpdated: RowStatus.CompetencyUpdated);
199+
competencyRow.RowStatus = (competencyRow.RowStatus == RowStatus.CompetencyGroupInserted ? RowStatus.CompetencyGroupAndCompetencyUpdated : RowStatus.CompetencyUpdated);
176200
}
177201
else
178202
{
@@ -204,7 +228,8 @@ CompetencyTableRow competencyRow
204228
{
205229
var flags = competencyRow.FlagsCsv.Split(',');
206230
var flagIds = new List<int>();
207-
foreach (var flag in flags) {
231+
foreach (var flag in flags)
232+
{
208233
int flagId = 0;
209234
var frameworkFlags = frameworkService.GetCompetencyFlagsByFrameworkId(frameworkId, null, null);
210235
if (frameworkFlags.Any())
@@ -224,7 +249,8 @@ CompetencyTableRow competencyRow
224249
}
225250
flagIds.Add(flagId);
226251
}
227-
if (flagIds.Count > 0) {
252+
if (flagIds.Count > 0)
253+
{
228254
var updated = frameworkService.UpdateCompetencyFlags(frameworkId, newCompetencyId, [.. flagIds]);
229255
if (updated > 0 && competencyRow.RowStatus == RowStatus.Skipped)
230256
{
@@ -233,23 +259,6 @@ CompetencyTableRow competencyRow
233259
}
234260
}
235261

236-
237-
// Add assessment questions if necessary:
238-
if (defaultQuestionIds.Count > 0 | customAssessmentQuestionID > 0)
239-
{
240-
if (competencyRow.RowStatus == RowStatus.CompetencyInserted | competencyRow.RowStatus == RowStatus.CompetencyGroupAndCompetencyInserted || addAssessmentQuestionsOption == 2 && competencyRow.RowStatus == RowStatus.CompetencyUpdated | competencyRow.RowStatus == RowStatus.CompetencyGroupAndCompetencyUpdated || addAssessmentQuestionsOption == 3)
241-
{
242-
foreach(var id in defaultQuestionIds)
243-
{
244-
frameworkService.AddCompetencyAssessmentQuestion(competencyRow.ID ?? newFrameworkCompetencyId, id, adminUserId);
245-
}
246-
if(customAssessmentQuestionID > 0)
247-
{
248-
frameworkService.AddCompetencyAssessmentQuestion(competencyRow.ID ?? newFrameworkCompetencyId, customAssessmentQuestionID, adminUserId);
249-
}
250-
}
251-
}
252-
253262
// Reorder competencies if required:
254263
if (reorderCompetenciesOption == 2)
255264
{
@@ -273,6 +282,24 @@ CompetencyTableRow competencyRow
273282
}
274283
}
275284

285+
// Add assessment questions if necessary:
286+
if (defaultQuestionIds.Count > 0 | customAssessmentQuestionID > 0)
287+
{
288+
if (competencyRow.RowStatus == RowStatus.CompetencyInserted | competencyRow.RowStatus == RowStatus.CompetencyGroupAndCompetencyInserted || addAssessmentQuestionsOption == 2 && competencyRow.RowStatus == RowStatus.CompetencyUpdated | competencyRow.RowStatus == RowStatus.CompetencyGroupAndCompetencyUpdated || addAssessmentQuestionsOption == 3)
289+
{
290+
foreach (var id in defaultQuestionIds)
291+
{
292+
frameworkService.AddCompetencyAssessmentQuestion(competencyRow.ID ?? newFrameworkCompetencyId, id, adminUserId);
293+
}
294+
if (customAssessmentQuestionID > 0)
295+
{
296+
frameworkService.AddCompetencyAssessmentQuestion(competencyRow.ID ?? newFrameworkCompetencyId, customAssessmentQuestionID, adminUserId);
297+
}
298+
}
299+
}
300+
301+
302+
276303
return maxFrameworkCompetencyGroupId;
277304
}
278305

0 commit comments

Comments
 (0)