diff --git a/Management.Test/Markdown/Quiz/MatchingTests.cs b/Management.Test/Markdown/Quiz/MatchingTests.cs index 2c96d70..11fcd14 100644 --- a/Management.Test/Markdown/Quiz/MatchingTests.cs +++ b/Management.Test/Markdown/Quiz/MatchingTests.cs @@ -103,4 +103,28 @@ Match the following terms & definitions var quiz = LocalQuiz.ParseMarkdown(rawMarkdownQuiz); quiz.Questions.First().Answers.First().MatchDistractors.Should().BeEquivalentTo(["this is the distractor"]); } + [Fact] + public void CanHaveDistractorsAndBePersisted() + { + var rawMarkdownQuiz = @" +Name: Test Quiz +ShuffleAnswers: true +OneQuestionAtATime: false +DueAt: 2023-08-21T23:59:00 +LockAt: 2023-08-21T23:59:00 +AssignmentGroup: Assignments +AllowedAttempts: -1 +Description: +--- +Match the following terms & definitions + +^statement - a single command to be executed +^ - this is the distractor +"; + + var quiz = LocalQuiz.ParseMarkdown(rawMarkdownQuiz); + var quizMarkdown = quiz.ToMarkdown(); + + quizMarkdown.Should().Contain("^ statement - a single command to be executed\n^ - this is the distractor"); + } } diff --git a/Management/Models/Local/Quiz/LocalQuizQuestion.cs b/Management/Models/Local/Quiz/LocalQuizQuestion.cs index 08065d3..c81eec2 100644 --- a/Management/Models/Local/Quiz/LocalQuizQuestion.cs +++ b/Management/Models/Local/Quiz/LocalQuizQuestion.cs @@ -1,6 +1,8 @@ using System.Security.Cryptography; using System.Text.RegularExpressions; +using Akka.Util.Internal; + namespace LocalModels; public record LocalQuizQuestion @@ -37,7 +39,10 @@ public record LocalQuizQuestion } else if (QuestionType == "matching") { - return $"^ {answer.Text} - {answer.MatchedText}"; + var distractorText = answer.MatchDistractors?.Select( + d => $"\n^ - {d}" + ).Join("") ?? ""; + return $"^ {answer.Text} - {answer.MatchedText}" + distractorText; } else { @@ -184,15 +189,16 @@ public record LocalQuizQuestion if (questionType != "matching") return accumulator.Append(answer); - if(accumulator.Count() == 0) + if (accumulator.Count() == 0) return accumulator.Append(answer); - - if(answer.Text != "") + + if (answer.Text != "") return accumulator.Append(answer); var previousDistractors = accumulator.Last().MatchDistractors ?? []; - var newLastAnswer = accumulator.Last() with { + var newLastAnswer = accumulator.Last() with + { MatchDistractors = previousDistractors.Append(answer.MatchedText ?? "").ToArray() }; diff --git a/Management/Services/Canvas/CanvasQuizService.cs b/Management/Services/Canvas/CanvasQuizService.cs index 1b94291..bc03685 100644 --- a/Management/Services/Canvas/CanvasQuizService.cs +++ b/Management/Services/Canvas/CanvasQuizService.cs @@ -196,7 +196,8 @@ public class CanvasQuizService( .Select(a => new { answer_match_left = a.Text, - answer_match_right = a.MatchedText + answer_match_right = a.MatchedText, + matching_answer_incorrect_matches = a.MatchDistractors, }) .ToArray();