mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
can create and drag and drop pages
This commit is contained in:
148
Management/Features/Configuration/PageEditorContext.cs
Normal file
148
Management/Features/Configuration/PageEditorContext.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using CanvasModel.Modules;
|
||||
using LocalModels;
|
||||
using Management.Planner;
|
||||
using Management.Services;
|
||||
using Management.Services.Canvas;
|
||||
|
||||
public class PageEditorContext(
|
||||
CoursePlanner planner,
|
||||
CanvasService canvas,
|
||||
MyLogger<PageEditorContext> logger)
|
||||
{
|
||||
public event Action? StateHasChanged;
|
||||
private CoursePlanner planner { get; } = planner;
|
||||
private CanvasService canvas { get; } = canvas;
|
||||
private readonly MyLogger<PageEditorContext> logger = logger;
|
||||
|
||||
|
||||
private LocalCoursePage? _page;
|
||||
|
||||
private LocalModule? _module;
|
||||
|
||||
|
||||
public LocalCoursePage? Page
|
||||
{
|
||||
get => _page;
|
||||
set
|
||||
{
|
||||
if (_page == null && value != null && planner != null && planner.LocalCourse != null)
|
||||
{
|
||||
_module = getCurrentLocalModule(value, planner.LocalCourse);
|
||||
}
|
||||
_page = value;
|
||||
StateHasChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
public void SavePage(LocalCoursePage newPage)
|
||||
{
|
||||
if (planner.LocalCourse != null && _module != null && Page != null)
|
||||
{
|
||||
// use Page not newPage because it is the version that was last stored
|
||||
|
||||
var updatedModules = planner.LocalCourse.Modules
|
||||
.Select(
|
||||
m =>
|
||||
m.Name == _module.Name
|
||||
? m with
|
||||
{
|
||||
Pages = m.Pages
|
||||
.Select(p => p == Page ? newPage : p)
|
||||
.ToArray()
|
||||
}
|
||||
: m
|
||||
)
|
||||
.ToArray();
|
||||
|
||||
planner.LocalCourse = planner.LocalCourse with { Modules = updatedModules };
|
||||
Page = newPage;
|
||||
}
|
||||
}
|
||||
|
||||
public void DeletePage()
|
||||
{
|
||||
if (planner.LocalCourse != null && Page != null && _module != null)
|
||||
{
|
||||
// not dealing with canvas rn
|
||||
var updatedModules = planner.LocalCourse.Modules
|
||||
.Select(m => m.Name != _module.Name
|
||||
? m
|
||||
: m with
|
||||
{
|
||||
Pages = m.Pages.Where(p => p == Page).ToArray()
|
||||
}
|
||||
)
|
||||
.ToArray();
|
||||
|
||||
planner.LocalCourse = planner.LocalCourse with { Modules = updatedModules };
|
||||
Page = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task AddPageToCanvas()
|
||||
{
|
||||
// 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 canvasQuizId = await planner.LocalCourse.AddQuizToCanvas(Quiz, 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 canvasModule = getCurrentCanvasModule(Quiz, planner.LocalCourse);
|
||||
|
||||
// await canvas.CreateModuleItem(
|
||||
// (ulong)courseCanvasId,
|
||||
// canvasModule.Id,
|
||||
// Quiz.Name,
|
||||
// "Quiz",
|
||||
// (ulong)canvasQuizId
|
||||
// );
|
||||
|
||||
// await planner.LocalCourse.Modules.First().SortModuleItems(
|
||||
// (ulong)courseCanvasId,
|
||||
// canvasModule.Id,
|
||||
// canvas
|
||||
// );
|
||||
// logger.Log($"finished adding quiz {Quiz.Name} to canvas");
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static LocalModule getCurrentLocalModule(LocalCoursePage page, LocalCourse course)
|
||||
{
|
||||
return course.Modules.FirstOrDefault(
|
||||
m => m.Pages.Contains(page)
|
||||
)
|
||||
?? throw new Exception("could not find current module in page editor context");
|
||||
}
|
||||
|
||||
private CanvasModule getCurrentCanvasModule(LocalCoursePage quiz, LocalCourse course)
|
||||
{
|
||||
var localModule = getCurrentLocalModule(quiz, course);
|
||||
var canvasModule = planner.CanvasModules?.FirstOrDefault(m => m.Name == localModule.Name)
|
||||
?? throw new Exception($"error in page context, canvas module with name {localModule.Name} not found in planner");
|
||||
return canvasModule;
|
||||
}
|
||||
}
|
||||
@@ -5,26 +5,20 @@ using Management.Planner;
|
||||
using Management.Services;
|
||||
using Management.Services.Canvas;
|
||||
|
||||
public class QuizEditorContext
|
||||
public class QuizEditorContext(
|
||||
CoursePlanner planner,
|
||||
CanvasService canvas,
|
||||
MyLogger<QuizEditorContext> logger)
|
||||
{
|
||||
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 CoursePlanner planner { get; } = planner;
|
||||
private CanvasService canvas { get; } = canvas;
|
||||
private readonly MyLogger<QuizEditorContext> logger = logger;
|
||||
|
||||
|
||||
private LocalQuiz? _quiz;
|
||||
|
||||
private LocalModule? _module;
|
||||
private readonly MyLogger<CanvasAssignmentService> logger;
|
||||
|
||||
public LocalQuiz? Quiz
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace LocalModels;
|
||||
|
||||
public record LocalAssignment
|
||||
public record LocalAssignment: IModuleItem
|
||||
{
|
||||
private string _name = "";
|
||||
public string Name
|
||||
@@ -28,6 +28,7 @@ public record LocalAssignment
|
||||
public IEnumerable<RubricItem> Rubric { get; init; } = Array.Empty<RubricItem>();
|
||||
public int PointsPossible => Rubric.Sum(r => r.IsExtraCredit ? 0 : r.Points);
|
||||
|
||||
|
||||
public string GetDescriptionHtml()
|
||||
{
|
||||
return Markdig.Markdown.ToHtml(Description);
|
||||
|
||||
9
Management/Models/Local/IModuleItem.cs
Normal file
9
Management/Models/Local/IModuleItem.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
namespace LocalModels;
|
||||
|
||||
public interface IModuleItem
|
||||
{
|
||||
public string Name { get; init; }
|
||||
public DateTime DueAt { get; init; }
|
||||
|
||||
}
|
||||
@@ -1,14 +1,14 @@
|
||||
namespace LocalModels;
|
||||
|
||||
public record LocalCoursePage
|
||||
public record LocalCoursePage: IModuleItem
|
||||
{
|
||||
public required string Name { get; init; }
|
||||
public required string Text { get; set; }
|
||||
public DateTime? DueDateForOrdering { get; init; }
|
||||
public DateTime DueAt { get; init; }
|
||||
|
||||
public string ToMarkdown()
|
||||
{
|
||||
var printableDueDate = DueDateForOrdering.ToString()?.Replace('\u202F', ' ');
|
||||
var printableDueDate = DueAt.ToString()?.Replace('\u202F', ' ');
|
||||
var settingsMarkdown = $"Name: {Name}\n"
|
||||
+ $"DueDateForOrdering: {printableDueDate}\n"
|
||||
+ "---\n";
|
||||
@@ -20,9 +20,9 @@ public record LocalCoursePage
|
||||
var name = MarkdownUtils.ExtractLabelValue(rawSettings, "Name");
|
||||
var rawDate = MarkdownUtils.ExtractLabelValue(rawSettings, "DueDateForOrdering");
|
||||
|
||||
DateTime? parsedDate = DateTime.TryParse(rawDate, out DateTime parsedDueAt)
|
||||
DateTime parsedDate = DateTime.TryParse(rawDate, out DateTime parsedDueAt)
|
||||
? parsedDueAt
|
||||
: null;
|
||||
: throw new LocalPageMarkdownParseException($"could not parse due date: {rawDate}");
|
||||
|
||||
|
||||
var text = pageMarkdown.Split("---\n")[1];
|
||||
@@ -30,7 +30,7 @@ public record LocalCoursePage
|
||||
return new LocalCoursePage
|
||||
{
|
||||
Name = name,
|
||||
DueDateForOrdering = parsedDate,
|
||||
DueAt = parsedDate,
|
||||
Text = text
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ using YamlDotNet.Serialization;
|
||||
|
||||
namespace LocalModels;
|
||||
|
||||
public record LocalQuiz
|
||||
public record LocalQuiz: IModuleItem
|
||||
{
|
||||
public required string Name { get; init; }
|
||||
public required string Description { get; init; }
|
||||
@@ -27,7 +27,7 @@ public record LocalQuiz
|
||||
.CanvasId;
|
||||
|
||||
public string GetDescriptionHtml() => Markdig.Markdown.ToHtml(Description);
|
||||
|
||||
|
||||
public string ToYaml()
|
||||
{
|
||||
var serializer = new SerializerBuilder().DisableAliases().Build();
|
||||
@@ -100,7 +100,7 @@ Description: {Description}
|
||||
|
||||
|
||||
var rawLockAt = extractLabelValue(settings, "LockAt");
|
||||
DateTime? lockAt = DateTime.TryParse(rawLockAt, out DateTime parsedLockAt)
|
||||
DateTime? lockAt = DateTime.TryParse(rawLockAt, out DateTime parsedLockAt)
|
||||
? parsedLockAt
|
||||
: null;
|
||||
|
||||
@@ -153,6 +153,6 @@ public class QuizMarkdownParseException : Exception
|
||||
{
|
||||
public QuizMarkdownParseException(string message): base(message)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,6 +131,7 @@ public class CourseMarkdownLoader
|
||||
}
|
||||
private async Task<IEnumerable<LocalCoursePage>> loadPagesFromPath(string modulePath)
|
||||
{
|
||||
using var activity = DiagnosticsConfig.Source?.StartActivity("loading Pages from file");
|
||||
var pagesPath = $"{modulePath}/pages";
|
||||
if (!Directory.Exists(pagesPath))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user