mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 07:38:33 -06:00
more layout and cleanup
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { canvasApi, canvasServiceUtils } from "./canvasServiceUtils";
|
||||
import { canvasApi, paginatedRequest } from "./canvasServiceUtils";
|
||||
import { axiosClient } from "../axiosUtils";
|
||||
import { CanvasAssignmentGroup } from "@/models/canvas/assignments/canvasAssignmentGroup";
|
||||
import { LocalAssignmentGroup } from "@/models/local/assignment/localAssignmentGroup";
|
||||
@@ -8,7 +8,7 @@ export const canvasAssignmentGroupService = {
|
||||
async getAll(courseId: number): Promise<CanvasAssignmentGroup[]> {
|
||||
console.log("Requesting assignment groups");
|
||||
const url = `${canvasApi}/courses/${courseId}/assignment_groups`;
|
||||
const assignmentGroups = await canvasServiceUtils.paginatedRequest<
|
||||
const assignmentGroups = await paginatedRequest<
|
||||
CanvasAssignmentGroup[]
|
||||
>({
|
||||
url,
|
||||
|
||||
@@ -1,75 +1,12 @@
|
||||
import { CanvasAssignment } from "@/models/canvas/assignments/canvasAssignment";
|
||||
import {
|
||||
canvasApi,
|
||||
canvasServiceUtils,
|
||||
paginatedRequest,
|
||||
} from "./canvasServiceUtils";
|
||||
import { canvasApi, paginatedRequest } from "./canvasServiceUtils";
|
||||
import { LocalAssignment } from "@/models/local/assignment/localAssignment";
|
||||
import { axiosClient } from "../axiosUtils";
|
||||
import { markdownToHTMLSafe } from "../htmlMarkdownUtils";
|
||||
import { CanvasRubricCreationResponse } from "@/models/canvas/assignments/canvasRubricCreationResponse";
|
||||
import { assignmentPoints } from "@/models/local/assignment/utils/assignmentPointsUtils";
|
||||
import {
|
||||
getDateFromString,
|
||||
getDateFromStringOrThrow,
|
||||
} from "@/models/local/timeUtils";
|
||||
import { getDateFromString } from "@/models/local/timeUtils";
|
||||
|
||||
const createRubric = async (
|
||||
courseId: number,
|
||||
assignmentCanvasId: number,
|
||||
localAssignment: LocalAssignment
|
||||
) => {
|
||||
const criterion = localAssignment.rubric
|
||||
.map((rubricItem) => ({
|
||||
description: rubricItem.label,
|
||||
points: rubricItem.points,
|
||||
ratings: {
|
||||
0: { description: "Full Marks", points: rubricItem.points },
|
||||
1: { description: "No Marks", points: 0 },
|
||||
},
|
||||
}))
|
||||
.reduce((acc, item, index) => {
|
||||
return {
|
||||
...acc,
|
||||
[index]: item,
|
||||
};
|
||||
}, {} as { [key: number]: { description: string; points: number; ratings: { [key: number]: { description: string; points: number } } } });
|
||||
|
||||
const rubricBody = {
|
||||
rubric_association_id: assignmentCanvasId,
|
||||
rubric: {
|
||||
title: `Rubric for Assignment: ${localAssignment.name}`,
|
||||
association_id: assignmentCanvasId,
|
||||
association_type: "Assignment",
|
||||
use_for_grading: true,
|
||||
criteria: criterion,
|
||||
},
|
||||
rubric_association: {
|
||||
association_id: assignmentCanvasId,
|
||||
association_type: "Assignment",
|
||||
purpose: "grading",
|
||||
use_for_grading: true,
|
||||
},
|
||||
};
|
||||
|
||||
const rubricUrl = `${canvasApi}/courses/${courseId}/rubrics`;
|
||||
const rubricResponse = await axiosClient.post<CanvasRubricCreationResponse>(
|
||||
rubricUrl,
|
||||
rubricBody
|
||||
);
|
||||
|
||||
if (!rubricResponse.data) throw new Error("Failed to create rubric");
|
||||
|
||||
const assignmentPointAdjustmentUrl = `${canvasApi}/courses/${courseId}/assignments/${assignmentCanvasId}`;
|
||||
const assignmentPointAdjustmentBody = {
|
||||
assignment: { points_possible: assignmentPoints(localAssignment) },
|
||||
};
|
||||
|
||||
await axiosClient.put(
|
||||
assignmentPointAdjustmentUrl,
|
||||
assignmentPointAdjustmentBody
|
||||
);
|
||||
};
|
||||
|
||||
export const canvasAssignmentService = {
|
||||
async getAll(courseId: number): Promise<CanvasAssignment[]> {
|
||||
@@ -163,3 +100,60 @@ export const canvasAssignmentService = {
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const createRubric = async (
|
||||
courseId: number,
|
||||
assignmentCanvasId: number,
|
||||
localAssignment: LocalAssignment
|
||||
) => {
|
||||
const criterion = localAssignment.rubric
|
||||
.map((rubricItem) => ({
|
||||
description: rubricItem.label,
|
||||
points: rubricItem.points,
|
||||
ratings: {
|
||||
0: { description: "Full Marks", points: rubricItem.points },
|
||||
1: { description: "No Marks", points: 0 },
|
||||
},
|
||||
}))
|
||||
.reduce((acc, item, index) => {
|
||||
return {
|
||||
...acc,
|
||||
[index]: item,
|
||||
};
|
||||
}, {} as { [key: number]: { description: string; points: number; ratings: { [key: number]: { description: string; points: number } } } });
|
||||
|
||||
const rubricBody = {
|
||||
rubric_association_id: assignmentCanvasId,
|
||||
rubric: {
|
||||
title: `Rubric for Assignment: ${localAssignment.name}`,
|
||||
association_id: assignmentCanvasId,
|
||||
association_type: "Assignment",
|
||||
use_for_grading: true,
|
||||
criteria: criterion,
|
||||
},
|
||||
rubric_association: {
|
||||
association_id: assignmentCanvasId,
|
||||
association_type: "Assignment",
|
||||
purpose: "grading",
|
||||
use_for_grading: true,
|
||||
},
|
||||
};
|
||||
|
||||
const rubricUrl = `${canvasApi}/courses/${courseId}/rubrics`;
|
||||
const rubricResponse = await axiosClient.post<CanvasRubricCreationResponse>(
|
||||
rubricUrl,
|
||||
rubricBody
|
||||
);
|
||||
|
||||
if (!rubricResponse.data) throw new Error("Failed to create rubric");
|
||||
|
||||
const assignmentPointAdjustmentUrl = `${canvasApi}/courses/${courseId}/assignments/${assignmentCanvasId}`;
|
||||
const assignmentPointAdjustmentBody = {
|
||||
assignment: { points_possible: assignmentPoints(localAssignment) },
|
||||
};
|
||||
|
||||
await axiosClient.put(
|
||||
assignmentPointAdjustmentUrl,
|
||||
assignmentPointAdjustmentBody
|
||||
);
|
||||
};
|
||||
|
||||
@@ -14,18 +14,26 @@ const getNextUrl = (
|
||||
? (headers.get("link") as string)
|
||||
: ((headers as RawAxiosResponseHeaders)["link"] as string);
|
||||
|
||||
if (!linkHeader) return undefined;
|
||||
if (!linkHeader) {
|
||||
console.log("could not find link header in the response");
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const links = linkHeader.split(",").map((link) => link.trim());
|
||||
const nextLink = links.find((link) => link.includes('rel="next"'));
|
||||
|
||||
if (!nextLink) return undefined;
|
||||
if (!nextLink) {
|
||||
// console.log(
|
||||
// "could not find next url in link header, reached end of pagination"
|
||||
// );
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const nextUrl = nextLink.split(";")[0].trim().slice(1, -1);
|
||||
return nextUrl;
|
||||
};
|
||||
|
||||
export async function paginatedRequest<T>(request: {
|
||||
export async function paginatedRequest<T extends any[]>(request: {
|
||||
url: string;
|
||||
}): Promise<T> {
|
||||
var requestCount = 1;
|
||||
@@ -36,20 +44,19 @@ export async function paginatedRequest<T>(request: {
|
||||
url.toString()
|
||||
);
|
||||
|
||||
if (!Array.isArray(firstData)) {
|
||||
return firstData;
|
||||
}
|
||||
// if (!Array.isArray(firstData)) {
|
||||
// return firstData;
|
||||
// }
|
||||
|
||||
|
||||
var returnData = firstData ? [firstData] : [];
|
||||
var returnData = [...firstData];
|
||||
var nextUrl = getNextUrl(firstHeaders);
|
||||
console.log("got first request", nextUrl, firstHeaders);
|
||||
// console.log("got first request", nextUrl, firstHeaders);
|
||||
|
||||
while (nextUrl) {
|
||||
requestCount += 1;
|
||||
const { data, headers } = await axiosClient.get<T>(nextUrl);
|
||||
if (data) {
|
||||
returnData = [...returnData, data];
|
||||
returnData = returnData.concat(data);
|
||||
}
|
||||
nextUrl = getNextUrl(headers);
|
||||
}
|
||||
@@ -60,5 +67,5 @@ export async function paginatedRequest<T>(request: {
|
||||
);
|
||||
}
|
||||
|
||||
return returnData;
|
||||
return returnData as T;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user