Skip to content

Commit 0930d3f

Browse files
committed
Updated the completion status logic
1 parent c58cfe7 commit 0930d3f

File tree

6 files changed

+201
-2
lines changed

6 files changed

+201
-2
lines changed

LearningHub.Nhs.WebUI/Interfaces/IMoodleApiService.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,14 @@ public interface IMoodleApiService
1717
/// <param name="pageNumber">pageNumber.</param>
1818
/// <returns> List of MoodleCourseResponseViewModel.</returns>
1919
Task<List<MoodleCourseResponseViewModel>> GetEnrolledCoursesAsync(int currentUserId, int pageNumber);
20+
21+
/// <summary>
22+
/// GetEnrolledCoursesAsync.
23+
/// </summary>
24+
/// <param name="userId">Moodle user id.</param>
25+
/// <param name="courseId">Moodle course id.</param>
26+
/// <param name="pageNumber">pageNumber.</param>
27+
/// <returns> List of MoodleCourseResponseViewModel.</returns>
28+
Task<MoodleCourseCompletionViewModel> GetCourseCompletionAsync(int userId, int courseId, int pageNumber);
2029
}
2130
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace LearningHub.Nhs.WebUI.Models
2+
{
3+
/// <summary>
4+
/// MoodleCompletionResponseViewModel.
5+
/// </summary>
6+
public class MoodleCompletionResponseViewModel
7+
{
8+
/// <summary>
9+
/// Gets or sets the completion status.
10+
/// </summary>
11+
public string Exception { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets error code.
15+
/// </summary>
16+
public string Errorcode { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets Error message.
20+
/// </summary>
21+
public string Message { get; set; }
22+
23+
/// <summary>
24+
/// Gets or sets Debug info.
25+
/// </summary>
26+
public string Debuginfo { get; set; }
27+
}
28+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
namespace LearningHub.Nhs.WebUI.Models
2+
{
3+
using System.Collections.Generic;
4+
5+
/// <summary>
6+
/// MoodleCourseCompletionViewModel.
7+
/// </summary>
8+
public class MoodleCourseCompletionViewModel
9+
{
10+
/// <summary>
11+
/// Gets or sets the completion status.
12+
/// </summary>
13+
public CompletStatus CompletionStatus { get; set; }
14+
15+
/// <summary>
16+
/// Gets or sets the list of warnings.
17+
/// </summary>
18+
public List<object> Warnings { get; set; }
19+
20+
/// <summary>
21+
/// CompletionStatus.
22+
/// </summary>
23+
public class CompletStatus
24+
{
25+
/// <summary>
26+
/// Gets or sets a value indicating whether the course is completed.
27+
/// </summary>
28+
public bool Completed { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the aggregation method.
32+
/// </summary>
33+
public int Aggregation { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the list of completions.
37+
/// </summary>
38+
public List<Completion> Completions { get; set; }
39+
40+
/// <summary>
41+
/// Completion.
42+
/// </summary>
43+
public class Completion
44+
{
45+
/// <summary>
46+
/// Gets or sets the type of completion.
47+
/// </summary>
48+
public int Type { get; set; }
49+
50+
/// <summary>
51+
/// Gets or sets the title of the completion requirement.
52+
/// </summary>
53+
public string Title { get; set; }
54+
55+
/// <summary>
56+
/// Gets or sets the status of the completion.
57+
/// </summary>
58+
public string Status { get; set; }
59+
60+
/// <summary>
61+
/// Gets or sets a value indicating whether the requirement is complete.
62+
/// </summary>
63+
public bool Complete { get; set; }
64+
65+
/// <summary>
66+
/// Gets or sets the timestamp when completion was achieved.
67+
/// </summary>
68+
public long? TimeCompleted { get; set; }
69+
70+
/// <summary>
71+
/// Gets or sets the completion details.
72+
/// </summary>
73+
public CompletionDetails Details { get; set; }
74+
75+
/// <summary>
76+
/// CompletionDetails.
77+
/// </summary>
78+
public class CompletionDetails
79+
{
80+
/// <summary>
81+
/// Gets or sets the type of completion requirement.
82+
/// </summary>
83+
public string Type { get; set; }
84+
85+
/// <summary>
86+
/// Gets or sets the criteria for completion.
87+
/// </summary>
88+
public string Criteria { get; set; }
89+
90+
/// <summary>
91+
/// Gets or sets the requirement for completion.
92+
/// </summary>
93+
public string Requirement { get; set; }
94+
95+
/// <summary>
96+
/// Gets or sets the status of the requirement.
97+
/// </summary>
98+
public string Status { get; set; }
99+
}
100+
}
101+
}
102+
}
103+
}

LearningHub.Nhs.WebUI/Models/MoodleCourseResponseViewModel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,10 @@ public class MoodleCourseResponseViewModel
151151
/// Gets or sets the last modified timestamp (Unix timestamp).
152152
/// </summary>
153153
public long? TimeModified { get; set; }
154+
155+
/// <summary>
156+
/// Gets or sets the moodle course completion view model.
157+
/// </summary>
158+
public MoodleCourseCompletionViewModel CourseCompletionViewModel { get; set; }
154159
}
155160
}

LearningHub.Nhs.WebUI/Services/MoodleApiService.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Net.Http;
66
using System.Threading.Tasks;
7+
using LearningHub.Nhs.Models.Entities.Reporting;
78
using LearningHub.Nhs.Services.Interface;
89
using LearningHub.Nhs.WebUI.Interfaces;
910
using LearningHub.Nhs.WebUI.Models;
@@ -49,6 +50,50 @@ public async Task<List<MoodleCourseResponseViewModel>> GetEnrolledCoursesAsync(i
4950
{
5051
var result = response.Content.ReadAsStringAsync().Result;
5152
viewmodel = JsonConvert.DeserializeObject<List<MoodleCourseResponseViewModel>>(result);
53+
54+
foreach (var course in viewmodel)
55+
{
56+
course.CourseCompletionViewModel = await moodleApiService.GetCourseCompletionAsync(userId, course.Id.Value, pageNumber);
57+
}
58+
}
59+
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
60+
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
61+
{
62+
throw new Exception("AccessDenied");
63+
}
64+
65+
return viewmodel;
66+
}
67+
68+
/// <summary>
69+
/// GetEnrolledCoursesAsync.
70+
/// </summary>
71+
/// <param name="userId">Moodle user id.</param>
72+
/// <param name="courseId">Moodle course id.</param>
73+
/// <param name="pageNumber">pageNumber.</param>
74+
/// <returns> List of MoodleCourseResponseViewModel.</returns>
75+
public async Task<MoodleCourseCompletionViewModel> GetCourseCompletionAsync(int userId, int courseId, int pageNumber)
76+
{
77+
MoodleCourseCompletionViewModel viewmodel = new MoodleCourseCompletionViewModel { };
78+
MoodleApiService moodleApiService = new MoodleApiService(this.moodleHttpClient);
79+
80+
var client = await this.moodleHttpClient.GetClient();
81+
string additionalParameters = $"userid={userId}&courseid={courseId}";
82+
string defaultParameters = this.moodleHttpClient.GetDefaultParameters();
83+
string url = $"&wsfunction=core_completion_get_course_completion_status&{additionalParameters}";
84+
85+
HttpResponseMessage response = await client.GetAsync("?" + defaultParameters + url);
86+
87+
if (response.IsSuccessStatusCode)
88+
{
89+
var result = response.Content.ReadAsStringAsync().Result;
90+
91+
var canViewReport = JsonConvert.DeserializeObject<MoodleCompletionResponseViewModel>(result);
92+
93+
if (string.IsNullOrEmpty(canViewReport.Exception))
94+
{
95+
viewmodel = JsonConvert.DeserializeObject<MoodleCourseCompletionViewModel>(result);
96+
}
5297
}
5398
else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
5499
response.StatusCode == System.Net.HttpStatusCode.Forbidden)

LearningHub.Nhs.WebUI/Views/Home/_CourseEnrolled.cshtml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
@{
66
bool providerExists = false;
77
string cardStyle = "card-provider-details--blank";
8+
9+
string GetUrl(int courseId)
10+
{
11+
string host = $"http://localhost";
12+
string path = $"course/view.php";
13+
string returnUrl = $@"{host}/{path}?id={courseId}";
14+
15+
return returnUrl;
16+
}
817
}
918
<div class="nhsuk-card dashboard-card">
1019
<div class="nhsuk-card-banner-container">
@@ -29,7 +38,7 @@
2938

3039
<div class="nhsuk-card__content dashboard-card-content">
3140
<h3 class="nhsuk-card__heading nhsuk-heading-m">
32-
<a class="nhsuk-card__link" href="http://localhost/course/view.php?id=@Model.Id">@Model.DisplayName</a>
41+
<a class="nhsuk-card__link" href="@GetUrl(Model.Id.Value)">@Model.DisplayName</a>
3342
</h3>
3443
<div class="nhsuk-card__description dashboard-card-body">
3544

@@ -38,7 +47,7 @@
3847
</div>
3948

4049
<div class="line-clamp-3 nhsuk-u-margin-bottom-2point5">
41-
Completed: @Model.Completed
50+
Completed: @Model.CourseCompletionViewModel?.CompletionStatus?.Completed
4251
</div>
4352

4453
<div class="catalogue-meta">

0 commit comments

Comments
 (0)