quiz form still works

This commit is contained in:
2023-10-10 14:39:25 -06:00
parent 9a06fd1cfd
commit 008b85b971
6 changed files with 31 additions and 62 deletions

View File

@@ -4,11 +4,16 @@
[Parameter, EditorRequired] [Parameter, EditorRequired]
public LocalQuizQuestionAnswer Answer { get; set; } = default!; public LocalQuizQuestionAnswer Answer { get; set; } = default!;
[Parameter, EditorRequired] [Parameter, EditorRequired]
public int AnswerIndex { get; set; } = default!;
[Parameter, EditorRequired]
public int QuestionIndex { get; set; } = default!;
[Parameter, EditorRequired]
public LocalQuizQuestion Question { get; set; } = default!; public LocalQuizQuestion Question { get; set; } = default!;
[Parameter, EditorRequired] [Parameter, EditorRequired]
public Action<LocalQuizQuestionAnswer> SaveAnswer { get; set; } = (_) => {}; public Action<LocalQuizQuestionAnswer, int> SaveAnswer { get; set; } = (_, _) => {};
private string label => "question_" + QuestionIndex + "_answer_" + AnswerIndex;
private string _text { get; set; } = string.Empty; private string _text { get; set; } = string.Empty;
private string text private string text
{ {
@@ -16,7 +21,7 @@
set set
{ {
_text = value; _text = value;
SaveAnswer(Answer with { Text = _text }); SaveAnswer(Answer with { Text = _text }, AnswerIndex);
} }
} }
@@ -29,7 +34,7 @@
private void handleOneAnswerChange() private void handleOneAnswerChange()
{ {
SaveAnswer(Answer with {Correct = !Answer.Correct}); SaveAnswer(Answer with {Correct = !Answer.Correct}, AnswerIndex);
} }
} }
@@ -43,14 +48,12 @@
class="form-check-input" class="form-check-input"
type="checkbox" type="checkbox"
role="switch" role="switch"
id="@("answer_" + Answer.Id)" id="@label"
checked="@Answer.Correct" checked="@Answer.Correct"
@onchange="@(() => handleOneAnswerChange())" @onchange="@(() => handleOneAnswerChange())"
> >
<label <label
class="form-check-label" class="form-check-label" for="@label">
for="@("answer_" + Answer.Id)"
>
Is Correct Is Correct
</label> </label>
</div> </div>
@@ -61,16 +64,13 @@
<input <input
class="form-check-input" class="form-check-input"
type="radio" type="radio"
name="@("correct_answer_" + Question.Id)" id="@label"
id="@("answer_" + Answer.Id)"
checked="@Answer.Correct" checked="@Answer.Correct"
@onchange="@(() => handleOneAnswerChange())" @onchange="@(() => handleOneAnswerChange())"
> >
<label <label
class="form-check-label" class="form-check-label" for="@label">
for="@("answer_" + Answer.Id)"
>
Is Correct Is Correct
</label> </label>
</div> </div>

View File

@@ -145,10 +145,11 @@
</div> </div>
<QuizSettings /> <QuizSettings />
@foreach(var question in quizContext.Quiz.Questions) @foreach(var (question, i) in quizContext.Quiz.Questions.Select((q, i) => (q, i)))
{ {
<QuizQuestionForm <QuizQuestionForm
@key="@question.Id" @key="@question.Id"
Index="i"
Question="question" Question="question"
UpdateQuestion="updateQuestion" UpdateQuestion="updateQuestion"
/> />

View File

@@ -4,6 +4,8 @@
[Parameter, EditorRequired] [Parameter, EditorRequired]
public LocalQuizQuestion Question { get; set; } = default!; public LocalQuizQuestion Question { get; set; } = default!;
[Parameter, EditorRequired] [Parameter, EditorRequired]
public int Index { get; set; } = default!;
[Parameter, EditorRequired]
public Action<LocalQuizQuestion> UpdateQuestion { get; set; } = (_) => {}; public Action<LocalQuizQuestion> UpdateQuestion { get; set; } = (_) => {};
protected override void OnParametersSet() protected override void OnParametersSet()
@@ -57,8 +59,7 @@
{ {
if(quizContext.Quiz != null) if(quizContext.Quiz != null)
{ {
var newAnswer = new LocalQuizQuestionAnswer var newAnswer = new LocalQuizQuestionAnswer();
{ Id = Guid.NewGuid().ToString() };
answers = answers.Append(newAnswer); answers = answers.Append(newAnswer);
UpdateQuestion(Question with { Answers = answers }); UpdateQuestion(Question with { Answers = answers });
@@ -70,28 +71,25 @@
if(quizContext.Quiz != null) if(quizContext.Quiz != null)
{ {
var newAnswer = new LocalQuizQuestionAnswer
{ Id = Guid.NewGuid().ToString() };
answers = answers.Take(Question.Answers.Count() - 1); answers = answers.Take(Question.Answers.Count() - 1);
UpdateQuestion(Question with { Answers = answers }); UpdateQuestion(Question with { Answers = answers });
} }
} }
private void saveAnswer(LocalQuizQuestionAnswer newAnswer) private void saveAnswer(LocalQuizQuestionAnswer newAnswer, int index)
{ {
if(questionType == QuestionType.MULTIPLE_CHOICE && newAnswer.Correct) if(questionType == QuestionType.MULTIPLE_CHOICE && newAnswer.Correct)
{ {
answers = answers.Select(a => answers = answers.Select((a, i) =>
a.Id == newAnswer.Id index == i
? newAnswer ? newAnswer
: a with { Correct = false } : a with { Correct = false }
).ToArray(); ).ToArray();
} }
else else
{ {
answers = answers.Select(a => answers = answers.Select((a, i) =>
a.Id == newAnswer.Id index == i
? newAnswer ? newAnswer
: a : a
).ToArray(); ).ToArray();
@@ -173,10 +171,12 @@
+ Add Answer + Add Answer
</button> </button>
@foreach(var answer in answers) @foreach(var (answer, i) in answers.Select((a, i) => (a, i)))
{ {
<EditableQuizAnswer <EditableQuizAnswer
Answer="answer" Answer="answer"
AnswerIndex="i"
QuestionIndex="Index"
SaveAnswer="saveAnswer" SaveAnswer="saveAnswer"
Question="Question" Question="Question"
/> />

View File

@@ -4,7 +4,6 @@ namespace LocalModels;
public record LocalQuizQuestion public record LocalQuizQuestion
{ {
public ulong? CanvasId { get; set; }
public string Id { get; set; } = ""; public string Id { get; set; } = "";
public string Text { get; init; } = string.Empty; public string Text { get; init; } = string.Empty;
public string HtmlText => Markdig.Markdown.ToHtml(Text); public string HtmlText => Markdig.Markdown.ToHtml(Text);

View File

@@ -4,9 +4,6 @@ namespace LocalModels;
public record LocalQuizQuestionAnswer public record LocalQuizQuestionAnswer
{ {
public ulong? CanvasId { get; init; }
public string Id { get; init; } = string.Empty;
//correct gets a weight of 100 in canvas //correct gets a weight of 100 in canvas
public bool Correct { get; init; } public bool Correct { get; init; }
public string Text { get; init; } = string.Empty; public string Text { get; init; } = string.Empty;

View File

@@ -79,7 +79,7 @@ public class CanvasQuizService
LocalQuiz localQuiz 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 Task.WhenAll(tasks);
await hackFixRedundantAssignments(canvasCourseId); await hackFixRedundantAssignments(canvasCourseId);
} }
@@ -107,45 +107,17 @@ public class CanvasQuizService
await Task.WhenAll(tasks); await Task.WhenAll(tasks);
} }
private Func<LocalQuizQuestion, Task<LocalQuizQuestion>> createQuestion( private Func<LocalQuizQuestion, Task> createQuestion(
ulong canvasCourseId, ulong canvasCourseId,
ulong canvasQuizId, ulong canvasQuizId
LocalQuiz localQuiz
) )
{ {
return async (question) => return async (question) => await createQuestionOnly(canvasCourseId, canvasQuizId, 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
};
};
} }
private async Task<CanvasQuizQuestion> createQuestionOnly( private async Task<CanvasQuizQuestion> createQuestionOnly(
ulong canvasCourseId, ulong canvasCourseId,
ulong canvasQuizId, ulong canvasQuizId,
LocalQuiz localQuiz,
LocalQuizQuestion q LocalQuizQuestion q
) )
{ {