From deea01db9a5116376dde8343e4f406bbea4e3507 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Sat, 17 Oct 2026 21:41:18 +0200 Subject: moodle-quiz: add support for multichoice questions --- pandoc/filters/moodle-quiz.lua | 66 +++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 11 deletions(-) (limited to 'pandoc') diff --git a/pandoc/filters/moodle-quiz.lua b/pandoc/filters/moodle-quiz.lua index ab26f93c..92a4e893 100644 --- a/pandoc/filters/moodle-quiz.lua +++ b/pandoc/filters/moodle-quiz.lua @@ -15,6 +15,19 @@ local concat = function(inlines) 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 @@ -27,12 +40,11 @@ local function detect_question_type(question) return ret end -local function write_shortanwser_answers(question) - local ret = "" - local a = function(s) ret = ret .. s end - +local function write_answers(question, default_fraction, a) for _, answer in ipairs(question.answers) do - a(' \n') + local answer_attributes = find_answer_attributes(answer) + local frac = answer_attributes.fraction or default_fraction + a(' \n') a(' \n') @@ -44,11 +56,43 @@ local function write_shortanwser_answers(question) end a(' \n') end - return ret end -local write_answers_for = { - shortanswer = write_shortanwser_answers, +local function write_shortanwser(question, a) + a(' 0\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(' '..(fractions == 1 and "true" or "false")..'') + a(' true') + a(' none') + a(' 0') + a(' ') + a(' Die Antwort ist richtig.

]]>
') + a('
') + a(' ') + a(' Die Antwort ist teilweise richtig.

]]>
') + a('
') + a(' ') + a(' Die Antwort ist falsch.

]]>
') + a('
') + a(' ') + + write_answers(question, 0, a) +end + +local write_question_for = { + shortanswer = write_shortanwser, + multichoice = write_multichoice, } local function write_questions() @@ -79,10 +123,10 @@ local function write_questions() a(' 0.3333333\n') a(' 0\n') a(' \n') - a(' 0\n') - local answer_fn = write_answers_for[question_type] - a(answer_fn(question)) + -- questiontype specific tags + local question_type_fn = write_question_for[question_type] + question_type_fn(question, a) a('\n') end -- cgit v1.2.3