updated quiz form edtor

This commit is contained in:
2023-08-12 10:03:48 -06:00
parent 93c1c754cd
commit 4de6122549
9 changed files with 421 additions and 15 deletions

View File

@@ -0,0 +1,88 @@
@code {
[Parameter, EditorRequired]
public LocalQuizQuestionAnswer Answer { get; set; } = default!;
[Parameter, EditorRequired]
public LocalQuizQuestion Question { get; set; } = default!;
[Parameter, EditorRequired]
public Action<LocalQuizQuestionAnswer> SaveAnswer { get; set; } = (_) => {};
private string _text { get; set; } = string.Empty;
private string text
{
get => _text;
set
{
_text = value;
SaveAnswer(Answer with { Text = _text });
}
}
protected override void OnParametersSet()
{
if(_text == string.Empty)
_text = Answer.Text;
base.OnParametersSet();
}
private void handleOneAnswerChange()
{
SaveAnswer(Answer with {Correct = !Answer.Correct});
}
}
<div class="row">
<div class="col-auto my-auto">
@if(Question.QuestionType == QuestionType.MULTIPLE_ANSWERS)
{
<div class="form-check form-switch">
<input
class="form-check-input"
type="checkbox"
role="switch"
id="@("answer_" + Answer.Id)"
checked="@Answer.Correct"
@onchange="@(() => handleOneAnswerChange())"
>
<label
class="form-check-label"
for="@("answer_" + Answer.Id)"
>
Is Correct
</label>
</div>
}
@if(Question.QuestionType == QuestionType.MULTIPLE_CHOICE)
{
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="@("correct_answer_" + Question.Id)"
id="@("answer_" + Answer.Id)"
checked="@Answer.Correct"
@onchange="@(() => handleOneAnswerChange())"
>
<label
class="form-check-label"
for="@("answer_" + Answer.Id)"
>
Is Correct
</label>
</div>
}
</div>
<div class="col">
<div class="m-1">
<textarea
class="form-control"
@bind="text"
@bind:event="oninput"
/>
</div>
</div>
</div>

View File

@@ -3,6 +3,8 @@
@code {
private Modal? modal { get; set; }
private string name { get; set; } = "";
private string description { get; set; } = "";
protected override void OnInitialized()
{
@@ -12,7 +14,9 @@
{
if (quizContext.Quiz != null)
{
//initialize
name = quizContext.Quiz.Name;
description = quizContext.Quiz.Description;
modal?.Show();
this.InvokeAsync(this.StateHasChanged);
}
@@ -21,6 +25,76 @@
{
quizContext.StateHasChanged -= reload;
}
private void addQuestion()
{
if(quizContext.Quiz != null)
{
var newQuestion = new LocalQuizQuestion
{
Id = Guid.NewGuid().ToString()
};
var newQuiz = quizContext.Quiz with
{
Questions = quizContext.Quiz.Questions.Append(newQuestion).ToArray()
};
quizContext.SaveQuiz(newQuiz);
}
}
private void removeQuestion()
{
if(quizContext.Quiz != null)
{
var newQuestion = new LocalQuizQuestion();
var newQuiz = quizContext.Quiz with
{
Questions = quizContext.Quiz.Questions.Take(quizContext.Quiz.Questions.Count() - 1)
};
quizContext.SaveQuiz(newQuiz);
}
}
private void updateQuestion(LocalQuizQuestion updatedQuestion)
{
if(quizContext.Quiz != null)
{
var newQuestions = quizContext.Quiz.Questions.Select(q =>
q.Id == updatedQuestion.Id
? updatedQuestion
: q
);
var newQuiz = quizContext.Quiz with
{
Questions = newQuestions,
};
quizContext.SaveQuiz(newQuiz);
}
}
private void handleNewName(ChangeEventArgs e)
{
if(quizContext.Quiz != null)
{
var newQuiz = quizContext.Quiz with
{
Name = e.Value?.ToString() ?? ""
};
quizContext.SaveQuiz(newQuiz);
}
}
private void handleNewDescription(ChangeEventArgs e)
{
if(quizContext.Quiz != null)
{
var newQuiz = quizContext.Quiz with
{
Description = e.Value?.ToString() ?? ""
};
quizContext.SaveQuiz(newQuiz);
}
}
}
<Modal @ref="modal" OnHide="() => quizContext.Quiz = null" >
@@ -28,7 +102,51 @@
@quizContext.Quiz?.Name
</Title>
<Body>
@if(quizContext.Quiz != null)
{
<div class="m-1">
<label class="form-label">
Name
</label>
<input
class="form-control"
@bind="name"
@oninput="handleNewName"
/>
</div>
<div class="m-1">
<label class="form-label">
Description
</label>
<textarea
class="form-control"
@bind="description"
@oninput="handleNewDescription"
/>
</div>
@foreach(var question in quizContext.Quiz.Questions)
{
<QuizQuestionForm
@key="@question.Id"
Question="question"
UpdateQuestion="updateQuestion"
/>
}
<button
class="btn btn-outline-danger"
@onclick="removeQuestion"
>
- question
</button>
<button
class="btn btn-outline-primary"
@onclick="addQuestion"
>
+ question
</button>
}
</Body>
<Footer>
<button

View File

@@ -0,0 +1,157 @@
@inject QuizEditorContext quizContext
@code {
[Parameter, EditorRequired]
public LocalQuizQuestion Question { get; set; } = default!;
[Parameter, EditorRequired]
public Action<LocalQuizQuestion> UpdateQuestion { get; set; } = (_) => {};
protected override void OnParametersSet()
{
if(questionType == string.Empty)
questionType = Question.QuestionType;
if(text == string.Empty)
text = Question.Text;
if(answers.Count() == 0)
answers = Question.Answers;
base.OnParametersSet();
}
private string text { get; set; } = string.Empty;
private string questionType { get; set; } = string.Empty;
private IEnumerable<LocalQuizQuestionAnswer> answers { get; set; } = Enumerable.Empty<LocalQuizQuestionAnswer>();
private void handleTypeUpdate(string type)
{
if(quizContext.Quiz != null)
{
questionType = type;
var newQuestion = Question with
{
QuestionType = questionType
};
UpdateQuestion(newQuestion);
}
}
private void addAnswer()
{
if(quizContext.Quiz != null)
{
var newAnswer = new LocalQuizQuestionAnswer
{ Id = Guid.NewGuid().ToString() };
answers = answers.Append(newAnswer);
UpdateQuestion(Question with { Answers = answers });
}
}
private void removeAnswer()
{
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)
{
if(questionType == QuestionType.MULTIPLE_CHOICE && newAnswer.Correct)
{
answers = answers.Select(a =>
a.Id == newAnswer.Id
? newAnswer
: a with { Correct = false }
).ToArray();
}
else
{
answers = answers.Select(a =>
a.Id == newAnswer.Id
? newAnswer
: a
).ToArray();
}
UpdateQuestion(Question with { Answers = answers });
}
}
<div class="my-1 p-2 border border-3 rounded">
<div class="row">
<div class="col">
<label
for="question_text"
class="form-label"
>
Question Text
</label>
<textarea
class="form-control"
id="question_text"
name="question_text"
@bind="text"
/>
</div>
<div class="col-auto">
@foreach(var typeOption in QuestionType.AllTypes)
{
<div class="form-check">
<input
class="form-check-input"
type="radio"
name="@(Question.Id + "question_type")"
id="@(Question.Id + typeOption)"
value="@typeOption"
checked="@(typeOption == questionType)"
@onchange="() => handleTypeUpdate(typeOption)"
>
<label
class="form-check-label"
for="@(Question.Id + typeOption)"
>
@typeOption
</label>
</div>
}
</div>
</div>
<div>Answers:</div>
@if(questionType == QuestionType.MULTIPLE_ANSWERS || questionType == QuestionType.MULTIPLE_CHOICE)
{
@foreach(var answer in answers)
{
<EditableQuizAnswer
Answer="answer"
SaveAnswer="saveAnswer"
Question="Question"
/>
}
<button
class="btn btn-outline-danger"
@onclick="removeAnswer"
>
- Remove Answer
</button>
<button
class="btn btn-outline-primary"
@onclick="addAnswer"
>
+ Add Answer
</button>
}
<div>
</div>
</div>

View File

@@ -45,6 +45,7 @@ public class QuizEditorContext
.ToArray();
planner.LocalCourse = planner.LocalCourse with { Modules = updatedModules };
Quiz = newQuiz;
}
}
}

View File

@@ -22,16 +22,16 @@ public static class SubmissionType
public static readonly string NONE = "none";
public static readonly IEnumerable<string> AllTypes = new string[]
{
SubmissionType.ONLINE_TEXT_ENTRY,
SubmissionType.ONLINE_UPLOAD,
SubmissionType.ONLINE_QUIZ,
SubmissionType.ON_PAPER,
SubmissionType.DISCUSSION_TOPIC,
SubmissionType.EXTERNAL_TOOL,
SubmissionType.ONLINE_URL,
SubmissionType.MEDIA_RECORDING,
SubmissionType.STUDENT_ANNOTATION,
SubmissionType.NONE,
ONLINE_TEXT_ENTRY,
ONLINE_UPLOAD,
ONLINE_QUIZ,
ON_PAPER,
DISCUSSION_TOPIC,
EXTERNAL_TOOL,
ONLINE_URL,
MEDIA_RECORDING,
STUDENT_ANNOTATION,
NONE,
};
}

View File

@@ -1,6 +1,9 @@
namespace LocalModels;
public record LocalQuiz
{
public string Id { get; init; } = ""; public ulong? CanvasId { get; init; } = null;
public string Id { get; init; } = "";
public ulong? CanvasId { get; init; } = null;
public string Name { get; init; } = "";
public string Description { get; init; } = "";
public bool LockAtDueDate { get; init; }
@@ -9,5 +12,6 @@ public record LocalQuiz
public bool ShuffleAnswers { get; init; }
public bool OneQuestionAtATime { get; init; }
public int AllowedAttempts { get; init; }
//quiz type
public IEnumerable<LocalQuizQuestion> Questions { get; init; } =
Enumerable.Empty<LocalQuizQuestion>();
}

View File

@@ -0,0 +1,28 @@
namespace LocalModels;
public record LocalQuizQuestion
{
public string Id { get; set; } = "";
public string Text { get; init; } = string.Empty;
public string QuestionType { get; init; } = string.Empty;
public int Points { get; init; }
public IEnumerable<LocalQuizQuestionAnswer> Answers { get; init; } =
Enumerable.Empty<LocalQuizQuestionAnswer>();
}
public static class QuestionType
{
public static readonly string MULTIPLE_ANSWERS = "multiple_answers";
public static readonly string MULTIPLE_CHOICE = "multiple_choice";
public static readonly string ESSAY = "essay";
public static readonly string SHORT_ANSWER = "short_answer";
// possible support for: calculated, file_upload, fill_in_multiple_blanks, matching, multiple_dropdowns, numerical, text_only, true_false,
public static readonly IEnumerable<string> AllTypes = new string[]
{
MULTIPLE_ANSWERS,
MULTIPLE_CHOICE,
ESSAY,
SHORT_ANSWER,
};
}

View File

@@ -0,0 +1,10 @@
namespace LocalModels;
public record LocalQuizQuestionAnswer
{
public string Id { get; set; } = string.Empty;
//correct gets a weight of 100 in canvas
public bool Correct { get; init; }
public string Text { get; init; } = string.Empty;
}

View File

@@ -6,7 +6,7 @@ public class YamlManager
{
public string CourseToYaml(LocalCourse course)
{
var serializer = new SerializerBuilder().Build();
var serializer = new SerializerBuilder().DisableAliases().Build();
var yaml = serializer.Serialize(course);
return yaml;