mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
got basic question and answer creation from canvas
This commit is contained in:
@@ -30,13 +30,13 @@ public class CanvasAssignmentService
|
||||
}
|
||||
|
||||
public async Task<LocalAssignment> Create(
|
||||
ulong courseId,
|
||||
ulong canvasCourseId,
|
||||
LocalAssignment localAssignment,
|
||||
string htmlDescription
|
||||
)
|
||||
{
|
||||
Console.WriteLine($"creating assignment: {localAssignment.Name}");
|
||||
var url = $"courses/{courseId}/assignments";
|
||||
var url = $"courses/{canvasCourseId}/assignments";
|
||||
var request = new RestRequest(url);
|
||||
var body = new CanvasAssignmentCreationRequest()
|
||||
{
|
||||
@@ -47,7 +47,6 @@ public class CanvasAssignmentService
|
||||
lock_at = localAssignment.LockAt,
|
||||
points_possible = localAssignment.PointsPossible
|
||||
};
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
var bodyObj = new { assignment = body };
|
||||
request.AddBody(bodyObj);
|
||||
var (canvasAssignment, response) = await webRequestor.PostAsync<CanvasAssignment>(request);
|
||||
@@ -56,7 +55,7 @@ public class CanvasAssignmentService
|
||||
|
||||
var updatedLocalAssignment = localAssignment with { CanvasId = canvasAssignment.Id };
|
||||
|
||||
await CreateRubric(courseId, updatedLocalAssignment);
|
||||
await CreateRubric(canvasCourseId, updatedLocalAssignment);
|
||||
|
||||
return updatedLocalAssignment;
|
||||
}
|
||||
@@ -75,7 +74,6 @@ public class CanvasAssignmentService
|
||||
lock_at = localAssignment.LockAt,
|
||||
points_possible = localAssignment.PointsPossible
|
||||
};
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
var bodyObj = new { assignment = body };
|
||||
request.AddBody(bodyObj);
|
||||
Console.WriteLine(url);
|
||||
@@ -145,7 +143,6 @@ public class CanvasAssignmentService
|
||||
var creationUrl = $"courses/{courseId}/rubrics";
|
||||
var rubricCreationRequest = new RestRequest(creationUrl);
|
||||
rubricCreationRequest.AddBody(body);
|
||||
rubricCreationRequest.AddHeader("Content-Type", "application/json");
|
||||
var (rubricCreationResponse, _) = await webRequestor.PostAsync<CanvasRubricCreationResponse>(
|
||||
rubricCreationRequest
|
||||
);
|
||||
@@ -160,7 +157,6 @@ public class CanvasAssignmentService
|
||||
var adjustmentUrl = $"courses/{courseId}/assignments/{localAssignment.CanvasId}";
|
||||
var pointAdjustmentRequest = new RestRequest(adjustmentUrl);
|
||||
pointAdjustmentRequest.AddBody(assignmentPointCorrectionBody);
|
||||
pointAdjustmentRequest.AddHeader("Content-Type", "application/json");
|
||||
var (_, _) = await webRequestor.PutAsync<CanvasAssignment>(pointAdjustmentRequest);
|
||||
}
|
||||
}
|
||||
|
||||
126
Management/Services/Canvas/CanvasQuizService.cs
Normal file
126
Management/Services/Canvas/CanvasQuizService.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using CanvasModel.Quizzes;
|
||||
using LocalModels;
|
||||
using RestSharp;
|
||||
|
||||
namespace Management.Services.Canvas;
|
||||
|
||||
public class CanvasQuizService
|
||||
{
|
||||
private readonly IWebRequestor webRequestor;
|
||||
private readonly CanvasServiceUtils utils;
|
||||
|
||||
public CanvasQuizService(IWebRequestor webRequestor, CanvasServiceUtils utils)
|
||||
{
|
||||
this.webRequestor = webRequestor;
|
||||
this.utils = utils;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CanvasQuiz>> GetAll(ulong courseId)
|
||||
{
|
||||
var url = $"courses/{courseId}/quizzes";
|
||||
var request = new RestRequest(url);
|
||||
var quizResponse = await utils.PaginatedRequest<IEnumerable<CanvasQuiz>>(request);
|
||||
return quizResponse.SelectMany(
|
||||
quizzes =>
|
||||
quizzes.Select(
|
||||
a => a with { DueAt = a.DueAt?.ToLocalTime(), LockAt = a.LockAt?.ToLocalTime() }
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public async Task<LocalQuiz> Create(ulong canvasCourseId, LocalQuiz localQuiz)
|
||||
{
|
||||
Console.WriteLine($"Creating Quiz ${localQuiz.Name}");
|
||||
|
||||
var url = $"courses/{canvasCourseId}/quizzes";
|
||||
var body = new
|
||||
{
|
||||
quiz = new
|
||||
{
|
||||
title = localQuiz.Name,
|
||||
description = localQuiz.Description,
|
||||
// assignment_group_id = "quiz", TODO: support specific assignment groups
|
||||
time_limit = localQuiz.TimeLimit,
|
||||
shuffle_answers = localQuiz.ShuffleAnswers,
|
||||
hide_results = localQuiz.HideResults,
|
||||
allowed_attempts = localQuiz.AllowedAttempts,
|
||||
one_question_at_a_time = true,
|
||||
cant_go_back = false,
|
||||
due_at = localQuiz.DueAt,
|
||||
lock_at = localQuiz.LockAt,
|
||||
}
|
||||
};
|
||||
var request = new RestRequest(url);
|
||||
request.AddBody(body);
|
||||
var (canvasQuiz, response) = await webRequestor.PostAsync<CanvasQuiz>(request);
|
||||
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;
|
||||
}
|
||||
|
||||
public async Task<LocalQuiz> CreateQuizQuestions(ulong canvasCourseId, LocalQuiz localQuiz)
|
||||
{
|
||||
var tasks = localQuiz.Questions
|
||||
.Select(
|
||||
async (question) =>
|
||||
{
|
||||
var newQuestion = await createQuestionOnly(canvasCourseId, localQuiz, question);
|
||||
|
||||
var answersWithIds = question.Answers
|
||||
.Select(answer =>
|
||||
{
|
||||
var canvasAnswer = newQuestion.Answers?.FirstOrDefault(ca => ca.Html == answer.Text);
|
||||
if (canvasAnswer == null)
|
||||
{
|
||||
Console.WriteLine(JsonSerializer.Serialize(newQuestion));
|
||||
Console.WriteLine(JsonSerializer.Serialize(question));
|
||||
throw new NullReferenceException(
|
||||
"Could not find canvas answer to update local answer id"
|
||||
);
|
||||
}
|
||||
return answer with { CanvasId = canvasAnswer.Id };
|
||||
})
|
||||
.ToArray();
|
||||
return question with { CanvasId = newQuestion.Id, Answers = answersWithIds };
|
||||
}
|
||||
)
|
||||
.ToArray();
|
||||
var updatedQuestions = await Task.WhenAll(tasks);
|
||||
return localQuiz with { Questions = updatedQuestions };
|
||||
}
|
||||
|
||||
private async Task<CanvasQuizQuestion> createQuestionOnly(
|
||||
ulong canvasCourseId,
|
||||
LocalQuiz localQuiz,
|
||||
LocalQuizQuestion q
|
||||
)
|
||||
{
|
||||
var url = $"courses/{canvasCourseId}/quizzes/{localQuiz.CanvasId}/questions";
|
||||
var answers = q.Answers
|
||||
.Select(a => new { answer_html = a.Text, answer_weight = a.Correct ? 100 : 0 })
|
||||
.ToArray();
|
||||
var body = new
|
||||
{
|
||||
question = new
|
||||
{
|
||||
question_text = q.Text,
|
||||
question_type = q.QuestionType+"_question",
|
||||
possible_points = q.Points,
|
||||
// position
|
||||
answers
|
||||
}
|
||||
};
|
||||
var request = new RestRequest(url);
|
||||
request.AddBody(body);
|
||||
var (newQuestion, response) = await webRequestor.PostAsync<CanvasQuizQuestion>(request);
|
||||
if (newQuestion == null)
|
||||
throw new NullReferenceException("error creating new question, created question is null");
|
||||
return newQuestion;
|
||||
}
|
||||
}
|
||||
@@ -13,16 +13,19 @@ public class CanvasService
|
||||
private readonly CanvasServiceUtils utils;
|
||||
|
||||
public CanvasAssignmentService Assignments { get; }
|
||||
public CanvasQuizService Quizzes { get; }
|
||||
|
||||
public CanvasService(
|
||||
IWebRequestor webRequestor,
|
||||
CanvasServiceUtils utils,
|
||||
CanvasAssignmentService Assignments
|
||||
CanvasAssignmentService Assignments,
|
||||
CanvasQuizService Quizzes
|
||||
)
|
||||
{
|
||||
this.webRequestor = webRequestor;
|
||||
this.utils = utils;
|
||||
this.Assignments = Assignments;
|
||||
this.Quizzes = Quizzes;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<EnrollmentTermModel>> GetTerms()
|
||||
@@ -51,8 +54,8 @@ public class CanvasService
|
||||
|
||||
if (data == null)
|
||||
{
|
||||
System.Console.WriteLine(response.Content);
|
||||
System.Console.WriteLine(response.ResponseUri);
|
||||
Console.WriteLine(response.Content);
|
||||
Console.WriteLine(response.ResponseUri);
|
||||
throw new Exception("error getting course from canvas");
|
||||
}
|
||||
return data;
|
||||
@@ -151,7 +154,6 @@ public class CanvasService
|
||||
var body = new { module_item = new { title = item.Title, position = item.Position } };
|
||||
var request = new RestRequest(url);
|
||||
request.AddBody(body);
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var (newItem, response) = await webRequestor.PutAsync<CanvasModuleItem>(request);
|
||||
if (newItem == null)
|
||||
@@ -179,7 +181,6 @@ public class CanvasService
|
||||
};
|
||||
var request = new RestRequest(url);
|
||||
request.AddBody(body);
|
||||
request.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var (newItem, response) = await webRequestor.PostAsync<CanvasModuleItem>(request);
|
||||
if (newItem == null)
|
||||
|
||||
Reference in New Issue
Block a user