Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions LearningHub.Nhs.WebUI/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace LearningHub.Nhs.WebUI.Controllers
using LearningHub.Nhs.Models.Content;
using LearningHub.Nhs.Models.Enums.Content;
using LearningHub.Nhs.Models.Extensions;
using LearningHub.Nhs.Models.Moodle.API;
using LearningHub.Nhs.WebUI.Configuration;
using LearningHub.Nhs.WebUI.Filters;
using LearningHub.Nhs.WebUI.Helpers;
Expand Down Expand Up @@ -218,13 +219,12 @@ public async Task<IActionResult> Index(string myLearningDashboard = "my-in-progr
var cataloguesTask = this.dashboardService.GetCataloguesAsync(catalogueDashboard, 1);
var userGroupsTask = this.userGroupService.UserHasCatalogueContributionPermission();

var enrolledCoursesTask = Task.FromResult(new List<MoodleCourseResponseViewModel>());
var enableMoodle = Task.Run(() => this.featureManager.IsEnabledAsync(FeatureFlags.EnableMoodle)).Result;
this.ViewBag.EnableMoodle = enableMoodle;
this.ViewBag.ValidMoodleUser = this.CurrentMoodleUserId > 0;
var enrolledCoursesTask = Task.FromResult(new List<MoodleCourseResponseModel>());
(bool enableMoodle, int currentMoodleUserId) = await this.GetMoodleFeatureStateAsync();

if (enableMoodle && myLearningDashboard == "my-enrolled-courses")
{
enrolledCoursesTask = this.dashboardService.GetEnrolledCoursesFromMoodleAsync(this.CurrentMoodleUserId, 1);
enrolledCoursesTask = this.dashboardService.GetEnrolledCoursesFromMoodleAsync(currentMoodleUserId, 1);
}

await Task.WhenAll(learningTask, resourcesTask, cataloguesTask, userGroupsTask);
Expand Down Expand Up @@ -280,9 +280,7 @@ public async Task<IActionResult> LoadPage(string dashBoardTray = "my-learning",
Catalogues = new Nhs.Models.Dashboard.DashboardCatalogueResponseViewModel { Type = catalogueDashBoard },
};

var enableMoodle = Task.Run(() => this.featureManager.IsEnabledAsync(FeatureFlags.EnableMoodle)).Result;
this.ViewBag.EnableMoodle = enableMoodle;
this.ViewBag.ValidMoodleUser = this.CurrentMoodleUserId > 0;
(bool enableMoodle, int currentMoodleUserId) = await this.GetMoodleFeatureStateAsync();

bool isAjax = this.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest";

Expand Down Expand Up @@ -449,5 +447,28 @@ private async Task<LandingPageViewModel> GetLandingPageContent(bool preview = fa
return new LandingPageViewModel { PageSectionDetailViewModels = new List<PageSectionDetailViewModel>(), PageViewModel = new PageViewModel { PageSections = new List<PageSectionViewModel> { } } };
}
}

/// <summary>
/// Asynchronously retrieves the state of the Moodle feature and the current Moodle user ID.
/// </summary>
/// <remarks>The method checks if the Moodle feature is enabled and retrieves the current Moodle
/// user ID. If the user ID is not already set, it attempts to obtain it asynchronously from the dashboard
/// service.</remarks>
/// <returns>A tuple containing a boolean indicating whether the Moodle feature is enabled and an integer representing
/// the current Moodle user ID.</returns>
private async Task<(bool enableMoodle, int currentMoodleUserId)> GetMoodleFeatureStateAsync()
{
var enableMoodle = Task.Run(() => this.featureManager.IsEnabledAsync(FeatureFlags.EnableMoodle)).Result;
this.ViewBag.EnableMoodle = enableMoodle;
int currentMoodleUserId = this.CurrentMoodleUserId;

if (currentMoodleUserId == 0)
{
currentMoodleUserId = await this.dashboardService.GetMoodleUserIdAsync(this.CurrentUserId);
}

this.ViewBag.ValidMoodleUser = currentMoodleUserId > 0;
return (enableMoodle, currentMoodleUserId);
}
}
}
54 changes: 26 additions & 28 deletions LearningHub.Nhs.WebUI/Helpers/UtilityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public static class UtilityHelper
/// <summary>
/// Findwise Moodle resource type dictionary.
/// </summary>
public static readonly Dictionary<string, ResourceTypeEnumMoodle> FindwiseResourceMoodleTypeDict = new Dictionary<string, ResourceTypeEnumMoodle>()
public static readonly Dictionary<string, ResourceTypeEnum> FindwiseResourceMoodleTypeDict = new Dictionary<string, ResourceTypeEnum>()
{
{ "video", ResourceTypeEnumMoodle.Video },
{ "article", ResourceTypeEnumMoodle.Article },
{ "case", ResourceTypeEnumMoodle.Case },
{ "weblink", ResourceTypeEnumMoodle.WebLink },
{ "audio", ResourceTypeEnumMoodle.Audio },
{ "scorm", ResourceTypeEnumMoodle.Scorm },
{ "assessment", ResourceTypeEnumMoodle.Assessment },
{ "genericfile", ResourceTypeEnumMoodle.GenericFile },
{ "image", ResourceTypeEnumMoodle.Image },
{ "html", ResourceTypeEnumMoodle.Html },
{ "moodle", ResourceTypeEnumMoodle.Course },
{ "video", ResourceTypeEnum.Video },
{ "article", ResourceTypeEnum.Article },
{ "case", ResourceTypeEnum.Case },
{ "weblink", ResourceTypeEnum.WebLink },
{ "audio", ResourceTypeEnum.Audio },
{ "scorm", ResourceTypeEnum.Scorm },
{ "assessment", ResourceTypeEnum.Assessment },
{ "genericfile", ResourceTypeEnum.GenericFile },
{ "image", ResourceTypeEnum.Image },
{ "html", ResourceTypeEnum.Html },
{ "moodle", ResourceTypeEnum.Moodle },
};

/// <summary>
Expand Down Expand Up @@ -173,41 +173,39 @@ public static string GetPrettifiedResourceTypeName(ResourceTypeEnum resourceType
/// <param name="resourceType">The resource type.</param>
/// <param name="durationInMilliseconds">The media duration in milliseconds.</param>
/// <returns>The resource type name, and duration if applicable.</returns>
public static string GetPrettifiedResourceTypeNameMoodle(ResourceTypeEnumMoodle resourceType, int? durationInMilliseconds = 0)
public static string GetPrettifiedResourceTypeNameMoodle(ResourceTypeEnum resourceType, int? durationInMilliseconds = 0)
{
switch (resourceType)
{
case ResourceTypeEnumMoodle.Assessment:
case ResourceTypeEnum.Assessment:
return "Assessment";
case ResourceTypeEnumMoodle.Article:
case ResourceTypeEnum.Article:
return "Article";
case ResourceTypeEnumMoodle.Audio:
case ResourceTypeEnum.Audio:
string durationText = GetDurationText(durationInMilliseconds ?? 0);
durationText = string.IsNullOrEmpty(durationText) ? string.Empty : " - " + durationText;
return "Audio" + durationText;
case ResourceTypeEnumMoodle.Equipment:
case ResourceTypeEnum.Equipment:
return "Equipment";
case ResourceTypeEnumMoodle.Image:
case ResourceTypeEnum.Image:
return "Image";
case ResourceTypeEnumMoodle.Scorm:
case ResourceTypeEnum.Scorm:
return "elearning";
case ResourceTypeEnumMoodle.Video:
case ResourceTypeEnum.Video:
durationText = GetDurationText(durationInMilliseconds ?? 0);
durationText = string.IsNullOrEmpty(durationText) ? string.Empty : " - " + durationText;
return "Video" + durationText;
case ResourceTypeEnumMoodle.WebLink:
case ResourceTypeEnum.WebLink:
return "Web link";
case ResourceTypeEnumMoodle.GenericFile:
case ResourceTypeEnum.GenericFile:
return "File";
case ResourceTypeEnumMoodle.Embedded:
case ResourceTypeEnum.Embedded:
return "Embedded";
case ResourceTypeEnumMoodle.Case:
case ResourceTypeEnum.Case:
return "Case";
case ResourceTypeEnumMoodle.Html:
case ResourceTypeEnum.Html:
return "HTML";
case ResourceTypeEnumMoodle.Moodle:
return "Course";
case ResourceTypeEnumMoodle.Course:
case ResourceTypeEnum.Moodle:
return "Course";
default:
return "File";
Expand Down
10 changes: 9 additions & 1 deletion LearningHub.Nhs.WebUI/Interfaces/IDashboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using LearningHub.Nhs.Models.Dashboard;
using LearningHub.Nhs.Models.Moodle.API;
using LearningHub.Nhs.WebUI.Models;

/// <summary>
Expand Down Expand Up @@ -47,6 +48,13 @@ public interface IDashboardService
/// <param name="currentUserId">The current User Id type.</param>
/// <param name="pageNumber">The page Number.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<List<MoodleCourseResponseViewModel>> GetEnrolledCoursesFromMoodleAsync(int currentUserId, int pageNumber);
Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesFromMoodleAsync(int currentUserId, int pageNumber);

/// <summary>
/// GetEnrolledCoursesFromMoodleAsync.
/// </summary>
/// <param name="currentUserId">The current User Id type.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<int> GetMoodleUserIdAsync(int currentUserId);
}
}
19 changes: 13 additions & 6 deletions LearningHub.Nhs.WebUI/Interfaces/IMoodleApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,36 @@
{
using System.Collections.Generic;
using System.Threading.Tasks;
using LearningHub.Nhs.Models.Dashboard;
using LearningHub.Nhs.WebUI.Models;
using LearningHub.Nhs.Models.Moodle.API;
using MoodleCourseCompletionModel = LearningHub.Nhs.Models.Moodle.API.MoodleCourseCompletionModel;

/// <summary>
/// IMoodleApiService.
/// </summary>
public interface IMoodleApiService
{
/// <summary>
/// GetMoodleUserIdByUsernameAsync.
/// </summary>
/// <param name="currentUserId">The current LH User Id.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<int> GetMoodleUserIdByUsernameAsync(int currentUserId);

/// <summary>
/// GetEnrolledCoursesAsync.
/// </summary>
/// <param name="currentUserId">Moodle user id.</param>
/// <param name="pageNumber">pageNumber.</param>
/// <returns> List of MoodleCourseResponseViewModel.</returns>
Task<List<MoodleCourseResponseViewModel>> GetEnrolledCoursesAsync(int currentUserId, int pageNumber);
/// <returns> List of MoodleCourseResponseModel.</returns>
Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesAsync(int currentUserId, int pageNumber);

/// <summary>
/// GetEnrolledCoursesAsync.
/// </summary>
/// <param name="userId">Moodle user id.</param>
/// <param name="courseId">Moodle course id.</param>
/// <param name="pageNumber">pageNumber.</param>
/// <returns> List of MoodleCourseResponseViewModel.</returns>
Task<MoodleCourseCompletionViewModel> GetCourseCompletionAsync(int userId, int courseId, int pageNumber);
/// <returns> List of MoodleCourseResponseModel.</returns>
Task<MoodleCourseCompletionModel> GetCourseCompletionAsync(int userId, int courseId, int pageNumber);
}
}
3 changes: 2 additions & 1 deletion LearningHub.Nhs.WebUI/Models/DashboardViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using System.Collections.Generic;
using LearningHub.Nhs.Models.Dashboard;
using LearningHub.Nhs.Models.Moodle.API;

/// <summary>
/// Defines the <see cref="DashboardViewModel" />.
Expand Down Expand Up @@ -33,6 +34,6 @@ public DashboardViewModel()
/// <summary>
/// Gets or sets a list of enrolled courses to be displayed in the dashboard.
/// </summary>
public List<MoodleCourseResponseViewModel> EnrolledCourses { get; set; }
public List<MoodleCourseResponseModel> EnrolledCourses { get; set; }
}
}
19 changes: 16 additions & 3 deletions LearningHub.Nhs.WebUI/Services/DashboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using LearningHub.Nhs.Models.Dashboard;
using LearningHub.Nhs.Models.Entities.Analytics;
using LearningHub.Nhs.Models.Entities.Reporting;
using LearningHub.Nhs.Models.Moodle.API;
using LearningHub.Nhs.Services.Interface;
using LearningHub.Nhs.WebUI.Interfaces;
using LearningHub.Nhs.WebUI.Models;
Expand Down Expand Up @@ -128,14 +129,26 @@ public async Task<DashboardResourceResponseViewModel> GetResourcesAsync(string d
/// <param name="currentUserId">The dashboard type.</param>
/// <param name="pageNumber">The page Number.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
public async Task<List<MoodleCourseResponseViewModel>> GetEnrolledCoursesFromMoodleAsync(int currentUserId, int pageNumber)
public async Task<List<MoodleCourseResponseModel>> GetEnrolledCoursesFromMoodleAsync(int currentUserId, int pageNumber)
{
List<MoodleCourseResponseViewModel> viewmodel = new List<MoodleCourseResponseViewModel> { };
MoodleApiService moodleApiService = new MoodleApiService(this.moodleHttpClient);
List<MoodleCourseResponseModel> viewmodel = new List<MoodleCourseResponseModel> { };
MoodleApiService moodleApiService = new MoodleApiService(this.moodleHttpClient, this.OpenApiHttpClient);
viewmodel = await moodleApiService.GetEnrolledCoursesAsync(currentUserId, pageNumber);
return viewmodel;
}

/// <summary>
/// GetEnrolledCoursesFromMoodleAsync.
/// </summary>
/// <param name="currentUserId">The current User Id type.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
public async Task<int> GetMoodleUserIdAsync(int currentUserId)
{
MoodleApiService moodleApiService = new MoodleApiService(this.moodleHttpClient, this.OpenApiHttpClient);
var moodleUserId = await moodleApiService.GetMoodleUserIdByUsernameAsync(currentUserId);
return moodleUserId;
}

/// <summary>
/// Logs Dashboared viewed event.
/// </summary>
Expand Down
Loading
Loading