Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGC: Grade Calculator (v1) #144

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CanvasPlusPlayground.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,19 @@
B7AD54F52CD41D7900FB09BB /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7AD54EF2CD41D7900FB09BB /* Data.swift */; };
B7AD54F62CD41D7900FB09BB /* DeviceStat.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7AD54F02CD41D7900FB09BB /* DeviceStat.swift */; };
B7AD550C2CD4257B00FB09BB /* IntelligenceOnboardingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7AD550B2CD4257400FB09BB /* IntelligenceOnboardingView.swift */; };
B7D1365E2D15B63E0044A9E4 /* CourseGradeCalculatorBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7D1365D2D15B63E0044A9E4 /* CourseGradeCalculatorBridge.swift */; };
B7D95D772D07C3D3002AD955 /* ICSParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7D95D762D07C3D3002AD955 /* ICSParser.swift */; };
B7D95DB72D0A8D78002AD955 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7D95DB62D0A8D75002AD955 /* SettingsView.swift */; };
B7E59A0B2D1CFF8C001836FE /* GetAssignmentGroupsRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7E59A0A2D1CFF8C001836FE /* GetAssignmentGroupsRequest.swift */; };
B7F950322D118EAF004BB470 /* String+StripHTML.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F950312D118EAA004BB470 /* String+StripHTML.swift */; };
B7F950342D11A00F004BB470 /* StatusToolbarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F950332D11A00F004BB470 /* StatusToolbarItem.swift */; };
B7F950372D127869004BB470 /* ProfileManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F950362D127861004BB470 /* ProfileManager.swift */; };
B7F950392D1279A1004BB470 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F950382D1279A1004BB470 /* User.swift */; };
B7F9503B2D127AD0004BB470 /* Profile.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F9503A2D127AD0004BB470 /* Profile.swift */; };
B7F9503D2D12ADAE004BB470 /* Submission.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F9503C2D12ADAE004BB470 /* Submission.swift */; };
B7F9503F2D133435004BB470 /* AssignmentGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F9503E2D133435004BB470 /* AssignmentGroup.swift */; };
B7F950412D149290004BB470 /* CourseGradeCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F950402D14928C004BB470 /* CourseGradeCalculator.swift */; };
B7F950432D149C5E004BB470 /* GradeCalculatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B7F950422D149C5E004BB470 /* GradeCalculatorView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -186,14 +191,19 @@
B7AD54F12CD41D7900FB09BB /* LLMEvaluator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LLMEvaluator.swift; sourceTree = "<group>"; };
B7AD54F22CD41D7900FB09BB /* Models.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Models.swift; sourceTree = "<group>"; };
B7AD550B2CD4257400FB09BB /* IntelligenceOnboardingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntelligenceOnboardingView.swift; sourceTree = "<group>"; };
B7D1365D2D15B63E0044A9E4 /* CourseGradeCalculatorBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseGradeCalculatorBridge.swift; sourceTree = "<group>"; };
B7D95D762D07C3D3002AD955 /* ICSParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ICSParser.swift; sourceTree = "<group>"; };
B7D95DB62D0A8D75002AD955 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
B7E59A0A2D1CFF8C001836FE /* GetAssignmentGroupsRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetAssignmentGroupsRequest.swift; sourceTree = "<group>"; };
B7F950312D118EAA004BB470 /* String+StripHTML.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+StripHTML.swift"; sourceTree = "<group>"; };
B7F950332D11A00F004BB470 /* StatusToolbarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusToolbarItem.swift; sourceTree = "<group>"; };
B7F950362D127861004BB470 /* ProfileManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileManager.swift; sourceTree = "<group>"; };
B7F950382D1279A1004BB470 /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
B7F9503A2D127AD0004BB470 /* Profile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Profile.swift; sourceTree = "<group>"; };
B7F9503C2D12ADAE004BB470 /* Submission.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Submission.swift; sourceTree = "<group>"; };
B7F9503E2D133435004BB470 /* AssignmentGroup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssignmentGroup.swift; sourceTree = "<group>"; };
B7F950402D14928C004BB470 /* CourseGradeCalculator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseGradeCalculator.swift; sourceTree = "<group>"; };
B7F950422D149C5E004BB470 /* GradeCalculatorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradeCalculatorView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -250,6 +260,7 @@
A3049B7E2D160C03002F3166 /* GetTabsRequest.swift */,
A3049B802D160F6B002F3166 /* GetAnnouncementsRequest.swift */,
A3049B822D161432002F3166 /* GetAssignmentsRequest.swift */,
B7E59A0A2D1CFF8C001836FE /* GetAssignmentGroupsRequest.swift */,
A3049B842D161455002F3166 /* GetEnrollmentsRequest.swift */,
A3049B862D16146D002F3166 /* GetQuizzesRequest.swift */,
A3D1614E2D1784E3004055FB /* GetUserRequest.swift */,
Expand Down Expand Up @@ -437,6 +448,7 @@
A324BA5F2D0798A1005F53FA /* Grades */ = {
isa = PBXGroup;
children = (
B7F950442D149C6E004BB470 /* GradeCalculator */,
7F8535732C98DE3C0023E384 /* CourseGradeView.swift */,
A31A81E62D0CCB69003C37EB /* GradesViewModel.swift */,
);
Expand All @@ -446,6 +458,7 @@
A324BA602D079927005F53FA /* Plain */ = {
isa = PBXGroup;
children = (
B7F9503E2D133435004BB470 /* AssignmentGroup.swift */,
B7F9503A2D127AD0004BB470 /* Profile.swift */,
B7F950382D1279A1004BB470 /* User.swift */,
192EC0492C963B9000AF8528 /* Assignment.swift */,
Expand Down Expand Up @@ -530,6 +543,16 @@
path = Profile;
sourceTree = "<group>";
};
B7F950442D149C6E004BB470 /* GradeCalculator */ = {
isa = PBXGroup;
children = (
B7F950402D14928C004BB470 /* CourseGradeCalculator.swift */,
B7D1365D2D15B63E0044A9E4 /* CourseGradeCalculatorBridge.swift */,
B7F950422D149C5E004BB470 /* GradeCalculatorView.swift */,
);
path = GradeCalculator;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -630,6 +653,7 @@
192EC0482C963ACB00AF8528 /* CourseAssignmentManager.swift in Sources */,
B7D95D772D07C3D3002AD955 /* ICSParser.swift in Sources */,
B7F9503B2D127AD0004BB470 /* Profile.swift in Sources */,
B7E59A0B2D1CFF8C001836FE /* GetAssignmentGroupsRequest.swift in Sources */,
B76455012C8DF61B002DF00E /* StorageKeys.swift in Sources */,
A3FFD03A2CDEC2AC006BAB51 /* LookupCondition.swift in Sources */,
B7D95DB72D0A8D78002AD955 /* SettingsView.swift in Sources */,
Expand All @@ -644,13 +668,16 @@
A3FFD03E2CE0065A006BAB51 /* NetworkError.swift in Sources */,
B53D95A22CA0A22A00647EE9 /* PeopleView.swift in Sources */,
A3049B752D15DC1C002F3166 /* GetCoursesRequest.swift in Sources */,
B7F9503F2D133435004BB470 /* AssignmentGroup.swift in Sources */,
B7F950432D149C5E004BB470 /* GradeCalculatorView.swift in Sources */,
B76455042C8DF61B002DF00E /* CourseView.swift in Sources */,
A3049B682D0F3ECA002F3166 /* QuizzesViewModel.swift in Sources */,
9B92A4252C93856100C21CFC /* CourseAnnouncementManager.swift in Sources */,
B76455052C8DF61B002DF00E /* SetupView.swift in Sources */,
A3049B772D15DC36002F3166 /* GetCourseRequest.swift in Sources */,
B7AD54F32CD41D7900FB09BB /* LLMEvaluator.swift in Sources */,
B7AD54F42CD41D7900FB09BB /* Models.swift in Sources */,
B7F950412D149290004BB470 /* CourseGradeCalculator.swift in Sources */,
B7AD54F52CD41D7900FB09BB /* Data.swift in Sources */,
A3049B852D161455002F3166 /* GetEnrollmentsRequest.swift in Sources */,
A3D161472D169C0A004055FB /* APIRequest+Network.swift in Sources */,
Expand Down Expand Up @@ -690,6 +717,7 @@
9B9C2E062C93F94C00E4B16B /* CourseAnnouncementDetailView.swift in Sources */,
A3D161512D178615004055FB /* GetUserProfileRequest.swift in Sources */,
A3D161492D169C23004055FB /* APIRequest+Storage.swift in Sources */,
B7D1365E2D15B63E0044A9E4 /* CourseGradeCalculatorBridge.swift in Sources */,
9B6663E32C9853BC0060990E /* HTMLTextView.swift in Sources */,
B7AD550C2CD4257B00FB09BB /* IntelligenceOnboardingView.swift in Sources */,
A3049B792D15E162002F3166 /* GetCourseRootFolder.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// GetAssignmentGroupsRequest.swift
// CanvasPlusPlayground
//
// Created by Rahul on 12/26/24.
//

import Foundation

struct GetAssignmentGroupsRequest: ArrayAPIRequest {
typealias Subject = AssignmentGroup

let courseId: String

// Path for the request
var path: String { "courses/\(courseId)/assignment_groups" }

// Query parameters
var queryParameters: [QueryParameter] {
[
("override_assignment_dates", overrideAssignmentDates),
("grading_period_id", gradingPeriodId),
("scope_assignments_to_student", scopeAssignmentsToStudent)
]
+ include.map { ("include[]", $0) }
+ assignmentIds.map { ("assignment_ids[]", $0) }
+ excludeAssignmentSubmissionTypes.map { ("exclude_assignment_submission_types[]", $0) }
}

// MARK: Query Params
let include: [String]
let assignmentIds: [String]
let excludeAssignmentSubmissionTypes: [String]
let overrideAssignmentDates: Bool?
let gradingPeriodId: Int?
let scopeAssignmentsToStudent: Bool?

// Initializer
init(
courseId: String,
include: [String] = [],
assignmentIds: [String] = [],
excludeAssignmentSubmissionTypes: [String] = [],
overrideAssignmentDates: Bool? = nil,
gradingPeriodId: Int? = nil,
scopeAssignmentsToStudent: Bool? = nil
) {
self.courseId = courseId
self.include = include
self.assignmentIds = assignmentIds
self.excludeAssignmentSubmissionTypes = excludeAssignmentSubmissionTypes
self.overrideAssignmentDates = overrideAssignmentDates
self.gradingPeriodId = gradingPeriodId
self.scopeAssignmentsToStudent = scopeAssignmentsToStudent
}

/* MARK: Request Caching (Optional Implementation)
var requestId: Int? { courseId.asInt }
var requestIdKey: ParentKeyPath<AssignmentGroup, Int?> { .createReadable(\.courseId) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should note now, use createWritable when the property (courseId in this case) doesn't come with fetch result. This allows the service layer to set that property before returning it. e.g. Since courseId isn't in assignment group by default, you probs wanna make it createWritable.

var idPredicate: Predicate<AssignmentGroup> {
#Predicate<AssignmentGroup> { group in
group.courseId == requestId
}
}

var customPredicate: Predicate<AssignmentGroup> {
let ids = assignmentIds.compactMap(\.?.asInt)
let assignmentIdsPred = assignmentIds.isEmpty ? .true : #Predicate<AssignmentGroup> { group in
ids.contains(group.id)
}
return assignmentIdsPred
}
*/
}
9 changes: 8 additions & 1 deletion CanvasPlusPlayground/Common/Network/CanvasRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ struct CanvasRequest {
static func getAssignments(courseId: String) -> GetAssignmentsRequest {
GetAssignmentsRequest(courseId: courseId)
}


static func getAssignmentGroups(
courseId: String,
include includeItems: [String] = ["assignments", "submission"]
) -> GetAssignmentGroupsRequest {
GetAssignmentGroupsRequest(courseId: courseId, include: includeItems)
}

static func getEnrollments(courseId: String, userId: Int? = nil, perPage: Int = 50) -> GetEnrollmentsRequest {
GetEnrollmentsRequest(courseId: courseId, userId: userId?.asString, perPage: perPage)
}
Expand Down
4 changes: 4 additions & 0 deletions CanvasPlusPlayground/Common/Utilities/String+Numbers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ extension Double {
var toInt: Int {
Int(self)
}

func rounded(toPlaces places: Int) -> String {
String(format: "%.\(places)f", self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,39 @@ import SwiftUI
@Observable
class CourseAssignmentManager {
private let courseID: String?
var assignments = [Assignment]()
var assignmentGroups: [AssignmentGroup] = []

init(courseID: String?) {
self.courseID = courseID
}

func fetchAssignments() async {
guard let courseID = courseID, let (data, _) = try? await CanvasService.shared.fetchResponse(CanvasRequest.getAssignments(courseId: courseID)) else {
print("Failed to fetch assignments.")
func fetchAssignmentGroups() async {
guard let courseID = courseID, let (data, _) = try? await CanvasService.shared.fetchResponse(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use fetch instead of fetchResponse. Handles the decoding and returns an array of assignment groups.

CanvasRequest.getAssignmentGroups(courseId: courseID)
) else {
print("Failed to fetch assignment groups.")
return
}

self.assignmentGroups = (try? JSONDecoder().decode([AssignmentGroup].self, from: data)) ?? []
}

static func getAssignmentsForCourse(courseID: String) async -> [Assignment] {
await CourseAssignmentManager.fetchAssignments(courseID: courseID)
}

private static func fetchAssignments(courseID: String) async -> [Assignment] {
guard let (data, _) = try? await CanvasService.shared.fetchResponse(CanvasRequest.getAssignments(courseId: courseID)) else {
print("Failed to fetch assignments.")
return []
}

Comment on lines +30 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the getAssignmentsForCourse redundant? Other than the name making more sense

do {
self.assignments = try JSONDecoder().decode([Assignment].self, from: data)
return try JSONDecoder().decode([Assignment].self, from: data)
} catch {
print(error)
}
}

static func getAssignmentsForCourse(courseID: String) async -> [Assignment] {
let manager = CourseAssignmentManager(courseID: courseID)
await manager.fetchAssignments()
return manager.assignments

return []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct CourseAssignmentsView: View {
@State private var assignmentManager: CourseAssignmentManager

@State private var isLoadingAssignments = true
@State private var showGradeCalculator: Bool = false

init(course: Course, showGrades: Bool = false) {
self.course = course
Expand All @@ -21,27 +22,20 @@ struct CourseAssignmentsView: View {
}

var body: some View {
List(assignmentManager.assignments, id: \.id) { assignment in
HStack {
VStack(alignment: .leading) {
Text(assignment.name)
.font(.headline)
Group {
if let submission = assignment.submission {
Text(
submission.workflowState?.rawValue.capitalized ?? "Unknown Status"
)
}
}
.font(.subheadline)
List(assignmentManager.assignmentGroups) { assignmentGroup in
Section {
ForEach(assignmentGroup.assignments ?? []) { assignment in
AssignmentRow(assignment: assignment, showGrades: showGrades)
}
} header: {
HStack {
Text(assignmentGroup.name ?? "")

if showGrades, let submission = assignment.submission {
Spacer()

Text(submission.score?.truncatingTrailingZeros ?? "--") +
Text("/") +
Text(assignment.pointsPossible?.truncatingTrailingZeros ?? "--")
if let groupWeight = assignmentGroup.groupWeight {
Text("\(groupWeight)%")
}
}
}
}
Expand All @@ -50,11 +44,52 @@ struct CourseAssignmentsView: View {
}
.statusToolbarItem("Assignments", isVisible: isLoadingAssignments)
.navigationTitle(course.displayName)
.sheet(isPresented: $showGradeCalculator) {
NavigationStack {
GradeCalculatorView(
assignmentGroups: assignmentManager.assignmentGroups
)
}
}
.toolbar {
Button("Calculate Grades") { showGradeCalculator = true }
.disabled(assignmentManager.assignmentGroups.isEmpty)
}
}

private func loadAssignments() async {
isLoadingAssignments = true
await assignmentManager.fetchAssignments()
await assignmentManager.fetchAssignmentGroups()
isLoadingAssignments = false
}
}

private struct AssignmentRow: View {
let assignment: Assignment
let showGrades: Bool

var body: some View {
HStack {
VStack(alignment: .leading) {
Text(assignment.name)
.font(.headline)
Group {
if let submission = assignment.submission {
Text(
submission.workflowState?.rawValue.capitalized ?? "Unknown Status"
)
}
}
.font(.subheadline)
}

if showGrades, let submission = assignment.submission {
Spacer()

Text(submission.score?.truncatingTrailingZeros ?? "--") +
Text("/") +
Text(assignment.pointsPossible?.truncatingTrailingZeros ?? "--")
}
}
}
}
5 changes: 4 additions & 1 deletion CanvasPlusPlayground/Features/Grades/CourseGradeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ struct CourseGradeView: View {
Group {
if let url = gradesVM.canvasURL {
Link("View on Canvas", destination: url)
} else {
} else if gradesVM.isLoading {
Text("Loading grades...")
.foregroundStyle(.secondary)
} else {
Text("Unable to load grades") // TODO: Display a relevant error message
.foregroundStyle(.secondary)
}
}
.font(.footnote)
Expand Down
Loading