mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
got rubric creation working
This commit is contained in:
@@ -22,12 +22,7 @@ public class CanvasAssignmentService
|
||||
return assignmentResponse.SelectMany(
|
||||
assignments =>
|
||||
assignments.Select(
|
||||
a =>
|
||||
a with
|
||||
{
|
||||
DueAt = a.DueAt?.ToLocalTime(),
|
||||
LockAt = a.LockAt?.ToLocalTime()
|
||||
}
|
||||
a => a with { DueAt = a.DueAt?.ToLocalTime(), LockAt = a.LockAt?.ToLocalTime() }
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -85,5 +80,75 @@ public class CanvasAssignmentService
|
||||
var bodyObj = new { assignment = body };
|
||||
request.AddBody(bodyObj);
|
||||
await webRequestor.PutAsync(request);
|
||||
|
||||
await CreateRubric(courseId, localAssignment);
|
||||
}
|
||||
|
||||
public async Task CreateRubric(ulong courseId, LocalAssignment localAssignment)
|
||||
{
|
||||
if (localAssignment.canvasId == null)
|
||||
throw new Exception("cannot create rubric if no canvas id in assignment");
|
||||
|
||||
var criterion = new Dictionary<int, object>();
|
||||
|
||||
var i = 0;
|
||||
foreach (var rubricItem in localAssignment.rubric)
|
||||
{
|
||||
var ratings = new Dictionary<int, object>
|
||||
{
|
||||
{ 0, new { description = "Full Marks", points = rubricItem.Points } },
|
||||
{ 1, new { description = "No Marks", points = 0 } },
|
||||
};
|
||||
criterion[i] = new
|
||||
{
|
||||
description = rubricItem.Label,
|
||||
points = rubricItem.Points,
|
||||
ratings = ratings
|
||||
};
|
||||
i++;
|
||||
}
|
||||
|
||||
// https://canvas.instructure.com/doc/api/rubrics.html#method.rubrics.create
|
||||
var body = new
|
||||
{
|
||||
rubric_association_id = localAssignment.canvasId,
|
||||
rubric = new
|
||||
{
|
||||
title = $"Rubric for Assignment: {localAssignment.name}",
|
||||
association_id = localAssignment.canvasId,
|
||||
association_type = "Assignment",
|
||||
use_for_grading = true,
|
||||
criteria = criterion,
|
||||
},
|
||||
rubric_association = new
|
||||
{
|
||||
association_id = localAssignment.canvasId,
|
||||
association_type = "Assignment",
|
||||
purpose = "grading",
|
||||
use_for_grading = true,
|
||||
}
|
||||
};
|
||||
var creationUrl = $"courses/{courseId}/rubrics";
|
||||
var rubricCreationRequest = new RestRequest(creationUrl);
|
||||
rubricCreationRequest.AddBody(body);
|
||||
rubricCreationRequest.AddHeader("Content-Type", "application/json");
|
||||
|
||||
var (rubricCreationResponse, creationResponse) =
|
||||
await webRequestor.PostAsync<CanvasRubricCreationResponse>(rubricCreationRequest);
|
||||
|
||||
if (rubricCreationResponse == null)
|
||||
throw new Exception("failed to create rubric before association");
|
||||
|
||||
var assignmentPointCorrectionBody = new
|
||||
{
|
||||
assignment = new { points_possible = localAssignment.points_possible }
|
||||
};
|
||||
var adjustmentUrl = $"courses/{courseId}/assignments/{localAssignment.canvasId}";
|
||||
var pointAdjustmentRequest = new RestRequest(adjustmentUrl);
|
||||
pointAdjustmentRequest.AddBody(assignmentPointCorrectionBody);
|
||||
pointAdjustmentRequest.AddHeader("Content-Type", "application/json");
|
||||
var (updatedAssignment, adjustmentResponse) = await webRequestor.PutAsync<CanvasAssignment>(
|
||||
pointAdjustmentRequest
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
using CanvasModel.Assignments;
|
||||
|
||||
public record CanvasRubricCreationResponse
|
||||
{
|
||||
public required CanvasRubric rubric { get; set; }
|
||||
public CanvasRubricAssociation? rubric_association { get; set; }
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
using RestSharp;
|
||||
|
||||
namespace Management.Services.Canvas;
|
||||
|
||||
public class CanvasServiceUtils
|
||||
{
|
||||
private const string BaseUrl = "https://snow.instructure.com/api/v1/";
|
||||
private readonly IWebRequestor webRequestor;
|
||||
|
||||
public CanvasServiceUtils(IWebRequestor webRequestor)
|
||||
{
|
||||
this.webRequestor = webRequestor;
|
||||
@@ -36,7 +38,8 @@ public class CanvasServiceUtils
|
||||
nextUrl = getNextUrl(nextResponse.Headers);
|
||||
}
|
||||
|
||||
System.Console.WriteLine($"Requesting {typeof(T)} took {requestCount} requests");
|
||||
if (requestCount > 1)
|
||||
System.Console.WriteLine($"Requesting {typeof(T)} took {requestCount} requests");
|
||||
|
||||
return returnData;
|
||||
}
|
||||
@@ -55,4 +58,4 @@ public class CanvasServiceUtils
|
||||
.TrimStart('<')
|
||||
.Replace(" ", "")
|
||||
.Replace(BaseUrl, "");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user