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
33 changes: 33 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/Configuration/MoodleApiConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace LearningHub.Nhs.AdminUI.Configuration
{
/// <summary>
/// The Moodle Settings.
/// </summary>
public class MoodleApiConfig
{
/// <summary>
/// Gets or sets the base url for the Moodle service.
/// </summary>
public string BaseUrl { get; set; } = null!;

/// <summary>
/// Gets or sets the Web service Rest Format.
/// </summary>
public string MoodleWSRestFormat { get; set; } = null!;

/// <summary>
/// Gets or sets the token.
/// </summary>
public string WSToken { get; set; } = null!;

/// <summary>
/// Gets or sets the token.
/// </summary>
public string ApiPath { get; set; } = "webservice/rest/server.php";

/// <summary>
/// Gets or sets the token.
/// </summary>
public string CoursePath { get; set; } = "course/view.php";
}
}
66 changes: 66 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/Controllers/CatalogueController.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
namespace LearningHub.Nhs.AdminUI.Controllers
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AngleSharp.Io;
using LearningHub.Nhs.AdminUI.Configuration;
using LearningHub.Nhs.AdminUI.Extensions;
using LearningHub.Nhs.AdminUI.Interfaces;
using LearningHub.Nhs.AdminUI.Models;
using LearningHub.Nhs.Models.Catalogue;
using LearningHub.Nhs.Models.Common;
using LearningHub.Nhs.Models.Common.Enums;
using LearningHub.Nhs.Models.Entities.Hierarchy;
using LearningHub.Nhs.Models.MyLearning;
using LearningHub.Nhs.Models.Paging;
using LearningHub.Nhs.Models.Resource;
using LearningHub.Nhs.Models.User;
using LearningHub.Nhs.Models.Validation;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand Down Expand Up @@ -65,6 +71,11 @@ public class CatalogueController : BaseController
/// </summary>
private readonly IProviderService providerService;

/// <summary>
/// Defines the moodleApiService.
/// </summary>
private readonly IMoodleApiService moodleApiService;

/// <summary>
/// Defines the _settings.
/// </summary>
Expand Down Expand Up @@ -98,6 +109,7 @@ public class CatalogueController : BaseController
/// <param name="userGroupService">The userService<see cref="IUserGroupService"/>.</param>
/// <param name="fileService">The fileService<see cref="IFileService"/>.</param>
/// <param name="providerService">The providerService<see cref="IProviderService"/>.</param>
/// <param name="moodleApiService">The moodleApiService<see cref="IMoodleApiService"/>.</param>
/// <param name="logger">The logger<see cref="ILogger{LogController}"/>.</param>
/// <param name="options">The options<see cref="IOptions{WebSettings}"/>.</param>
/// <param name="websettings">The websettings<see cref="IOptions{WebSettings}"/>.</param>
Expand All @@ -107,6 +119,7 @@ public CatalogueController(
IUserGroupService userGroupService,
IFileService fileService,
IProviderService providerService,
IMoodleApiService moodleApiService,
ILogger<LogController> logger,
IOptions<WebSettings> options,
IOptions<WebSettings> websettings)
Expand All @@ -116,6 +129,7 @@ public CatalogueController(
this.userGroupService = userGroupService;
this.fileService = fileService;
this.providerService = providerService;
this.moodleApiService = moodleApiService;
this.logger = logger;
this.websettings = websettings;
this.settings = options.Value;
Expand Down Expand Up @@ -254,6 +268,29 @@ public async Task<IActionResult> CatalogueOwner(int id)
return this.View(vm);
}

/// <summary>
/// The CatalogueOwner.
/// </summary>
/// <param name="id">The id<see cref="int"/>.</param>
/// <returns>The <see cref="Task{IActionResult}"/>.</returns>
[HttpGet]
[Route("MoodleCategory/{id}")]
public async Task<IActionResult> MoodleCategory(int id)
{
var vm = await this.catalogueService.GetCatalogueAsync(id);
if (vm == null)
{
return this.RedirectToAction("Error");
}

vm.MoodleCategories = await this.moodleApiService.GetAllMoodleCategoriesAsync();
vm.MoodleCategorySelectList = new SelectList(vm.MoodleCategories, "Id", "Name");
this.ViewData["CatalogueName"] = vm.Name;
this.ViewData["id"] = id;

return this.View(vm);
}

/// <summary>
/// The UserGroups.
/// </summary>
Expand Down Expand Up @@ -678,6 +715,35 @@ public async Task<IActionResult> AddUserGroupsToCatalogue(int catalogueNodeId, i
}
}

/// <summary>
/// The AddCategoryToCatalogue.
/// </summary>
/// <param name="catalogueViewModel">The catalogueViewModel<see cref="CatalogueViewModel"/>.</param>
/// <returns>The <see cref="Task{IActionResult}"/>.</returns>
[HttpPost]
[Route("AddCategoryToCatalogue")]
public async Task<IActionResult> AddCategoryToCatalogue(CatalogueViewModel catalogueViewModel)
{
////if (catalogueViewModel.SelectedCategoryId == 0)
////{
//// this.ModelState.AddModelError("SelectedCategoryId", "Please select a category.");
////}
var vm = await this.catalogueService.GetCatalogueAsync(catalogueViewModel.CatalogueNodeVersionId);
vm.SelectedCategoryId = catalogueViewModel.SelectedCategoryId;
var vr = await this.catalogueService.AddCategoryToCatalogue(vm);
if (vr.Success)
{
vm.MoodleCategories = await this.moodleApiService.GetAllMoodleCategoriesAsync();
vm.MoodleCategorySelectList = new SelectList(vm.MoodleCategories, "Id", "Name");
return this.View("MoodleCategory", vm);
}
else
{
this.ViewBag.ErrorMessage = $"Category Update failed.";
return this.View("MoodleCategory", vm);
}
}

/// <summary>
/// The CreateCatalogue.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,12 @@ public interface ICatalogueService
/// <param name="catalogueOwner">The catalogue owner<see cref="CatalogueOwnerViewModel"/>.</param>
/// <returns>The <see cref="Task"/>.</returns>
Task<ApiResponse> UpdateCatalogueOwnerAsync(CatalogueOwnerViewModel catalogueOwner);

/// <summary>
/// AddCategoryToCatalogue
/// </summary>
/// <param name="catalogue">The catalogue.</param>
/// <returns></returns>
Task<ApiResponse> AddCategoryToCatalogue(CatalogueViewModel catalogue);
}
}
26 changes: 26 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/Interfaces/IMoodleApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace LearningHub.Nhs.AdminUI.Interfaces
{
using System.Collections.Generic;
using System.Threading.Tasks;
using LearningHub.Nhs.Models.Moodle;
using LearningHub.Nhs.Models.Moodle.API;

/// <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>
/// <returns> List of MoodleCategory.</returns>
Task<List<MoodleCategory>> GetAllMoodleCategoriesAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<PackageReference Include="HtmlSanitizer" Version="6.0.453" />
<PackageReference Include="IdentityModel" Version="4.6.0" />
<PackageReference Include="LearningHub.Nhs.Caching" Version="2.0.2" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.2" />
<PackageReference Include="LearningHub.Nhs.Models" Version="4.0.3" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.19.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.36" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.36" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public enum CatalogueNavPage
/// Defines the Catalogue Owner.
/// </summary>
CatalogueOwner,

/// <summary>
/// Defines the Category.
/// </summary>
Category
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur
services.AddTransient<IUserSessionHelper, UserSessionHelper>();
services.AddTransient<IInternalSystemService, InternalSystemService>();
services.AddScoped<IProviderService, ProviderService>();
services.AddScoped<IMoodleApiService, MoodleApiService>();

// web settings binding
var webSettings = new WebSettings();
Expand Down
16 changes: 16 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/Services/CatalogueService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
namespace LearningHub.Nhs.AdminUI.Services
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using LearningHub.Nhs.AdminUI.Helpers;
using LearningHub.Nhs.AdminUI.Interfaces;
using LearningHub.Nhs.Models.Catalogue;
using LearningHub.Nhs.Models.Common;
using LearningHub.Nhs.Models.Enums;
using LearningHub.Nhs.Models.Paging;
using LearningHub.Nhs.Models.Provider;
using LearningHub.Nhs.Models.User;
using LearningHub.Nhs.Models.Validation;
using Newtonsoft.Json;

/// <summary>
Expand Down Expand Up @@ -132,5 +138,15 @@ public async Task<ApiResponse> UpdateCatalogueOwnerAsync(CatalogueOwnerViewModel
{
return await this.facade.PutAsync("Catalogue/UpdateCatalogueOwner", catalogueOwner);
}

/// <summary>
/// The AddUserGroupsToCatalogue.
/// </summary>
/// <param name="catalogue">The CatalogueViewModel.</param>
/// <returns>The <see cref="Task{IActionResult}"/>.</returns>
public async Task<ApiResponse> AddCategoryToCatalogue(CatalogueViewModel catalogue)
{
return await this.facade.PostAsync<ApiResponse, CatalogueViewModel>("Catalogue/AddCategoryToCatalogue", catalogue);
}
}
}
99 changes: 99 additions & 0 deletions AdminUI/LearningHub.Nhs.AdminUI/Services/MoodleApiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace LearningHub.Nhs.AdminUI.Services
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using LearningHub.Nhs.AdminUI.Configuration;
using LearningHub.Nhs.AdminUI.Interfaces;
using LearningHub.Nhs.Models.Moodle;
using LearningHub.Nhs.Models.Moodle.API;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

/// <summary>
/// MoodleApiService.
/// </summary>
public class MoodleApiService : IMoodleApiService
{
private readonly IOpenApiHttpClient openApiHttpClient;
private readonly MoodleApiConfig configuration;

/// <summary>
/// Initializes a new instance of the <see cref="MoodleApiService"/> class.
/// </summary>
/// <param name="openApiHttpClient">The Open Api Http Client.</param>
/// <param name="configuration">configuration.</param>
public MoodleApiService(IOpenApiHttpClient openApiHttpClient, IOptions<MoodleApiConfig> configuration)
{
this.openApiHttpClient = openApiHttpClient;
this.configuration = configuration.Value;
}

/// <summary>
/// GetMoodleUserIdByUsernameAsync.
/// </summary>
/// <param name="currentUserId">current User Id.</param>
/// <returns>UserId from Moodle.</returns>
public async Task<int> GetMoodleUserIdByUsernameAsync(int currentUserId)
{
int moodleUserId = 0;

try
{
var client = await this.openApiHttpClient.GetClientAsync();

var request = $"Moodle/GetMoodleUserId/{currentUserId}";
var response = await client.GetAsync(request).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
moodleUserId = JsonConvert.DeserializeObject<int>(result);
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("AccessDenied");
}

return moodleUserId;
}
catch (Exception ex)
{
return moodleUserId;
}
}

/// <summary>
/// GetAllMoodleCategoriesAsync.
/// </summary>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
public async Task<List<MoodleCategory>> GetAllMoodleCategoriesAsync()
{
List<MoodleCategory> viewmodel = new List<MoodleCategory>();

try
{
var client = await this.openApiHttpClient.GetClientAsync();

var request = $"Moodle/GetAllMoodleCategories";
var response = await client.GetAsync(request).ConfigureAwait(false);

if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
viewmodel = JsonConvert.DeserializeObject<List<MoodleCategory>>(result);
}
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized || response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new Exception("AccessDenied");
}

return viewmodel;
}
catch (Exception ex)
{
return viewmodel;
}
}
}
}
5 changes: 4 additions & 1 deletion AdminUI/LearningHub.Nhs.AdminUI/Styles/nhsuk/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@

.menu-font {
font-size: 19px !important;
}
}



Loading
Loading