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]
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<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
{
@@ -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())"
>
<label
class="form-check-label"
for="@("answer_" + Answer.Id)"
>
class="form-check-label" for="@label">
Is Correct
</label>
</div>
@@ -61,16 +64,13 @@
<input
class="form-check-input"
type="radio"
name="@("correct_answer_" + Question.Id)"
id="@("answer_" + Answer.Id)"
id="@label"
checked="@Answer.Correct"
@onchange="@(() => handleOneAnswerChange())"
>
<label
class="form-check-label"
for="@("answer_" + Answer.Id)"
>
class="form-check-label" for="@label">
Is Correct
</label>
</div>

View File

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

View File

@@ -4,6 +4,8 @@
[Parameter, EditorRequired]
public LocalQuizQuestion Question { get; set; } = default!;
[Parameter, EditorRequired]
public int Index { get; set; } = default!;
[Parameter, EditorRequired]
public Action<LocalQuizQuestion> 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
</button>
@foreach(var answer in answers)
@foreach(var (answer, i) in answers.Select((a, i) => (a, i)))
{
<EditableQuizAnswer
Answer="answer"
SaveAnswer="saveAnswer"
AnswerIndex="i"
QuestionIndex="Index"
SaveAnswer="saveAnswer"
Question="Question"
/>
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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<LocalQuizQuestion, Task<LocalQuizQuestion>> createQuestion(
private Func<LocalQuizQuestion, Task> 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<CanvasQuizQuestion> createQuestionOnly(
ulong canvasCourseId,
ulong canvasQuizId,
LocalQuiz localQuiz,
LocalQuizQuestion q
)
{