From 008b85b971e8876e0be15943f10357a2e299fb59 Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Tue, 10 Oct 2023 14:39:25 -0600 Subject: [PATCH] quiz form still works --- .../Components/Quiz/EditableQuizAnswer.razor | 24 ++++++------- .../Shared/Components/Quiz/QuizForm.razor | 3 +- .../Components/Quiz/QuizQuestionForm.razor | 26 +++++++------- Management/Models/Local/LocalQuizQuestion.cs | 1 - .../Models/Local/LocalQuizQuestionAnswer.cs | 3 -- .../Services/Canvas/CanvasQuizService.cs | 36 +++---------------- 6 files changed, 31 insertions(+), 62 deletions(-) diff --git a/Management.Web/Shared/Components/Quiz/EditableQuizAnswer.razor b/Management.Web/Shared/Components/Quiz/EditableQuizAnswer.razor index 990d127..ee3f7ce 100644 --- a/Management.Web/Shared/Components/Quiz/EditableQuizAnswer.razor +++ b/Management.Web/Shared/Components/Quiz/EditableQuizAnswer.razor @@ -4,11 +4,16 @@ [Parameter, EditorRequired] public LocalQuizQuestionAnswer Answer { get; set; } = default!; [Parameter, EditorRequired] + public int AnswerIndex { get; set; } = default!; + [Parameter, EditorRequired] + public int QuestionIndex { get; set; } = default!; + [Parameter, EditorRequired] public LocalQuizQuestion Question { get; set; } = default!; [Parameter, EditorRequired] - public Action SaveAnswer { get; set; } = (_) => {}; + public Action SaveAnswer { get; set; } = (_, _) => {}; + private string label => "question_" + QuestionIndex + "_answer_" + AnswerIndex; private string _text { get; set; } = string.Empty; private string text { @@ -16,7 +21,7 @@ set { _text = value; - SaveAnswer(Answer with { Text = _text }); + SaveAnswer(Answer with { Text = _text }, AnswerIndex); } } @@ -29,7 +34,7 @@ private void handleOneAnswerChange() { - SaveAnswer(Answer with {Correct = !Answer.Correct}); + SaveAnswer(Answer with {Correct = !Answer.Correct}, AnswerIndex); } } @@ -43,14 +48,12 @@ class="form-check-input" type="checkbox" role="switch" - id="@("answer_" + Answer.Id)" + id="@label" checked="@Answer.Correct" @onchange="@(() => handleOneAnswerChange())" > @@ -61,16 +64,13 @@ diff --git a/Management.Web/Shared/Components/Quiz/QuizForm.razor b/Management.Web/Shared/Components/Quiz/QuizForm.razor index 9e55c25..19cbead 100644 --- a/Management.Web/Shared/Components/Quiz/QuizForm.razor +++ b/Management.Web/Shared/Components/Quiz/QuizForm.razor @@ -145,10 +145,11 @@ - @foreach(var question in quizContext.Quiz.Questions) + @foreach(var (question, i) in quizContext.Quiz.Questions.Select((q, i) => (q, i))) { diff --git a/Management.Web/Shared/Components/Quiz/QuizQuestionForm.razor b/Management.Web/Shared/Components/Quiz/QuizQuestionForm.razor index 61c09e1..dfefdf1 100644 --- a/Management.Web/Shared/Components/Quiz/QuizQuestionForm.razor +++ b/Management.Web/Shared/Components/Quiz/QuizQuestionForm.razor @@ -4,6 +4,8 @@ [Parameter, EditorRequired] public LocalQuizQuestion Question { get; set; } = default!; [Parameter, EditorRequired] + public int Index { get; set; } = default!; + [Parameter, EditorRequired] public Action UpdateQuestion { get; set; } = (_) => {}; protected override void OnParametersSet() @@ -57,8 +59,7 @@ { if(quizContext.Quiz != null) { - var newAnswer = new LocalQuizQuestionAnswer - { Id = Guid.NewGuid().ToString() }; + var newAnswer = new LocalQuizQuestionAnswer(); answers = answers.Append(newAnswer); UpdateQuestion(Question with { Answers = answers }); @@ -69,29 +70,26 @@ { if(quizContext.Quiz != null) - { - var newAnswer = new LocalQuizQuestionAnswer - { Id = Guid.NewGuid().ToString() }; - + { answers = answers.Take(Question.Answers.Count() - 1); UpdateQuestion(Question with { Answers = answers }); } } - private void saveAnswer(LocalQuizQuestionAnswer newAnswer) + private void saveAnswer(LocalQuizQuestionAnswer newAnswer, int index) { if(questionType == QuestionType.MULTIPLE_CHOICE && newAnswer.Correct) { - answers = answers.Select(a => - a.Id == newAnswer.Id + answers = answers.Select((a, i) => + index == i ? newAnswer : a with { Correct = false } ).ToArray(); } else { - answers = answers.Select(a => - a.Id == newAnswer.Id + answers = answers.Select((a, i) => + index == i ? newAnswer : a ).ToArray(); @@ -173,11 +171,13 @@ + Add Answer - @foreach(var answer in answers) + @foreach(var (answer, i) in answers.Select((a, i) => (a, i))) { } diff --git a/Management/Models/Local/LocalQuizQuestion.cs b/Management/Models/Local/LocalQuizQuestion.cs index ef7eb7c..bee407e 100644 --- a/Management/Models/Local/LocalQuizQuestion.cs +++ b/Management/Models/Local/LocalQuizQuestion.cs @@ -4,7 +4,6 @@ namespace LocalModels; public record LocalQuizQuestion { - public ulong? CanvasId { get; set; } public string Id { get; set; } = ""; public string Text { get; init; } = string.Empty; public string HtmlText => Markdig.Markdown.ToHtml(Text); diff --git a/Management/Models/Local/LocalQuizQuestionAnswer.cs b/Management/Models/Local/LocalQuizQuestionAnswer.cs index e40137f..e18952b 100644 --- a/Management/Models/Local/LocalQuizQuestionAnswer.cs +++ b/Management/Models/Local/LocalQuizQuestionAnswer.cs @@ -4,9 +4,6 @@ namespace LocalModels; public record LocalQuizQuestionAnswer { - public ulong? CanvasId { get; init; } - public string Id { get; init; } = string.Empty; - //correct gets a weight of 100 in canvas public bool Correct { get; init; } public string Text { get; init; } = string.Empty; diff --git a/Management/Services/Canvas/CanvasQuizService.cs b/Management/Services/Canvas/CanvasQuizService.cs index bf33840..085460d 100644 --- a/Management/Services/Canvas/CanvasQuizService.cs +++ b/Management/Services/Canvas/CanvasQuizService.cs @@ -79,7 +79,7 @@ public class CanvasQuizService LocalQuiz localQuiz ) { - var tasks = localQuiz.Questions.Select(createQuestion(canvasCourseId, canvasQuizId, localQuiz)).ToArray(); + var tasks = localQuiz.Questions.Select(createQuestion(canvasCourseId, canvasQuizId)).ToArray(); await Task.WhenAll(tasks); await hackFixRedundantAssignments(canvasCourseId); } @@ -107,45 +107,17 @@ public class CanvasQuizService await Task.WhenAll(tasks); } - private Func> createQuestion( + private Func createQuestion( ulong canvasCourseId, - ulong canvasQuizId, - LocalQuiz localQuiz + ulong canvasQuizId ) { - return async (question) => - { - var newQuestion = await createQuestionOnly(canvasCourseId, canvasQuizId, localQuiz, question); - - var answersWithIds = question.Answers - .Select((answer, i) => - { - var canvasAnswer = newQuestion.Answers?.ElementAt(i); - if (canvasAnswer == null) - { - Console.WriteLine(i); - Console.WriteLine(JsonSerializer.Serialize(newQuestion)); - Console.WriteLine(JsonSerializer.Serialize(question)); - throw new NullReferenceException( - "Could not find canvas answer to update local answer id" - ); - } - return answer with { CanvasId = canvasAnswer.Id }; - }) - .ToArray(); - - return question with - { - CanvasId = newQuestion.Id, - Answers = answersWithIds - }; - }; + return async (question) => await createQuestionOnly(canvasCourseId, canvasQuizId, question); } private async Task createQuestionOnly( ulong canvasCourseId, ulong canvasQuizId, - LocalQuiz localQuiz, LocalQuizQuestion q ) {