Skip to content

Commit e2b8258

Browse files
Implemented character indentation in the dropdown list.
1 parent 72a79a9 commit e2b8258

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

AdminUI/LearningHub.Nhs.AdminUI/Controllers/CatalogueController.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using LearningHub.Nhs.Models.Common;
1919
using LearningHub.Nhs.Models.Common.Enums;
2020
using LearningHub.Nhs.Models.Entities.Hierarchy;
21+
using LearningHub.Nhs.Models.Moodle;
2122
using LearningHub.Nhs.Models.MyLearning;
2223
using LearningHub.Nhs.Models.Paging;
2324
using LearningHub.Nhs.Models.Resource;
@@ -283,8 +284,14 @@ public async Task<IActionResult> MoodleCategory(int id)
283284
return this.RedirectToAction("Error");
284285
}
285286

286-
vm.MoodleCategories = await this.moodleApiService.GetAllMoodleCategoriesAsync();
287-
vm.MoodleCategorySelectList = new SelectList(vm.MoodleCategories, "Id", "Name");
287+
var categories = await this.moodleApiService.GetAllMoodleCategoriesAsync();
288+
289+
vm.MoodleCategories = categories;
290+
291+
// Build hierarchical select list
292+
var selectList = BuildList(categories, parentId: null, depth: 0);
293+
294+
vm.MoodleCategorySelectList = new SelectList(selectList, "Value", "Text");
288295
this.ViewData["CatalogueName"] = vm.Name;
289296
this.ViewData["id"] = id;
290297

@@ -733,8 +740,11 @@ public async Task<IActionResult> AddCategoryToCatalogue(CatalogueViewModel catal
733740
var vr = await this.catalogueService.AddCategoryToCatalogue(vm);
734741
if (vr.Success)
735742
{
736-
vm.MoodleCategories = await this.moodleApiService.GetAllMoodleCategoriesAsync();
737-
vm.MoodleCategorySelectList = new SelectList(vm.MoodleCategories, "Id", "Name");
743+
var categories = await this.moodleApiService.GetAllMoodleCategoriesAsync();
744+
vm.MoodleCategories = categories;
745+
// Build hierarchical select list
746+
var selectList = BuildList(categories, parentId: null, depth: 0);
747+
vm.MoodleCategorySelectList = new SelectList(selectList, "Value", "Text");
738748
return this.View("MoodleCategory", vm);
739749
}
740750
else
@@ -1044,5 +1054,34 @@ private void ValidateCatalogueOwnerVm(CatalogueOwnerViewModel vm)
10441054
this.ModelState.AddModelError("Notes", "The notes are required.");
10451055
}
10461056
}
1057+
1058+
private List<SelectListItem> BuildList(IEnumerable<MoodleCategory> allCategories, int? parentId, int depth)
1059+
{
1060+
var selectList = new List<SelectListItem>();
1061+
1062+
// Handle both null and 0 as top-level depending on Moodle data
1063+
var children = allCategories
1064+
.Where(c => c.Parent == parentId || (parentId == null && (c.Parent == 0 || c.Parent == 0)))
1065+
.OrderBy(c => c.Name)
1066+
.ToList();
1067+
1068+
foreach (var child in children)
1069+
{
1070+
// Indent with non-breaking spaces so browser keeps them
1071+
string indent = new string('\u00A0', depth * 3);
1072+
1073+
selectList.Add(new SelectListItem
1074+
{
1075+
Value = child.Id.ToString(),
1076+
Text = $"{indent}{child.Name}"
1077+
});
1078+
1079+
// Recursively add nested children
1080+
selectList.AddRange(BuildList(allCategories, child.Id, depth + 1));
1081+
}
1082+
1083+
return selectList;
1084+
}
1085+
10471086
}
10481087
}

0 commit comments

Comments
 (0)