-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_cmds.py
93 lines (70 loc) · 2.94 KB
/
problem_cmds.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
from ..ProblemParser.parser import parse
from ..ProblemParser.parser_webassign_new import parse_webassign
# from ..ProblemParser.parser_webassign import parse_webassign
from ..MathProblems.problem_basic import MultipleChoice
from ..MathProblems.problem_parametermultchoice import MultipleChoiceParameterProblem
from ..QTI.problem_txtqti import Problems2Text
from ..widgets.widgets_problem_basic import MultipleChoiceWidget
from ..widgets.widgets_problem_param import MultipleChoiceParameterWidget
import text2qti as t2q
def make_problem(prob_string, externals=None, webassign=False):
"""Generate a problem from a string in Problem format
:param prob_string: string containing the problem
:returns: object of Problem class
:param externals: additional dictionary to be used for instantiation
:param webassign: Boolean whether to use WebAssign parser instead
"""
if externals is None:
externals = {}
if webassign:
init_dict = parse_webassign(prob_string)
else:
init_dict = parse(prob_string)
if 'parameters' in init_dict:
return MultipleChoiceParameterProblem(init_dict, externals)
else:
return MultipleChoice(init_dict)
def make_qti(problem, num_questions: int, solution=False, externals=None):
"""Generate a QTI quiz file from a problem
:param problem: Multiple Choice Parameter Problem
:param num_questions: number of questions to be generated
:param solution: Boolean whether to include solution or not
:param externals: additional dictionary to be used for instantiation
:returns: nothing
"""
# print(num_questions)
if externals is None:
externals = {}
title_string = problem.problem_id+" - "+problem.title
if not solution:
title_string += " (no solutions)"
plist = problem.get_list(num_questions, externals)
qti_problem = Problems2Text(plist, with_solution=solution)
qti_problem.make_text_quiz(
title=title_string, shuffle_answers=True)
print("Generating " + str(num_questions) + "problems and saving as QTI")
if not solution:
print("(without solutions)")
# print all questions as txt (uncomment for debugging)
# print(qti_problem.txt_quiz)
# load text2qti config
config = t2q.Config()
config.load()
# text2qti.Quiz object, passing contents of txt file
quiz = t2q.Quiz(qti_problem.txt_quiz, config=config)
# text2qti.QTI object (to be written into a .zip)
qti = t2q.QTI(quiz)
# write the text2qti.QTI object into .zip
if solution:
file_name = problem.problem_id+".zip"
else:
file_name = problem.problem_id+"_nosol.zip"
qti.save(file_name)
print("QTI file saved as "+file_name)
def show_problem(problem):
if isinstance(problem, MultipleChoiceParameterProblem):
MultipleChoiceParameterWidget(problem).show()
elif isinstance(problem, MultipleChoice):
MultipleChoiceWidget(problem).show()
else:
pass