+class Luki(Exercise):
+ INSTRUCTION = u"Przeciągnij odpowiedzi i upuść w wybranym polu."
+ def find_pieces(self, question):
+ return question.xpath(".//luka")
+
+ def solution_html(self, 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)
+
+ luki = list(enumerate(self.find_pieces(element)))
+ luki_html = ""
+ i = 0
+ random.shuffle(luki)
+ for (i, luka) in luki:
+ i += 1
+ luka_html = self.solution_html(luka)
+ luki_html += u'<span class="draggable question-piece" data-no="%d">%s</span>' % (i, luka_html)
+ self.words_html = '<div class="words">%s</div>' % luki_html
+
+ return qpre, qpost
+
+ def handle_opis(self, element):
+ return '', self.words_html
+
+ def handle_luka(self, element):
+ self.piece_counter += 1
+ return '<span class="placeholder" data-solution="%d"></span>' % self.piece_counter
+
+
+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")
+
+ def solution_html(self, piece):
+ return piece.attrib.get('rozw', '')
+
+ def handle_zastap(self, element):
+ self.piece_counter += 1
+ return '<span class="placeholder zastap question-piece" data-solution="%d">' \
+ % self.piece_counter, '</span>'
+
+
+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'<span class="instruction">%s</span>' % 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}
+ else:
+ attrs = {}
+ pre, post = super(Przyporzadkuj, self).handle_lista(lista, attrs)
+ return pre, post + '<br class="clr"/>'
+
+ def handle_punkt(self, element):
+ if self.options['subject']:
+ self.piece_counter += 1
+ if self.options['handles']:
+ return '<li><span data-solution="%s" data-no="%s" class="question-piece draggable handle add-li">%s</span>' % (element.attrib.get('rozw', ''), self.piece_counter, self.piece_counter), '</li>'
+ else:
+ return '<li data-solution="%s" data-no="%s" class="question-piece draggable">' % (element.attrib.get('rozw', ''), self.piece_counter), '</li>'
+
+ elif self.options['predicate']:
+ if self.options['min']:
+ placeholders = u'<li class="placeholder"></li>' * self.options['min']
+ else:
+ placeholders = u'<li class="placeholder multiple"></li>'
+ return '<li data-predicate="%s">' % element.attrib.get('nazwa', ''), '<ul class="subjects">' + placeholders + '</ul></li>'
+
+ else:
+ return super(Przyporzadkuj, self).handle_punkt(element)
+
+
+class PrawdaFalsz(Exercise):
+ def handle_punkt(self, element):
+ if 'rozw' in element.attrib:
+ return u'''<li data-solution="%s" class="question-piece">
+ <span class="buttons">
+ <a href="#" data-value="true" class="true">Prawda</a>
+ <a href="#" data-value="false" class="false">Fałsz</a>
+ </span>''' % {'prawda': 'true', 'falsz': 'false'}[element.attrib['rozw']], '</li>'
+ else:
+ 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)
+
+