.
[HamExam.git] / questions.py
blobe7543faf6c05c4c2149320ef81bf4473e60ec042
1 #!/usr/bin/python
2 import sys, string
3 from xml.dom import minidom, Node
4 import os
6 class Questions:
7 loud = False
9 def GetQuestions(self):
10 if self.loud:
11 print "Get Questions"
13 self.GetHints ()
15 self.questions = []
17 for q in self.file.getElementsByTagName("question"):
18 id = q.getAttribute ("id")
20 hint = self.GetHint(id)
22 answers = []
23 for c in q.childNodes:
24 if c.nodeType == Node.ELEMENT_NODE:
26 content=[]
27 for child in c.childNodes:
28 if child.nodeType == Node.TEXT_NODE:
29 content.append(child.nodeValue)
30 if content:
31 strContent = string.join(content)
33 if c.nodeName == "textquestion":
34 textquestion = strContent
35 elif c.nodeName == "textanswer":
36 if c.getAttribute("correct") == "true":
37 correct = True
38 else:
39 correct = False
40 answer = content
41 answers.append ([answer, correct])
43 self.questions.append ([id, textquestion,answers, hint])
45 def GetHints(self):
46 if self.loud:
47 print "Get hints"
49 self.hints = []
50 for h in self.root.getElementsByTagName ("hint"):
51 id = h.getAttribute ("question")
52 content=[]
53 for child in h.childNodes:
54 if child.nodeType == Node.TEXT_NODE:
55 content.append(child.nodeValue)
56 if content:
57 strContent = string.join(content)
59 ss = strContent.split("<a href=\"http://")[1].split("\">")[0]
61 self.hints.append ([id, ss])
63 def GetHint(self,id):
64 if self.loud:
65 print "Get hint:",id
67 for h in self.hints:
68 if h[0] == id:
69 return h[1]
70 return ""
72 def GetChapters(self):
73 if self.loud:
74 print "Get chapters"
76 chapters=[]
77 for c in self.root.childNodes:
78 if c.nodeType == Node.ELEMENT_NODE:
79 if c.nodeName == "chapter":
80 id1 = c.getAttribute("id")
81 name1 = c.getAttribute("name")
82 for d in c.childNodes:
83 if d.nodeType == Node.ELEMENT_NODE:
84 if d.nodeName == "chapter":
85 id2 = d.getAttribute("id")
86 name2 = d.getAttribute("name")
87 for e in d.childNodes:
88 if e.nodeType == Node.ELEMENT_NODE:
89 if e.nodeName == "chapter":
90 id3 = e.getAttribute("id")
91 name3 = e.getAttribute("name")
93 def GetBasicProperties(self):
94 if self.loud:
95 print "Get basic properties"
97 #self.title = self.root.getAttribute("title")
98 #publisher
99 #version -published
100 #contact
101 #exam id="T" name="Technische Kenntnisse / Klasse A" duration="90" maxerrorpoints="13"
102 return
104 def AskQuestion(self,id):
105 if self.loud:
106 print "Ask question:",id
108 for q in self.questions:
109 if q[0] == id:
110 break
112 self.id = id
113 self.question = q[1]
114 self.correct = "a" #FIXME
115 self.answera = q[2][0][0][0]
116 self.answerb = q[2][1][0][0]
117 self.answerc = q[2][2][0][0]
118 self.answerd = q[2][3][0][0]
119 self.hint = q[3]
121 def __init__(self,filename="Questions/questions.xml"):
122 self.filename=filename
123 self.file = minidom.parse (self.filename)
124 self.root = self.file.documentElement
126 self.GetQuestions()
129 #q = Questions()
131 #q.AskQuestion("TB305")