|
18 | 18 | using LearningHub.Nhs.Models.Common; |
19 | 19 | using LearningHub.Nhs.Models.Common.Enums; |
20 | 20 | using LearningHub.Nhs.Models.Entities.Hierarchy; |
| 21 | + using LearningHub.Nhs.Models.Moodle; |
21 | 22 | using LearningHub.Nhs.Models.MyLearning; |
22 | 23 | using LearningHub.Nhs.Models.Paging; |
23 | 24 | using LearningHub.Nhs.Models.Resource; |
@@ -283,8 +284,14 @@ public async Task<IActionResult> MoodleCategory(int id) |
283 | 284 | return this.RedirectToAction("Error"); |
284 | 285 | } |
285 | 286 |
|
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"); |
288 | 295 | this.ViewData["CatalogueName"] = vm.Name; |
289 | 296 | this.ViewData["id"] = id; |
290 | 297 |
|
@@ -733,8 +740,11 @@ public async Task<IActionResult> AddCategoryToCatalogue(CatalogueViewModel catal |
733 | 740 | var vr = await this.catalogueService.AddCategoryToCatalogue(vm); |
734 | 741 | if (vr.Success) |
735 | 742 | { |
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"); |
738 | 748 | return this.View("MoodleCategory", vm); |
739 | 749 | } |
740 | 750 | else |
@@ -1044,5 +1054,34 @@ private void ValidateCatalogueOwnerVm(CatalogueOwnerViewModel vm) |
1044 | 1054 | this.ModelState.AddModelError("Notes", "The notes are required."); |
1045 | 1055 | } |
1046 | 1056 | } |
| 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 | + |
1047 | 1086 | } |
1048 | 1087 | } |
0 commit comments