@@ -11,6 +11,7 @@ namespace LearningHub.Nhs.WebUI.Controllers
1111 using LearningHub . Nhs . Models . Content ;
1212 using LearningHub . Nhs . Models . Enums . Content ;
1313 using LearningHub . Nhs . Models . Extensions ;
14+ using LearningHub . Nhs . Models . Moodle . API ;
1415 using LearningHub . Nhs . WebUI . Configuration ;
1516 using LearningHub . Nhs . WebUI . Filters ;
1617 using LearningHub . Nhs . WebUI . Helpers ;
@@ -218,13 +219,12 @@ public async Task<IActionResult> Index(string myLearningDashboard = "my-in-progr
218219 var cataloguesTask = this . dashboardService . GetCataloguesAsync ( catalogueDashboard , 1 ) ;
219220 var userGroupsTask = this . userGroupService . UserHasCatalogueContributionPermission ( ) ;
220221
221- var enrolledCoursesTask = Task . FromResult ( new List < MoodleCourseResponseViewModel > ( ) ) ;
222- var enableMoodle = Task . Run ( ( ) => this . featureManager . IsEnabledAsync ( FeatureFlags . EnableMoodle ) ) . Result ;
223- this . ViewBag . EnableMoodle = enableMoodle ;
224- this . ViewBag . ValidMoodleUser = this . CurrentMoodleUserId > 0 ;
222+ var enrolledCoursesTask = Task . FromResult ( new List < MoodleCourseResponseModel > ( ) ) ;
223+ ( bool enableMoodle , int currentMoodleUserId ) = await this . GetMoodleFeatureStateAsync ( ) ;
224+
225225 if ( enableMoodle && myLearningDashboard == "my-enrolled-courses" )
226226 {
227- enrolledCoursesTask = this . dashboardService . GetEnrolledCoursesFromMoodleAsync ( this . CurrentMoodleUserId , 1 ) ;
227+ enrolledCoursesTask = this . dashboardService . GetEnrolledCoursesFromMoodleAsync ( currentMoodleUserId , 1 ) ;
228228 }
229229
230230 await Task . WhenAll ( learningTask , resourcesTask , cataloguesTask , userGroupsTask ) ;
@@ -280,9 +280,7 @@ public async Task<IActionResult> LoadPage(string dashBoardTray = "my-learning",
280280 Catalogues = new Nhs . Models . Dashboard . DashboardCatalogueResponseViewModel { Type = catalogueDashBoard } ,
281281 } ;
282282
283- var enableMoodle = Task . Run ( ( ) => this . featureManager . IsEnabledAsync ( FeatureFlags . EnableMoodle ) ) . Result ;
284- this . ViewBag . EnableMoodle = enableMoodle ;
285- this . ViewBag . ValidMoodleUser = this . CurrentMoodleUserId > 0 ;
283+ ( bool enableMoodle , int currentMoodleUserId ) = await this . GetMoodleFeatureStateAsync ( ) ;
286284
287285 bool isAjax = this . HttpContext . Request . Headers [ "X-Requested-With" ] == "XMLHttpRequest" ;
288286
@@ -449,5 +447,28 @@ private async Task<LandingPageViewModel> GetLandingPageContent(bool preview = fa
449447 return new LandingPageViewModel { PageSectionDetailViewModels = new List < PageSectionDetailViewModel > ( ) , PageViewModel = new PageViewModel { PageSections = new List < PageSectionViewModel > { } } } ;
450448 }
451449 }
450+
451+ /// <summary>
452+ /// Asynchronously retrieves the state of the Moodle feature and the current Moodle user ID.
453+ /// </summary>
454+ /// <remarks>The method checks if the Moodle feature is enabled and retrieves the current Moodle
455+ /// user ID. If the user ID is not already set, it attempts to obtain it asynchronously from the dashboard
456+ /// service.</remarks>
457+ /// <returns>A tuple containing a boolean indicating whether the Moodle feature is enabled and an integer representing
458+ /// the current Moodle user ID.</returns>
459+ private async Task < ( bool enableMoodle , int currentMoodleUserId ) > GetMoodleFeatureStateAsync ( )
460+ {
461+ var enableMoodle = Task . Run ( ( ) => this . featureManager . IsEnabledAsync ( FeatureFlags . EnableMoodle ) ) . Result ;
462+ this . ViewBag . EnableMoodle = enableMoodle ;
463+ int currentMoodleUserId = this . CurrentMoodleUserId ;
464+
465+ if ( currentMoodleUserId == 0 )
466+ {
467+ currentMoodleUserId = await this . dashboardService . GetMoodleUserIdAsync ( this . CurrentUserId ) ;
468+ }
469+
470+ this . ViewBag . ValidMoodleUser = currentMoodleUserId > 0 ;
471+ return ( enableMoodle , currentMoodleUserId ) ;
472+ }
452473 }
453474}
0 commit comments