14c700ce4b8ccdb469827ce83a8698b0c9d7ab02
[redakcja.git] / redakcja / static / edumed / js / edumed.coffee
1
2 $ = jQuery
3
4 class Binding
5   constructor: (@handler, @element) ->
6     $(@element).data(@handler, this)
7
8
9 class EduModule extends Binding
10   constructor: (element) ->
11     super 'edumodule', element
12
13     # $("[name=teacher-toggle]").change (ev) =>
14     #   if $(ev.target).is(":checked")
15     #     $(".teacher", @element).addClass "show"
16     #   else
17     #     $(".teacher", @element).removeClass "show"
18
19
20 class Exercise extends Binding
21   constructor: (element) ->
22     super 'exercise', element
23     # just save the html to reset the exercise
24     $(@element).data("exercise-html", $(@element).html())
25
26     $(".check", @element).click (ev) =>
27       @check()
28       $(ev.target).next(".retry").show()
29       $(ev.target).hide()
30     $(".retry", @element).click (ev) =>
31       $(".correct, .incorrect", @element).removeClass("correct incorrect")
32       $(ev.target).prev(".check").show()
33       $(ev.target).hide()
34     $('.solutions', @element).click =>
35       @show_solutions()
36       $(".comment", @element).show()
37     $('.reset', @element).click =>
38       @reset()
39
40   reset: ->
41     $(@element).html($(@element).data('exercise-html'))
42     exercise @element
43
44   piece_correct: (qpiece) ->
45     $(qpiece).removeClass('incorrect').addClass('correct')
46
47   piece_incorrect: (qpiece) ->
48     $(qpiece).removeClass('correct').addClass('incorrect')
49
50   check: ->
51     scores = []
52     $(".question", @element).each (i, question) =>
53       scores.push(@check_question question)
54
55     score = [0, 0]
56     $.each scores, (i, s) ->
57       score[0] += s[0]
58       score[1] += s[1]
59     @show_score(score)
60
61   show_solutions: ->
62     @reset()
63     $(".question", @element).each (i, question) =>
64       @solve_question question
65
66   # Parses a list of values, separated by space or comma.
67   # The list is read from data attribute of elem using data_key
68   # Returns a list with elements
69   # eg.: things_i_need: "house bike tv playstation"
70   # yields ["house", "bike", "tv", "playstation"]
71   # If optional numbers argument is true, returns list of numbers
72   # instead of strings
73   get_value_list: (elem, data_key, numbers) ->
74     vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim) #.map((x) -> parseInt(x))
75     if numbers
76       vl = vl.map((x) -> parseInt(x))
77     return vl
78
79   # Parses a list of values, separated by space or comma.
80   # The list is read from data attribute of elem using data_key
81   # Returns a 2-element list with mandatory and optional
82   # items. optional items are marked with a question mark on the end
83   # eg.: things_i_need: "house bike tv? playstation?"
84   # yields [[ "house", "bike"], ["tv", "playstation"]]
85   get_value_optional_list: (elem, data_key) ->
86     vals = @get_value_list(elem, data_key)
87     mandat = []
88     opt = []
89     for v in vals
90       if v.slice(-1) == "?"
91         opt.push v.slice(0, -1)
92       else
93         mandat.push v
94     return [mandat, opt]
95
96   show_score: (score) ->
97     $(".message", @element).text("Wynik: #{score[0]} / #{score[1]}")
98
99
100   draggable_equal: ($draggable1, $draggable2) ->
101     return false
102
103   draggable_accept: ($draggable, $droppable) ->
104     dropped = $droppable.closest("ul, ol").find(".draggable")
105     for d in dropped
106       if @draggable_equal $draggable, $(d)
107         return false
108     return true
109
110   draggable_move: ($draggable, $placeholder, ismultiple) ->
111     $added = $draggable.clone()
112     $added.data("original", $draggable.get(0))
113     if not ismultiple
114       $draggable.addClass('disabled').draggable('disable')
115
116     $placeholder.after($added)
117     if not $placeholder.hasClass('multiple')
118       $placeholder.hide()
119     $added.append('<span class="remove">x</span>')
120     $('.remove', $added).click (ev) =>
121       $added.prev(".placeholder:not(.multiple)").show()
122       if not ismultiple
123         $($added.data('original')).removeClass('disabled').draggable('enable')
124       $added.remove()
125
126 ## XXX co z issortable?
127   dragging: (ismultiple, issortable) ->
128     $(".question", @element).each (i, question) =>
129       draggable_opts =
130         revert: 'invalid'
131         helper: 'clone'
132
133       $(".draggable", question).draggable(draggable_opts)
134       self = this
135       $(".placeholder", question).droppable
136         accept: (draggable) ->
137           $draggable = $(draggable)
138           is_accepted = true
139
140           if not $draggable.is(".draggable")
141             is_accepted = false
142
143           if is_accepted
144             is_accepted= self.draggable_accept $draggable, $(this)
145
146           if is_accepted
147             $(this).addClass 'accepting'
148           else
149             $(this).removeClass 'accepting'
150           return is_accepted
151
152         drop: (ev, ui) =>
153           $(ev.target).removeClass 'accepting dragover'
154
155           @draggable_move $(ui.draggable), $(ev.target), ismultiple
156
157           # $added = $(ui.draggable).clone()
158           # $added.data("original", ui.draggable)
159           # if not ismultiple
160           #   $(ui.draggable).addClass('disabled').draggable('disable')
161
162           # $(ev.target).after(added)
163           # if not $(ev.target).hasClass('multiple')
164           #   $(ev.target).hide()
165           # $added.append('<span class="remove">x</span>')
166           # $('.remove', added).click (ev) =>
167           #   $added.prev(".placeholder:not(.multiple)").show()
168           #   if not ismultiple
169           #     $added.data('original').removeClass('disabled').draggable('enable')
170           #   $(added).remove()
171
172         over: (ev, ui) ->
173           $(ev.target).addClass 'dragover'
174
175
176         out: (ev, ui) ->
177           $(ev.target).removeClass 'dragover'
178
179
180
181 class Wybor extends Exercise
182   constructor: (element) ->
183     super element
184
185
186   check_question: (question) ->
187     all = 0
188     good = 0
189     solution = @get_value_list(question, 'solution')
190     $(".question-piece", question).each (i, qpiece) =>
191       piece_no = $(qpiece).attr 'data-no'
192       piece_name = $(qpiece).attr 'data-name'
193       if piece_name
194         should_be_checked = solution.indexOf(piece_name) >= 0
195       else
196         should_be_checked = solution.indexOf(piece_no) >= 0
197       is_checked = $("input", qpiece).is(":checked")
198
199       if should_be_checked
200         all += 1
201
202       if is_checked
203         if should_be_checked
204           good += 1
205           @piece_correct qpiece
206         else
207           @piece_incorrect qpiece
208       else
209         $(qpiece).removeClass("correct,incorrect")
210
211     return [good, all]
212
213   solve_question: (question) ->
214     solution = @get_value_list(question, 'solution')
215     $(".question-piece", question).each (i, qpiece) =>
216       piece_no = $(qpiece).attr 'data-no'
217       piece_name = $(qpiece).attr 'data-name'
218       if piece_name
219         should_be_checked = solution.indexOf(piece_name) >= 0
220       else
221         should_be_checked = solution.indexOf(piece_no) >= 0
222       console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked)
223       $("input[type=checkbox]", qpiece).prop 'checked', should_be_checked
224
225
226
227 class Uporzadkuj extends Exercise
228   constructor: (element) ->
229     super element
230     $('ol, ul', @element).sortable({ items: "> li" })
231
232   check_question: (question) ->
233     positions = @get_value_list(question, 'original', true)
234     sorted = positions.sort()
235     pkts = $('.question-piece', question)
236
237     correct = 0
238     all = 0
239
240     for pkt in [0...pkts.length]
241       all += 1
242       if pkts.eq(pkt).data('pos') == sorted[pkt]
243         correct += 1
244         @piece_correct pkts.eq(pkt)
245       else
246         @piece_incorrect pkts.eq(pkt)
247     return [correct, all]
248
249   solve_question: (question) ->
250     positions = @get_value_list(question, 'original', true)
251     sorted = positions.sort()
252     pkts = $('.question-piece', question)
253     pkts.sort (a, b) ->
254       q = $(a).data('pos')
255       w = $(b).data('pos')
256       return 1 if q < w
257       return -1 if q > w
258       return 0
259
260     parent = pkts.eq(0).parent()
261     for p in pkts
262       parent.prepend(p)
263
264
265 # XXX propozycje="1/0"
266 class Luki extends Exercise
267   constructor: (element) ->
268     super element
269     @dragging false, false
270
271   check: ->
272     all = 0
273     correct = 0
274     $(".placeholder + .question-piece", @element).each (i, qpiece) =>
275       $placeholder = $(qpiece).prev(".placeholder")
276       if $placeholder.data('solution') == $(qpiece).data('no')
277         @piece_correct qpiece
278         correct += 1
279       else
280         @piece_incorrect qpiece
281       all += 1
282
283     @show_score [correct, all]
284
285   solve_question: (question) ->
286     $(".placeholder", question).each (i, placeholder) =>
287       $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question)
288       @draggable_move $qp, $(placeholder), false
289
290
291 class Zastap extends Exercise
292   constructor: (element) ->
293     super element
294     $(".paragraph", @element).each (i, par) =>
295       @wrap_words $(par), $('<span class="placeholder zastap"/>')
296     @dragging false, false
297
298   check: ->
299     all = 0
300     correct = 0
301
302     $(".paragraph", @element).each (i, par) =>
303       $(".placeholder", par).each (j, qpiece) =>
304         $qp = $(qpiece)
305         $dragged = $qp.next(".draggable")
306         if $qp.data("solution")
307           if $dragged and $qp.data("solution") == $dragged.data("no")
308             @piece_correct $dragged
309             correct += 1
310 #          else -- we dont mark enything here, so not to hint user about solution. He sees he hasn't used all the draggables
311
312           all += 1
313
314     @show_score [correct, all]
315
316   show_solutions: ->
317     @reset()
318     $(".paragraph", @element).each (i, par) =>
319       $(".placeholder[data-solution]", par).each (j, qpiece) =>
320         $qp = $(qpiece)
321         $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", @element)
322         @draggable_move $dr, $qp, false
323
324
325   wrap_words: (element, wrapper) ->
326     # This function wraps each word of element in wrapper, but does not descend into child-tags of element.
327     # It doesn't wrap things between words (defined by ignore RE below). Warning - ignore must begin with ^
328     ignore = /^[ \t.,:;()]+/
329
330     insertWrapped = (txt, elem) ->
331       nw = wrapper.clone()
332       $(document.createTextNode(txt))
333         .wrap(nw).parent().attr("data-original", txt).insertBefore(elem)
334
335     for j in [element.get(0).childNodes.length-1..0]
336       chld = element.get(0).childNodes[j]
337       if chld.nodeType == document.TEXT_NODE
338         len = chld.textContent.length
339         wordb = 0
340         i = 0
341         while i < len
342           space = ignore.exec(chld.textContent.substr(i))
343           if space?
344             if wordb < i
345               insertWrapped(chld.textContent.substr(wordb, i-wordb), chld)
346
347             $(document.createTextNode(space[0])).insertBefore(chld)
348             i += space[0].length
349             wordb = i
350           else
351             i = i + 1
352         if wordb < len - 1
353           insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld)
354         $(chld).remove()
355
356
357 class Przyporzadkuj extends Exercise
358   is_multiple: ->
359     for qp in $(".question-piece", @element)
360       if $(qp).data('solution').split(/[ ,]+/).length > 1
361         return true
362     return false
363
364   constructor: (element) ->
365     super element
366
367     @multiple = @is_multiple()
368
369     @dragging @multiple, true
370
371   draggable_equal: (d1, d2) ->
372     return d1.data("no") == d2.data("no")
373
374
375   check_question: (question) ->
376     # subjects placed in predicates
377     minimum = $(question).data("minimum")
378     count = 0
379     all = 0
380     if not minimum
381       all = $(".subjects .question-piece", question).length
382
383     for pred in $(".predicate [data-predicate]", question)
384       pn = $(pred).attr('data-predicate')
385       if minimum?
386         all += minimum
387
388       for qp in $(".question-piece", pred)
389         v = @get_value_optional_list qp, 'solution'
390         mandatory = v[0]
391         optional = v[1]
392
393         if mandatory.indexOf(pn) >= 0 or (minimum and optional.indexOf(pn) >= 0)
394           count += 1
395           @piece_correct qp
396         else
397           @piece_incorrect qp
398
399     return [count, all]
400
401   solve_question: (question) ->
402     minimum = $(question).data("min")
403
404     for qp in $(".subject .question-piece", question)
405       v = @get_value_optional_list qp, 'solution'
406       mandatory = v[0]
407       optional = v[1]
408
409       if minimum
410         draggables = mandatory.count(optional)[0...minimum]
411       else
412         draggables = mandatory
413       for m in draggables
414         $pr = $(".predicate [data-predicate=" + m + "]", question)
415         $ph = $pr.find ".placeholder:visible"
416         @draggable_move $(qp), $ph, @multiple
417
418
419
420 class PrawdaFalsz extends Exercise
421   constructor: (element) ->
422     super element
423
424     for qp in $(".question-piece", @element)
425       $(".true", qp).click (ev) ->
426         ev.preventDefault()
427         $(this).closest(".question-piece").data("value", "true")
428         $(this).addClass('chosen').siblings('a').removeClass('chosen')
429       $(".false", qp).click (ev) ->
430         ev.preventDefault()
431         $(this).closest(".question-piece").data("value", "false")
432         $(this).addClass('chosen').siblings('a').removeClass('chosen')
433
434
435   check_question: ->
436     all = 0
437     good = 0
438     for qp in $(".question-piece", @element)
439       if $(qp).data("solution").toString() == $(qp).data("value")
440         good += 1
441         @piece_correct qp
442       else
443         @piece_incorrect qp
444
445       all += 1
446
447     return [good, all]
448
449   show_solutions: ->
450     reset()
451     for qp in $(".question-piece", @element)
452       if $(qp).data('solution') == 'true'
453         $(".true", qp).click()
454       else
455         $(".false", qp).click()
456
457
458 ##########
459
460 exercise = (ele) ->
461   es =
462     wybor: Wybor
463     uporzadkuj: Uporzadkuj
464     luki: Luki
465     zastap: Zastap
466     przyporzadkuj: Przyporzadkuj
467     prawdafalsz: PrawdaFalsz
468
469
470   cls = es[$(ele).attr('data-type')]
471   new cls(ele)
472
473
474 window.edumed =
475   'EduModule': EduModule
476
477
478
479
480 $(document).ready () ->
481   new EduModule($("#book-text"))
482
483   $(".exercise").each (i, el) ->
484     exercise(this)