-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Answering of Adaptivity Questions
- Loading branch information
Showing
5 changed files
with
126 additions
and
38 deletions.
There are no files selected for viewing
87 changes: 73 additions & 14 deletions
87
...ackend.Application/Adaptivity/AnswerAdaptivityQuestion/AnswerAdaptivityQuestionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,101 @@ | ||
using AdLerBackend.Application.Common.Responses.Adaptivity; | ||
using AdLerBackend.Application.Adaptivity.GetAdaptivityModuleQuestionDetails; | ||
using AdLerBackend.Application.Common.DTOs; | ||
using AdLerBackend.Application.Common.Interfaces; | ||
using AdLerBackend.Application.Common.InternalUseCases.GetAllElementsFromLms; | ||
using AdLerBackend.Application.Common.Responses.Adaptivity; | ||
using AdLerBackend.Application.Common.Responses.Adaptivity.Common; | ||
using AdLerBackend.Application.Common.Responses.Elements; | ||
using AdLerBackend.Application.Common.Responses.LMSAdapter.Adaptivity; | ||
using AdLerBackend.Application.Common.Responses.World; | ||
using MediatR; | ||
|
||
namespace AdLerBackend.Application.Adaptivity.AnswerAdaptivityQuestion; | ||
|
||
public class | ||
AnswerAdaptivityQuestionHandler : IRequestHandler<AnswerAdaptivityQuestionCommand, AnswerAdaptivityQuestionResponse> | ||
{ | ||
private readonly ILMS _lms; | ||
private readonly IMediator _mediator; | ||
private readonly ISerialization _serialization; | ||
private readonly IWorldRepository _worldRepository; | ||
|
||
public AnswerAdaptivityQuestionHandler(ILMS lms, IMediator mediator, IWorldRepository worldRepository, | ||
ISerialization serialization) | ||
{ | ||
_lms = lms; | ||
_mediator = mediator; | ||
_worldRepository = worldRepository; | ||
_serialization = serialization; | ||
} | ||
|
||
public async Task<AnswerAdaptivityQuestionResponse> Handle(AnswerAdaptivityQuestionCommand request, | ||
CancellationToken cancellationToken) | ||
{ | ||
var learningElementModules = await _mediator.Send(new GetAllElementsFromLmsCommand | ||
{ | ||
WorldId = request.WorldId, | ||
WebServiceToken = request.WebServiceToken | ||
}, cancellationToken); | ||
|
||
// Get LearningElement Activity Id | ||
var learningElementModule = learningElementModules.ModulesWithAdLerId | ||
.FirstOrDefault(x => x.AdLerId == request.ElementId); | ||
|
||
var learningWorld = await _worldRepository.GetAsync(request.WorldId); | ||
|
||
var atfObject = _serialization.GetObjectFromJsonString<WorldAtfResponse>(learningWorld.AtfJson); | ||
|
||
var adaptivityElementInAtf = atfObject.World.Elements.FirstOrDefault(x => | ||
x.ElementId == request.ElementId) as AdaptivityElement; | ||
|
||
var questionUUID = GetUuidFromQuestionId(request.QuestionId, adaptivityElementInAtf); | ||
|
||
|
||
var resultFromLMS = await _lms.AnswerAdaptivityQuestionsAsync(request.WebServiceToken, | ||
learningElementModule.LmsModule.Id, | ||
new List<AdaptivityAnsweredQuestionTo> | ||
{ | ||
new() | ||
{ | ||
Uuid = questionUUID.ToString(), | ||
Answer = "[true]" | ||
} | ||
}); | ||
|
||
return new AnswerAdaptivityQuestionResponse | ||
{ | ||
ElementScore = new ElementScoreResponse | ||
{ | ||
ElementId = 123, | ||
Success = true | ||
ElementId = request.ElementId, | ||
Success = resultFromLMS.State == AdaptivityStates.Correct | ||
}, | ||
GradedQuestion = new GradedQuestion | ||
{ | ||
Status = AdaptivityStates.Correct.ToString(), | ||
Id = 1234, | ||
Answers = new List<GradedQuestion.GradedAnswer> | ||
Status = resultFromLMS.Questions.First().Status.ToString(), | ||
Id = request.QuestionId, | ||
Answers = resultFromLMS.Questions?.First().Answers?.Select(answer => new GradedQuestion.GradedAnswer | ||
{ | ||
new() | ||
{ | ||
Correct = true, | ||
Checked = true | ||
} | ||
} | ||
Checked = answer.Checked, | ||
Correct = answer.User_Answer_correct | ||
}) ?? null | ||
}, | ||
GradedTask = new GradedTask | ||
{ | ||
TaskId = 1234, | ||
TaskStatus = AdaptivityStates.Correct.ToString() | ||
TaskId = GetAdaptivityElementDetailsHandler.GetTaskIdFromUuid(resultFromLMS.Tasks.First().Uuid, | ||
adaptivityElementInAtf!), | ||
TaskStatus = resultFromLMS.Tasks.First().State.ToString() | ||
} | ||
}; | ||
} | ||
|
||
public static Guid GetUuidFromQuestionId(int questionId, AdaptivityElement adaptivityElement) | ||
{ | ||
foreach (var question in from task in adaptivityElement.AdaptivityContent.AdaptivityTasks | ||
from question in task.AdaptivityQuestions | ||
where question.QuestionId == questionId | ||
select question) | ||
return question.QuestionUuid; | ||
|
||
throw new Exception("No uuid for the Adaptivity Question found!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...cation/Common/Responses/LMSAdapter/Adaptivity/AdaptivityModuleStateResponseAfterAnswer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace AdLerBackend.Application.Common.Responses.LMSAdapter.Adaptivity; | ||
|
||
public class AdaptivityModuleStateResponseAfterAnswer | ||
{ | ||
public IEnumerable<LMSAdaptivityQuestionStateResponse> Questions { get; init; } | ||
public IEnumerable<LMSAdaptivityTaskStateResponse> Tasks { get; init; } | ||
public AdaptivityStates State { get; init; } = AdaptivityStates.NotAttempted; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters