mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
new syncronization for quizzes, add only
This commit is contained in:
@@ -23,7 +23,7 @@ this is my description in markdown
|
||||
DueAt = DateTime.MaxValue,
|
||||
ShuffleAnswers = true,
|
||||
OneQuestionAtATime = false,
|
||||
LocalAssignmentGroupId = "someId",
|
||||
LocalAssignmentGroupName = "someId",
|
||||
AllowedAttempts = -1,
|
||||
Questions = new LocalQuizQuestion[] { }
|
||||
};
|
||||
@@ -54,7 +54,7 @@ this is my description in markdown
|
||||
DueAt = DateTime.MaxValue,
|
||||
ShuffleAnswers = true,
|
||||
OneQuestionAtATime = false,
|
||||
LocalAssignmentGroupId = "someId",
|
||||
LocalAssignmentGroupName = "someId",
|
||||
AllowedAttempts = -1,
|
||||
Questions = new LocalQuizQuestion[]
|
||||
{
|
||||
@@ -114,7 +114,7 @@ b) false
|
||||
DueAt = DateTime.MaxValue,
|
||||
ShuffleAnswers = true,
|
||||
OneQuestionAtATime = false,
|
||||
LocalAssignmentGroupId = "someId",
|
||||
LocalAssignmentGroupName = "someId",
|
||||
AllowedAttempts = -1,
|
||||
Questions = new LocalQuizQuestion[]
|
||||
{
|
||||
|
||||
@@ -188,7 +188,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="col-6">
|
||||
<textarea
|
||||
id="description"
|
||||
class="form-control h-100"
|
||||
@@ -197,7 +197,7 @@
|
||||
@oninput="handleNewDescription"
|
||||
/>
|
||||
</div>
|
||||
<div class="col" @key="descriptionForPreview">
|
||||
<div class="col-6" @key="descriptionForPreview">
|
||||
@(preview)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -102,6 +102,11 @@
|
||||
quizContext.SaveQuiz(newQuiz);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task addToCanvas()
|
||||
{
|
||||
await quizContext.AddQuizToCanvas();
|
||||
}
|
||||
}
|
||||
|
||||
<Modal @ref="modal" OnHide="() => quizContext.Quiz = null" >
|
||||
@@ -166,6 +171,13 @@
|
||||
<Footer>
|
||||
<ConfirmationModal Label="Delete" Class="btn btn-danger" OnConfirm="deleteQuiz" />
|
||||
|
||||
|
||||
<button
|
||||
class="btn btn-outline-secondary"
|
||||
@onclick="addToCanvas"
|
||||
>
|
||||
Add to Canvas
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-primary"
|
||||
@onclick="() => modal?.Hide()"
|
||||
|
||||
@@ -155,7 +155,7 @@ public class CoursePlanner
|
||||
};
|
||||
|
||||
|
||||
var newModules = await LocalCourse.EnsureAllModulesExistInCanvas(
|
||||
var newModules = await LocalCourse.CreateAllModules(
|
||||
canvasId,
|
||||
CanvasModules,
|
||||
canvas
|
||||
@@ -163,15 +163,16 @@ public class CoursePlanner
|
||||
LocalCourse = LocalCourse with { Modules = newModules };
|
||||
CanvasModules = await canvas.Modules.GetModules(canvasId);
|
||||
|
||||
await LocalCourse.SortCanvasModules(canvasId, CanvasModules, canvas);
|
||||
await LocalCourse.SortCanvasModulesByLocalOrder(canvasId, CanvasModules, canvas);
|
||||
CanvasModulesItems = await canvas.Modules.GetAllModulesItems(canvasId, CanvasModules);
|
||||
|
||||
LocalCourse = await LocalCourse.SyncModulesWithCanvasData(canvasId, CanvasModules, canvas);
|
||||
LocalCourse = await LocalCourse.GetCanvasIdsForLocalModules(canvasId, canvas);
|
||||
|
||||
LocalCourse = await LocalCourse.SyncAssignmentsWithCanvas(canvasId, CanvasAssignments, canvas);
|
||||
CanvasAssignments = await canvas.Assignments.GetAll(canvasId);
|
||||
|
||||
LocalCourse = await LocalCourse.SyncQuizzesWithCanvas(canvasId, CanvasQuizzes, canvas);
|
||||
CanvasModulesItems = await canvas.Modules.GetAllModulesItems(canvasId, CanvasModules);
|
||||
LocalCourse = await LocalCourse.SyncQuizzesWithCanvas(CanvasQuizzes, canvas);
|
||||
|
||||
await LocalCourse.SyncModuleItemsWithCanvas(canvasId, CanvasModulesItems, canvas);
|
||||
CanvasModulesItems = await canvas.Modules.GetAllModulesItems(canvasId, CanvasModules);
|
||||
@@ -181,6 +182,7 @@ public class CoursePlanner
|
||||
Console.WriteLine("done syncing with canvas\n");
|
||||
}
|
||||
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
LocalCourse = null;
|
||||
|
||||
@@ -1,17 +1,26 @@
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using LocalModels;
|
||||
using Management.Planner;
|
||||
using Management.Services;
|
||||
using Management.Services.Canvas;
|
||||
|
||||
public class QuizEditorContext
|
||||
{
|
||||
public event Action? StateHasChanged;
|
||||
private CoursePlanner planner { get; }
|
||||
|
||||
public QuizEditorContext(CoursePlanner planner)
|
||||
public QuizEditorContext(CoursePlanner planner, CanvasService canvas,
|
||||
MyLogger<CanvasAssignmentService> logger)
|
||||
{
|
||||
this.planner = planner;
|
||||
this.canvas = canvas;
|
||||
this.logger = logger;
|
||||
}
|
||||
public event Action? StateHasChanged;
|
||||
private CoursePlanner planner { get; }
|
||||
private CanvasService canvas { get; }
|
||||
|
||||
|
||||
private LocalQuiz? _quiz;
|
||||
private readonly MyLogger<CanvasAssignmentService> logger;
|
||||
|
||||
public LocalQuiz? Quiz
|
||||
{
|
||||
get => _quiz;
|
||||
@@ -62,6 +71,49 @@ public class QuizEditorContext
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task AddQuizToCanvas()
|
||||
{
|
||||
logger.Log("started to add quiz to canvas");
|
||||
if(Quiz == null)
|
||||
{
|
||||
logger.Log("cannot add null quiz to canvas");
|
||||
return;
|
||||
}
|
||||
await planner.LoadCanvasData();
|
||||
if(planner.CanvasQuizzes == null)
|
||||
{
|
||||
logger.Log("cannot add quiz to canvas, failed to retrieve current quizzes");
|
||||
return;
|
||||
}
|
||||
if(planner.LocalCourse == null)
|
||||
{
|
||||
logger.Log("cannot add quiz to canvas, no course stored in planner");
|
||||
return;
|
||||
}
|
||||
var updatedQuiz = await planner.LocalCourse.AddQuizToCanvas(Quiz, planner.CanvasQuizzes, canvas);
|
||||
|
||||
|
||||
|
||||
var courseCanvasId = planner.LocalCourse.Settings.CanvasId;
|
||||
var currentModule = getCurrentModule(Quiz, planner.LocalCourse);
|
||||
|
||||
await canvas.CreateModuleItem(
|
||||
(ulong)courseCanvasId,
|
||||
(ulong)currentModule.CanvasId,
|
||||
updatedQuiz.Name,
|
||||
"Quiz",
|
||||
(ulong)updatedQuiz.CanvasId
|
||||
);
|
||||
|
||||
await planner.LocalCourse.Modules.First().SortModuleItems(
|
||||
(ulong)courseCanvasId,
|
||||
(ulong)currentModule.CanvasId,
|
||||
canvas
|
||||
);
|
||||
logger.Log("added quiz to canvas");
|
||||
}
|
||||
|
||||
private static LocalModule getCurrentModule(LocalQuiz newQuiz, LocalCourse course)
|
||||
{
|
||||
return course.Modules.First(m => m.Quizzes.Select(q => q.Id).Contains(newQuiz.Id))
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Management.Planner;
|
||||
|
||||
public static partial class ModuleSyncronizationExtensions
|
||||
{
|
||||
internal static async Task<IEnumerable<LocalModule>> EnsureAllModulesExistInCanvas(
|
||||
internal static async Task<IEnumerable<LocalModule>> CreateAllModules(
|
||||
this LocalCourse localCourse,
|
||||
ulong canvasCourseId,
|
||||
IEnumerable<CanvasModule> canvasModules,
|
||||
@@ -32,7 +32,7 @@ public static partial class ModuleSyncronizationExtensions
|
||||
return newModules ?? throw new Exception("Error ensuring all modules exist in canvas");
|
||||
}
|
||||
|
||||
internal static async Task SortCanvasModules(
|
||||
internal static async Task SortCanvasModulesByLocalOrder(
|
||||
this LocalCourse localCourse,
|
||||
ulong canvasId,
|
||||
IEnumerable<CanvasModule> canvasModules,
|
||||
@@ -53,14 +53,13 @@ public static partial class ModuleSyncronizationExtensions
|
||||
}
|
||||
}
|
||||
|
||||
internal static async Task<LocalCourse> SyncModulesWithCanvasData(
|
||||
internal static async Task<LocalCourse> GetCanvasIdsForLocalModules(
|
||||
this LocalCourse localCourse,
|
||||
ulong canvasId,
|
||||
IEnumerable<CanvasModule> canvasModules,
|
||||
CanvasService canvas
|
||||
)
|
||||
{
|
||||
canvasModules = await canvas.Modules.GetModules(canvasId);
|
||||
var canvasModules = await canvas.Modules.GetModules(canvasId);
|
||||
return localCourse with
|
||||
{
|
||||
Modules = localCourse.Modules.Select(m =>
|
||||
@@ -70,35 +69,38 @@ public static partial class ModuleSyncronizationExtensions
|
||||
})
|
||||
};
|
||||
}
|
||||
internal static async Task SortModuleItems(
|
||||
|
||||
public static async Task SortModuleItems(
|
||||
this LocalModule localModule,
|
||||
ulong canvasId,
|
||||
ulong moduleCanvasId,
|
||||
IEnumerable<CanvasModuleItem> canvasModuleItems,
|
||||
CanvasService canvas
|
||||
)
|
||||
{
|
||||
var localItemsWithCorrectOrder = localModule.Assignments
|
||||
.OrderBy(a => a.DueAt)
|
||||
.Select((a, i) => (Assignment: a, Position: i + 1));
|
||||
|
||||
var canvasContentIdsByCurrentPosition =
|
||||
canvasModuleItems.ToDictionary(item => item.Position, item => item.ContentId)
|
||||
?? new Dictionary<int, ulong?>();
|
||||
var canvasModuleItems = await canvas.Modules.GetModuleItems(canvasId, moduleCanvasId);
|
||||
var moduleItemsInCorrectOrder = canvasModuleItems
|
||||
.OrderBy(i => i.ContentDetails?.DueAt)
|
||||
.Select((a, i) => (Item: a, Position: i + 1));
|
||||
// var localItemsWithCorrectOrder = localModule.Assignments
|
||||
// .OrderBy(a => a.DueAt)
|
||||
// .Select((a, i) => (Assignment: a, Position: i + 1));
|
||||
|
||||
foreach (var (localAssignment, position) in localItemsWithCorrectOrder)
|
||||
// var canvasContentIdsByCurrentPosition =
|
||||
// canvasModuleItems.ToDictionary(item => item.Position, item => item.ContentId)
|
||||
// ?? new Dictionary<int, ulong?>();
|
||||
|
||||
foreach (var (moduleItem, position) in moduleItemsInCorrectOrder)
|
||||
{
|
||||
var itemIsInCorrectOrder =
|
||||
canvasContentIdsByCurrentPosition.ContainsKey(position)
|
||||
&& canvasContentIdsByCurrentPosition[position] == localAssignment.CanvasId;
|
||||
var itemIsInCorrectOrder = moduleItem.Position == position;
|
||||
|
||||
var currentCanvasItem = canvasModuleItems.First(i => i.ContentId == localAssignment.CanvasId);
|
||||
// var currentCanvasItem = canvasModuleItems.First(i => i.ContentId == moduleItem.CanvasId);
|
||||
if (!itemIsInCorrectOrder)
|
||||
{
|
||||
await canvas.UpdateModuleItem(
|
||||
canvasId,
|
||||
moduleCanvasId,
|
||||
currentCanvasItem with
|
||||
moduleItem with
|
||||
{
|
||||
Position = position
|
||||
}
|
||||
@@ -182,7 +184,7 @@ public static partial class ModuleSyncronizationExtensions
|
||||
? await canvas.Modules.GetModuleItems(canvasId, moduleCanvasId)
|
||||
: canvasModulesItems[moduleCanvasId];
|
||||
|
||||
await localModule.SortModuleItems(canvasId, moduleCanvasId, canvasModuleItems, canvas);
|
||||
await localModule.SortModuleItems(canvasId, moduleCanvasId, canvas);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,47 +16,45 @@ public static partial class QuizSyncronizationExtensions
|
||||
|
||||
internal static async Task<LocalCourse> SyncQuizzesWithCanvas(
|
||||
this LocalCourse localCourse,
|
||||
ulong canvasId,
|
||||
IEnumerable<CanvasQuiz> canvasQuizzes,
|
||||
CanvasService canvas
|
||||
)
|
||||
{
|
||||
var moduleTasks = localCourse.Modules.Select(async m =>
|
||||
{
|
||||
return localCourse;
|
||||
// var moduleTasks = localCourse.Modules.Select(async m =>
|
||||
// {
|
||||
|
||||
var quizTasks = m.Quizzes
|
||||
.Select(
|
||||
async (q) => q.DueAt > DateTime.Now
|
||||
? await localCourse.SyncQuizToCanvas(canvasId, q, canvasQuizzes, canvas)
|
||||
: q
|
||||
);
|
||||
var quizzes = await Task.WhenAll(quizTasks);
|
||||
return m with { Quizzes = quizzes };
|
||||
});
|
||||
// var quizTasks = m.Quizzes
|
||||
// .Select(
|
||||
// async (q) => q.DueAt > DateTime.Now
|
||||
// ? await localCourse.AddQuizToCanvas(q, canvasQuizzes, canvas)
|
||||
// : q
|
||||
// );
|
||||
// var quizzes = await Task.WhenAll(quizTasks);
|
||||
// return m with { Quizzes = quizzes };
|
||||
// });
|
||||
|
||||
var modules = await Task.WhenAll(moduleTasks);
|
||||
return localCourse with { Modules = modules };
|
||||
// var modules = await Task.WhenAll(moduleTasks);
|
||||
// return localCourse with { Modules = modules };
|
||||
}
|
||||
|
||||
internal static async Task<LocalQuiz> SyncQuizToCanvas(
|
||||
public static async Task<LocalQuiz> AddQuizToCanvas(
|
||||
this LocalCourse localCourse,
|
||||
ulong canvasCourseId,
|
||||
LocalQuiz localQuiz,
|
||||
IEnumerable<CanvasQuiz> canvasQuizzes,
|
||||
CanvasService canvas
|
||||
)
|
||||
{
|
||||
var isCreated = localQuiz.QuizIsCreated(canvasQuizzes);
|
||||
var canvasAssignmentGroupId = localQuiz.GetCanvasAssignmentGroupId(localCourse.Settings.AssignmentGroups);
|
||||
if (isCreated)
|
||||
if (localCourse.Settings.CanvasId == null)
|
||||
{
|
||||
// TODO write update
|
||||
}
|
||||
else
|
||||
{
|
||||
return await canvas.Quizzes.Create(canvasCourseId, localQuiz, canvasAssignmentGroupId);
|
||||
Console.WriteLine("Cannot add quiz to canvas without canvas course id");
|
||||
return localQuiz;
|
||||
}
|
||||
ulong courseCanvasId = (ulong)localCourse.Settings.CanvasId;
|
||||
|
||||
return localQuiz;
|
||||
var canvasAssignmentGroupId = localQuiz.GetCanvasAssignmentGroupId(localCourse.Settings.AssignmentGroups);
|
||||
|
||||
var canvasQuizId = await canvas.Quizzes.Create(courseCanvasId, localQuiz, canvasAssignmentGroupId);
|
||||
return localQuiz with { CanvasId = canvasQuizId };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,5 +16,13 @@ public record CanvasModuleItem(
|
||||
// [OptIn]
|
||||
[property: JsonPropertyName("completion_requirement")]
|
||||
CanvasCompletionRequirement? CompletionRequirement,
|
||||
[property: JsonPropertyName("published")] bool? Published
|
||||
[property: JsonPropertyName("published")] bool? Published,
|
||||
[property: JsonPropertyName("content_details")] CanvasModuleItemContentDetails? ContentDetails
|
||||
);
|
||||
|
||||
public record CanvasModuleItemContentDetails(
|
||||
[property: JsonPropertyName("due_at")] DateTime? DueAt,
|
||||
[property: JsonPropertyName("lock_at")] DateTime? LockAt,
|
||||
[property: JsonPropertyName("points_possible")] double PointsPossible,
|
||||
[property: JsonPropertyName("locked_for_user")] bool LockedForUser
|
||||
);
|
||||
@@ -57,7 +57,7 @@ public class CanvasModuleService
|
||||
|
||||
public async Task<IEnumerable<CanvasModuleItem>> GetModuleItems(ulong courseId, ulong moduleId)
|
||||
{
|
||||
var url = $"courses/{courseId}/modules/{moduleId}/items";
|
||||
var url = $"courses/{courseId}/modules/{moduleId}/items?include[]=content_details";
|
||||
var request = new RestRequest(url);
|
||||
var (items, response) = await webRequestor.GetAsync<IEnumerable<CanvasModuleItem>>(request);
|
||||
if (items == null)
|
||||
|
||||
@@ -36,7 +36,7 @@ public class CanvasQuizService
|
||||
);
|
||||
}
|
||||
|
||||
public async Task<LocalQuiz> Create(
|
||||
public async Task<ulong> Create(
|
||||
ulong canvasCourseId,
|
||||
LocalQuiz localQuiz,
|
||||
ulong? canvasAssignmentGroupId
|
||||
@@ -69,19 +69,15 @@ public class CanvasQuizService
|
||||
if (canvasQuiz == null)
|
||||
throw new Exception("Created canvas quiz was null");
|
||||
|
||||
var updatedQuiz = localQuiz with { CanvasId = canvasQuiz.Id };
|
||||
var quizWithQuestions = await CreateQuizQuestions(canvasCourseId, updatedQuiz);
|
||||
|
||||
return quizWithQuestions;
|
||||
await CreateQuizQuestions(canvasCourseId, localQuiz);
|
||||
return canvasQuiz.Id;
|
||||
}
|
||||
|
||||
public async Task<LocalQuiz> CreateQuizQuestions(ulong canvasCourseId, LocalQuiz localQuiz)
|
||||
public async Task CreateQuizQuestions(ulong canvasCourseId, LocalQuiz localQuiz)
|
||||
{
|
||||
var tasks = localQuiz.Questions.Select(createQuestion(canvasCourseId, localQuiz)).ToArray();
|
||||
var updatedQuestions = await Task.WhenAll(tasks);
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
await hackFixRedundantAssignments(canvasCourseId);
|
||||
return localQuiz with { Questions = updatedQuestions };
|
||||
}
|
||||
|
||||
private async Task hackFixRedundantAssignments(ulong canvasCourseId)
|
||||
|
||||
Reference in New Issue
Block a user