Skip to content

Commit

Permalink
Implemented Answering of Adaptivity Questions
Browse files Browse the repository at this point in the history
  • Loading branch information
philgei committed Oct 21, 2023
1 parent bd35e03 commit 9e8deaf
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 38 deletions.
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!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ await _lms.GetAdaptivityElementDetailsAsync(request.WebServiceToken,
};
}

private static int GetQuestionIdFromUuid(Guid uuid, AdaptivityElement adaptivityElement)

public static int GetQuestionIdFromUuid(Guid uuid, AdaptivityElement adaptivityElement)
{
foreach (var question in from task in adaptivityElement.AdaptivityContent.AdaptivityTasks
from question in task.AdaptivityQuestions
Expand All @@ -93,7 +94,7 @@ from question in task.AdaptivityQuestions
throw new Exception("No id for the Adaptivity Question found!");
}

private static int GetTaskIdFromUuid(Guid uuid, AdaptivityElement adaptivityElement)
public static int GetTaskIdFromUuid(Guid uuid, AdaptivityElement adaptivityElement)
{
foreach (var task in from task in adaptivityElement.AdaptivityContent.AdaptivityTasks
where task.TaskUuid == uuid
Expand Down
2 changes: 1 addition & 1 deletion AdLerBackend.Application/Common/Interfaces/ILMS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Task<IEnumerable<LmsUuidResponse>> GetLmsElementIdsByUuidsAsync(string token, in
/// <param name="elementId">The Module ID of the Element</param>
/// <param name="answeredQuestions"></param>
/// <returns> A list of the given Answers and there State of correctness</returns>
Task<IEnumerable<LMSAdaptivityQuestionStateResponse>> AnswerAdaptivityQuestionsAsync(string token, int elementId,
Task<AdaptivityModuleStateResponseAfterAnswer> AnswerAdaptivityQuestionsAsync(string token, int elementId,
IEnumerable<AdaptivityAnsweredQuestionTo> answeredQuestions);

/// <summary>
Expand Down
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;
}
62 changes: 41 additions & 21 deletions AdLerBackend.Infrastructure/Moodle/MoodleWebApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,6 @@ public async Task DeleteCourseAsync(string token, int courseId)
JsonSerializer.Serialize(warnings.Data));
}

public async Task<IEnumerable<LMSAdaptivityQuestionStateResponse>> AnswerAdaptivityQuestionsAsync(string token,
int elementId, IEnumerable<AdaptivityAnsweredQuestionTo> answeredQuestions)
{
var wsParams = new Dictionary<string, HttpContent>
{
{"wstoken", new StringContent(token)},
{"wsfunction", new StringContent("mod_adleradaptivity_answer_questions")},
{"module[module_id]", new StringContent(elementId.ToString())}
};

for (var i = 0; i < answeredQuestions.Count(); i++)
{
wsParams.Add($"questions[{i}][uuid]", new StringContent(answeredQuestions.ElementAt(i).Uuid));
wsParams.Add($"questions[{i}][answer]", new StringContent(answeredQuestions.ElementAt(i).Answer));
}

var result = await MoodleCallAsync<ResponseWithData<AdaptivityModuleAnsweredResponse>>(wsParams);

return new List<LMSAdaptivityQuestionStateResponse>();
}

public async Task<IEnumerable<LMSAdaptivityQuestionStateResponse>> GetAdaptivityElementDetailsAsync(string token,
int elementId)
{
Expand Down Expand Up @@ -291,6 +270,47 @@ public async Task<IEnumerable<LmsUuidResponse>> GetLmsElementIdsByUuidsAsync(str
});
}

public async Task<AdaptivityModuleStateResponseAfterAnswer> AnswerAdaptivityQuestionsAsync(string token,
int elementId, IEnumerable<AdaptivityAnsweredQuestionTo> answeredQuestions)
{
var wsParams = new Dictionary<string, HttpContent>
{
{"wstoken", new StringContent(token)},
{"wsfunction", new StringContent("mod_adleradaptivity_answer_questions")},
{"module[module_id]", new StringContent(elementId.ToString())}
};

for (var i = 0; i < answeredQuestions.Count(); i++)
{
wsParams.Add($"questions[{i}][uuid]", new StringContent(answeredQuestions.ElementAt(i).Uuid));
wsParams.Add($"questions[{i}][answer]", new StringContent(answeredQuestions.ElementAt(i).Answer));
}

var result = await MoodleCallAsync<ResponseWithData<AdaptivityModuleAnsweredResponse>>(wsParams);

return new AdaptivityModuleStateResponseAfterAnswer
{
Questions = result.Data.Questions.Select(x => new LMSAdaptivityQuestionStateResponse
{
Uuid = Guid.Parse(x.Uuid),
Status = Enum.Parse<AdaptivityStates>(x.Status, true),
Answers = x.Answers != null
? JsonSerializer.Deserialize<IList<LMSAdaptivityQuestionStateResponse.LMSAdaptivityAnswers>>(
x.Answers, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
})
: null
}).ToList(),
Tasks = result.Data.Tasks.Select(x => new LMSAdaptivityTaskStateResponse
{
Uuid = Guid.Parse(x.Uuid),
State = Enum.Parse<AdaptivityStates>(x.Status, true)
}).ToList(),
State = Enum.Parse<AdaptivityStates>(result.Data.Module.Status, true)
};
}


private async Task<TDtoType> MoodleCallAsync<TDtoType>(Dictionary<string, HttpContent> wsParams,
PostToMoodleOptions? options = null)
Expand Down

0 comments on commit 9e8deaf

Please sign in to comment.