diff --git a/Management.Web/Shared/Components/Quiz/Markdown/MarkdownQuizForm.razor b/Management.Web/Shared/Components/Quiz/Markdown/MarkdownQuizForm.razor index 9999c61..738dd3d 100644 --- a/Management.Web/Shared/Components/Quiz/Markdown/MarkdownQuizForm.razor +++ b/Management.Web/Shared/Components/Quiz/Markdown/MarkdownQuizForm.razor @@ -95,7 +95,7 @@
@if(error != null) { -

@error

+

Error: @error

}
diff --git a/Management.Web/Shared/Components/Quiz/Markdown/QuizPreview.razor b/Management.Web/Shared/Components/Quiz/Markdown/QuizPreview.razor index 9ac9333..a8b7295 100644 --- a/Management.Web/Shared/Components/Quiz/Markdown/QuizPreview.razor +++ b/Management.Web/Shared/Components/Quiz/Markdown/QuizPreview.razor @@ -54,7 +54,7 @@
@Quiz.LocalAssignmentGroupName
-
@Quiz.Description
+
@Quiz.Description
@foreach(var question in Quiz.Questions) { diff --git a/Management/Features/Configuration/QuizEditorContext.cs b/Management/Features/Configuration/QuizEditorContext.cs index f1b6520..f5e395f 100644 --- a/Management/Features/Configuration/QuizEditorContext.cs +++ b/Management/Features/Configuration/QuizEditorContext.cs @@ -19,6 +19,8 @@ public class QuizEditorContext private LocalQuiz? _quiz; + + private LocalModule? _module; private readonly MyLogger logger; public LocalQuiz? Quiz @@ -26,6 +28,10 @@ public class QuizEditorContext get => _quiz; set { + if(_quiz == null) + { + _module = getCurrentModule(value, planner.LocalCourse); + } _quiz = value; StateHasChanged?.Invoke(); } @@ -33,18 +39,18 @@ public class QuizEditorContext public void SaveQuiz(LocalQuiz newQuiz) { - if (planner.LocalCourse != null) + if (planner.LocalCourse != null && _module != null && Quiz != null) { - var currentModule = getCurrentModule(newQuiz, planner.LocalCourse); + // use Quiz not newQuiz because it is the version that was last stored var updatedModules = planner.LocalCourse.Modules .Select( m => - m.Name == currentModule.Name - ? currentModule with + m.Name == _module.Name + ? m with { - Quizzes = currentModule.Quizzes - .Select(q => q.Name + q.Description == newQuiz.Name + newQuiz.Description ? newQuiz : q) + Quizzes = m.Quizzes + .Select(q => q.Name + q.Description == Quiz.Name + Quiz.Description ? newQuiz : q) .ToArray() } : m @@ -58,12 +64,11 @@ public class QuizEditorContext public void DeleteQuiz() { - if (planner.LocalCourse != null && Quiz != null) + if (planner.LocalCourse != null && Quiz != null && _module != null) { - var currentModule = getCurrentModule(Quiz, planner.LocalCourse); var updatedModules = planner.LocalCourse.Modules - .Select(m => m.Name != currentModule.Name + .Select(m => m.Name != _module.Name ? m : m with { Quizzes = m.Quizzes.Where(q => q.Name + q.Description != Quiz.Name + Quiz.Description).ToArray() @@ -132,7 +137,9 @@ public class QuizEditorContext private static LocalModule getCurrentModule(LocalQuiz newQuiz, LocalCourse course) { - return course.Modules.FirstOrDefault(m => m.Quizzes.Select(q => q.Name + q.Description).Contains(newQuiz.Name + newQuiz.Description)) + return course.Modules.FirstOrDefault( + m => m.Quizzes.Select(q => q.Name + q.Description).Contains(newQuiz.Name + newQuiz.Description) + ) ?? throw new Exception("could not find current module in quiz editor context"); } } diff --git a/Management/Models/Local/LocalQuiz.cs b/Management/Models/Local/LocalQuiz.cs index 6307b56..9a9374c 100644 --- a/Management/Models/Local/LocalQuiz.cs +++ b/Management/Models/Local/LocalQuiz.cs @@ -75,17 +75,34 @@ Description: {Description} { var name = extractLabelValue(settings, "Name"); - - var shuffleAnswers = bool.Parse(extractLabelValue(settings, "ShuffleAnswers")); - var oneQuestionAtATime = bool.Parse(extractLabelValue(settings, "OneQuestionAtATime")); - var allowedAttempts = int.Parse(extractLabelValue(settings, "AllowedAttempts")); - var dueAt = DateTime.Parse(extractLabelValue(settings, "DueAt")); + + var rawShuffleAnswers = extractLabelValue(settings, "ShuffleAnswers"); + var shuffleAnswers = bool.TryParse(rawShuffleAnswers, out bool parsedShuffleAnswers) + ? parsedShuffleAnswers + : throw new QuizMarkdownParseException($"Error with ShuffleAnswers: {rawShuffleAnswers}"); + + + var rawOneQuestionAtATime = extractLabelValue(settings, "OneQuestionAtATime"); + var oneQuestionAtATime = bool.TryParse(rawOneQuestionAtATime, out bool parsedOneQuestion) + ? parsedOneQuestion + : throw new QuizMarkdownParseException($"Error with oneQuestionAtATime: {rawOneQuestionAtATime}"); + + var rawAllowedAttempts = extractLabelValue(settings, "AllowedAttempts"); + var allowedAttempts = int.TryParse(rawAllowedAttempts, out int parsedAllowedAttempts) + ? parsedAllowedAttempts + : throw new QuizMarkdownParseException($"Error with AllowedAttempts: {rawAllowedAttempts}"); + + + var rawDueAt = extractLabelValue(settings, "DueAt"); + var dueAt = DateTime.TryParse(rawDueAt, out DateTime parsedDueAt) + ? parsedDueAt + : throw new QuizMarkdownParseException($"Error with DueAt: {rawDueAt}"); var rawLockAt = extractLabelValue(settings, "LockAt"); DateTime? lockAt = DateTime.TryParse(rawLockAt, out DateTime parsedLockAt) - ? parsedLockAt - : null; + ? parsedLockAt + : null; var description = extractDescription(settings);