@using MediatR @using NexusReader.Application.Queries.Quiz @using NexusReader.Application.Commands.Quiz @using NexusReader.Application.Abstractions.Services @using NexusReader.UI.Shared.Components.Atoms @using NexusReader.UI.Shared.Services @inject IMediator Mediator @inject IPlatformService PlatformService @inject IQuizStateService QuizService @inject IIdentityService IdentityService @inject IKnowledgeGraphService GraphService @inject KnowledgeCoordinator Coordinator
Sprawdzian Wiedzy
@if (QuizService.IsHydrating || _isGenerating) {
Skanowanie wiedzy przez AI...
} else if (_isSubmitted) {
@_score / @_totalQuestions
@((int)_percentage)% poprawnych odpowiedzi
} else if (QuizService.CurrentQuiz != null) {
@foreach (var question in QuizService.CurrentQuiz.Questions) {

@question.Question

@for (int i = 0; i < question.Options.Count; i++) { var index = i; var letter = (char)('A' + i); }
}
} else {

Brak Aktywnego Quizu

Generuj spersonalizowany sprawdzian wiedzy na podstawie bieżącego rozdziału książki.

}
@code { [Parameter] public string ContextBlockId { get; set; } = string.Empty; private Dictionary _states = new(); private bool _isSubmitting = false; private bool _isSubmitted = false; private bool _isGenerating = false; private int _score = 0; private int _totalQuestions = 0; private double _percentage = 0.0; protected override void OnInitialized() { QuizService.OnQuizUpdated += HandleUpdate; } private Task HandleUpdate() => InvokeAsync(StateHasChanged); public void Dispose() { QuizService.OnQuizUpdated -= HandleUpdate; } private async Task GenerateChapterQuizAsync() { if (_isGenerating || string.IsNullOrWhiteSpace(Coordinator.CurrentFullPageContent)) return; _isGenerating = true; StateHasChanged(); try { await Coordinator.RequestSummaryAndQuizAsync(Coordinator.CurrentFullPageContent); } finally { _isGenerating = false; StateHasChanged(); } } private async Task SelectOptionAsync(QuizQuestionDto question, int index) { if (_states.ContainsKey(question)) return; // Haptic feedback await PlatformService.VibrateAsync(40); var cmd = new SubmitAnswerCommand(index, question.CorrectIndex); var res = await Mediator.Send(cmd); _states[question] = (index, res.IsSuccess); if (res.IsSuccess) await PlatformService.VibrateSuccessAsync(); else await PlatformService.VibrateErrorAsync(); StateHasChanged(); } private bool AllQuestionsAnswered() { return QuizService.CurrentQuiz != null && _states.Count == QuizService.CurrentQuiz.Questions.Count; } private async Task SubmitQuizAsync() { if (QuizService.CurrentQuiz == null || !AllQuestionsAnswered() || _isSubmitting) return; _isSubmitting = true; StateHasChanged(); try { _score = _states.Values.Count(s => s.IsCorrect); _totalQuestions = QuizService.CurrentQuiz.Questions.Count; _percentage = _totalQuestions > 0 ? ((double)_score / _totalQuestions) * 100 : 0.0; string topic = "Quiz wiedzy"; var graph = GraphService.CurrentGraphData; if (graph != null && !string.IsNullOrEmpty(QuizService.CurrentQuizBlockId)) { var node = graph.Nodes.FirstOrDefault(n => n.Id == QuizService.CurrentQuizBlockId); if (node != null && !string.IsNullOrEmpty(node.Label)) { topic = $"Test: {node.Label}"; } } var profileResult = await IdentityService.GetProfileAsync(); if (profileResult.IsSuccess && profileResult.Value != null) { var userId = profileResult.Value.UserId; var cmd = new SubmitQuizResultCommand(userId, topic, _score, _totalQuestions); var result = await Mediator.Send(cmd); if (result.IsSuccess) { IdentityService.ClearCache(); _isSubmitted = true; await PlatformService.VibrateSuccessAsync(); } else { await PlatformService.VibrateErrorAsync(); } } } catch { await PlatformService.VibrateErrorAsync(); } finally { _isSubmitting = false; StateHasChanged(); } } private void CloseQuiz() { _isSubmitted = false; _states.Clear(); QuizService.SetQuiz(null, null); } private string GetBlockClass(QuizQuestionDto question) { if (!_states.TryGetValue(question, out var state)) return ""; return state.IsCorrect ? "state-correct" : "state-incorrect"; } private string GetOptionClass(QuizQuestionDto question, int index) { if (!_states.TryGetValue(question, out var state)) return ""; if (state.SelectedIndex == index) return state.IsCorrect ? "option-correct" : "option-incorrect"; if (state.IsCorrect == false && question.CorrectIndex == index) return "option-revealed-correct"; return "option-faded"; } }