From 4420db3d7633086fc3ddf55419350476d159ac46 Mon Sep 17 00:00:00 2001
From: Mathieu Loiseau <mathieu.loiseau@univ-grenoble-alpes.fr>
Date: Tue, 25 Jan 2022 12:42:34 +0100
Subject: [PATCH] nb points

---
 .gitignore    |  2 +-
 XML_Moodle.py | 32 +++++++++++++++++++-------------
 utils.py      |  7 +++++++
 3 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/.gitignore b/.gitignore
index efae346..ecc788e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,2 @@
-quiz-GI-4-SID-S1-top-20201204-1620.xml
+quiz-*.xml
 __pycache__
diff --git a/XML_Moodle.py b/XML_Moodle.py
index 199cd19..eadbb07 100755
--- a/XML_Moodle.py
+++ b/XML_Moodle.py
@@ -1,11 +1,11 @@
 #!/usr/bin/env python3
 import xml.etree.ElementTree as ET
-from utils import strip_tags, mlang_2_multiling
+from utils import strip_tags, mlang_2_multiling, score_2_str
 class Quizz:
 	def __init__(self, file_name):
 		self.file_name = file_name
 		self.tree = ET.parse(self.file_name)
-		self.questions = []
+		self.questions = {}
 		self.parse()
 
 	def get_tree(self):
@@ -17,33 +17,39 @@ class Quizz:
 			if q.attrib["type"] == "category":
 				c = q.find("category/text").text.replace("$course$/top/","").replace("/",":").replace("::","/")
 			else:
-				self.questions.append(Question(q,c))
+				if c not in self.questions.keys():
+					self.questions[c] = []
+				self.questions[c].append(Question(q,c,len(self.questions[c])))
 
 	def __str__(self):
 		res = ""
-		for q in self.questions:
-			res += str(q)
+		for c_q in self.questions.values():
+			for q in c_q:
+				res += str(q)
 		return res
 
 class Question:
-	def __init__(self,xmlQ,c):
+	def __init__(self,xmlQ,c,n):
 		self.q = mlang_2_multiling(strip_tags(xmlQ.find("questiontext/text").text))
 		self.category = c
+		self.id = f"{c[c.rfind(':')+1:]}_{n}"
+		self.max = float(xmlQ.find("defaultgrade").text)
 	def __str__(self):
 		return """\\element{"""+self.category+"""}{
-	\\begin{questionmult}{Def}\\nbpoints{0,5}
+	\\begin{questionmult}{"""+self.id+"""}\\nbpoints{"""+score_2_str(self.max)+"""}
 		"""+self.q+"""
 		\\begin{choices}
-			\\correctchoice{L'utilisation d'un traitement de texte en cours de langue.}\bareme{b=0.25}
-			\\correctchoice{L'évaluation de l'interface d'un concordancier en vue de son utilisation en classe d'anglais.}\bareme{b=0.15}
-			\\correctchoice{L'utilisation d'un glossaire en ligne sur l'informatique en cours de FLE.}\bareme{b=0.15}
-			\\wrongchoice{L'utilisation d'un dictionnaire en cours d'anglais.}\bareme{b=0,m=-0.25}
-			\\wrongchoice{La création d'un glossaire en ligne sur l'informatique.}\bareme{b=0}
+			\\correctchoice{L'utilisation d'un traitement de texte en cours de langue.}\\bareme{b=0.25}
+			\\correctchoice{L'évaluation de l'interface d'un concordancier en vue de son utilisation en classe d'anglais.}\\bareme{b=0.15}
+			\\correctchoice{L'utilisation d'un glossaire en ligne sur l'informatique en cours de FLE.}\\bareme{b=0.15}
+			\\wrongchoice{L'utilisation d'un dictionnaire en cours d'anglais.}\\bareme{b=0,m=-0.25}
+			\\wrongchoice{La création d'un glossaire en ligne sur l'informatique.}\\bareme{b=0}
 		\\end{choices}
 		\\explain{La création d'un lexique ne relève pas intrinsèquement de l'ALAO, encore faut-il que son usage soit lié à l'apprentissage d'une langue.}
 	\\end{questionmult}
 }\n\n"""
 
 if __name__ == "__main__":
-	quizz = Quizz("quiz-GI-4-SID-S1-top-20201204-1620.xml")
+	# quizz = Quizz("quiz-GI-4-SID-S1-top-20201204-1620.xml")
+	quizz = Quizz("quiz-GI-4.xml")
 	print(quizz)
diff --git a/utils.py b/utils.py
index 0c927f3..79dd3b5 100644
--- a/utils.py
+++ b/utils.py
@@ -9,3 +9,10 @@ def strip_tags(txt):
 
 def mlang_2_multiling(txt):
 	return sub(r"\{mlang en\}(.*?)\{mlang\}\{mlang other\}(.*?)\{mlang\}", r"\\multiling{\2}{\1}",txt)
+
+def score_2_str(v):
+	if int(v)==float(v):
+		res = str(int(v))
+	else:
+		res = str(float(v))
+	return res
-- 
GitLab