-
Notifications
You must be signed in to change notification settings - Fork 2
/
quiz2pdf.py
executable file
·333 lines (293 loc) · 12.9 KB
/
quiz2pdf.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#! /usr/bin/python3
import os
import csv
import re
from os import path
import json
import zipfile
import argparse
import requests
import weasyprint
import canvas
def start_file(file_name):
if path.exists(file_name):
os.rename(file_name, file_name + '~')
htmlfile = open(file_name, 'w')
htmlfile.write('''<!DOCTYPE html>
<html>
<head></head>
<body>
''')
htmlfile_list.append(file_name)
return htmlfile
def save_raw_answer(answer, identification):
question = questions[answer['question_id']]
if question['question_type'] == 'essay_question':
raw_file_name = f'answer_{identification}.html'
rawanswers_file.writestr(raw_file_name, answer['text'])
elif question['question_type'] == 'file_upload_question':
answer['text'] = '<div class="file-upload">See file(s): <ul>'
for cfile in [canvas.file(a) for a in answer['attachment_ids']]:
raw_file_name = f"answer_{identification}_{cfile['display_name']}"
result = requests.get(cfile['url'])
if result:
rawanswers_file.writestr(raw_file_name, result.content)
answer['text'] += f'<li>{raw_file_name}</li>'
answer['text'] += '</ul></div>'
def write_exam_file(htmlfile, question_dict, quiz_submission=None):
acct = ''
snum = ''
sname = ''
answers = {}
sub_questions = {}
num_attempts = 0
if quiz_submission is not None:
sub = submissions[quiz_submission['submission_id']]
snum = sub['user']['sis_user_id']
sname = sub['user']['name']
if args.classlist:
if snum in student_accounts:
acct = student_accounts[snum]
else:
print(f'Account not found for student: {snum}')
else:
acct = snum
sub_questions = quiz.submission_questions(quiz_submission)
previous_score = -1
previous_attempt = -1
variation = {}
for attempt in sub['submission_history']:
if 'submission_data' not in attempt:
continue
num_attempts += 1
update_answer = False
if attempt['score'] > previous_score:
previous_score = attempt['score']
previous_attempt = attempt['attempt']
update_answer = True
elif attempt['score'] == previous_score and \
attempt['attempt'] > previous_attempt:
previous_attempt = attempt['attempt']
update_answer = True
if attempt['attempt'] in variation.keys():
variation[attempt['attempt']] += 'x'
else:
variation[attempt['attempt']] = ''
for answer in attempt['submission_data']:
if not question_included(answer['question_id']):
continue
save_raw_answer(
answer,
f"{answer['question_id']}_{acct}_v{attempt['attempt']}{variation[attempt['attempt']]}")
if update_answer:
answers[answer['question_id']] = answer
if args.classlist:
htmlfile.write(f'''<div class='student-wrapper'>
<span class='account-label'>Account:</span>
<span><span class='account'>{acct}</span></span>
</div>''')
else:
htmlfile.write(f'''<div class='student-wrapper'>
<span class='snum-label'>Student Number:</span>
<span><span class='snum'>{snum}</span></span>
<span class='sname-label'>Name:</span>
<span><span class='sname'>{sname}</span></span>
</div>''')
qnum = 1
for (question_id, question) in question_dict.items():
question_name = question['question_name']
question_text = question['question_text']
question_type = question['question_type']
if question_id in sub_questions and question_type == 'calculated_question':
question_text = sub_questions[question_id]['question_text']
if question_type == 'text_only_question':
htmlfile.write(f"<div class='text-only-question'>{question_text}</div>")
continue
worth = question['points_possible']
answer = None
answer_text = ''
points = ''
if question_id in answers:
answer = answers[question_id]
answer_text = answer['text'] if 'text' in answer else ''
points = answer['points']
elif quiz_submission is not None:
question_type = None # To avoid formatting of multiple-choice
answer_text = '''
*** NO SUBMISSION ***<br/><br/>
This typically means that this question is part of a question
group, and the student did not receive this question in the
group (i.e., the student answered a different question in
this set).
'''
if question_type in ('calculated_question',
'short_answer_question',
'essay_question',
'numerical_question'):
pass # use answer exactly as provided
elif question_type in ('true_false_question',
'multiple_choice_question',
'multiple_answers_question'):
answer_text = ''
for pan in question['answers']:
if question_type == 'multiple_answers_question':
key = f"answer_{pan['id']}"
choice = answer[key] if answer is not None and key in answer else ''
if choice == '0':
choice = ''
else:
choice = 'X' if answer is not None and 'answer_id' in answer and pan['id'] == answer['answer_id'] else ''
answer_text += '<div class="mc-item">'
answer_text += f'<span class="mc-item-space"><span> {choice} </span></span>'
answer_text += ' '
answer_text += f'<span class="mc-item-text">{pan["text"]}</span>'
answer_text += '</div>'
elif question_type in ('fill_in_multiple_blanks_question',
'multiple_dropdowns_question'):
answer_text = '<table class="multiple-blanks-table">'
tokens = []
dd_answers = {}
for pan in question['answers']:
if pan['blank_id'] not in tokens:
tokens.append(pan['blank_id'])
dd_answers[pan['id']] = pan['text']
for token in tokens:
key = f'answer_for_{token}'
choice = answer[key] if answer is not None and key in answer else ''
if choice != '' and question_type == 'multiple_dropdowns_question' and choice in dd_answers:
choice = dd_answers[choice]
answer_text += '<tr>'
answer_text += f'<td class="multiple-blanks-token">{token}</td>'
answer_text += '<td>=></td>'
answer_text += f'<td class=multiple-blanks-answer>{choice}</td>'
answer_text += '</tr>'
answer_text += '</table>'
elif question_type == 'matching_question':
answer_text = '<table class="multiple-blanks-table">'
matches = {}
for match in question['matches']:
matches[f"{match['match_id']}"] = match['text']
for pan in question['answers']:
key = f"answer_{pan['id']}"
choice = matches[answer[key]] if answer is not None and key in answer and answer[key] in matches else ''
answer_text += '<tr>'
answer_text += f'<td class="multiple-blanks-token">{pan["text"]}</td>'
answer_text += '<td>=></td>'
answer_text += f'<td class="multiple-blanks-answer">{choice}</td>'
answer_text += '</tr>'
answer_text += '</table>'
elif question_type == 'file_upload_question':
pass # This is handled in the processing of history above.
elif question_type is not None:
raise ValueError(f'Invalid question type: "{question_type}"')
num_attempts_text = '' if num_attempts <= 1 else f' ({num_attempts} attempts)'
htmlfile.write(f'''<div class="question-preamble question-{question_id}"></div>
<div class="question-container question-{question_id}">
<h2 class="question-title">Question {question_id} [{question_name}]:</h2>
<div class=question>{question_text}</div>
<div class=points-container>
<span class=points-possible><span>{worth} </span></span>
<span class=points-canvas><span>{points} </span></span>
</div>
<h3 class=answer-title>Answer{num_attempts_text}:</h3>
<div class=answer>{answer_text}</div>
</div>
''')
qnum += 1
def end_file(htmlfile):
htmlfile.write('</body>\n</html>')
htmlfile.close()
def question_included(qid):
if args.not_question and qid in args.not_question:
return False
if args.only_question:
return qid in args.only_question
return True
parser = argparse.ArgumentParser()
canvas.Canvas.add_arguments(parser, quiz=True)
parser.add_argument("-l", "--classlist", type=str, #type=argparse.FileType('r', newline=''),
help="""CSV file containing student number and account.
If used, account is provided on the front page, otherwise
it will include name and student number.""")
parser.add_argument("-p", "--output-prefix",
help="Path/prefix for output files")
group = parser.add_mutually_exclusive_group()
group.add_argument("--only-question", action='extend', nargs='+', type=int, metavar="QUESTIONID",
help="Questions to include")
group.add_argument("--not-question", action='extend', nargs='+', type=int, metavar="QUESTIONID",
help="Questions to exclude")
parser.add_argument("--css",
help="Additional CSS file to use in PDF creation.")
parser.add_argument("--template-only", action='store_true',
help="Create only the template, without students.")
args = parser.parse_args()
canvas = canvas.Canvas(args=args)
student_accounts = {}
htmlfile_list = []
if args.classlist:
print('Reading classlist...')
with open(args.classlist, 'r', newline='') as file:
reader = csv.DictReader(file)
if 'SNUM' not in reader.fieldnames:
raise ValueError('Classlist CSV file does not contain student number.')
if 'ACCT' not in reader.fieldnames:
raise ValueError('Classlist CSV file does not contain account.')
for row in reader:
student_accounts[row['SNUM']] = row['ACCT']
print('Reading data from Canvas...')
course = canvas.course(args.course, prompt_if_needed=True)
print(f"Using course: {course['term']['name']} / {course['course_code']}")
quiz = course.quiz(args.quiz, prompt_if_needed=True)
print(f"Using quiz: {quiz['title']}")
if not args.output_prefix:
args.output_prefix = re.sub(r'[^A-Za-z0-9-_]+', '', quiz['title'])
print(f'Using prefix: {args.output_prefix}')
# Reading questions
print('Retrieving quiz questions...')
(questions, groups) = quiz.questions(question_included)
print('Retrieving quiz submissions...')
if args.template_only:
quiz_submissions = []
submissions = {}
else:
(quiz_submissions, submissions) = quiz.submissions()
print('Generating HTML files...')
file_no = 1
template_file = start_file(f'{args.output_prefix}_template.html')
if not args.template_only:
exams_file = start_file(f'{args.output_prefix}_exams_{file_no}.html')
rawanswers_file = zipfile.ZipFile(f'{args.output_prefix}_raw_answers.zip', 'w')
write_exam_file(template_file, questions)
if args.debug:
with open('debug.json', 'w') as file:
data = {}
data['quiz'] = quiz.data
data['questions'] = questions
data['quiz_submissions'] = quiz_submissions
data['submissions'] = submissions
json.dump(data, file, indent=2)
num_exams = 0
for qs in quiz_submissions:
print(f"Exporting student {num_exams + 1} out of {len(quiz_submissions)}...", end='\r')
write_exam_file(exams_file, questions, qs)
num_exams += 1
if num_exams % 20 == 0:
end_file(exams_file)
file_no += 1
exams_file = start_file(f'{args.output_prefix}_exams_{file_no}.html')
end_file(template_file)
if not args.template_only:
end_file(exams_file)
rawanswers_file.close()
print('\nConverting to PDF...')
css = [weasyprint.CSS(path.join(path.dirname(__file__), 'canvasquiz.css'))]
if args.css:
css.append(weasyprint.CSS(args.css))
for file in htmlfile_list:
print(f'{file}... ', end='\r')
weasyprint.HTML(filename=file).write_pdf(f'{file}.pdf', stylesheets=css)
print('\nDONE. Created files:')
for file in htmlfile_list:
print(f'- {file}.pdf')
if not args.template_only:
print(f'- {args.output_prefix}_raw_answers.zip')