@@ -20,7 +20,6 @@ public interface IImportCompetenciesFromFileService
2020 byte [ ] GetCompetencyFileForFramework ( int frameworkId , bool isBlank , string vocabulary ) ;
2121 public ImportCompetenciesResult PreProcessCompetenciesTable ( IXLWorkbook workbook , string vocabulary , int frameworkId ) ;
2222 public ImportCompetenciesResult ProcessCompetenciesFromFile ( IXLWorkbook workbook , int adminUserId , int frameworkId , string vocabulary , int reorderCompetenciesOption , int addAssessmentQuestionsOption , int customAssessmentQuestionID , List < int > defaultQuestionIds ) ;
23- public ImportCompetenciesResult ProcessCompetenciesFromFile ( IXLWorkbook workbook , int adminUserId , int frameworkId , string vocabulary , int reorderCompetenciesOption , int addAssessmentQuestionsOption , int customAssessmentQuestionID , List < int > defaultQuestionIds ) ;
2423 }
2524 public class ImportCompetenciesFromFileService : IImportCompetenciesFromFileService
2625 {
@@ -72,12 +71,11 @@ private void PreProcessCompetencyRow(CompetencyTableRow competencyRow, List<int>
7271 competencyRow . Validate ( ) ;
7372 }
7473 public ImportCompetenciesResult ProcessCompetenciesFromFile ( IXLWorkbook workbook , int adminUserId , int frameworkId , string vocabulary , int reorderCompetenciesOption , int addAssessmentQuestionsOption , int customAssessmentQuestionID , List < int > defaultQuestionIds )
75- public ImportCompetenciesResult ProcessCompetenciesFromFile ( IXLWorkbook workbook , int adminUserId , int frameworkId , string vocabulary , int reorderCompetenciesOption , int addAssessmentQuestionsOption , int customAssessmentQuestionID , List < int > defaultQuestionIds )
7674 {
7775 int maxFrameworkCompetencyId = frameworkService . GetMaxFrameworkCompetencyID ( ) ;
7876 int maxFrameworkCompetencyGroupId = frameworkService . GetMaxFrameworkCompetencyGroupID ( ) ;
7977 var table = OpenCompetenciesTable ( workbook , vocabulary ) ;
80- return ProcessCompetenciesTable ( table , adminUserId , frameworkId , maxFrameworkCompetencyId , maxFrameworkCompetencyGroupId , addAssessmentQuestionsOption , customAssessmentQuestionID , defaultQuestionIds ) ;
78+ return ProcessCompetenciesTable ( table , adminUserId , frameworkId , maxFrameworkCompetencyId , maxFrameworkCompetencyGroupId , addAssessmentQuestionsOption , reorderCompetenciesOption , customAssessmentQuestionID , defaultQuestionIds ) ;
8179 }
8280 internal IXLTable OpenCompetenciesTable ( IXLWorkbook workbook , string vocabulary )
8381 {
@@ -94,7 +92,7 @@ internal IXLTable OpenCompetenciesTable(IXLWorkbook workbook, string vocabulary)
9492 }
9593 return table ;
9694 }
97- internal ImportCompetenciesResult ProcessCompetenciesTable ( IXLTable table , int adminUserId , int frameworkId , int maxFrameworkCompetencyId , int maxFrameworkCompetencyGroupId , int addAssessmentQuestionsOption , int customAssessmentQuestionID , List < int > defaultQuestionIds )
95+ internal ImportCompetenciesResult ProcessCompetenciesTable ( IXLTable table , int adminUserId , int frameworkId , int maxFrameworkCompetencyId , int maxFrameworkCompetencyGroupId , int addAssessmentQuestionsOption , int reorderCompetenciesOption , int customAssessmentQuestionID , List < int > defaultQuestionIds )
9896 {
9997 var competenciesRows = table . Rows ( ) . Skip ( 1 ) . Select ( row => new CompetencyTableRow ( table , row ) ) . ToList ( ) ;
10098 int rowCount = 0 ;
@@ -122,7 +120,7 @@ internal ImportCompetenciesResult ProcessCompetenciesTable(IXLTable table, int a
122120 . Count ( ) ;
123121 foreach ( var competencyRow in competenciesRows )
124122 {
125- maxFrameworkCompetencyGroupId = ProcessCompetencyRow ( adminUserId , frameworkId , maxFrameworkCompetencyId , maxFrameworkCompetencyGroupId , addAssessmentQuestionsOption , customAssessmentQuestionID , defaultQuestionIds , competencyRow ) ;
123+ maxFrameworkCompetencyGroupId = ProcessCompetencyRow ( adminUserId , frameworkId , maxFrameworkCompetencyId , maxFrameworkCompetencyGroupId , addAssessmentQuestionsOption , reorderCompetenciesOption , customAssessmentQuestionID , defaultQuestionIds , competencyRow ) ;
126124 }
127125 // Check for changes to competency group order and apply them if appropriate:
128126 if ( reorderCompetenciesOption == 2 )
@@ -161,6 +159,7 @@ private int ProcessCompetencyRow(
161159 int maxFrameworkCompetencyId ,
162160 int maxFrameworkCompetencyGroupId ,
163161 int addAssessmentQuestionsOption ,
162+ int reorderCompetenciesOption ,
164163 int customAssessmentQuestionID ,
165164 List < int > defaultQuestionIds ,
166165 CompetencyTableRow competencyRow
@@ -170,10 +169,10 @@ CompetencyTableRow competencyRow
170169 {
171170 return maxFrameworkCompetencyGroupId ;
172171 }
173- int newCompetencyGroupId = 0 ;
174172 int newCompetencyId = 0 ;
173+ int newFrameworkCompetencyId = 0 ;
175174 //If competency group is set, check if competency group exists within framework and add if not and get the Framework Competency Group ID
176- int ? frameworkCompetencyGroupId = null ;
175+ int ? frameworkCompetencyGroupId = null ;
177176 if ( competencyRow . CompetencyGroup != null )
178177 {
179178 newCompetencyGroupId = frameworkService . InsertCompetencyGroup ( competencyRow . CompetencyGroup , competencyRow . GroupDescription , adminUserId ) ;
@@ -194,10 +193,10 @@ CompetencyTableRow competencyRow
194193 if ( frameworkCompetency != null )
195194 {
196195 newCompetencyId = frameworkCompetency . CompetencyID ;
197- 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 )
198197 {
199198 frameworkService . UpdateFrameworkCompetency ( ( int ) competencyRow . ID , competencyRow . Competency , competencyRow . CompetencyDescription , adminUserId , competencyRow . AlwaysShowDescription ?? false ) ;
200- competencyRow . RowStatus = ( competencyRow . RowStatus == RowStatus . CompetencyGroupInserted ? RowStatus . CompetencyGroupAndCompetencyUpdated : RowStatus . CompetencyUpdated ) ;
199+ competencyRow . RowStatus = ( competencyRow . RowStatus == RowStatus . CompetencyGroupInserted ? RowStatus . CompetencyGroupAndCompetencyUpdated : RowStatus . CompetencyUpdated ) ;
201200 }
202201 else
203202 {
@@ -211,7 +210,7 @@ CompetencyTableRow competencyRow
211210 newCompetencyId = frameworkService . InsertCompetency ( competencyRow . Competency , competencyRow . CompetencyDescription , adminUserId ) ;
212211 if ( newCompetencyId > 0 )
213212 {
214- var newFrameworkCompetencyId = frameworkService . InsertFrameworkCompetency ( newCompetencyId , frameworkCompetencyGroupId , adminUserId , frameworkId , competencyRow . AlwaysShowDescription ?? false ) ; //including always show desc flag
213+ newFrameworkCompetencyId = frameworkService . InsertFrameworkCompetency ( newCompetencyId , frameworkCompetencyGroupId , adminUserId , frameworkId , competencyRow . AlwaysShowDescription ?? false ) ; //including always show desc flag
215214 if ( newFrameworkCompetencyId > maxFrameworkCompetencyId )
216215 {
217216 competencyRow . RowStatus = ( competencyRow . RowStatus == RowStatus . CompetencyGroupInserted ? RowStatus . CompetencyGroupAndCompetencyInserted : RowStatus . CompetencyInserted ) ;
@@ -225,11 +224,12 @@ CompetencyTableRow competencyRow
225224
226225
227226 // If flags are supplied, add them:
228- if ( competencyRow . FlagsCsv != null )
227+ if ( ! string . IsNullOrWhiteSpace ( competencyRow . FlagsCsv . Trim ( ) ) )
229228 {
230229 var flags = competencyRow . FlagsCsv . Split ( ',' ) ;
231- int [ ] flagIds = [ ] ;
232- foreach ( var flag in flags ) {
230+ var flagIds = new List < int > ( ) ;
231+ foreach ( var flag in flags )
232+ {
233233 int flagId = 0 ;
234234 var frameworkFlags = frameworkService . GetCompetencyFlagsByFrameworkId ( frameworkId , null , null ) ;
235235 if ( frameworkFlags . Any ( ) )
@@ -239,37 +239,64 @@ CompetencyTableRow competencyRow
239239 if ( frameworkFlag . FlagName == flag )
240240 {
241241 flagId = frameworkFlag . FlagId ;
242+ break ;
242243 }
243244 }
244245 }
245246 if ( flagId == 0 )
246247 {
247248 flagId = frameworkService . AddCustomFlagToFramework ( frameworkId , flag , "Flag" , "nhsuk-tag--white" ) ;
248249 }
249- flagIds . Append ( flagId ) ;
250+ flagIds . Add ( flagId ) ;
250251 }
251- if ( flagIds . Any ( ) ) {
252- frameworkService . UpdateCompetencyFlags ( frameworkId , newCompetencyId , flagIds ) ;
252+ if ( flagIds . Count > 0 )
253+ {
254+ var updated = frameworkService . UpdateCompetencyFlags ( frameworkId , newCompetencyId , [ .. flagIds ] ) ;
255+ if ( updated > 0 && competencyRow . RowStatus == RowStatus . Skipped )
256+ {
257+ competencyRow . RowStatus = RowStatus . CompetencyUpdated ;
258+ }
253259 }
254260 }
255261
262+ // Reorder competencies if required:
263+ if ( reorderCompetenciesOption == 2 )
264+ {
265+ var frameworkCompetencyId = ( int ) competencyRow . ID ;
266+ var frameworkCompetency = frameworkService . GetFrameworkCompetencyById ( frameworkCompetencyId ) ;
267+ var placesToMove = Math . Abs ( frameworkCompetency . Ordering - competencyRow . CompetencyOrderNumber ) ;
268+
269+ if ( placesToMove > 0 )
270+ {
271+ var direction = frameworkCompetency . Ordering > competencyRow . CompetencyOrderNumber ? "UP" : "DOWN" ;
272+
273+ for ( int i = 0 ; i < placesToMove ; i ++ )
274+ {
275+ frameworkService . MoveFrameworkCompetency ( frameworkCompetencyId , true , direction ) ;
276+ }
277+
278+ competencyRow . Reordered = true ;
279+ }
280+ }
256281
257282 // Add assessment questions if necessary:
258283 if ( defaultQuestionIds . Count > 0 | customAssessmentQuestionID > 0 )
259284 {
260285 if ( competencyRow . RowStatus == RowStatus . CompetencyInserted | competencyRow . RowStatus == RowStatus . CompetencyGroupAndCompetencyInserted || addAssessmentQuestionsOption == 2 && competencyRow . RowStatus == RowStatus . CompetencyUpdated | competencyRow . RowStatus == RowStatus . CompetencyGroupAndCompetencyUpdated || addAssessmentQuestionsOption == 3 )
261286 {
262- foreach ( var id in defaultQuestionIds )
287+ foreach ( var id in defaultQuestionIds )
263288 {
264- frameworkService . AddCompetencyAssessmentQuestion ( ( int ) competencyRow . ID , id , adminUserId ) ;
289+ frameworkService . AddCompetencyAssessmentQuestion ( competencyRow . ID ?? newFrameworkCompetencyId , id , adminUserId ) ;
265290 }
266- if ( customAssessmentQuestionID > 0 )
291+ if ( customAssessmentQuestionID > 0 )
267292 {
268- frameworkService . AddCompetencyAssessmentQuestion ( ( int ) competencyRow . ID , customAssessmentQuestionID , adminUserId ) ;
293+ frameworkService . AddCompetencyAssessmentQuestion ( competencyRow . ID ?? newFrameworkCompetencyId , customAssessmentQuestionID , adminUserId ) ;
269294 }
270295 }
271296 }
272297
298+
299+
273300 return maxFrameworkCompetencyGroupId ;
274301 }
275302
0 commit comments