+
"
+ def get_instruction(self):
+ if not self.instruction_printed:
+ self.instruction_printed = True
+ return u'
%s' % self.INSTRUCTION
+ else:
+ return ""
+
-class Wybor(Excercise):
+
+class Wybor(Exercise):
+ INSTRUCTION = None
def handle_cwiczenie(self, element):
pre, post = super(Wybor, self).handle_cwiczenie(element)
is_single_choice = True
- for p in element.xpath(".//pytanie"):
+ pytania = element.xpath(".//pytanie")
+ if not pytania:
+ pytania = [element]
+ for p in pytania:
solutions = re.split(r"[, ]+", p.attrib['rozw'])
if len(solutions) != 1:
is_single_choice = False
break
+ choices = p.xpath(".//*[@nazwa]")
+ uniq = set()
+ for n in choices: uniq.add(n.attrib['nazwa'])
+ if len(choices) != len(uniq):
+ is_single_choice = False
+ break
+
self.options = {'single': is_single_choice}
return pre, post
def handle_punkt(self, element):
- if self.options['excercise'] and element.attrib.get('nazwa', None):
+ if self.options['exercise'] and element.attrib.get('nazwa', None):
qc = self.question_counter
self.piece_counter += 1
no = self.piece_counter
@@ -230,7 +397,9 @@ class Wybor(Excercise):
return super(Wybor, self).handle_punkt(element)
-class Uporzadkuj(Excercise):
+class Uporzadkuj(Exercise):
+ INSTRUCTION = u"Kliknij wybranÄ
odpowiedź i przeciÄ
gnij w nowe miejsce."
+
def handle_pytanie(self, element):
"""
Overrides the returned content default handle_pytanie
@@ -249,14 +418,18 @@ Overrides the returned content default handle_pytanie
""
-class Luki(Excercise):
+class Luki(Exercise):
+ INSTRUCTION = u"PrzeciÄ
gnij odpowiedzi i upuÅÄ w wybranym polu."
def find_pieces(self, question):
- return question.xpath("//luka")
+ return question.xpath(".//luka")
def solution_html(self, piece):
- return piece.text + ''.join(
- [etree.tostring(n, encoding=unicode)
- for n in piece])
+ sub = EduModule()
+ return sub.generate(piece)
+ # print piece.text
+ # return piece.text + ''.join(
+ # [etree.tostring(n, encoding=unicode)
+ # for n in piece])
def handle_pytanie(self, element):
qpre, qpost = super(Luki, self).handle_pytanie(element)
@@ -274,8 +447,7 @@ class Luki(Excercise):
return qpre, qpost
def handle_opis(self, element):
- pre, post = super(Luki, self).handle_opis(element)
- return pre, self.words_html + post
+ return '', self.words_html
def handle_luka(self, element):
self.piece_counter += 1
@@ -283,8 +455,10 @@ class Luki(Excercise):
class Zastap(Luki):
+ INSTRUCTION = u"PrzeciÄ
gnij odpowiedzi i upuÅÄ je na sÅowie lub wyrażeniu, które chcesz zastÄ
piÄ."
+
def find_pieces(self, question):
- return question.xpath("//zastap")
+ return question.xpath(".//zastap")
def solution_html(self, piece):
return piece.attrib['rozw']
@@ -295,7 +469,25 @@ class Zastap(Luki):
% self.piece_counter, ''
-class Przyporzadkuj(Excercise):
+class Przyporzadkuj(Exercise):
+ INSTRUCTION = [u"PrzeciÄ
gnij odpowiedzi i upuÅÄ w wybranym polu.",
+ u"Kliknij numer odpowiedzi, przeciÄ
gnij i upuÅÄ w wybranym polu."]
+
+ def get_instruction(self):
+ print self.options['handles']
+ if not self.instruction_printed:
+ self.instruction_printed = True
+ return u'
%s' % self.INSTRUCTION[self.options['handles'] and 1 or 0]
+ else:
+ return ""
+
+ def handle_cwiczenie(self, element):
+ pre, post = super(Przyporzadkuj, self).handle_cwiczenie(element)
+ lista_with_handles = element.xpath(".//*[@uchwyty]")
+ if lista_with_handles:
+ self.options = {'handles': True}
+ return pre, post
+
def handle_pytanie(self, element):
pre, post = super(Przyporzadkuj, self).handle_pytanie(element)
minimum = element.attrib.get("min", None)
@@ -315,7 +507,7 @@ class Przyporzadkuj(Excercise):
'data-target': lista.attrib['cel'],
'class': 'subject'
}
- self.options = {'subject': True, 'handles': 'uchwyty' in lista.attrib}
+ self.options = {'subject': True}
else:
attrs = {}
pre, post = super(Przyporzadkuj, self).handle_lista(lista, attrs)
@@ -325,7 +517,7 @@ class Przyporzadkuj(Excercise):
if self.options['subject']:
self.piece_counter += 1
if self.options['handles']:
- return '
%s' % (element.attrib['rozw'], self.piece_counter, self.piece_counter), ''
+ return '
%s' % (element.attrib['rozw'], self.piece_counter, self.piece_counter), ''
else:
return '
' % (element.attrib['rozw'], self.piece_counter), ''
@@ -340,7 +532,7 @@ class Przyporzadkuj(Excercise):
return super(Przyporzadkuj, self).handle_punkt(element)
-class PrawdaFalsz(Excercise):
+class PrawdaFalsz(Exercise):
def handle_punkt(self, element):
if 'rozw' in element.attrib:
return u'''
@@ -352,6 +544,45 @@ class PrawdaFalsz(Excercise):
return super(PrawdaFalsz, self).handle_punkt(element)
+class EduModuleFormat(Format):
+ PRIMARY_MATERIAL_FORMATS = ('pdf', 'odt')
+
+ class MaterialNotFound(BaseException):
+ pass
+
+ def __init__(self, wldoc, **kwargs):
+ super(EduModuleFormat, self).__init__(wldoc, **kwargs)
+
+ def build(self):
+ # Sort materials by slug.
+ self.materials_by_slug = {}
+ for name, att in self.wldoc.source.attachments.items():
+ parts = name.rsplit('.', 1)
+ if len(parts) == 1:
+ continue
+ slug, ext = parts
+ if slug not in self.materials_by_slug:
+ self.materials_by_slug[slug] = {}
+ self.materials_by_slug[slug][ext] = att
+
+ edumod = EduModule({'provider': self.wldoc.provider, 'urlmapper': self, 'wldoc': self.wldoc})
+
+ html = edumod.generate(self.wldoc.edoc.getroot())
+
+ return IOFile.from_string(html.encode('utf-8'))
+
+ def materials(self, slug):
+ """Returns a list of pairs: (ext, iofile)."""
+ order = dict(reversed(k) for k in enumerate(self.PRIMARY_MATERIAL_FORMATS))
+ mats = self.materials_by_slug.get(slug, {}).items()
+ if not mats:
+ print "!! Material missing: '%s'" % slug
+ return sorted(mats, key=lambda (x, y): order.get(x, x))
+
+ def url_for_material(self, slug, fmt):
+ return "%s.%s" % (slug, fmt)
+
+
def transform(wldoc, stylesheet='edumed', options=None, flags=None):
"""Transforms the WL document to XHTML.
@@ -359,8 +590,5 @@ def transform(wldoc, stylesheet='edumed', options=None, flags=None):
otherwise returns True if file has been written,False if it hasn't.
File won't be written if it has no content.
"""
- edumod = EduModule(options)
-# from pdb import set_trace; set_trace()
- html = edumod.generate(wldoc.edoc.getroot())
-
- return OutputFile.from_string(html.encode('utf-8'))
+ edumodfor = EduModuleFormat(wldoc)
+ return edumodfor.build()