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', true)
76 $(".question-piece", question).each (i, qpiece) =>
77 piece_no = parseInt $(qpiece).attr 'data-no'
78 should_be_checked = solution.indexOf(piece_no) >= 0
79 is_checked = $("input", qpiece).is(":checked")
89 @piece_incorrect qpiece
91 $(qpiece).removeClass("correct,incorrect")
98 class Uporzadkuj extends Excercise
99 constructor: (element) ->
101 $('ol, ul', @element).sortable({ items: "> li" })
103 check_question: (question) ->
104 positions = @get_value_list(question, 'original', true)
105 sorted = positions.sort()
106 pkts = $('.question-piece', question)
111 for pkt in [0...pkts.length]
113 if pkts.eq(pkt).data('pos') == sorted[pkt]
115 @piece_correct pkts.eq(pkt)
117 @piece_incorrect pkts.eq(pkt)
118 return [correct, all]
121 # XXX propozycje="1/0"
122 class Luki extends Excercise
123 constructor: (element) ->
129 $(".question-piece", @element).each (i, qpiece) =>
130 if $(qpiece).data('solution') == $(qpiece).val()
131 @piece_correct qpiece
134 @piece_incorrect qpiece
137 @show_score [correct, all]
140 class Zastap extends Excercise
141 constructor: (element) ->
143 $(".paragraph", @element).each (i, par) =>
144 @wrap_words $(par), $('<span class="zastap question-piece"/>')
145 spans = $("> span", par).attr("contenteditable", "true")
147 spans.filter(':not(:empty)').removeClass('editing')
148 $(ev.target).addClass('editing')
154 $(".question-piece", @element).each (i, qpiece) =>
155 txt = $(qpiece).data('original')
156 should_be_changed = false
158 txt = $(qpiece).data('solution')
159 should_be_changed = true
166 if txt != $(qpiece).text()
167 @piece_incorrect qpiece
170 @piece_correct qpiece
173 @show_score [correct, all]
175 wrap_words: (element, wrapper) ->
176 # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
177 # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
178 ignore = /^[ \t.,:;()]+/
180 insertWrapped = (txt, elem) ->
182 $(document.createTextNode(txt))
183 .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
185 for j in [element.get(0).childNodes.length-1..0]
186 chld = element.get(0).childNodes[j]
187 if chld.nodeType == document.TEXT_NODE
188 len = chld.textContent.length
192 space = ignore.exec(chld.textContent.substr(i))
195 insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
197 $(document.createTextNode(space[0])).insertBefore(chld)
203 insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
207 class Przyporzadkuj extends Excercise
209 for qp in $(".question-piece", @element)
210 if $(qp).data('solution').split(/[ ,]+/).length > 1
214 constructor: (element) ->
217 @multiple = @is_multiple()
219 $(".question", @element).each (i, question) =>
223 helper_opts = { helper: "clone" }
224 else helper_opts = {}
226 $(".draggable", question).draggable($.extend({}, draggable_opts,
229 $(".predicate .droppable", question).droppable
230 accept: (draggable) ->
231 $draggable = $(draggable)
232 if not $draggable.is(".draggable")
236 for added in $predicate.find("li")
237 if $(added).text() == $draggable.text()
242 added = ui.draggable.clone()
244 added.attr('style', '')
245 $(ev.target).append(added)
246 added.draggable(draggable_opts)
248 if not @multiple or ui.draggable.closest(".predicate").length > 0
249 ui.draggable.remove()
252 $(".subject", question).droppable
255 # this is to prevent a situation of dragging out and
256 # dropping back to the same place
257 if $(ui.draggable).closest(".subject").length > 0
261 added = ui.draggable.clone()
263 added.attr('style', '')
265 $(ev.target).append(added)
266 added.draggable($.extend({}, draggable_opts, helper_opts))
268 ui.draggable.remove()
270 check_question: (question) ->
271 # subjects placed in predicates
275 for qp in $(".predicate .question-piece", question)
276 pred = $(qp).closest("[data-predicate]")
277 v = @get_value_optional_list qp, 'solution'
280 all_multiple += mandatory.length + optional.length
281 pn = pred.data('predicate')
282 if mandatory.indexOf(pn) >= 0 or optional.indexOf(pn) >= 0
290 for qp in $(".subject .question-piece", question)
291 v = @get_value_optional_list qp, 'solution'
294 all_multiple += mandatory.length + optional.length
295 return [count, all_multiple]
309 uporzadkuj: Uporzadkuj
312 przyporzadkuj: Przyporzadkuj
315 cls = es[$(ele).attr('data-type')]
320 'EduModule': EduModule
325 $(document).ready () ->
326 new EduModule($("#book-text"))
328 $(".excercise").each (i, el) ->