aboutsummaryrefslogtreecommitdiff
path: root/pandoc/writers/moodle-quiz.lua
blob: a2cc6fe88c6b0d285ccc112d0866c41cf388e17d (plain)
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
--- moodle-test.lua – writer to convert markdown questions into Moodle XML
---
--- Copyright: © 2026 Florian Fischer
--- License:   GPL-3.0-or-later

-- Collected questions
local questions = {}
local cur_question = nil

local concat = function(inlines)
	local s = pandoc.write(pandoc.Pandoc(pandoc.Plain(inlines)), 'plain', {wrap_text='wrap-none'})
	if s:sub(-1) == '\n' then
		s = s:sub(1, -2)
	end
	return s
end

local function find_answer_attributes(answer)
	local attributes = {}
	answer:walk({Span=function(s)
		if #s.classes > 0 then -- this is not the attribute span
			return
		end
		for k, v in pairs(s.attributes) do
			attributes[k] = v
		end
	end})
	return attributes
end

local function detect_question_type(question)
	local ret = ""
	if question.header.classes:includes("shortanswer") then
		ret = "shortanswer"
	elseif question.header.classes:includes("multichoice") then
		ret = "multichoice"
	elseif question.header.classes:includes("cloze") then
		ret = "cloze"
	elseif question.header.classes:includes("ddwtos") then
		ret = "ddwtos"
	else
		pandoc.log.warn("No question type found")
	end
	return ret
end

local function write_answers(question, default_fraction, a)
	for _, answer in ipairs(question.answers) do
		local answer_attributes = find_answer_attributes(answer)
		local frac = answer_attributes.fraction or default_fraction
		a('  <answer fraction="'..frac..'" format="html">\n')
		a('    <text><![CDATA[');
		a(pandoc.write(pandoc.Pandoc(answer), "html"))
		a(']]></text>\n')

		if answer.feedback then
			a('    <feedback format="html"><text><![CDATA[')
			a(pandoc.write(pandoc.Pandoc(answer.feedback), "html"))
			a(']]></text></feedback>\n')
		end
		a('  </answer>\n')
	end
end

local function write_shortanwser(question, a)
	a('  <defaultgrade>1.0000000</defaultgrade>\n')
	a('  <usecase>0</usecase>\n')
	write_answers(question, 100, a)
end

local function write_multichoice(question, a)
	local fractions = 0
	for _, answ in ipairs(question.answers) do
		local answer_attributes = find_answer_attributes(answ)
		if  answer_attributes.fraction and tonumber(answer_attributes.fraction) > 0 then
			fractions = fractions + 1
		end
	end

	a('  <defaultgrade>1.0000000</defaultgrade>\n')
	a('  <single>'..(fractions == 1 and "true" or "false")..'</single>')
	a('  <shuffleanswers>true</shuffleanswers>')
	a('  <answernumbering>none</answernumbering>')
	a('  <showstandardinstruction>0</showstandardinstruction>')
	a('  <correctfeedback format="html">')
	a('    <text><![CDATA[<p>Die Antwort ist richtig.</p>]]></text>')
	a('  </correctfeedback>')
	a('  <partiallycorrectfeedback format="html">')
	a('    <text><![CDATA[<p>Die Antwort ist teilweise richtig.</p>]]></text>')
	a('  </partiallycorrectfeedback>')
	a('  <incorrectfeedback format="html">')
	a('    <text><![CDATA[<p>Die Antwort ist falsch.</p>]]></text>')
	a('  </incorrectfeedback>')
	a('  <shownumcorrect/>')

	write_answers(question, 0, a)
end

local function write_cloze(question, a)
end

local function write_ddwtos(question, a)
	a('  <defaultgrade>1.0000000</defaultgrade>\n')
	a('  <shuffleanswers>0</shuffleanswers>\n')
	a('  <shownumcorrect/>\n')

	for _, answer in ipairs(question.answers) do
		local answer_attributes = find_answer_attributes(answer)
		local group = answer_attributes.fraction or 1

		a('  <dragbox>\n')
		a('    <text>');
		a(pandoc.write(pandoc.Pandoc(answer), "plain"))
		a('    </text>\n')
		a('    <group>' .. group..'</group>\n')

		if answer.feedback then
			a('    <feedback format="html"><text><![CDATA[')
			a(pandoc.write(pandoc.Pandoc(answer.feedback), "html"))
			a('    ]]></text></feedback>\n')
		end
		a('  </dragbox>\n')
	end
end


local write_question_for = {
	cloze       = write_cloze,
	ddwtos      = write_ddwtos,
	multichoice = write_multichoice,
	shortanswer = write_shortanwser,
}

local function write_questions()
	local res = ""
	local a = function(s) res = res .. s end

	for _, question in ipairs(questions) do
		if not question then
			io.stderr:write("nil question\n")
			-- pandoc.log.warn("nil question")
		end
		local question_type = detect_question_type(question)
		-- Question type
		a('<question type="' ..  question_type .. '">\n')

		-- Question name
		a('  <name><text>');
		a(concat(question.name))
		a('</text></name>\n')

		-- Question text
		a('  <questiontext format="html">\n    <text><![CDATA[')
		for _, part in ipairs(question.text) do
			a(pandoc.write(pandoc.Pandoc(part), "html"))
		end
		a(']]></text>\n  </questiontext>\n')
		a('  <generalfeedback format="html"><text></text></generalfeedback>\n')
		a('  <penalty>0.3333333</penalty>\n')
		a('  <hidden>0</hidden>\n')
		a('  <idnumber></idnumber>\n')

		-- questiontype specific tags
		local question_type_fn = write_question_for[question_type]
		question_type_fn(question, a)
		a('</question>\n')
	end

	return res
end

local function Header(h)
	cur_question = { header=h, text={}}
	table.insert(questions, cur_question)

	cur_question.name = h.content
end

local function Image(i)
	local datauri = pandoc.mediabag.make_data_uri(pandoc.mediabag.fetch(i.src))
	local ni = pandoc.Image(i.caption, datauri)
	ni.attributes = i.attributes
	return ni
end

local function Div(d)
	-- append question content
	table.insert(cur_question.text, pandoc.walk_block(d, {Image=Image})) -- embed all images
end

local function addAnswer(l)
	local fl = l:walk({Image=Image})
	cur_question.answers = fl.content
end

local filter = {
	-- Traverse the AST depth-first to reach the content of a question after
	-- reaching its header
	traverse = 'topdown',
	Header = Header, -- Detect questions
	Div = Div, -- Collect question content
	BulletList = addAnswer, -- Collect question answers
	OrderedList = addAnswer, -- Collect question answers
}

Writer = function(doc, opts)
	local ctx = pandoc.template.meta_to_context(doc.meta, concat, concat)

	-- collect questions
	doc:walk(filter)
	local body = write_questions()

	ctx['body'] = body
	for k, v in pairs(opts.variables) do
		ctx[k] = v
	end

	if not opts.template then
		pandoc.log.warn("No quiz template specified")
		os.exit(1)
	end
	local complete = pandoc.template.apply(opts.template, ctx)
	return complete:render()
end