mdlAufgabengeneratoren/aufgabengenerator_runden_v2.py

232 lines
9.4 KiB
Python
Raw Normal View History

import xml.etree.ElementTree as ET
import random
units = ["m", "cm", "mm", "kg", "g", "L", "ml", "s", "ms"]
def create_multichoice_question(name, questiontext, feedback, answers, tags, unit):
question = ET.Element('question', attrib={'type': 'multichoice'})
name_elem = ET.SubElement(question, 'name')
text_elem = ET.SubElement(name_elem, 'text')
text_elem.text = name
questiontext_elem = ET.SubElement(question, 'questiontext', attrib={'format': 'html'})
text_elem = ET.SubElement(questiontext_elem, 'text')
text_elem.text = questiontext
generalfeedback_elem = ET.SubElement(question, 'generalfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(generalfeedback_elem, 'text')
text_elem.text = feedback
defaultgrade_elem = ET.SubElement(question, 'defaultgrade')
defaultgrade_elem.text = '1.0000000'
penalty_elem = ET.SubElement(question, 'penalty')
penalty_elem.text = '0.3333333'
hidden_elem = ET.SubElement(question, 'hidden')
hidden_elem.text = '0'
single_elem = ET.SubElement(question, 'single')
single_elem.text = 'true'
shuffleanswers_elem = ET.SubElement(question, 'shuffleanswers')
shuffleanswers_elem.text = 'true'
answernumbering_elem = ET.SubElement(question, 'answernumbering')
answernumbering_elem.text = 'abc'
correctfeedback_elem = ET.SubElement(question, 'correctfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(correctfeedback_elem, 'text')
text_elem.text = 'Richtig!'
partiallycorrectfeedback_elem = ET.SubElement(question, 'partiallycorrectfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(partiallycorrectfeedback_elem, 'text')
text_elem.text = 'Teilweise richtig. (Einheit fehlt oder ist falsch)'
incorrectfeedback_elem = ET.SubElement(question, 'incorrectfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(incorrectfeedback_elem, 'text')
text_elem.text = 'Falsch.'
for answer_text, fraction, feedback_text in answers:
answer_elem = ET.SubElement(question, 'answer', attrib={'fraction': str(fraction), 'format': 'html'})
text_elem = ET.SubElement(answer_elem, 'text')
text_elem.text = answer_text
feedback_elem = ET.SubElement(answer_elem, 'feedback', attrib={'format': 'html'})
text_elem = ET.SubElement(feedback_elem, 'text')
text_elem.text = feedback_text
tags_elem = ET.SubElement(question, 'tags')
for tag in tags:
tag_elem = ET.SubElement(tags_elem, 'tag')
text_elem = ET.SubElement(tag_elem, 'text')
text_elem.text = tag
# Einheitsangabe hinzufügen
units_elem = ET.SubElement(question, 'units')
unit_elem = ET.SubElement(units_elem, 'unit')
multiplier_elem = ET.SubElement(unit_elem, 'multiplier')
multiplier_elem.text = '1'
unit_name_elem = ET.SubElement(unit_elem, 'unit_name')
unit_name_elem.text = unit
unitgradingtype_elem = ET.SubElement(question, 'unitgradingtype')
unitgradingtype_elem.text = '1'
unitpenalty_elem = ET.SubElement(question, 'unitpenalty')
unitpenalty_elem.text = '0.1000000'
showunits_elem = ET.SubElement(question, 'showunits')
showunits_elem.text = '2'
unitsleft_elem = ET.SubElement(question, 'unitsleft')
unitsleft_elem.text = '0'
return question
def create_numerical_question(name, questiontext, feedback, correct_answer, tolerance, unit, tags):
question = ET.Element('question', attrib={'type': 'numerical'})
name_elem = ET.SubElement(question, 'name')
text_elem = ET.SubElement(name_elem, 'text')
text_elem.text = name
questiontext_elem = ET.SubElement(question, 'questiontext', attrib={'format': 'html'})
text_elem = ET.SubElement(questiontext_elem, 'text')
text_elem.text = questiontext
generalfeedback_elem = ET.SubElement(question, 'generalfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(generalfeedback_elem, 'text')
text_elem.text = feedback
defaultgrade_elem = ET.SubElement(question, 'defaultgrade')
defaultgrade_elem.text = '1.0000000'
penalty_elem = ET.SubElement(question, 'penalty')
penalty_elem.text = '0.3333333'
hidden_elem = ET.SubElement(question, 'hidden')
hidden_elem.text = '0'
answer_elem = ET.SubElement(question, 'answer', attrib={'fraction': '100'})
text_elem = ET.SubElement(answer_elem, 'text')
text_elem.text = f"{str(correct_answer).replace('.', ',')} {unit}"
tolerance_elem = ET.SubElement(answer_elem, 'tolerance')
tolerance_elem.text = str(tolerance).replace('.', ',')
feedback_elem = ET.SubElement(answer_elem, 'feedback', attrib={'format': 'html'})
text_elem = ET.SubElement(feedback_elem, 'text')
text_elem.text = 'Richtig!'
incorrectfeedback_elem = ET.SubElement(question, 'incorrectfeedback', attrib={'format': 'html'})
text_elem = ET.SubElement(incorrectfeedback_elem, 'text')
text_elem.text = 'Falsch.'
# Einheitsangabe hinzufügen
units_elem = ET.SubElement(question, 'units')
unit_elem = ET.SubElement(units_elem, 'unit')
multiplier_elem = ET.SubElement(unit_elem, 'multiplier')
multiplier_elem.text = '1'
unit_name_elem = ET.SubElement(unit_elem, 'unit_name')
unit_name_elem.text = unit
unitgradingtype_elem = ET.SubElement(question, 'unitgradingtype')
unitgradingtype_elem.text = '1'
unitpenalty_elem = ET.SubElement(question, 'unitpenalty')
unitpenalty_elem.text = '0.1000000'
showunits_elem = ET.SubElement(question, 'showunits')
showunits_elem.text = '2'
unitsleft_elem = ET.SubElement(question, 'unitsleft')
unitsleft_elem.text = '0'
tags_elem = ET.SubElement(question, 'tags')
for tag in tags:
tag_elem = ET.SubElement(tags_elem, 'tag')
text_elem = ET.SubElement(tag_elem, 'text')
text_elem.text = tag
return question
def create_quiz(questions):
quiz = ET.Element('quiz')
for q in questions:
quiz.append(q)
return quiz
def save_to_file(xml_element, filename):
tree = ET.ElementTree(xml_element)
with open(filename, 'wb') as f:
tree.write(f, encoding='utf-8', xml_declaration=True)
questions = []
# Generiere 5 Single-Choice-Fragen zum Runden auf ganze Zahlen
for i in range(1, 6):
number = round(random.uniform(1, 100), 2)
unit = random.choice(units)
rounded = round(number)
wrong1 = rounded + random.choice([1, 2])
wrong2 = rounded - random.choice([1, 2])
if wrong1 == rounded:
wrong1 += 1
if wrong2 == rounded:
wrong2 -= 1
answers = [
(f"{rounded} {unit}", 100, "Richtig!"),
(f"{wrong1} {unit}", 0, "Falsch."),
(f"{wrong2} {unit}", 0, "Falsch.")
]
questiontext = f"Runde {str(number).replace('.', ',')} {unit} auf die nächste ganze Zahl. (Bitte Einheit angeben!)"
feedback = f"Die korrekte Antwort ist {rounded} {unit}."
tags = ["Runden", "Dezimalbrüche", "Ganze Zahl", "Physikalische Einheit"]
questions.append(create_multichoice_question(f"Runden von Dezimalbrüchen {i}", questiontext, feedback, answers, tags, unit))
# Generiere 5 Single-Choice-Fragen zum Runden auf eine Dezimalstelle
for i in range(6, 11):
number = round(random.uniform(1, 100), 2)
unit = random.choice(units)
rounded = round(number, 1)
wrong1 = round(number + random.uniform(0.1, 0.2), 1)
wrong2 = round(number - random.uniform(0.1, 0.2), 1)
answers = [
(f"{str(rounded).replace('.', ',')} {unit}", 100, "Richtig!"),
(f"{str(wrong1).replace('.', ',')} {unit}", 0, "Falsch."),
(f"{str(wrong2).replace('.', ',')} {unit}", 0, "Falsch.")
]
questiontext = f"Runde {str(number).replace('.', ',')} {unit} auf eine Dezimalstelle. (Bitte Einheit angeben!)"
feedback = f"Die korrekte Antwort ist {str(rounded).replace('.', ',')} {unit}."
tags = ["Runden", "Dezimalbrüche", "Eine Dezimalstelle", "Physikalische Einheit"]
questions.append(create_multichoice_question(f"Runden von Dezimalbrüchen {i}", questiontext, feedback, answers, tags, unit))
# Generiere 5 numerische Fragen zum Runden auf zwei Dezimalstellen
for i in range(11, 16):
number = round(random.uniform(1, 100), 3)
unit = random.choice(units)
rounded = round(number, 2)
questiontext = f"Runde {str(number).replace('.', ',')} {unit} auf zwei Dezimalstellen. (Bitte Einheit angeben!)"
feedback = f"Die korrekte Antwort ist {str(rounded).replace('.', ',')} {unit}."
tolerance = 0.0049
tags = ["Runden", "Dezimalbrüche", "Zwei Dezimalstellen", "Physikalische Einheit"]
questions.append(create_numerical_question(f"Runden von Dezimalbrüchen {i}", questiontext, feedback, rounded, tolerance, unit, tags))
# Generiere 5 numerische Fragen zum Runden auf drei Dezimalstellen
for i in range(16, 21):
number = round(random.uniform(1, 100), 4)
unit = random.choice(units)
rounded = round(number, 3)
questiontext = f"Runde {str(number).replace('.', ',')} {unit} auf drei Dezimalstellen. (Bitte Einheit angeben!)"
feedback = f"Die korrekte Antwort ist {str(rounded).replace('.', ',')} {unit}."
tolerance = 0.00049
tags = ["Runden", "Dezimalbrüche", "Drei Dezimalstellen", "Physikalische Einheit"]
questions.append(create_numerical_question(f"Runden von Dezimalbrüchen {i}", questiontext, feedback, rounded, tolerance, unit, tags))
quiz = create_quiz(questions)
save_to_file(quiz, 'runden_von_dezimalbruechen_quiz.xml')
print("Moodle-XML-Datei 'runden_von_dezimalbruechen_quiz.xml' erfolgreich erstellt.")