smarter canvas updates, only update when a change is detected

This commit is contained in:
2023-07-31 14:06:40 -06:00
parent e987df30d3
commit 70db40867c
13 changed files with 465 additions and 204 deletions

View File

@@ -4,11 +4,14 @@ using CanvasModel;
using LocalModels;
using CanvasModel.Assignments;
using CanvasModel.Modules;
using Management.Services.Canvas;
using System.Text.RegularExpressions;
public class CoursePlanner
{
private readonly YamlManager yamlManager;
private readonly CanvasService canvas;
public bool LoadingCanvasData { get; internal set; } = false;
public CoursePlanner(YamlManager yamlManager, CanvasService canvas)
{
@@ -29,7 +32,7 @@ public class CoursePlanner
return;
}
var verifiedCourse = verifyCourse(value);
var verifiedCourse = cleanupCourse(value);
// ignore initial load of course
if (_localCourse != null)
{
@@ -41,7 +44,7 @@ public class CoursePlanner
}
public event Action? StateHasChanged;
private LocalCourse verifyCourse(LocalCourse incomingCourse)
private LocalCourse cleanupCourse(LocalCourse incomingCourse)
{
var modulesWithUniqueAssignments = incomingCourse.Modules.Select(
module => module with { Assignments = module.Assignments.DistinctBy(a => a.id) }
@@ -53,8 +56,42 @@ public class CoursePlanner
};
}
public IEnumerable<CanvasAssignment>? CanvasAssignments { get; set; } = null;
public IEnumerable<CanvasModule>? CanvasModules { get; set; } = null;
private IEnumerable<CanvasAssignment>? canvasAssignments = null;
public IEnumerable<CanvasAssignment>? CanvasAssignments
{
get => canvasAssignments;
set
{
canvasAssignments = value;
StateHasChanged?.Invoke();
}
}
private IEnumerable<CanvasModule>? canvasModules = null;
public IEnumerable<CanvasModule>? CanvasModules
{
get => canvasModules;
set
{
canvasModules = value;
StateHasChanged?.Invoke();
}
}
public async Task LoadCanvasData()
{
LoadingCanvasData = true;
StateHasChanged?.Invoke();
Thread.Sleep(1000);
var canvasId =
LocalCourse?.CanvasId ?? throw new Exception("no canvas id found for selected course");
CanvasAssignments = await canvas.Assignments.GetAll(canvasId);
CanvasModules = await canvas.GetModules(canvasId);
LoadingCanvasData = false;
StateHasChanged?.Invoke();
}
public async Task SyncWithCanvas()
{
@@ -66,12 +103,18 @@ public class CoursePlanner
)
return;
LoadingCanvasData = true;
StateHasChanged?.Invoke();
var canvasId =
LocalCourse.CanvasId ?? throw new Exception("no course canvas id to sync with canvas");
await ensureAllModulesCreated(canvasId);
await reloadModules_UpdateLocalModulesWithNewId(canvasId);
await ensureAllAssignmentsCreated_updateIds(canvasId);
LoadingCanvasData = false;
StateHasChanged?.Invoke();
}
private async Task reloadModules_UpdateLocalModulesWithNewId(ulong canvasId)
@@ -139,21 +182,121 @@ public class CoursePlanner
throw new Exception(
"cannot create canvas assignment if local course is null or other values not set"
);
ulong canvasId = LocalCourse.CanvasId ?? throw new Exception("no canvas id to create course");
var canvasAssignment = await canvas.CreateAssignment(
courseId: canvasId,
name: localAssignment.name,
submissionTypes: localAssignment.submission_types,
description: localAssignment.description,
dueAt: localAssignment.due_at,
lockAt: localAssignment.lock_at,
pointsPossible: localAssignment.points_possible
var canvasAssignment = CanvasAssignments.FirstOrDefault(
ca => ca.Id == localAssignment.canvasId
);
string localHtmlDescription = getAssignmentDescriptionHtml(localAssignment);
if (canvasAssignment != null)
{
var assignmentNeedsUpdates = AssignmentNeedsUpdates(localAssignment);
if (assignmentNeedsUpdates)
{
await canvas.Assignments.Update(courseId: canvasId, localAssignment, localHtmlDescription);
}
return localAssignment;
}
else
{
var createdAssignment = await canvas.Assignments.Create(
courseId: canvasId,
name: localAssignment.name,
submissionTypes: localAssignment.submission_types,
description: localHtmlDescription,
dueAt: localAssignment.due_at,
lockAt: localAssignment.lock_at,
pointsPossible: localAssignment.points_possible
);
return localAssignment with
{
canvasId = createdAssignment.Id
};
}
}
private string getAssignmentDescriptionHtml(LocalAssignment localAssignment)
{
if (LocalCourse == null)
throw new Exception(
"cannot get assignment description if local course is null or other values not set"
);
return localAssignment.use_template
? AssignmentTemplate.GetHtml(
LocalCourse.AssignmentTemplates.First(t => t.Id == localAssignment.template_id),
localAssignment
)
: Markdig.Markdown.ToHtml(localAssignment.description);
}
public bool AssignmentNeedsUpdates(LocalAssignment localAssignment)
{
if (
LocalCourse == null
|| LocalCourse.CanvasId == null
|| CanvasAssignments == null
|| CanvasModules == null
)
throw new Exception(
"cannot check if assignment needs updates if local course is null or other values not set"
);
var canvasAssignment = CanvasAssignments.First(ca => ca.Id == localAssignment.canvasId);
var localHtmlDescription = getAssignmentDescriptionHtml(localAssignment);
var canvasHtmlDescription = canvasAssignment.Description;
canvasHtmlDescription = Regex.Replace(canvasHtmlDescription, "<script.*script>", "");
canvasHtmlDescription = Regex.Replace(canvasHtmlDescription, "<link .*\">", "");
var dueDatesSame = canvasAssignment.DueAt == localAssignment.due_at;
var descriptionSame = canvasHtmlDescription == localHtmlDescription;
var nameSame = canvasAssignment.Name == localAssignment.name;
var lockDateSame = canvasAssignment.LockAt == localAssignment.lock_at;
var pointsSame = canvasAssignment.PointsPossible == localAssignment.points_possible;
var submissionTypesSame = canvasAssignment.SubmissionTypes.SequenceEqual(
localAssignment.submission_types.Select(t => t.ToString())
);
return localAssignment with
if (!dueDatesSame)
Console.WriteLine(
$"Due dates different for {localAssignment.name}, local: {localAssignment.due_at}, in canvas {canvasAssignment.DueAt}"
);
if (!descriptionSame)
{
canvasId = canvasAssignment.Id
};
Console.WriteLine($"descriptions different for {localAssignment.name}");
Console.WriteLine("Local Description:");
Console.WriteLine(localHtmlDescription);
Console.WriteLine("Canvas Description: ");
Console.WriteLine(canvasHtmlDescription);
}
if (!nameSame)
Console.WriteLine(
$"names different for {localAssignment.name}, local: {localAssignment.name}, in canvas {canvasAssignment.Name}"
);
if (!lockDateSame)
Console.WriteLine(
$"Lock Dates different for {localAssignment.name}, local: {localAssignment.lock_at}, in canvas {canvasAssignment.LockAt}"
);
if (!pointsSame)
Console.WriteLine(
$"Points different for {localAssignment.name}, local: {localAssignment.points_possible}, in canvas {canvasAssignment.PointsPossible}"
);
if (!submissionTypesSame)
Console.WriteLine(
$"Submission Types different for {localAssignment.name}, local: {JsonSerializer.Serialize(localAssignment.submission_types.Select(t => t.ToString()))}, in canvas {JsonSerializer.Serialize(canvasAssignment.SubmissionTypes)}"
);
return !nameSame
|| !dueDatesSame
|| !lockDateSame
|| !descriptionSame
|| !pointsSame
|| !submissionTypesSame;
}
public void Clear()