+
"
+
+ def get_instruction(self):
+ if not self.instruction_printed:
+ self.instruction_printed = True
+ if self.INSTRUCTION:
+ return u'
%s' % self.INSTRUCTION
+ else:
+ return ""
+ else:
+ return ""
-class Wybor(Excercise):
- def handle_pytanie(self, element):
- pre, post = super(Wybor, self).handle_pytanie(element)
- solutions = re.split(r"[, ]+", element.attrib['rozw'])
- if len(solutions) == 1:
- self.options = { 'single': True }
+
+class Wybor(Exercise):
+ 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:
+ 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
@@ -221,8 +418,9 @@ class Wybor(Excercise):
return super(Wybor, self).handle_punkt(element)
+class Uporzadkuj(Exercise):
+ INSTRUCTION = u"Kliknij wybranÄ
odpowiedź i przeciÄ
gnij w nowe miejsce."
-class Uporzadkuj(Excercise):
def handle_pytanie(self, element):
"""
Overrides the returned content default handle_pytanie
@@ -241,14 +439,16 @@ 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])
+ piece = deepcopy(piece)
+ piece.tail = None
+ sub = EduModule()
+ return sub.generate(piece)
def handle_pytanie(self, element):
qpre, qpost = super(Luki, self).handle_pytanie(element)
@@ -266,8 +466,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
@@ -275,8 +474,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']
@@ -287,20 +488,44 @@ 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):
+ 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)
+ if minimum:
+ self.options = {"min": int(minimum)}
+ return pre, post
+
def handle_lista(self, lista):
if 'nazwa' in lista.attrib:
attrs = {
'data-name': lista.attrib['nazwa'],
'class': 'predicate'
- }
+ }
self.options = {'predicate': True}
elif 'cel' in lista.attrib:
attrs = {
'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)
@@ -310,19 +535,22 @@ 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), ''
elif self.options['predicate']:
- placeholders = u'
'
+ if self.options['min']:
+ placeholders = u'
' * self.options['min']
+ else:
+ placeholders = u'
'
return '
' % element.attrib, ''
else:
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'''
@@ -334,6 +562,48 @@ 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 url_for_image(self, slug, fmt, width=None):
+ return self.url_for_material(self, slug, fmt)
+
+
def transform(wldoc, stylesheet='edumed', options=None, flags=None):
"""Transforms the WL document to XHTML.
@@ -341,8 +611,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()