-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExtractor.py
More file actions
191 lines (169 loc) · 7.04 KB
/
DataExtractor.py
File metadata and controls
191 lines (169 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from canvasapi import Canvas
import datetime
import xlsxwriter
# Details needed to access Canvas and the course
API_URL = 'https://ufl.instructure.com/'
API_KEY = '1016~40RIzLLTgT01gRVzfhbsvMFIPWWZZZY4KhPF3WfAiPWhdv9Gi2HAZMarQ6uR8oAR'
courseID = 378337
section_num = ["000.UFL.2019-08-UF-0.13148"]
section_ID = "COP2271-29AD(13148)"
# Initialize a new Canvas object
canvas = Canvas(API_URL, API_KEY)
# Get a course object for this course
course = canvas.get_course(courseID)
# Get students for the specified section
enrollments = course.get_enrollments(type=["StudentEnrollment"], sis_section_id=section_num)
# Build a map of Canvas ID to their student ID using a nested dictionary for HW
idHW = {}
for e in enrollments:
idHW[e.user['id']] = {'canvas': e.user['id'], 'UF': e.sis_user_id, 'name': e.user['name']}
assignmentsHW = course.get_assignments(includes=['overrides'], search_term="HW:")
# Build a map of Canvas ID to their student ID using a nested dictionary for Quizzes
idQuiz = {}
for e in enrollments:
idQuiz[e.user['id']] = {'canvas': e.user['id'], 'UF': e.sis_user_id, 'name': e.user['name']}
assignmentsQuiz = course.get_assignments(includes=['overrides'], search_term="Quiz")
# Build a map of Canvas ID to their student ID using a nested dictionary for Exams
idExam = {}
for e in enrollments:
idExam[e.user['id']] = {'canvas': e.user['id'], 'UF': e.sis_user_id, 'name': e.user['name']}
# Ensure that only exams are included in assignments
groups = course.get_assignment_groups()
for g in groups:
if g.name == "Exams":
examID = g.id
break
totalAssignments = course.get_assignments()
exams = []
for t in totalAssignments:
if t.assignment_group_id == examID:
exams.append(t.id)
assignmentsExam = course.get_assignments(includes=['overrides'], assignment_ids=exams)
# Build a map of Canvas ID to their student ID using a nested dictionary for Final Grades
idFinalGrade = {}
for e in enrollments:
idFinalGrade[e.user['id']] = {'canvas': e.user['id'], 'UF': e.sis_user_id, 'name': e.user['name'], 'final_grade': e.grades['current_score']}
# # Need to figure out how to get past attempt history
# assignment = course.get_assignment(3970170)
# submissions = assignment.get_submission(1028980, include="submission_history")
#
# print(type(submissions.submission_history))
# Compile all the information into an excel workbook
# ----------------------------------------------------
# Creating the file
data = xlsxwriter.Workbook(section_ID + ".xlsx")
sheetQuiz = data.add_worksheet("Quiz Submission")
sheetHW = data.add_worksheet("HW Submission")
sheetExam = data.add_worksheet("Exam Score")
sheetFinalGrade = data.add_worksheet("Final Grade")
cell_format = data.add_format({'bold': True, 'center_across': True})
def createMap(assignments, idStructure):
# Searching for all of the homework assignments
for a in assignments:
print(a.name)
# Due date of the assignment
try:
due_date = datetime.datetime.strptime(a.due_at, "%Y-%m-%dT%H:%M:%SZ")
# Except needed for when there are multiple due dates
except:
overrides = a.get_overrides()
# Getting the override ID for the assignment
for o in overrides:
if section_ID in str(o):
num = str(o).split(") (")
override_ID = (num[1][:-1])
override = a.get_override(override_ID)
due_date = datetime.datetime.strptime(override.due_at, "%Y-%m-%dT%H:%M:%SZ")
for s in a.get_submissions(include=["submission_history"]):
try:
# Only acting on students in specified section
for person in idStructure:
if s.user_id == person:
sub_date = datetime.datetime.strptime(s.submitted_at, "%Y-%m-%dT%H:%M:%SZ")
duration = due_date - sub_date
duration_seconds = duration.total_seconds()
hours = duration_seconds / 3600
idStructure[person].update({a.name: str(hours)})
except:
print("NaN for: " + str(person))
print("\n")
def dataStorage(assignments, idStructure, sheet):
# Inputting the headers
row = 0
column = 0
sheet.write(row, column, "Student Name", cell_format)
column = column + 1
sheet.write(row, column, "UF ID", cell_format)
column = column + 1
sheet.write(row, column, "Canvas ID", cell_format)
column = column + 1
for a in assignments:
label = a.name.split(':')[0]
sheet.write(row, column, label, cell_format)
column = column + 1
# Writing each person and their associated data
row = 1
for person in idStructure:
col = 0
sheet.write(row, col, idStructure[person]['name'])
col = col + 1
sheet.write_number(row, col, int(idStructure[person]['UF']))
col = col + 1
sheet.write_number(row, col, int(idStructure[person]['canvas']))
for a in assignments:
col = col + 1
try:
sheet.write_number(row, col, float(idStructure[person][a.name]))
except:
sheet.write(row, col, "NaN")
row = row + 1
sheet.set_column(0, row, 18)
def dataStorageFinalGrade(idStructure, sheet):
# Inputting the headers
row = 0
column = 0
sheet.write(row, column, "Student Name", cell_format)
column = column + 1
sheet.write(row, column, "UF ID", cell_format)
column = column + 1
sheet.write(row, column, "Canvas ID", cell_format)
column = column + 1
sheet.write(row, column, "Final Grade", cell_format)
# Writing each person and their associated data
row = 1
for person in idStructure:
col = 0
sheet.write(row, col, idStructure[person]['name'])
col = col + 1
sheet.write_number(row, col, int(idStructure[person]['UF']))
col = col + 1
sheet.write_number(row, col, int(idStructure[person]['canvas']))
col = col + 1
sheet.write_number(row, col, float(idStructure[person]['final_grade']))
row = row + 1
sheet.set_column(0, row, 18)
def createMapExams(assignments, idStructure):
# Searching for all of the Exams
for a in assignments:
print(a.name)
for s in a.get_submissions(include=["submission_history"]):
try:
# Only acting on students in specified section
for person in idStructure:
if s.user_id == person:
score = s.score
idStructure[person].update({a.name: str(score)})
except:
print("NaN for: " + str(person))
print("\n")
# Create all the information maps
createMap(assignmentsQuiz, idQuiz)
createMap(assignmentsHW, idHW)
createMapExams(assignmentsExam, idExam)
# Store all the information maps
dataStorage(assignmentsQuiz, idQuiz, sheetQuiz)
dataStorage(assignmentsHW, idHW, sheetHW)
dataStorage(assignmentsExam, idExam, sheetExam)
dataStorageFinalGrade(idFinalGrade, sheetFinalGrade)
# Saving the excel file
data.close()