Files
canvasManagement/Management.Web/Shared/Components/AssignmentForm/AssignmentForm.razor

164 lines
4.1 KiB
Plaintext

@using Management.Web.Shared.Components
@inject CoursePlanner planner
@code {
[Parameter]
[EditorRequired]
public LocalAssignment Assignment
{
get;
set;
} = default!;
[Parameter]
[EditorRequired]
public bool Show { get; set; }
[Parameter]
public Action OnHide { get; set; } = () => { };
public Modal? AssignmentModal { get; set; } = null;
private bool useTemplate { get; set; } = false;
private string? templateId { get; set; }
public Dictionary<string, string> templateVariables { get; set; } =
new Dictionary<string, string>();
private string description { get; set; } = String.Empty;
private string name { get; set; } = String.Empty;
private bool lockAtDueDate { get; set; }
private IEnumerable<RubricItem> rubric { get; set; } = Enumerable.Empty<RubricItem>();
private IEnumerable<string> submissionTypes { get; set; } = Enumerable.Empty<string>();
protected override void OnParametersSet()
{
if(Show)
{
AssignmentModal?.Show();
}
name = Assignment.Name;
description = Assignment.Description;
lockAtDueDate = Assignment.LockAtDueDate;
rubric = Assignment.Rubric;
submissionTypes = Assignment.SubmissionTypes;
templateId = Assignment.TemplateId;
useTemplate = Assignment.UseTemplate;
templateVariables = Assignment.TemplateVariables;
}
private void submitHandler()
{
var totalRubricPoints = rubric
.Where(r => !r.Label.Contains(RubricItem.extraCredit))
.Select(s => s.Points)
.Sum();
var newAssignment = Assignment with
{
Name=name,
Description=description,
LockAtDueDate=lockAtDueDate,
Rubric=rubric,
PointsPossible=totalRubricPoints,
SubmissionTypes=submissionTypes,
UseTemplate=useTemplate,
TemplateId=templateId,
TemplateVariables=templateVariables,
};
if(planner.LocalCourse != null)
{
var currentModule = planner
.LocalCourse
.Modules
.First(m =>
m.Assignments
.Select(a => a.Id)
.Contains(Assignment.Id)
) ?? throw new Exception("could not find current module in assignment form");
var updatedModules = planner.LocalCourse.Modules.Select(m =>
m.Name == currentModule.Name
? currentModule with
{
Assignments=currentModule.Assignments.Select(a =>
a.Id == newAssignment.Id
? newAssignment
: a
).ToArray()
}
: m
).ToArray();
planner.LocalCourse = planner.LocalCourse with
{
Modules=updatedModules
};
}
AssignmentModal?.Hide();
}
private void updateRubric(IEnumerable<RubricItem> newRubric)
{
rubric = newRubric;
StateHasChanged();
}
private void SetTypes(IEnumerable<string> newTypes)
{
submissionTypes = newTypes;
StateHasChanged();
}
}
<Modal @ref="AssignmentModal" OnHide="@(() => OnHide())">
<Title>
@Assignment.Name
</Title>
<Body>
<form @onsubmit:preventDefault="true" @onsubmit="submitHandler">
<div class="m-1">
<label
class="form-label"
>
Name
</label>
<input
class="form-control"
@bind="name"
/>
</div>
<div class="m-1">
<AssignmentDescriptionEditor
@bind-Description="description"
@bind-UseTemplate="useTemplate"
@bind-TemplateId="templateId"
@bind-VariableValues="templateVariables"
/>
</div>
<div class="form-check m-1">
<input
class="form-check-input"
id="lockAtDueDate"
type="checkbox"
/>
<label
class="form-check-label"
for="lockAtDueDate"
@bind="lockAtDueDate"
>
Lock At Due Date
</label>
</div>
<RubricEditor Rubric="rubric" SetRubric="updateRubric" />
<SubmissionTypeSelector
Types="submissionTypes"
SetTypes="SetTypes"
/>
</form>
</Body>
<Footer>
<button class="btn btn-primary" @onclick="submitHandler">
Save
</button>
</Footer>
</Modal>