.
[HamExam.git] / questions.py
blob546dfa8df83f8b5a36abad9f5ee4aa08fa2675b0
1 #!/usr/bin/python
2 import sys, string
3 from xml.dom import minidom, Node
4 import os
6 class Questions:
7 def GetQuestions(self):
8 self.questions = []
10 for q in self.file.getElementsByTagName("question"):
11 id = q.getAttribute ("id")
13 answers = []
14 for c in q.childNodes:
15 if c.nodeType == Node.ELEMENT_NODE:
17 content=[]
18 for child in c.childNodes:
19 if child.nodeType == Node.TEXT_NODE:
20 content.append(child.nodeValue)
21 if content:
22 strContent = string.join(content)
24 if c.nodeName == "textquestion":
25 textquestion = strContent
26 elif c.nodeName == "textanswer":
27 if c.getAttribute("correct") == "true":
28 correct = True
29 else:
30 correct = False
31 answer = content
32 answers.append ([answer, correct])
34 self.questions.append ([id, textquestion,answers])
36 def GetHints(self):
37 return
39 def GetChapters(self):
40 chapters=[]
41 for c in self.root.childNodes:
42 if c.nodeType == Node.ELEMENT_NODE:
43 if c.nodeName == "chapter":
44 id1 = c.getAttribute("id")
45 name1 = c.getAttribute("name")
46 for d in c.childNodes:
47 if d.nodeType == Node.ELEMENT_NODE:
48 if d.nodeName == "chapter":
49 id2 = d.getAttribute("id")
50 name2 = d.getAttribute("name")
51 for e in d.childNodes:
52 if e.nodeType == Node.ELEMENT_NODE:
53 if e.nodeName == "chapter":
54 id3 = e.getAttribute("id")
55 name3 = e.getAttribute("name")
57 def GetBasicProperties(self):
58 return
60 def AskQuestion(self,id):
61 for q in self.questions:
62 if q[0] == id:
63 break
65 self.id = id
66 self.question = str(q[1])
67 self.correct = "a" #FIXME
68 self.answera = q[2][0][0][0]
69 self.answerb = q[2][1][0][0]
70 self.answerc = q[2][2][0][0]
71 self.answerd = q[2][3][0][0]
73 def __init__(self,filename="Questions/questions.xml"):
74 self.filename=filename
75 self.file = minidom.parse (self.filename)
76 self.root = self.file.documentElement
78 self.GetQuestions()
81 #q = Questions()
83 #q.AskQuestion("TB305")