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).droppable
234 accept: (draggable) ->
235 $draggable = $(draggable)
236 if not $draggable.is(".draggable")
240 for added in $predicate.find("li")
241 if $(added).text() == $draggable.text()
246 added = ui.draggable.clone()
248 added.attr('style', '')
249 $(ev.target).append(added)
250 added.draggable(draggable_opts)
252 if not @multiple or ui.draggable.closest(".predicate").length > 0
253 ui.draggable.remove()
256 $(".subject", question).droppable
259 # this is to prevent a situation of dragging out and
260 # dropping back to the same place
261 if $(ui.draggable).closest(".subject").length > 0
265 added = ui.draggable.clone()
267 added.attr('style', '')
269 $(ev.target).append(added)
270 added.draggable($.extend({}, draggable_opts, helper_opts))
272 ui.draggable.remove()
274 check_question: (question) ->
275 # subjects placed in predicates
279 for qp in $(".predicate .question-piece", question)
280 pred = $(qp).closest("[data-predicate]")
281 v = @get_value_optional_list qp, 'solution'
284 all_multiple += mandatory.length + optional.length
285 pn = pred.data('predicate')
286 if mandatory.indexOf(pn) >= 0 or optional.indexOf(pn) >= 0
294 for qp in $(".subject .question-piece", question)
295 v = @get_value_optional_list qp, 'solution'
298 all_multiple += mandatory.length + optional.length
299 return [count, all_multiple]
313 uporzadkuj: Uporzadkuj
316 przyporzadkuj: Przyporzadkuj
319 cls = es[$(ele).attr('data-type')]
324 'EduModule': EduModule
329 $(document).ready () ->
330 new EduModule($("#book-text"))
332 $(".excercise").each (i, el) ->