-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupdate_gradesheet.py
104 lines (77 loc) · 4.53 KB
/
update_gradesheet.py
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
import nbgrader, csv, codecs, sys, os, shutil
from nbgrader.apps import NbGraderAPI
import zipfile
verbose = False
def zip(out, root):
shutil.make_archive(out, 'zip', root)
def moodle_gradesheet(assignment, with_feedback=True):
api = NbGraderAPI()
gradebook = api.gradebook
csvfile = os.path.join("imports", assignment+".csv")
with open(csvfile, newline='', encoding='utf-8-sig') as f:
reader = csv.DictReader(f)
fname = os.path.join("exports", assignment+".csv")
if with_feedback:
archive = zipfile.ZipFile(os.path.join("exports", "feedback_"+assignment+".zip"), 'w', zipfile.ZIP_DEFLATED)
with open(fname, 'w', encoding='utf-8', newline='') as out:
writer = csv.DictWriter(out, reader.fieldnames)
writer.writeheader()
for line in reader:
email, ident, fullname, status, grade, max_grade = line["Registration number"], line['Identifier'], line['Full name'], line['Status'], line['Grade'], line['Maximum Grade']
unique_id = email[0:7]
try:
submission = gradebook.find_submission(assignment, unique_id)
except:
if "Submitted" in status:
print("WARNING: No submission for {id} in assignment {assign}".format(id=unique_id ,assign=assignment))
else:
if verbose:
print("\tNo submission for {id} in assignment {assign}, as expected".format(id=unique_id, assign=assignment))
else:
if verbose:
print("\tProcessing submission for {id} in assignment {assign}".format(id=unique_id, assign=assignment))
fbk_path = os.path.join("feedback", unique_id, assignment)
try:
files = [os.path.join(fbk_path, f) for f in os.listdir(fbk_path) if f.endswith('.html')]
assign_id = ident[12:]
# remove asterisks
name = 'blank'
# create the path to the feedback file
fbk_full_path = "{fullname}_{assign_id}_assignsubmission_file_".format(fullname=name,
assign_id=assign_id)
for f in files:
archive.write(f, arcname=os.path.join(fbk_full_path, os.path.basename(f)))
except FileNotFoundError:
print("HTML feedback file for {fullname} {id} {assign} is missing".format(id=unique_id,
fullname=fullname, assign=assignment))
# no feedback to generate
line['Grade'] = submission.score
# warn about dubious scores
if line['Grade']<=0 or line['Grade']>submission.max_score:
print("Warning: {matric} {name} has a score of {grade}".format(matric=unique_id,
name=fullname, grade=line['Grade']))
# correct the maximum grade
line['Maximum Grade'] = submission.max_score
writer.writerow(line)
print("Wrote to {0}".format(fname))
# tidy up the feedback file
if with_feedback:
archive.close()
if __name__=="__main__":
if len(sys.argv)!=2:
print("""
Usage:
update_gradesheet.py <assign>
Updates a CSV file gradesheet (which must have be downloaded from
Moodle with "offline gradesheets" enabled in the assignment settings) with
the results from grading the assignment <assign>.
The input will be imports/<assign>.csv
The output will be in exports/<assign>.csv
Feedback will be zipped up into the file exports/<assign>_feedback.zip and this
can be uploaded to Moodle if "Feedback files" is enabled. This uploads all student
feedback in one go.
""")
exit(-1)
assignment= sys.argv[1]
print("Updating gradesheet for {0}...".format(assignment))
moodle_gradesheet(assignment)