.
[HamExam.git] / questions.py
blob044b67b7ca74d4f8f1abcaba1ef2c6e0106ceee2
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
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]
126 if q[2][0][1] == True:
127 self.correct = ["a","b","c","d"][s.index(0)]
128 self.answercorrect = q[2][0][0][0]
129 elif q[2][1][1] == True:
130 self.correct = ["a","b","c","d"][s.index(1)]
131 self.answercorrect = q[2][1][0][0]
132 elif q[2][2][1] == True:
133 self.correct = ["a","b","c","d"][s.index(2)]
134 self.answercorrect = q[2][2][0][0]
135 elif q[2][3][1] == True:
136 self.correct = ["a","b","c","d"][s.index(3)]
137 self.answercorrect = q[2][3][0][0]
138 else:
139 print "No answer is right?"
141 self.hint = q[3]
143 def RealAnswer(self,answer):
144 s=self.series
145 a=["a","b","c","d"]
146 realchar = ["a","b","c","d"][[a[s[0]],a[s[1]],a[s[2]],a[s[3]]].index(answer)]
147 return realchar
149 def Random(self):
150 s = []
151 ss = 0
152 while True:
153 nn = r.randint(0,3)
154 if not nn in s:
155 s.append (nn)
156 ss += 1
157 if ss == 4:
158 break
159 return s
161 def __init__(self,filename="Questions/questions.xml"):
162 self.filename=filename
163 self.file = minidom.parse (self.filename)
164 self.root = self.file.documentElement
166 self.GetQuestions()