mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
got tests working without quiz id
This commit is contained in:
@@ -9,7 +9,6 @@ public class QuizMarkdownTests
|
||||
var quiz = new LocalQuiz()
|
||||
{
|
||||
Id = "string",
|
||||
CanvasId = 8324723,
|
||||
Name = "Test Quiz",
|
||||
Description = @"
|
||||
# quiz description
|
||||
@@ -31,13 +30,12 @@ this is my description in markdown
|
||||
var markdown = quiz.ToMarkdown();
|
||||
|
||||
markdown.Should().Contain("Id: string");
|
||||
markdown.Should().Contain("CanvasId: 8324723");
|
||||
markdown.Should().Contain("Name: Test Quiz");
|
||||
markdown.Should().Contain(quiz.Description);
|
||||
markdown.Should().Contain("LockAtDueDate: true");
|
||||
markdown.Should().Contain("ShuffleAnswers: true");
|
||||
markdown.Should().Contain("OneQuestionAtATime: false");
|
||||
markdown.Should().Contain("LocalAssignmentGroupId: someId");
|
||||
markdown.Should().Contain("LocalAssignmentGroupName: someId");
|
||||
markdown.Should().Contain("AllowedAttempts: -1");
|
||||
}
|
||||
[Test]
|
||||
@@ -46,7 +44,6 @@ this is my description in markdown
|
||||
var quiz = new LocalQuiz()
|
||||
{
|
||||
Id = "string",
|
||||
CanvasId = 8324723,
|
||||
Name = "Test Quiz",
|
||||
Description = "desc",
|
||||
LockAtDueDate = true,
|
||||
@@ -60,7 +57,6 @@ this is my description in markdown
|
||||
{
|
||||
new LocalQuizQuestion()
|
||||
{
|
||||
CanvasId = 32423,
|
||||
Id = "someid",
|
||||
Points = 2,
|
||||
Text = @"`some type` of question
|
||||
@@ -74,8 +70,8 @@ lines
|
||||
QuestionType = QuestionType.MULTIPLE_CHOICE,
|
||||
Answers = new LocalQuizQuestionAnswer[]
|
||||
{
|
||||
new LocalQuizQuestionAnswer() { CanvasId = 324, Id = "asdfa", Correct = true, Text = "true" },
|
||||
new LocalQuizQuestionAnswer() { CanvasId = 32544, Id = "wef", Correct = false, Text = "false" + Environment.NewLine +Environment.NewLine + "endline" },
|
||||
new LocalQuizQuestionAnswer() { Id = "asdfa", Correct = true, Text = "true" },
|
||||
new LocalQuizQuestionAnswer() { Id = "wef", Correct = false, Text = "false" + Environment.NewLine +Environment.NewLine + "endline" },
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,7 +92,6 @@ lines
|
||||
b) false
|
||||
|
||||
endline
|
||||
---
|
||||
";
|
||||
markdown.Should().Contain(expectedQuestionString);
|
||||
}
|
||||
@@ -106,7 +101,6 @@ b) false
|
||||
var quiz = new LocalQuiz()
|
||||
{
|
||||
Id = "string",
|
||||
CanvasId = 8324723,
|
||||
Name = "Test Quiz",
|
||||
Description = "desc",
|
||||
LockAtDueDate = true,
|
||||
@@ -120,16 +114,15 @@ b) false
|
||||
{
|
||||
new LocalQuizQuestion()
|
||||
{
|
||||
CanvasId = 3253,
|
||||
Id = "somesdid",
|
||||
Text = "oneline question",
|
||||
Points = 1,
|
||||
QuestionType = QuestionType.MULTIPLE_ANSWERS,
|
||||
Answers = new LocalQuizQuestionAnswer[]
|
||||
{
|
||||
new LocalQuizQuestionAnswer() { CanvasId = 3324, Id = "asdfsa", Correct = true, Text = "true" },
|
||||
new LocalQuizQuestionAnswer() { CanvasId = 325344, Id = "wsef", Correct = true, Text = "false"},
|
||||
new LocalQuizQuestionAnswer() { CanvasId = 3253244, Id = "ws5ef", Correct = false, Text = "neither"},
|
||||
new LocalQuizQuestionAnswer() { Id = "asdfsa", Correct = true, Text = "true" },
|
||||
new LocalQuizQuestionAnswer() { Id = "wsef", Correct = true, Text = "false"},
|
||||
new LocalQuizQuestionAnswer() { Id = "ws5ef", Correct = false, Text = "neither"},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -141,7 +134,6 @@ oneline question
|
||||
[*] true
|
||||
[*] false
|
||||
[ ] neither
|
||||
---
|
||||
";
|
||||
markdown.Should().Contain(expectedQuestionString);
|
||||
}
|
||||
@@ -203,9 +195,7 @@ lines
|
||||
*a) true
|
||||
b) false
|
||||
|
||||
endline
|
||||
---
|
||||
";
|
||||
endline";
|
||||
|
||||
var quiz = LocalQuiz.ParseMarkdown(rawMarkdownQuiz);
|
||||
var firstQuestion = quiz.Questions.First();
|
||||
|
||||
@@ -94,7 +94,6 @@ public static class CoursePlannerExtensions
|
||||
Assignments = module.Assignments
|
||||
.Select((a) => a.validateAssignmentForCanvasId(canvasAssignments))
|
||||
.ToArray(),
|
||||
Quizzes = module.Quizzes.Select((s) => s.validateQuizForCanvasId(canvasQuizzes)).ToArray()
|
||||
};
|
||||
|
||||
if (!moduleIdInCanvas)
|
||||
@@ -124,20 +123,6 @@ public static class CoursePlannerExtensions
|
||||
return assignment;
|
||||
}
|
||||
|
||||
private static LocalQuiz validateQuizForCanvasId(
|
||||
this LocalQuiz quiz,
|
||||
IEnumerable<CanvasQuiz> canvasQuizzes
|
||||
)
|
||||
{
|
||||
var assignmentIdInCanvas = canvasQuizzes.FirstOrDefault(cq => cq.Id == quiz.CanvasId) != null;
|
||||
if (!assignmentIdInCanvas)
|
||||
{
|
||||
Console.WriteLine($"no id in canvas for quiz, removing old canvas id: {quiz.Name}");
|
||||
return quiz with { CanvasId = null };
|
||||
}
|
||||
return quiz;
|
||||
}
|
||||
|
||||
public static LocalAssignment validateSubmissionTypes(this LocalAssignment assignment)
|
||||
{
|
||||
var containsDiscussion =
|
||||
|
||||
@@ -91,27 +91,38 @@ public class QuizEditorContext
|
||||
logger.Log("cannot add quiz to canvas, no course stored in planner");
|
||||
return;
|
||||
}
|
||||
var updatedQuiz = await planner.LocalCourse.AddQuizToCanvas(Quiz, canvas);
|
||||
var canvasQuizId = await planner.LocalCourse.AddQuizToCanvas(Quiz, canvas);
|
||||
|
||||
|
||||
|
||||
var courseCanvasId = planner.LocalCourse.Settings.CanvasId;
|
||||
if (courseCanvasId == null)
|
||||
{
|
||||
logger.Log("was able to add course to canvas, but errored while making module item. CourseCanvasId is null");
|
||||
return;
|
||||
}
|
||||
|
||||
var currentModule = getCurrentModule(Quiz, planner.LocalCourse);
|
||||
if (currentModule.CanvasId == null)
|
||||
{
|
||||
logger.Log("was able to add course to canvas, but errored while making module item. module canvasId is null");
|
||||
return;
|
||||
}
|
||||
|
||||
await canvas.CreateModuleItem(
|
||||
(ulong)courseCanvasId,
|
||||
(ulong)currentModule.CanvasId,
|
||||
updatedQuiz.Name,
|
||||
"Quiz",
|
||||
(ulong)updatedQuiz.CanvasId
|
||||
);
|
||||
(ulong)courseCanvasId,
|
||||
(ulong)currentModule.CanvasId,
|
||||
Quiz.Name,
|
||||
"Quiz",
|
||||
(ulong)canvasQuizId
|
||||
);
|
||||
|
||||
await planner.LocalCourse.Modules.First().SortModuleItems(
|
||||
(ulong)courseCanvasId,
|
||||
(ulong)currentModule.CanvasId,
|
||||
canvas
|
||||
);
|
||||
logger.Log("added quiz to canvas");
|
||||
logger.Log($"finished adding quiz {Quiz.Name} to canvas");
|
||||
}
|
||||
|
||||
private static LocalModule getCurrentModule(LocalQuiz newQuiz, LocalCourse course)
|
||||
|
||||
@@ -137,26 +137,6 @@ public static partial class ModuleSyncronizationExtensions
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var localQuiz in localModule.Quizzes.Where(q => q.DueAt > DateTime.Now))
|
||||
{
|
||||
|
||||
var canvasModuleItemContentIds = canvasModulesItems[moduleCanvasId].Select(i => i.ContentId);
|
||||
if (!canvasModuleItemContentIds.Contains(localQuiz.CanvasId))
|
||||
{
|
||||
var canvasAssignmentId =
|
||||
localQuiz.CanvasId
|
||||
?? throw new Exception("cannot create module item if quiz does not have canvas id");
|
||||
await canvas.CreateModuleItem(
|
||||
canvasId,
|
||||
moduleCanvasId,
|
||||
localQuiz.Name,
|
||||
"Quiz",
|
||||
canvasAssignmentId
|
||||
);
|
||||
anyUpdated = true;
|
||||
}
|
||||
}
|
||||
|
||||
return anyUpdated;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,10 @@ public static partial class QuizSyncronizationExtensions
|
||||
{
|
||||
public static bool QuizIsCreated(this LocalQuiz localQuiz, IEnumerable<CanvasQuiz> canvasQuizzes)
|
||||
{
|
||||
return canvasQuizzes.Any(q => q.Id == localQuiz.CanvasId);
|
||||
return canvasQuizzes.Any(q => q.Title == localQuiz.Name);
|
||||
}
|
||||
|
||||
public static async Task<LocalQuiz> AddQuizToCanvas(
|
||||
public static async Task<ulong?> AddQuizToCanvas(
|
||||
this LocalCourse localCourse,
|
||||
LocalQuiz localQuiz,
|
||||
CanvasService canvas
|
||||
@@ -23,13 +23,13 @@ public static partial class QuizSyncronizationExtensions
|
||||
if (localCourse.Settings.CanvasId == null)
|
||||
{
|
||||
Console.WriteLine("Cannot add quiz to canvas without canvas course id");
|
||||
return localQuiz;
|
||||
return null;
|
||||
}
|
||||
ulong courseCanvasId = (ulong)localCourse.Settings.CanvasId;
|
||||
|
||||
var canvasAssignmentGroupId = localQuiz.GetCanvasAssignmentGroupId(localCourse.Settings.AssignmentGroups);
|
||||
|
||||
var canvasQuizId = await canvas.Quizzes.Create(courseCanvasId, localQuiz, canvasAssignmentGroupId);
|
||||
return localQuiz with { CanvasId = canvasQuizId };
|
||||
return canvasQuizId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace LocalModels;
|
||||
public record LocalQuiz
|
||||
{
|
||||
public required string Id { get; init; }
|
||||
public ulong? CanvasId { get; init; } = null;
|
||||
// public ulong? CanvasId { get; init; } = null;
|
||||
public required string Name { get; init; }
|
||||
public required string Description { get; init; }
|
||||
public bool LockAtDueDate { get; init; } = true;
|
||||
@@ -44,7 +44,6 @@ public record LocalQuiz
|
||||
|
||||
return $@"Name: {Name}
|
||||
Id: {Id}
|
||||
CanvasId: {CanvasId}
|
||||
LockAtDueDate: {LockAtDueDate.ToString().ToLower()}
|
||||
LockAt: {LockAt}
|
||||
DueAt: {DueAt}
|
||||
|
||||
@@ -69,13 +69,17 @@ public class CanvasQuizService
|
||||
if (canvasQuiz == null)
|
||||
throw new Exception("Created canvas quiz was null");
|
||||
|
||||
await CreateQuizQuestions(canvasCourseId, localQuiz);
|
||||
await CreateQuizQuestions(canvasCourseId, canvasQuiz.Id, localQuiz);
|
||||
return canvasQuiz.Id;
|
||||
}
|
||||
|
||||
public async Task CreateQuizQuestions(ulong canvasCourseId, LocalQuiz localQuiz)
|
||||
public async Task CreateQuizQuestions(
|
||||
ulong canvasCourseId,
|
||||
ulong canvasQuizId,
|
||||
LocalQuiz localQuiz
|
||||
)
|
||||
{
|
||||
var tasks = localQuiz.Questions.Select(createQuestion(canvasCourseId, localQuiz)).ToArray();
|
||||
var tasks = localQuiz.Questions.Select(createQuestion(canvasCourseId, canvasQuizId, localQuiz)).ToArray();
|
||||
await Task.WhenAll(tasks);
|
||||
await hackFixRedundantAssignments(canvasCourseId);
|
||||
}
|
||||
@@ -105,12 +109,13 @@ public class CanvasQuizService
|
||||
|
||||
private Func<LocalQuizQuestion, Task<LocalQuizQuestion>> createQuestion(
|
||||
ulong canvasCourseId,
|
||||
ulong canvasQuizId,
|
||||
LocalQuiz localQuiz
|
||||
)
|
||||
{
|
||||
return async (question) =>
|
||||
{
|
||||
var newQuestion = await createQuestionOnly(canvasCourseId, localQuiz, question);
|
||||
var newQuestion = await createQuestionOnly(canvasCourseId, canvasQuizId, localQuiz, question);
|
||||
|
||||
var answersWithIds = question.Answers
|
||||
.Select((answer, i) =>
|
||||
@@ -139,11 +144,12 @@ public class CanvasQuizService
|
||||
|
||||
private async Task<CanvasQuizQuestion> createQuestionOnly(
|
||||
ulong canvasCourseId,
|
||||
ulong canvasQuizId,
|
||||
LocalQuiz localQuiz,
|
||||
LocalQuizQuestion q
|
||||
)
|
||||
{
|
||||
var url = $"courses/{canvasCourseId}/quizzes/{localQuiz.CanvasId}/questions";
|
||||
var url = $"courses/{canvasCourseId}/quizzes/{canvasQuizId}/questions";
|
||||
var answers = q.Answers
|
||||
.Select(a => new { answer_html = a.HtmlText, answer_weight = a.Correct ? 100 : 0 })
|
||||
.ToArray();
|
||||
|
||||
Reference in New Issue
Block a user