Trying to get to moodle formulas

This commit is contained in:
Martin Putzlocher 2024-11-29 18:28:13 +01:00
parent fc082a29d1
commit 087f1d7af1
4 changed files with 365 additions and 21 deletions

View File

@ -1,8 +1,34 @@
import xml.etree.ElementTree as ET
import random
from math import gcd
import re
def create_cloze_question(name, questiontext, correct_answers, wrong_answers, feedback, tags):
def create_html_table_for_fracture_input(rows: list):
table_rows = []
# Tabellenzeilen festlegen
for i, row in enumerate(rows):
if (i % 2) == 0:
table_row = "<tr style='border-bottom: 1px solid black;'>"
else:
table_row = "<tr>"
for cell in row:
table_row += f"<td style='padding: 5px;'>{cell}</td>"
table_row += "</tr>"
table_rows.append(table_row)
# Tabelle zusammenbauen
table_html = (
f"<table style='border-collapse: collapse;'>\n"
+ "\n".join(table_rows)
+ "</table>"
)
return re.sub("\\s+", " ", table_html)
def create_cloze_question(name, questiontext, question_math, calcstep_math, correct_answers, wrong_answers, feedback, tags):
question = ET.Element('question', attrib={'type': 'cloze'})
name_elem = ET.SubElement(question, 'name')
@ -13,13 +39,11 @@ def create_cloze_question(name, questiontext, correct_answers, wrong_answers, fe
text_elem = ET.SubElement(questiontext_elem, 'text')
# Erstelle die Tabelle für die Lücken
cloze_text = "<table style='border-collapse: collapse;'>"
cloze_text += "<tr>"
cloze_text += f"<td style='border-bottom: 1px solid black; padding: 5px;'>Zähler: {{1:SHORTANSWER:={correct_answers[0]}~%0%{wrong_answers[0][0]}#{wrong_answers[0][1]}}}</td>"
cloze_text += "</tr>"
cloze_text += "<tr>"
cloze_text += f"<td style='padding: 5px;'>Nenner: {{1:SHORTANSWER:={correct_answers[1]}~%0%{wrong_answers[1][0]}#{wrong_answers[1][1]}}}</td>"
cloze_text += "</tr>"
rows_list = [
["Zähler:", f"{{1:SHORTANSWER:={correct_answers[0]}~%0%{wrong_answers[0][0]}#{wrong_answers[0][1]}}}"],
["Nenner:", f"{{1:SHORTANSWER:={correct_answers[1]}~%0%{wrong_answers[1][0]}#{wrong_answers[1][1]}}}"]
]
# Prüfe, ob der Bruch gekürzt werden kann
if gcd(correct_answers[0], correct_answers[1]) > 1:
@ -27,14 +51,10 @@ def create_cloze_question(name, questiontext, correct_answers, wrong_answers, fe
reduced_numerator = correct_answers[0] // common_divisor
reduced_denominator = correct_answers[1] // common_divisor
cloze_text += "<tr>"
cloze_text += f"<td style='border-bottom: 1px solid black; padding: 5px;'>Gekürzter Zähler: {{1:SHORTANSWER:={reduced_numerator}~%0%{wrong_answers[2][0]}#{wrong_answers[2][1]}}}</td>"
cloze_text += "</tr>"
cloze_text += "<tr>"
cloze_text += f"<td style='padding: 5px;'>Gekürzter Nenner: {{1:SHORTANSWER:={reduced_denominator}~%0%{wrong_answers[3][0]}#{wrong_answers[3][1]}}}</td>"
cloze_text += "</tr>"
rows_list.append(["Gekürzter Zähler:",f"{{1:SHORTANSWER:={reduced_numerator}~%0%{wrong_answers[2][0]}#{wrong_answers[2][1]}}}"])
rows_list.append(["Gekürzter Nenner:",f"{{1:SHORTANSWER:={reduced_denominator}~%0%{wrong_answers[3][0]}#{wrong_answers[3][1]}}}"])
cloze_text += "</table>"
cloze_text = create_html_table_for_fracture_input(rows_list)
# Füge die Frage hinzu
text_elem.text = questiontext + " " + cloze_text
@ -42,11 +62,15 @@ def create_cloze_question(name, questiontext, correct_answers, wrong_answers, fe
generalfeedback_elem = ET.SubElement(question, 'generalfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(generalfeedback_elem, 'text')
solution = f"<h3>Lösungshinweise:</h3>"
solution += "<p>"
# Musterlösung im LaTeX-Format
if gcd(correct_answers[0], correct_answers[1]) > 1:
solution = f"Die Musterlösung ist: \\( \\Large \\displaystyle \\frac{{{reduced_numerator}}}{{{reduced_denominator}}} \\)."
solution += f"\\( {question_math} {calcstep_math} = \\frac{{{correct_answers[0]}}}{{{correct_answers[1]}}} = \\frac{{{reduced_numerator}}}{{{reduced_denominator}}} \\)."
else:
solution = f"Die Musterlösung ist: \\( \\Large \\displaystyle \\frac{{{correct_answers[0]}}}{{{correct_answers[1]}}} \\)."
solution += f"\\( {question_math} {calcstep_math} = \\frac{{{correct_answers[0]}}}{{{correct_answers[1]}}} \\)"
solution += "</p>"
text_elem.text = solution
@ -97,7 +121,7 @@ for i in range(1, (n+1)):
# Berechne den gemeinsamen Nenner, der kleiner als 30 sein muss
common_denominator = lcm(denominator1, lcm(denominator2, denominator3))
while common_denominator >= 30:
while common_denominator >= 50:
denominator1 = random.randint(2, 10)
denominator2 = random.randint(2, 10)
denominator3 = random.randint(2, 10)
@ -121,10 +145,14 @@ for i in range(1, (n+1)):
# Prüfe, ob der Bruch gekürzt werden kann
needs_reduction = gcd(result_numerator, common_denominator) > 1
question_puremath = f"\\Large \\displaystyle \\frac{{{numerator1}}}{{{denominator1}}} {operations[0]} \\left(\\frac{{{numerator2}}}{{{denominator2}}} {operations[2]} \\frac{{{numerator3}}}{{{denominator3}}}\\right) ="
# Frage im LaTeX-Format
questiontext = (f"<p>Berechne und kürze das Ergebnis, wenn möglich:</p> "
f"<p> \\( \\Large \\displaystyle \\frac{{{numerator1}}}{{{denominator1}}} {operations[0]} "
f"\\left(\\frac{{{numerator2}}}{{{denominator2}}} {operations[2]} \\frac{{{numerator3}}}{{{denominator3}}}\\right) = \\) </p>")
f"<p> \\( {question_puremath} \\) </p>")
# Feedback-Ausgabe für notwendigen Zwischenschritt
calculation_step_math = f"\\frac{{{equivalent_numerator1}}}{{{common_denominator}}} {operations[0]} \\left(\\frac{{{equivalent_numerator2}}}{{{common_denominator}}} {operations[2]} \\frac{{{equivalent_numerator3}}}{{{common_denominator}}}\\right)"
# Antworten
correct_answers = [result_numerator, common_denominator]
@ -153,6 +181,8 @@ for i in range(1, (n+1)):
questions.append(create_cloze_question(
f"Addition/Subtraktion von 3 ungleichnamigen Brüchen {i}",
questiontext,
question_puremath,
calculation_step_math,
correct_answers,
wrong_answers,
feedback,

View File

@ -0,0 +1,187 @@
import random
from math import gcd
import xml.etree.ElementTree as ET
def lcm(a, b):
"""Berechnet das kleinste gemeinsame Vielfache."""
return abs(a * b) // gcd(a, b)
def create_formula_question(numerator1, denominator1, numerator2, denominator2, n):
"""Erzeugt eine Frage im Format 'Formulas'."""
# GCD und das kleinste gemeinsame Vielfache für die Denominator
common_denominator = lcm(denominator1, denominator2)
new_numerator = (numerator1 * (common_denominator // denominator1)) + (numerator2 * (common_denominator // denominator2))
question = ET.Element('question', attrib={'type': 'formulas'})
name_elem = ET.SubElement(question, 'name')
text_elem = ET.SubElement(name_elem, 'text')
text_elem.text = 'Addition von 2 ungleichnamigen Brüchen {}'.format(n)
questiontext_elem = ET.SubElement(question, 'questiontext', attrib={'format': 'html'})
text_elem = ET.SubElement(questiontext_elem, 'text')
text_elem.text = '''<![CDATA[ <p>Addiere die Brüche und kürze das Ergebnis so weit wie möglich.</p>
<p>{#A} </p> ]]> '''
# Allgemeines Feedback
generalfeedback_elem = ET.SubElement(question, 'generalfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(generalfeedback_elem, 'text')
text_elem.text = ' '
# Grundlegende Bewertungsinformationen
ET.SubElement(question, 'defaultgrade').text = '1.0000000'
ET.SubElement(question, 'penalty').text = '0.3333333'
ET.SubElement(question, 'hidden').text = '0'
ET.SubElement(question, 'idnumber').text = ' '
# Feedback für korrekte und inkorrekte Antworten
correctfeedback_elem = ET.SubElement(question, 'correctfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(correctfeedback_elem, 'text')
text_elem.text = '''<![CDATA[ <p>Die Antwort ist richtig.</p> ]]> '''
partiallycorrectfeedback_elem = ET.SubElement(question, 'partiallycorrectfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(partiallycorrectfeedback_elem, 'text')
text_elem.text = '''<![CDATA[ <p>Die Antwort ist teilweise richtig.</p> ]]> '''
incorrectfeedback_elem = ET.SubElement(question, 'incorrectfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(incorrectfeedback_elem, 'text')
text_elem.text = '''<![CDATA[ <p>Die Antwort ist falsch.</p> ]]>'''
ET.SubElement(question, 'shownumcorrect')
varsrandom_elem = ET.SubElement(question, 'varsrandom')
text_elem = ET.SubElement(varsrandom_elem, 'text')
text_elem.text = f' '
# Globale Variablen definieren
varsglobal_elem = ET.SubElement(question, 'varsglobal')
text_elem = ET.SubElement(varsglobal_elem, 'text')
text_elem.text = f'n = [{numerator1}, {numerator2}];\n d = [{denominator1}, {denominator2}];'
answernumbering_elem = ET.SubElement(question, 'answernumbering')
text_elem = ET.SubElement(answernumbering_elem, 'text')
text_elem.text = 'ABCD'
# Antworten definieren
answers_elem = ET.SubElement(question, 'answers')
# Indizes für die Antworten
partindex_elem = ET.SubElement(answers_elem, 'partindex')
ET.SubElement(partindex_elem, 'text').text = '0'
placeholder_elem = ET.SubElement(answers_elem, 'placeholder')
ET.SubElement(placeholder_elem, 'text').text = '#A'
answermark_elem = ET.SubElement(answers_elem, 'answermark')
ET.SubElement(answermark_elem, 'text').text = '1'
answertype_elem = ET.SubElement(answers_elem, 'answertype')
ET.SubElement(answertype_elem, 'text').text = '0'
numbox_elem = ET.SubElement(answers_elem, 'numbox')
ET.SubElement(numbox_elem, 'text').text = '2'
# Berechnungsvariablen
vars1_elem = ET.SubElement(answers_elem, 'vars1')
vars1_elem_text = f'g = gcd(d[0],d[1]);\ncd = d[0]*d[1]/pow(g,2);\ne = [cd / d[0], cd / d[1]];\nnums = [n[0]*e[0], n[1]*e[1]];\nthesumnum = nums[0] + nums[1]'
ET.SubElement(vars1_elem, 'text').text = vars1_elem_text
# Antwort
answer_elem = ET.SubElement(answers_elem, 'answer')
answer_text = f'[thesumnum, cd]'
ET.SubElement(answer_elem, 'text').text = answer_text
answernotunique_elem = ET.SubElement(answers_elem, 'answernotunique')
ET.SubElement(answernotunique_elem, 'text').text = '0'
vars2_elem = ET.SubElement(answers_elem, 'vars2')
vars2_elem_text = u"<![CDATA[ ga = _0 == thesumnum && _1 == cd; gb = (_0 / _1) == (thesumnum / cd); ]]>"
ET.SubElement(vars2_elem, 'text').text = vars2_elem_text
correctness_elem = ET.SubElement(answers_elem, 'correctness')
ET.SubElement(correctness_elem, 'text').text = 'ga + 0.5*gb'
unitpenalty_elem = ET.SubElement(answers_elem, 'unitpenalty')
ET.SubElement(unitpenalty_elem, 'text').text = '1'
postunit_elem = ET.SubElement(answers_elem, 'postunit')
ET.SubElement(postunit_elem, 'text').text = ' '
ruleid_elem = ET.SubElement(answers_elem, 'ruleid')
ET.SubElement(ruleid_elem, 'text').text = '1'
otherrule_elem = ET.SubElement(answers_elem, 'otherrule')
ET.SubElement(otherrule_elem, 'text').text = ' '
# Unterfragen-Text
subqtext_elem = ET.SubElement(answers_elem, 'subqtext', attrib={'format': 'html'})
sub_question_text = '''<![CDATA[ <p>Berechne:</p> <p>\\(\\Large \\displaystyle \\frac{{n[0]}}{{d[0]}} + \\frac{{n[1]}}{{d[1]}} =\\) {_0} / {_1}</p> ]]> '''
ET.SubElement(subqtext_elem, 'text').text = sub_question_text
# Lösungshinweis
feedback_elem = ET.SubElement(answers_elem, 'feedback', attrib={'format': 'html'})
feedback_text = '''<![CDATA[ <h4>Lösungshinweis</h4>
<p>Die korrekte Lösung ist:</p>
<p>\\(\\Large \\displaystyle \\frac{{n[0]}}{{d[0]}} + \\frac{{n[1]}}{{d[1]}} = \\frac{{nums[0]}}{{cd}} + \\frac{{nums[1]}}{{cd}} = \\frac{{thesumnum}}{{cd}}\\) </p> ]]>'''
ET.SubElement(feedback_elem, 'text').text = feedback_text
# correctfeedback
correctfeedback_elem = ET.SubElement(answers_elem, 'correctfeedback', attrib={'format': 'html'})
correctfeedback_text = '''<![CDATA[ <p>Richtig!</p>]]>'''
ET.SubElement(correctfeedback_elem, 'text').text = correctfeedback_text
# partially correct feedback
pcorrectfeedback_elem = ET.SubElement(answers_elem, 'partiallycorrectfeedback', attrib={'format': 'html'})
pcorrectfeedback_text = '''<![CDATA[ <p>Teilweise richtig.</p>]]>'''
ET.SubElement(pcorrectfeedback_elem, 'text').text = pcorrectfeedback_text
# incorrect feedback
incorrectfeedback_elem = ET.SubElement(answers_elem, 'incorrectfeedback', attrib={'format': 'html'})
incorrectfeedback_text = '''<![CDATA[ <p>Leider nicht richtig.</p>]]>'''
ET.SubElement(incorrectfeedback_elem, 'text').text = incorrectfeedback_text
return question
def generate_questions(num_questions):
"""Generiert Aufgaben für die Addition ungleichnamiger Brüche."""
questions = []
for i in range(num_questions):
numerator1 = random.randint(1, 10)
denominator1 = random.randint(2, 10)
numerator2 = random.randint(1, 10)
denominator2 = random.randint(2, 10)
# Sicherstellen, dass die Nenner unterschiedlich sind
while denominator1 == denominator2:
denominator2 = random.randint(2, 10)
question = create_formula_question(numerator1, denominator1, numerator2, denominator2, i)
questions.append(question)
return questions
def create_quiz(questions):
"""Erzeugt die gesamte Quiz-Struktur."""
quiz = ET.Element('quiz')
for q in questions:
quiz.append(q)
return quiz
def save_to_file(xml_element, filename):
"""Speichert das XML-Dokument in einer Datei."""
tree = ET.ElementTree(xml_element)
with open(filename, 'wb') as f:
tree.write(f, encoding='utf-8', xml_declaration=True)
# Hauptprogramm
if __name__ == "__main__":
num_questions = 1 # Anzahl der zu generierenden Aufgaben
questions = generate_questions(num_questions)
quiz = create_quiz(questions)
save_to_file(quiz, 'formulas_fraction_addition_quiz.xml')
print("Moodle-XML-Datei 'formulas_fraction_addition_quiz.xml' erfolgreich erstellt.")

View File

@ -0,0 +1,10 @@
<?xml version='1.0' encoding='utf-8'?>
<quiz><question type="formulas"><name><text>Addition von 2 ungleichnamigen Brüchen 0</text></name><questiontext format="html"><text>&lt;![CDATA[ &lt;p&gt;Addiere die Brüche und kürze das Ergebnis so weit wie möglich.&lt;/p&gt;
&lt;p&gt;{#A} &lt;/p&gt; ]]&gt; </text></questiontext><generalfeedback format="html"><text> </text></generalfeedback><defaultgrade>1.0000000</defaultgrade><penalty>0.3333333</penalty><hidden>0</hidden><idnumber> </idnumber><correctfeedback format="html"><text>&lt;![CDATA[ &lt;p&gt;Die Antwort ist richtig.&lt;/p&gt; ]]&gt; </text></correctfeedback><partiallycorrectfeedback format="html"><text>&lt;![CDATA[ &lt;p&gt;Die Antwort ist teilweise richtig.&lt;/p&gt; ]]&gt; </text></partiallycorrectfeedback><incorrectfeedback format="html"><text>&lt;![CDATA[ &lt;p&gt;Die Antwort ist falsch.&lt;/p&gt; ]]&gt;</text></incorrectfeedback><shownumcorrect /><varsrandom><text> </text></varsrandom><varsglobal><text>n = [5, 3];
d = [9, 6];</text></varsglobal><answernumbering><text>ABCD</text></answernumbering><answers><partindex><text>0</text></partindex><placeholder><text>#A</text></placeholder><answermark><text>1</text></answermark><answertype><text>0</text></answertype><numbox><text>2</text></numbox><vars1><text>g = gcd(d[0],d[1]);
cd = d[0]*d[1]/pow(g,2);
e = [cd / d[0], cd / d[1]];
nums = [n[0]*e[0], n[1]*e[1]];
thesumnum = nums[0] + nums[1]</text></vars1><answer><text>[thesumnum, cd]</text></answer><answernotunique><text>0</text></answernotunique><vars2><text>&lt;![CDATA[ ga = _0 == thesumnum &amp;&amp; _1 == cd; gb = (_0 / _1) == (thesumnum / cd); ]]&gt;</text></vars2><correctness><text>ga + 0.5*gb</text></correctness><unitpenalty><text>1</text></unitpenalty><postunit><text> </text></postunit><ruleid><text>1</text></ruleid><otherrule><text> </text></otherrule><subqtext format="html"><text>&lt;![CDATA[ &lt;p&gt;Berechne:&lt;/p&gt; &lt;p&gt;\(\Large \displaystyle \frac{{n[0]}}{{d[0]}} + \frac{{n[1]}}{{d[1]}} =\) {_0} / {_1}&lt;/p&gt; ]]&gt; </text></subqtext><feedback format="html"><text>&lt;![CDATA[ &lt;h4&gt;Lösungshinweis&lt;/h4&gt;
&lt;p&gt;Die korrekte Lösung ist:&lt;/p&gt;
&lt;p&gt;\(\Large \displaystyle \frac{{n[0]}}{{d[0]}} + \frac{{n[1]}}{{d[1]}} = \frac{{nums[0]}}{{cd}} + \frac{{nums[1]}}{{cd}} = \frac{{thesumnum}}{{cd}}\) &lt;/p&gt; ]]&gt;</text></feedback><correctfeedback format="html"><text>&lt;![CDATA[ &lt;p&gt;Richtig!&lt;/p&gt;]]&gt;</text></correctfeedback><partiallycorrectfeedback format="html"><text>&lt;![CDATA[ &lt;p&gt;Teilweise richtig.&lt;/p&gt;]]&gt;</text></partiallycorrectfeedback><incorrectfeedback format="html"><text>&lt;![CDATA[ &lt;p&gt;Leider nicht richtig.&lt;/p&gt;]]&gt;</text></incorrectfeedback></answers></question></quiz>

View File

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<quiz>
<!-- question: 35853 -->
<question type="formulas">
<name>
<text>Addition von 2 ungleichnamigen Brüchen</text>
</name>
<questiontext format="html">
<text>
<![CDATA[<p>Addiere die Brüche und kürze das Ergebnis so weit wie möglich.</p><p>{#A}</p>]]>
</text>
</questiontext>
<generalfeedback format="html">
<text></text>
</generalfeedback>
<defaultgrade>1.0000000</defaultgrade>
<penalty>0.3333333</penalty>
<hidden>0</hidden>
<idnumber></idnumber>
<correctfeedback format="html">
<text>
<![CDATA[<p>Die Antwort ist richtig.</p>]]>
</text>
</correctfeedback>
<partiallycorrectfeedback format="html">
<text>
<![CDATA[<p>Die Antwort ist teilweise richtig.</p>]]>
</text>
</partiallycorrectfeedback>
<incorrectfeedback format="html">
<text>
<![CDATA[<p>Die Antwort ist falsch.</p>]]>
</text>
</incorrectfeedback>
<shownumcorrect/>
<varsrandom>
<text></text>
</varsrandom>
<varsglobal>
<text>n = [3, 7];
d = [2, 3];</text>
</varsglobal>
<answernumbering>
<text>ABCD</text>
</answernumbering>
<answers>
<partindex>
<text>0</text>
</partindex>
<placeholder>
<text>#A</text>
</placeholder>
<answermark>
<text>1</text>
</answermark>
<answertype>
<text>0</text>
</answertype>
<numbox>
<text>2</text>
</numbox>
<vars1>
<text>g = gcd(d[0],d[1]);
cd = d[0]*d[1]/pow(g,2);
e = [cd / d[0], cd / d[1]];
nums = [n[0]*e[0], n[1]*e[1]];
thesumnum = nums[0] + nums[1]</text>
</vars1>
<answer>
<text>[thesumnum, cd]</text>
</answer>
<answernotunique>
<text>0</text>
</answernotunique>
<vars2>
<text>
<![CDATA[ga = _0 == thesumnum && _1 == cd;
gb = (_0 / _1) == (thesumnum / cd);]]>
</text>
</vars2>
<correctness>
<text>ga + 0.5*gb</text>
</correctness>
<unitpenalty>
<text>1</text>
</unitpenalty>
<postunit>
<text></text>
</postunit>
<ruleid>
<text>1</text>
</ruleid>
<otherrule>
<text></text>
</otherrule>
<subqtext format="html">
<text>
<![CDATA[<p>Berechne:</p><p>\(\Large \displaystyle \frac{{n[0]}}{{d[0]}} + \frac{{n[1]}}{{d[1]}} =\) {_0} / {_1}</p>]]>
</text>
</subqtext>
<feedback format="html">
<text>
<![CDATA[<h4>Lösungshinweis</h4><p>Die korrekte Lösung ist:</p><p>\(\Large \displaystyle \frac{{n[0]}}{{d[0]}} + \frac{{n[1]}}{{d[1]}} = \frac{{nums[0]}}{{cd}} + \frac{{nums[1]}}{{cd}} = \frac{{thesumnum}}{{cd}}\)</p>]]>
</text>
</feedback>
<correctfeedback format="html">
<text></text>
</correctfeedback>
<partiallycorrectfeedback format="html">
<text></text>
</partiallycorrectfeedback>
<incorrectfeedback format="html">
<text></text>
</incorrectfeedback>
</answers>
</question>
</quiz>