5 constructor: (@handler, @element) ->
6 $(@element).data(@handler, this)
9 class EduModule extends Binding
10 constructor: (element) ->
11 super 'edumodule', element
13 $("[name=teacher-toggle]").change (ev) =>
14 if $(ev.target).is(":checked")
15 $(".teacher", @element).addClass "show"
17 $(".teacher", @element).removeClass "show"
20 class Excercise extends Binding
21 constructor: (element) ->
22 super 'excercise', element
24 $(".check", @element).click =>
26 $('.solutions', @element).click =>
29 piece_correct: (qpiece) ->
30 $(qpiece).removeClass('incorrect').addClass('correct')
32 piece_incorrect: (qpiece) ->
33 $(qpiece).removeClass('correct').addClass('incorrect')
37 $(".question", @element).each (i, question) =>
38 scores.push(@check_question question)
41 $.each scores, (i, s) ->
46 get_value_list: (elem, data_key, numbers) ->
47 vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
49 vl = vl.map((x) -> parseInt(x))
52 get_value_optional_list: (elem, data_key) ->
53 vals = @get_value_list(elem, data_key)
58 opt.push v.slice(0, -1)
63 show_score: (score) ->
64 $(".message", @element).text("Wynik: #{score[0]} / #{score[1]}")
67 class Wybor extends Excercise
68 constructor: (element) ->
72 check_question: (question) ->
75 solution = @get_value_list(question, 'solution')
76 $(".question-piece", question).each (i, qpiece) =>
77 piece_no = $(qpiece).attr 'data-no'
78 piece_name = $(qpiece).attr 'data-name'
80 should_be_checked = solution.indexOf(piece_name) >= 0
82 should_be_checked = solution.indexOf(piece_no) >= 0
83 is_checked = $("input", qpiece).is(":checked")
93 @piece_incorrect qpiece
95 $(qpiece).removeClass("correct,incorrect")
102 class Uporzadkuj extends Excercise
103 constructor: (element) ->
105 $('ol, ul', @element).sortable({ items: "> li" })
107 check_question: (question) ->
108 positions = @get_value_list(question, 'original', true)
109 sorted = positions.sort()
110 pkts = $('.question-piece', question)
115 for pkt in [0...pkts.length]
117 if pkts.eq(pkt).data('pos') == sorted[pkt]
119 @piece_correct pkts.eq(pkt)
121 @piece_incorrect pkts.eq(pkt)
122 return [correct, all]
125 # XXX propozycje="1/0"
126 class Luki extends Excercise
127 constructor: (element) ->
133 $(".question-piece", @element).each (i, qpiece) =>
134 if $(qpiece).data('solution') == $(qpiece).val()
135 @piece_correct qpiece
138 @piece_incorrect qpiece
141 @show_score [correct, all]
144 class Zastap extends Excercise
145 constructor: (element) ->
147 $(".paragraph", @element).each (i, par) =>
148 @wrap_words $(par), $('<span class="zastap question-piece"/>')
149 spans = $("> span", par).attr("contenteditable", "true")
151 spans.filter(':not(:empty)').removeClass('editing')
152 $(ev.target).addClass('editing')
158 $(".question-piece", @element).each (i, qpiece) =>
159 txt = $(qpiece).data('original')
160 should_be_changed = false
162 txt = $(qpiece).data('solution')
163 should_be_changed = true
170 if txt != $(qpiece).text()
171 @piece_incorrect qpiece
174 @piece_correct qpiece
177 @show_score [correct, all]
179 wrap_words: (element, wrapper) ->
180 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
181 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
182 ignore = /^[ \t.,:;()]+/
184 insertWrapped = (txt, elem) ->
186 $(document.createTextNode(txt))
187 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
189 for j in [element.get(0).childNodes.length-1..0]
190 chld = element.get(0).childNodes[j]
191 if chld.nodeType == document.TEXT_NODE
192 len = chld.textContent.length
196 space = ignore.exec(chld.textContent.substr(i))
199 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
201 $(document.createTextNode(space[0])).insertBefore(chld)
207 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
211 class Przyporzadkuj extends Excercise
213 for qp in $(".question-piece", @element)
214 if $(qp).data('solution').split(/[ ,]+/).length > 1
218 constructor: (element) ->
221 @multiple = @is_multiple()
223 $(".question", @element).each (i, question) =>
227 helper_opts = { helper: "clone" }
228 else helper_opts = {}
230 $(".draggable", question).draggable($.extend({}, draggable_opts,
233 $(".predicate .droppable", question).parent().droppable
234 accept: (draggable) ->
235 $draggable = $(draggable)
236 if not $draggable.is(".draggable")
237 console.log('not draggable?')
241 for added in $predicate.find("li")
242 if $(added).text() == $draggable.text()
243 console.log('already here:' + $draggable.text())
248 added = ui.draggable.clone()
250 added.attr('style', '')
251 $(ev.target).find(".droppable").append(added)
252 added.draggable(draggable_opts)
254 if not @multiple or ui.draggable.closest(".predicate").length > 0
255 ui.draggable.remove()
257 $(".predicate .droppable", question).sortable
260 $(".subject", question).droppable
263 # this is to prevent a situation of dragging out and
264 # dropping back to the same place
265 if $(ui.draggable).closest(".subject").length > 0
269 added = ui.draggable.clone()
271 added.attr('style', '')
273 $(ev.target).append(added)
274 added.draggable($.extend({}, draggable_opts, helper_opts))
276 ui.draggable.remove()
278 check_question: (question) ->
279 # subjects placed in predicates
283 for qp in $(".predicate .question-piece", question)
284 pred = $(qp).closest("[data-predicate]")
285 v = @get_value_optional_list qp, 'solution'
288 all_multiple += mandatory.length + optional.length
289 pn = pred.data('predicate')
290 if mandatory.indexOf(pn) >= 0 or optional.indexOf(pn) >= 0
298 for qp in $(".subject .question-piece", question)
299 v = @get_value_optional_list qp, 'solution'
302 all_multiple += mandatory.length + optional.length
303 return [count, all_multiple]
308 class PrawdaFalsz extends Excercise
309 constructor: (element) ->
312 for qp in $(".question-piece", @element)
313 $(".true", qp).click (ev) ->
315 $(this).closest(".question-piece").data("value", "true")
316 $(this).addClass('chosen').siblings('a').removeClass('chosen')
317 $(".false", qp).click (ev) ->
319 $(this).closest(".question-piece").data("value", "false")
320 $(this).addClass('chosen').siblings('a').removeClass('chosen')
326 for qp in $(".question-piece", @element)
327 if $(qp).data("solution").toString() == $(qp).data("value")
342 uporzadkuj: Uporzadkuj
345 przyporzadkuj: Przyporzadkuj
346 prawdafalsz: PrawdaFalsz
349 cls = es[$(ele).attr('data-type')]
354 'EduModule': EduModule
359 $(document).ready () ->
360 new EduModule($("#book-text"))
362 $(".excercise").each (i, el) ->