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