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()
|
var quiz = new LocalQuiz()
|
||||||
{
|
{
|
||||||
Id = "string",
|
Id = "string",
|
||||||
CanvasId = 8324723,
|
|
||||||
Name = "Test Quiz",
|
Name = "Test Quiz",
|
||||||
Description = @"
|
Description = @"
|
||||||
# quiz description
|
# quiz description
|
||||||
@@ -31,13 +30,12 @@ this is my description in markdown
|
|||||||
var markdown = quiz.ToMarkdown();
|
var markdown = quiz.ToMarkdown();
|
||||||
|
|
||||||
markdown.Should().Contain("Id: string");
|
markdown.Should().Contain("Id: string");
|
||||||
markdown.Should().Contain("CanvasId: 8324723");
|
|
||||||
markdown.Should().Contain("Name: Test Quiz");
|
markdown.Should().Contain("Name: Test Quiz");
|
||||||
markdown.Should().Contain(quiz.Description);
|
markdown.Should().Contain(quiz.Description);
|
||||||
markdown.Should().Contain("LockAtDueDate: true");
|
markdown.Should().Contain("LockAtDueDate: true");
|
||||||
markdown.Should().Contain("ShuffleAnswers: true");
|
markdown.Should().Contain("ShuffleAnswers: true");
|
||||||
markdown.Should().Contain("OneQuestionAtATime: false");
|
markdown.Should().Contain("OneQuestionAtATime: false");
|
||||||
markdown.Should().Contain("LocalAssignmentGroupId: someId");
|
markdown.Should().Contain("LocalAssignmentGroupName: someId");
|
||||||
markdown.Should().Contain("AllowedAttempts: -1");
|
markdown.Should().Contain("AllowedAttempts: -1");
|
||||||
}
|
}
|
||||||
[Test]
|
[Test]
|
||||||
@@ -46,7 +44,6 @@ this is my description in markdown
|
|||||||
var quiz = new LocalQuiz()
|
var quiz = new LocalQuiz()
|
||||||
{
|
{
|
||||||
Id = "string",
|
Id = "string",
|
||||||
CanvasId = 8324723,
|
|
||||||
Name = "Test Quiz",
|
Name = "Test Quiz",
|
||||||
Description = "desc",
|
Description = "desc",
|
||||||
LockAtDueDate = true,
|
LockAtDueDate = true,
|
||||||
@@ -60,7 +57,6 @@ this is my description in markdown
|
|||||||
{
|
{
|
||||||
new LocalQuizQuestion()
|
new LocalQuizQuestion()
|
||||||
{
|
{
|
||||||
CanvasId = 32423,
|
|
||||||
Id = "someid",
|
Id = "someid",
|
||||||
Points = 2,
|
Points = 2,
|
||||||
Text = @"`some type` of question
|
Text = @"`some type` of question
|
||||||
@@ -74,8 +70,8 @@ lines
|
|||||||
QuestionType = QuestionType.MULTIPLE_CHOICE,
|
QuestionType = QuestionType.MULTIPLE_CHOICE,
|
||||||
Answers = new LocalQuizQuestionAnswer[]
|
Answers = new LocalQuizQuestionAnswer[]
|
||||||
{
|
{
|
||||||
new LocalQuizQuestionAnswer() { CanvasId = 324, Id = "asdfa", Correct = true, Text = "true" },
|
new LocalQuizQuestionAnswer() { Id = "asdfa", Correct = true, Text = "true" },
|
||||||
new LocalQuizQuestionAnswer() { CanvasId = 32544, Id = "wef", Correct = false, Text = "false" + Environment.NewLine +Environment.NewLine + "endline" },
|
new LocalQuizQuestionAnswer() { Id = "wef", Correct = false, Text = "false" + Environment.NewLine +Environment.NewLine + "endline" },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,7 +92,6 @@ lines
|
|||||||
b) false
|
b) false
|
||||||
|
|
||||||
endline
|
endline
|
||||||
---
|
|
||||||
";
|
";
|
||||||
markdown.Should().Contain(expectedQuestionString);
|
markdown.Should().Contain(expectedQuestionString);
|
||||||
}
|
}
|
||||||
@@ -106,7 +101,6 @@ b) false
|
|||||||
var quiz = new LocalQuiz()
|
var quiz = new LocalQuiz()
|
||||||
{
|
{
|
||||||
Id = "string",
|
Id = "string",
|
||||||
CanvasId = 8324723,
|
|
||||||
Name = "Test Quiz",
|
Name = "Test Quiz",
|
||||||
Description = "desc",
|
Description = "desc",
|
||||||
LockAtDueDate = true,
|
LockAtDueDate = true,
|
||||||
@@ -120,16 +114,15 @@ b) false
|
|||||||
{
|
{
|
||||||
new LocalQuizQuestion()
|
new LocalQuizQuestion()
|
||||||
{
|
{
|
||||||
CanvasId = 3253,
|
|
||||||
Id = "somesdid",
|
Id = "somesdid",
|
||||||
Text = "oneline question",
|
Text = "oneline question",
|
||||||
Points = 1,
|
Points = 1,
|
||||||
QuestionType = QuestionType.MULTIPLE_ANSWERS,
|
QuestionType = QuestionType.MULTIPLE_ANSWERS,
|
||||||
Answers = new LocalQuizQuestionAnswer[]
|
Answers = new LocalQuizQuestionAnswer[]
|
||||||
{
|
{
|
||||||
new LocalQuizQuestionAnswer() { CanvasId = 3324, Id = "asdfsa", Correct = true, Text = "true" },
|
new LocalQuizQuestionAnswer() { Id = "asdfsa", Correct = true, Text = "true" },
|
||||||
new LocalQuizQuestionAnswer() { CanvasId = 325344, Id = "wsef", Correct = true, Text = "false"},
|
new LocalQuizQuestionAnswer() { Id = "wsef", Correct = true, Text = "false"},
|
||||||
new LocalQuizQuestionAnswer() { CanvasId = 3253244, Id = "ws5ef", Correct = false, Text = "neither"},
|
new LocalQuizQuestionAnswer() { Id = "ws5ef", Correct = false, Text = "neither"},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -141,7 +134,6 @@ oneline question
|
|||||||
[*] true
|
[*] true
|
||||||
[*] false
|
[*] false
|
||||||
[ ] neither
|
[ ] neither
|
||||||
---
|
|
||||||
";
|
";
|
||||||
markdown.Should().Contain(expectedQuestionString);
|
markdown.Should().Contain(expectedQuestionString);
|
||||||
}
|
}
|
||||||
@@ -203,9 +195,7 @@ lines
|
|||||||
*a) true
|
*a) true
|
||||||
b) false
|
b) false
|
||||||
|
|
||||||
endline
|
endline";
|
||||||
---
|
|
||||||
";
|
|
||||||
|
|
||||||
var quiz = LocalQuiz.ParseMarkdown(rawMarkdownQuiz);
|
var quiz = LocalQuiz.ParseMarkdown(rawMarkdownQuiz);
|
||||||
var firstQuestion = quiz.Questions.First();
|
var firstQuestion = quiz.Questions.First();
|
||||||
|
|||||||
@@ -94,7 +94,6 @@ public static class CoursePlannerExtensions
|
|||||||
Assignments = module.Assignments
|
Assignments = module.Assignments
|
||||||
.Select((a) => a.validateAssignmentForCanvasId(canvasAssignments))
|
.Select((a) => a.validateAssignmentForCanvasId(canvasAssignments))
|
||||||
.ToArray(),
|
.ToArray(),
|
||||||
Quizzes = module.Quizzes.Select((s) => s.validateQuizForCanvasId(canvasQuizzes)).ToArray()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!moduleIdInCanvas)
|
if (!moduleIdInCanvas)
|
||||||
@@ -124,20 +123,6 @@ public static class CoursePlannerExtensions
|
|||||||
return assignment;
|
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)
|
public static LocalAssignment validateSubmissionTypes(this LocalAssignment assignment)
|
||||||
{
|
{
|
||||||
var containsDiscussion =
|
var containsDiscussion =
|
||||||
|
|||||||
@@ -91,19 +91,30 @@ public class QuizEditorContext
|
|||||||
logger.Log("cannot add quiz to canvas, no course stored in planner");
|
logger.Log("cannot add quiz to canvas, no course stored in planner");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var updatedQuiz = await planner.LocalCourse.AddQuizToCanvas(Quiz, canvas);
|
var canvasQuizId = await planner.LocalCourse.AddQuizToCanvas(Quiz, canvas);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var courseCanvasId = planner.LocalCourse.Settings.CanvasId;
|
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);
|
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(
|
await canvas.CreateModuleItem(
|
||||||
(ulong)courseCanvasId,
|
(ulong)courseCanvasId,
|
||||||
(ulong)currentModule.CanvasId,
|
(ulong)currentModule.CanvasId,
|
||||||
updatedQuiz.Name,
|
Quiz.Name,
|
||||||
"Quiz",
|
"Quiz",
|
||||||
(ulong)updatedQuiz.CanvasId
|
(ulong)canvasQuizId
|
||||||
);
|
);
|
||||||
|
|
||||||
await planner.LocalCourse.Modules.First().SortModuleItems(
|
await planner.LocalCourse.Modules.First().SortModuleItems(
|
||||||
@@ -111,7 +122,7 @@ public class QuizEditorContext
|
|||||||
(ulong)currentModule.CanvasId,
|
(ulong)currentModule.CanvasId,
|
||||||
canvas
|
canvas
|
||||||
);
|
);
|
||||||
logger.Log("added quiz to canvas");
|
logger.Log($"finished adding quiz {Quiz.Name} to canvas");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static LocalModule getCurrentModule(LocalQuiz newQuiz, LocalCourse course)
|
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;
|
return anyUpdated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ public static partial class QuizSyncronizationExtensions
|
|||||||
{
|
{
|
||||||
public static bool QuizIsCreated(this LocalQuiz localQuiz, IEnumerable<CanvasQuiz> canvasQuizzes)
|
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,
|
this LocalCourse localCourse,
|
||||||
LocalQuiz localQuiz,
|
LocalQuiz localQuiz,
|
||||||
CanvasService canvas
|
CanvasService canvas
|
||||||
@@ -23,13 +23,13 @@ public static partial class QuizSyncronizationExtensions
|
|||||||
if (localCourse.Settings.CanvasId == null)
|
if (localCourse.Settings.CanvasId == null)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Cannot add quiz to canvas without canvas course id");
|
Console.WriteLine("Cannot add quiz to canvas without canvas course id");
|
||||||
return localQuiz;
|
return null;
|
||||||
}
|
}
|
||||||
ulong courseCanvasId = (ulong)localCourse.Settings.CanvasId;
|
ulong courseCanvasId = (ulong)localCourse.Settings.CanvasId;
|
||||||
|
|
||||||
var canvasAssignmentGroupId = localQuiz.GetCanvasAssignmentGroupId(localCourse.Settings.AssignmentGroups);
|
var canvasAssignmentGroupId = localQuiz.GetCanvasAssignmentGroupId(localCourse.Settings.AssignmentGroups);
|
||||||
|
|
||||||
var canvasQuizId = await canvas.Quizzes.Create(courseCanvasId, localQuiz, canvasAssignmentGroupId);
|
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 record LocalQuiz
|
||||||
{
|
{
|
||||||
public required string Id { get; init; }
|
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 Name { get; init; }
|
||||||
public required string Description { get; init; }
|
public required string Description { get; init; }
|
||||||
public bool LockAtDueDate { get; init; } = true;
|
public bool LockAtDueDate { get; init; } = true;
|
||||||
@@ -44,7 +44,6 @@ public record LocalQuiz
|
|||||||
|
|
||||||
return $@"Name: {Name}
|
return $@"Name: {Name}
|
||||||
Id: {Id}
|
Id: {Id}
|
||||||
CanvasId: {CanvasId}
|
|
||||||
LockAtDueDate: {LockAtDueDate.ToString().ToLower()}
|
LockAtDueDate: {LockAtDueDate.ToString().ToLower()}
|
||||||
LockAt: {LockAt}
|
LockAt: {LockAt}
|
||||||
DueAt: {DueAt}
|
DueAt: {DueAt}
|
||||||
|
|||||||
@@ -69,13 +69,17 @@ public class CanvasQuizService
|
|||||||
if (canvasQuiz == null)
|
if (canvasQuiz == null)
|
||||||
throw new Exception("Created canvas quiz was null");
|
throw new Exception("Created canvas quiz was null");
|
||||||
|
|
||||||
await CreateQuizQuestions(canvasCourseId, localQuiz);
|
await CreateQuizQuestions(canvasCourseId, canvasQuiz.Id, localQuiz);
|
||||||
return canvasQuiz.Id;
|
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 Task.WhenAll(tasks);
|
||||||
await hackFixRedundantAssignments(canvasCourseId);
|
await hackFixRedundantAssignments(canvasCourseId);
|
||||||
}
|
}
|
||||||
@@ -105,12 +109,13 @@ public class CanvasQuizService
|
|||||||
|
|
||||||
private Func<LocalQuizQuestion, Task<LocalQuizQuestion>> createQuestion(
|
private Func<LocalQuizQuestion, Task<LocalQuizQuestion>> createQuestion(
|
||||||
ulong canvasCourseId,
|
ulong canvasCourseId,
|
||||||
|
ulong canvasQuizId,
|
||||||
LocalQuiz localQuiz
|
LocalQuiz localQuiz
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return async (question) =>
|
return async (question) =>
|
||||||
{
|
{
|
||||||
var newQuestion = await createQuestionOnly(canvasCourseId, localQuiz, question);
|
var newQuestion = await createQuestionOnly(canvasCourseId, canvasQuizId, localQuiz, question);
|
||||||
|
|
||||||
var answersWithIds = question.Answers
|
var answersWithIds = question.Answers
|
||||||
.Select((answer, i) =>
|
.Select((answer, i) =>
|
||||||
@@ -139,11 +144,12 @@ public class CanvasQuizService
|
|||||||
|
|
||||||
private async Task<CanvasQuizQuestion> createQuestionOnly(
|
private async Task<CanvasQuizQuestion> createQuestionOnly(
|
||||||
ulong canvasCourseId,
|
ulong canvasCourseId,
|
||||||
|
ulong canvasQuizId,
|
||||||
LocalQuiz localQuiz,
|
LocalQuiz localQuiz,
|
||||||
LocalQuizQuestion q
|
LocalQuizQuestion q
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var url = $"courses/{canvasCourseId}/quizzes/{localQuiz.CanvasId}/questions";
|
var url = $"courses/{canvasCourseId}/quizzes/{canvasQuizId}/questions";
|
||||||
var answers = q.Answers
|
var answers = q.Answers
|
||||||
.Select(a => new { answer_html = a.HtmlText, answer_weight = a.Correct ? 100 : 0 })
|
.Select(a => new { answer_html = a.HtmlText, answer_weight = a.Correct ? 100 : 0 })
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|||||||
Reference in New Issue
Block a user