.
[HamExam.git] / questions.py
blob38924639068ffdb812b9a6869bbfef926f8cc257
1 #!/usr/bin/python
2 import sys, string
3 from xml.dom import minidom, Node
4 import os
5 import random as r
7 class Questions:
8 loud = False
10 def GetQuestions(self):
11 if self.loud:
12 print "Get Questions"
14 self.GetHints ()
16 self.questions = []
17 self.nquestions = 0
19 for q in self.file.getElementsByTagName("question"):
20 id = q.getAttribute ("id")
22 hint = self.GetHint(id)
24 answers = []
25 for c in q.childNodes:
26 if c.nodeType == Node.ELEMENT_NODE:
28 content=[]
29 for child in c.childNodes:
30 if child.nodeType == Node.TEXT_NODE:
31 content.append(child.nodeValue)
32 if content:
33 strContent = string.join(content)
35 if c.nodeName == "textquestion":
36 textquestion = strContent
37 elif c.nodeName == "textanswer":
38 if c.getAttribute("correct") == "true":
39 correct = True
40 else:
41 correct = False
42 answer = content
43 answers.append ([answer, correct])
45 self.questions.append ([id, textquestion,answers, hint])
46 self.nquestions += 1
48 def GetHints(self):
49 if self.loud:
50 print "Get hints"
52 self.hints = []
53 for h in self.root.getElementsByTagName ("hint"):
54 id = h.getAttribute ("question")
55 content=[]
56 for child in h.childNodes:
57 if child.nodeType == Node.TEXT_NODE:
58 content.append(child.nodeValue)
59 if content:
60 strContent = string.join(content)
62 ss = strContent.split("<a href=\"http://")[1].split("\">")[0]
64 self.hints.append ([id, ss])
66 def GetHint(self,id):
67 if self.loud:
68 print "Get hint:",id
70 for h in self.hints:
71 if h[0] == id:
72 return h[1]
73 return ""
75 def GetChapters(self):
76 if self.loud:
77 print "Get chapters"
79 chapters=[]
80 for c in self.root.childNodes:
81 if c.nodeType == Node.ELEMENT_NODE:
82 if c.nodeName == "chapter":
83 id1 = c.getAttribute("id")
84 name1 = c.getAttribute("name")
85 for d in c.childNodes:
86 if d.nodeType == Node.ELEMENT_NODE:
87 if d.nodeName == "chapter":
88 id2 = d.getAttribute("id")
89 name2 = d.getAttribute("name")
90 for e in d.childNodes:
91 if e.nodeType == Node.ELEMENT_NODE:
92 if e.nodeName == "chapter":
93 id3 = e.getAttribute("id")
94 name3 = e.getAttribute("name")
96 def GetBasicProperties(self):
97 if self.loud:
98 print "Get basic properties"
100 #self.title = self.root.getAttribute("title")
101 #publisher
102 #version -published
103 #contact
104 #exam id="T" name="Technische Kenntnisse / Klasse A" duration="90" maxerrorpoints="13"
105 return
107 def AskQuestion(self,id):
108 if self.loud:
109 print "Ask question:",id
111 for q in self.questions:
112 if q[0] == id:
113 break
115 self.id = id
116 self.question = q[1]
118 self.series = self.Random()
119 s = self.series
120 self.correct = ["a","b","c","d"][s.index(0)]
121 self.answera = q[2][s[0]][0][0]
122 self.answerb = q[2][s[1]][0][0]
123 self.answerc = q[2][s[2]][0][0]
124 self.answerd = q[2][s[3]][0][0]
125 self.answercorrect = self.answera #FIXME
126 self.hint = q[3]
128 def RealAnswer(self,answer):
129 s=self.series
130 a=["a","b","c","d"]
131 realchar = ["a","b","c","d"][[a[s[0]],a[s[1]],a[s[2]],a[s[3]]].index(answer)]
132 return realchar
134 def Random(self):
135 s = []
136 ss = 0
137 while True:
138 nn = r.randint(0,3)
139 if not nn in s:
140 s.append (nn)
141 ss += 1
142 if ss == 4:
143 break
144 return s
146 def __init__(self,filename="Questions/questions.xml"):
147 self.filename=filename
148 self.file = minidom.parse (self.filename)
149 self.root = self.file.documentElement
151 self.GetQuestions()
154 #q = Questions()
156 #q.AskQuestion("TB305")