diff --git a/Management/Features/Configuration/CoursePlanner.cs b/Management/Features/Configuration/CoursePlanner.cs index 277f07b..9f245fa 100644 --- a/Management/Features/Configuration/CoursePlanner.cs +++ b/Management/Features/Configuration/CoursePlanner.cs @@ -118,7 +118,8 @@ public class CoursePlanner IEnumerable CanvasModules, Dictionary> CanvasModulesItems, IEnumerable canvasQuizzes, - IEnumerable canvasAssignmentGroups + IEnumerable canvasAssignmentGroups, + IEnumerable canvasPages )> LoadCanvasData() { @@ -139,12 +140,13 @@ public class CoursePlanner CanvasQuizzes = await quizzesTask; CanvasModules = await modulesTask; CanvasAssignmentGroups = await assignmentGroupsTask; + CanvasPages = await coursePagesTask; CanvasModulesItems = await canvas.Modules.GetAllModulesItems(canvasId, CanvasModules); LoadingCanvasData = false; StateHasChanged?.Invoke(); - return (CanvasAssignments, CanvasModules, CanvasModulesItems, CanvasQuizzes, CanvasAssignmentGroups); + return (CanvasAssignments, CanvasModules, CanvasModulesItems, CanvasQuizzes, CanvasAssignmentGroups, CanvasPages); } public async Task CreateModule(LocalModule newModule) diff --git a/Management/Features/Configuration/PageEditorContext.cs b/Management/Features/Configuration/PageEditorContext.cs index f2e1d7b..4479fc8 100644 --- a/Management/Features/Configuration/PageEditorContext.cs +++ b/Management/Features/Configuration/PageEditorContext.cs @@ -89,46 +89,49 @@ public class PageEditorContext( 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 canvasQuizId = await planner.LocalCourse.AddQuizToCanvas(Quiz, canvas); + if (planner.CanvasPages == null) + { + logger.Log("cannot add page to canvas, failed to retrieve current pages"); + return; + } + if (planner.LocalCourse == null) + { + logger.Log("cannot add page to canvas, no course stored in planner"); + return; + } + var canvasPageId = await planner.LocalCourse.AddPageToCanvas(Page, canvas); - // var courseCanvasId = planner.LocalCourse.Settings.CanvasId; - // if (courseCanvasId == null) - // { - // logger.Log("was able to add quiz to canvas, but errored while making module item. CourseCanvasId is null"); - // return; - // } + var courseCanvasId = planner.LocalCourse.Settings.CanvasId; + if (courseCanvasId == null) + { + logger.Log("was able to add page to canvas, but errored while making module item. CourseCanvasId is null"); + return; + } - // var canvasModule = getCurrentCanvasModule(Quiz, planner.LocalCourse); + var canvasModule = getCurrentCanvasModule(Page, planner.LocalCourse); - // await canvas.CreateModuleItem( - // (ulong)courseCanvasId, - // canvasModule.Id, - // Quiz.Name, - // "Quiz", - // (ulong)canvasQuizId - // ); + if(canvasPageId != null) + { + await canvas.CreateModuleItem( + (ulong)courseCanvasId, + canvasModule.Id, + Page.Name, + "Page", + (ulong)canvasPageId + ); - // await planner.LocalCourse.Modules.First().SortModuleItems( - // (ulong)courseCanvasId, - // canvasModule.Id, - // canvas - // ); - // logger.Log($"finished adding quiz {Quiz.Name} to canvas"); + await planner.LocalCourse.Modules.First().SortModuleItems( + (ulong)courseCanvasId, + canvasModule.Id, + canvas + ); + } + logger.Log($"finished adding page {Page.Name} to canvas"); } - public async Task UpdateInCanvas(string pageId) + public async Task UpdateInCanvas(ulong pageId) { } diff --git a/Management/Features/Configuration/Synchronization/ModuleSyncronizationExtensions.cs b/Management/Features/Configuration/Synchronization/ModuleSyncronizationExtensions.cs index c099b4a..4f85190 100644 --- a/Management/Features/Configuration/Synchronization/ModuleSyncronizationExtensions.cs +++ b/Management/Features/Configuration/Synchronization/ModuleSyncronizationExtensions.cs @@ -37,10 +37,17 @@ public static partial class ModuleSyncronizationExtensions CanvasService canvas ) { - var canvasModuleItems = await canvas.Modules.GetModuleItems(canvasId, moduleCanvasId); var moduleItemsInCorrectOrder = canvasModuleItems - .OrderBy(i => i.ContentDetails?.DueAt) + .OrderBy(canvasItem => { + if(canvasItem.Type == "Page") + { + var localPage = localModule.Pages.FirstOrDefault(p => p.Name == canvasItem.Title); + if(localPage != null) + return localPage.DueAt; + } + return canvasItem.ContentDetails?.DueAt; + }) .Select((a, i) => (Item: a, Position: i + 1)); foreach (var (moduleItem, position) in moduleItemsInCorrectOrder) diff --git a/Management/Features/Configuration/Synchronization/PageSynchronizationExtension.cs b/Management/Features/Configuration/Synchronization/PageSynchronizationExtension.cs new file mode 100644 index 0000000..25bf87d --- /dev/null +++ b/Management/Features/Configuration/Synchronization/PageSynchronizationExtension.cs @@ -0,0 +1,22 @@ +using LocalModels; +using Management.Services.Canvas; + +public static class PageSynchronizationExtension +{ + public static async Task AddPageToCanvas( + this LocalCourse localCourse, + LocalCoursePage localPage, + CanvasService canvas + ) + { + if (localCourse.Settings.CanvasId == null) + { + Console.WriteLine("Cannot add page to canvas without canvas course id"); + return null; + } + ulong courseCanvasId = (ulong)localCourse.Settings.CanvasId; + + var canvasPageId = await canvas.Pages.Create(courseCanvasId, localPage); + return canvasPageId; + } +} diff --git a/Management/Models/CanvasModels/Pages/CanvasPage.cs b/Management/Models/CanvasModels/Pages/CanvasPage.cs index 55b6fdd..52abe17 100644 --- a/Management/Models/CanvasModels/Pages/CanvasPage.cs +++ b/Management/Models/CanvasModels/Pages/CanvasPage.cs @@ -1,7 +1,7 @@ namespace CanvasModel.Pages; public record CanvasPage ( - [property: JsonPropertyName("page_id")] string PageId, + [property: JsonPropertyName("page_id")] ulong PageId, [property: JsonPropertyName("url")] string Url, [property: JsonPropertyName("title")] string Title, [property: JsonPropertyName("published")] bool Published, diff --git a/Management/Services/Canvas/CanvasCoursePageService.cs b/Management/Services/Canvas/CanvasCoursePageService.cs index 3f0f382..bf265ad 100644 --- a/Management/Services/Canvas/CanvasCoursePageService.cs +++ b/Management/Services/Canvas/CanvasCoursePageService.cs @@ -30,7 +30,7 @@ public class CanvasCoursePageService( } - public async Task Create( + public async Task Create( ulong canvasCourseId, LocalCoursePage localCourse ) diff --git a/Management/Services/Canvas/CanvasService.cs b/Management/Services/Canvas/CanvasService.cs index aa8c2fa..668f919 100644 --- a/Management/Services/Canvas/CanvasService.cs +++ b/Management/Services/Canvas/CanvasService.cs @@ -123,4 +123,30 @@ public class CanvasService( if (newItem == null) throw new Exception("something went wrong updating module item"); } + public async Task CreateModuleItem( + ulong canvasCourseId, + ulong canvasModuleId, + string title, + string type, + string contentId + ) + { + logger.Log($"creating new module item {title}"); + var url = $"courses/{canvasCourseId}/modules/{canvasModuleId}/items"; + var body = new + { + module_item = new + { + title, + type = type.ToString(), + content_id = contentId, + } + }; + var request = new RestRequest(url); + request.AddBody(body); + + var (newItem, _response) = await webRequestor.PostAsync(request); + if (newItem == null) + throw new Exception("something went wrong updating module item with string content id"); + } }