diff --git a/XML_Moodle.py b/XML_Moodle.py index da3c8ba7fdb14ad6cf4973a88ca4ada70fb759f2..b77d20bb04220ee50a322b7155167f0e90fc5546 100755 --- a/XML_Moodle.py +++ b/XML_Moodle.py @@ -22,7 +22,7 @@ class Quizz: if q.attrib['type'] == "multichoice": newQ = MCQ(q,c,len(self.questions[c]),self.folder) elif q.attrib['type'] == "shortanswer": - newQ = Question(q,c,len(self.questions[c]),self.folder) + newQ = ShortAnswer(q,c,len(self.questions[c]),self.folder) elif q.attrib['type'] == "truefalse": newQ = TF(q,c,len(self.questions[c]),self.folder) elif q.attrib['type'] == "matching": @@ -101,11 +101,15 @@ class Answer: class MCQ(Question): + HORIZONTAL = 1 + VERTICAL = 0 def __init__(self, xmlQ,c,n,f): super().__init__(xmlQ,c,n,f) self.choices = [] + self.choice_type = MCQ.VERTICAL self.env = "questionmult" - self.__parseAnswers(xmlQ) + if self.__class__.__name__ == "MCQ": + self.__parseAnswers(xmlQ) def __parseAnswers(self, xmlQ): self.shuffle = xmlQ.find("shuffleanswers").text == "true" @@ -115,25 +119,32 @@ class MCQ(Question): fb = mlang_2_multiling(strip_tags(fb, self.folder)) self.choices.append(Answer(mlang_2_multiling(strip_tags(a.find("text").text, self.folder)), a.attrib['fraction'], self.max, fb)) + def get_choice_env(self): + if self.choice_type == MCQ.VERTICAL: + return "choices" + else: + return "choiceshoriz" + def __str__(self): res = """\\element{"""+self.category+"""}{ \\begin{"""+self.env+"""}{"""+self.id+"""}\\nbpoints{"""+score_2_str(self.max)+"""} - """+self.q+"\n\t\t\\begin{choices}" + """+self.q+"\n\t\t\\begin{"+self.get_choice_env()+"}" if not self.shuffle: res += "[o]" res += "\n" for c in self.choices: res += str(c) - res += "\n\t\t\\end{choices}\n\t\\end{"+self.env+"}\n}\n\n" + res += "\n\t\t\\end{"+self.get_choice_env()+"}\n\t\\end{"+self.env+"}\n}\n\n" return res class TF(MCQ): def __init__(self, xmlQ,c,n,f): - super(MCQ, self).__init__(xmlQ,c,n,f) + super().__init__(xmlQ,c,n,f) self.choices = [] self.env = "question" self.shuffle = False - self.__parseAnswers(xmlQ) + if self.__class__.__name__ == "MCQ": + self.__parseAnswers(xmlQ) def __parseAnswers(self, xmlQ): for a in xmlQ.findall("answer"): @@ -145,6 +156,29 @@ class TF(MCQ): else: self.choices.append(Answer("\\multiling{faux}{false}", a.attrib['fraction'], self.max, fb)) +class ShortAnswer(TF): + def __init__(self, xmlQ,c,n,f,l=4): + super().__init__(xmlQ,c,n,f) + self.nb_lines = l + if self.__class__.__name__ == "ShortAnswer": + self.__parseAnswers(xmlQ) + + def __parseAnswers(self, xmlQ): + note = 0 + while note < self.max: + self.choices.append(note) + note += 0.25 + + def __str__(self): + res = """\\element{"""+self.category+"""}{ + \\begin{"""+self.env+"""}{"""+self.id+"""}\\nbpoints{"""+score_2_str(self.max)+"""} + """+self.q+"\n\t\t\\AMCOpen{lineheigh=0.8cm,lines="+str(self.nb_lines)+"}{" + for c in self.choices: + res += "\\wrongchoice{"+score_2_str(c)+"}\\scoring{"+score_2_str(c)+"}" + res += "\\correctchoice{"+score_2_str(self.max)+"}\\scoring{"+score_2_str(self.max)+"}" + res += "}\n\t\\end{"+self.env+"}\n}\n\n" + return res + if __name__ == "__main__": quizz = Quizz("data/quiz-GI-4-SID-S1-top-20201204-1620.xml", "data") #quizz = Quizz("data/quiz-GI-4.xml" , "data")