-
Notifications
You must be signed in to change notification settings - Fork 2
/
MdlClz.py
87 lines (70 loc) · 2.03 KB
/
MdlClz.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 20 18:24:17 2020
@author: cghiaus
Module MOODLE cloze questions generated by Python
"""
from itertools import product
# In <question>Ò
penalty = 0.3333 # for each tried answer
question_structure = """
\n <question type="cloze">
\n <name>
\n <text>{question_name:s}</text>
\n </name>
\n <questiontext format="markdown">
\n <text>
\n <![CDATA[
\n {text:s}
\n ]]>
\n </text>
\n </questiontext>
\n <generalfeedback format="html">
\n <text></text>
\n </generalfeedback>
\n <penalty>{p:.3f}</penalty>
\n <hidden>0</hidden>
\n <idnumber></idnumber>
\n </question>
"""
def cprod(dictionary):
"""Generate a list of dicts of cartesian product combinations."""
return (dict(zip(dictionary, x)) for x
in product(*dictionary.values()))
def generate_quiz(question_name, problem_fun, x_ranges, text):
"""
Parameters
----------
question_name : str
Used for the .xml file and the 'Question name' in Moodle.
In Moodle the number of the question is added (e.g. question_003)
problem_fun : function
DESCRIPTION.
x_ranges : dict
DESCRIPTION.
text : str
DESCRIPTION.
Returns
-------
quiz : str
Test of .xml file
"""
question_number = 0
questions = ""
for x in cprod(x_ranges):
question_text = text.format(**x, **problem_fun(x))
question_name_number = question_name + '_'
question_name_number += str(question_number).zfill(3)
questions += question_structure.format(
question_name=question_name_number,
text=question_text,
p=penalty)
question_number += 1
quiz = '<?xml version="1.0" encoding="UTF-8"?>' + '\n<quiz>'
quiz += questions
quiz += '\n</quiz>'
xml_file_name = question_name + '.xml'
with open(xml_file_name, 'w', encoding="utf-8") as MOODLE_cloze:
MOODLE_cloze.write(quiz)
return quiz