nowa -> 2017
[librarian.git] / librarian / pyhtml.py
index 163d11c..3bda4c8 100644 (file)
@@ -131,7 +131,7 @@ class EduModule(Xmill):
 
         counter = self.activity_counter
 
 
         counter = self.activity_counter
 
-        if element.getnext().tag == 'aktywnosc' or (self.activity_last and self.activity_last.getnext() == element):
+        if element.getnext().tag == 'aktywnosc' or (len(self.activity_last) and self.activity_last.getnext() == element):
             counter_html = """<span class="act_counter">%(counter)d.</span>""" % {'counter': counter}
         else:
             counter_html = ''
             counter_html = """<span class="act_counter">%(counter)d.</span>""" % {'counter': counter}
         else:
             counter_html = ''
@@ -351,13 +351,18 @@ class Exercise(EduModule):
     def handle_rozw_kom(self, element):
         return u"""<div style="display:none" class="comment">""", u"""</div>"""
 
     def handle_rozw_kom(self, element):
         return u"""<div style="display:none" class="comment">""", u"""</div>"""
 
+    def extra_attributes(self):
+        return {}
+
     def handle_cwiczenie(self, element):
         self.options = {'exercise': element.attrib['typ']}
         self.question_counter = 0
         self.piece_counter = 0
 
     def handle_cwiczenie(self, element):
         self.options = {'exercise': element.attrib['typ']}
         self.question_counter = 0
         self.piece_counter = 0
 
+        extra_attrs = self.extra_attributes()
+
         pre = u"""
         pre = u"""
-<div class="exercise %(typ)s" data-type="%(typ)s">
+<div class="exercise %(typ)s" data-type="%(typ)s"%(extra_attrs)s>
 <form action="#" method="POST">
 <h3>Zadanie %(exercies_counter)d</h3>
 <div class="buttons">
 <form action="#" method="POST">
 <h3>Zadanie %(exercies_counter)d</h3>
 <div class="buttons">
@@ -368,7 +373,12 @@ class Exercise(EduModule):
 <input type="button" class="reset" value="reset"/>
 </div>
 
 <input type="button" class="reset" value="reset"/>
 </div>
 
-""" % {'exercies_counter': self.options['exercise_counter'], 'typ': element.attrib['typ']}
+""" % {
+            'exercies_counter': self.options['exercise_counter'],
+            'typ': element.attrib['typ'],
+            'extra_attrs': ' ' + ' '.join(
+                'data-%s="%s"' % item for item in extra_attrs.iteritems()) if extra_attrs else '',
+        }
         post = u"""
 <div class="buttons">
 <span class="message"></span>
         post = u"""
 <div class="buttons">
 <span class="message"></span>
@@ -420,48 +430,39 @@ class Exercise(EduModule):
 
 
 class Wybor(Exercise):
 
 
 class Wybor(Exercise):
+    def extra_attributes(self):
+        return {'subtype': 'single' if self.options['single'] else 'multiple'}
+
     def handle_cwiczenie(self, element):
     def handle_cwiczenie(self, element):
-        pre, post = super(Wybor, self).handle_cwiczenie(element)
         is_single_choice = True
         pytania = element.xpath(".//pytanie")
         if not pytania:
             pytania = [element]
         for p in pytania:
         is_single_choice = True
         pytania = element.xpath(".//pytanie")
         if not pytania:
             pytania = [element]
         for p in pytania:
-            solutions = re.split(r"[, ]+", p.attrib.get('rozw', ''))
+            solutions = p.xpath(".//punkt[@rozw='prawda']")
             if len(solutions) != 1:
                 is_single_choice = False
                 break
             if len(solutions) != 1:
                 is_single_choice = False
                 break
-            choices = p.xpath(".//*[@nazwa]")
-            uniq = set()
-            for n in choices:
-                uniq.add(n.attrib.get('nazwa', ''))
-            if len(choices) != len(uniq):
-                is_single_choice = False
-                break
 
         self.options = {'single': is_single_choice}
 
         self.options = {'single': is_single_choice}
-        return pre, post
+        return super(Wybor, self).handle_cwiczenie(element)
 
     def handle_punkt(self, element):
 
     def handle_punkt(self, element):
-        if self.options['exercise'] and element.attrib.get('nazwa', None):
+        if self.options['exercise'] and element.attrib.get('rozw', None):
             qc = self.question_counter
             self.piece_counter += 1
             no = self.piece_counter
             eid = "q%(qc)d_%(no)d" % locals()
             qc = self.question_counter
             self.piece_counter += 1
             no = self.piece_counter
             eid = "q%(qc)d_%(no)d" % locals()
-            aname = element.attrib.get('nazwa', None)
+            sol = element.attrib.get('rozw', None)
+            params = {'qc': qc, 'no': no, 'sol': sol, 'eid': eid}
             if self.options['single']:
             if self.options['single']:
-                return u"""
-<li class="question-piece" data-qc="%(qc)d" data-no="%(no)d" data-name="%(aname)s">
-<input type="radio" name="q%(qc)d" id="%(eid)s" value="%(aname)s" />
-<label for="%(eid)s">
-                """ % locals(), u"</label></li>"
+                input_tag = u'<input type="radio" name="q%(qc)d" id="%(eid)s" value="%(eid)s" />'
             else:
             else:
-                return u"""
-<li class="question-piece" data-qc="%(qc)d" data-no="%(no)d" data-name="%(aname)s">
-<input type="checkbox" name="%(eid)s" id="%(eid)s" />
-<label for="%(eid)s">
-""" % locals(), u"</label></li>"
-
+                input_tag = u'<input type="checkbox" name="%(eid)s" id="%(eid)s" />'
+            return (u"""
+<li class="question-piece" data-qc="%(qc)d" data-no="%(no)d" data-sol="%(sol)s">
+                """ + input_tag + u"""
+<label for="%(eid)s">""") % params, u"</label></li>"
         else:
             return super(Wybor, self).handle_punkt(element)
 
         else:
             return super(Wybor, self).handle_punkt(element)