wip: showing first couple of questions
[edumed.git] / wtem / static / wtem / edumed.js
1 // Generated by CoffeeScript 1.6.3
2 (function() {
3   var $, Binding, EduModule, Exercise, Luki, PrawdaFalsz, Przyporzadkuj, Uporzadkuj, Wybor, Zastap, exercise,
4     __hasProp = {}.hasOwnProperty,
5     __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
6
7   $ = jQuery;
8
9   Binding = (function() {
10     function Binding(handler, element) {
11       this.handler = handler;
12       this.element = element;
13       $(this.element).data(this.handler, this);
14     }
15
16     return Binding;
17
18   })();
19
20   EduModule = (function(_super) {
21     __extends(EduModule, _super);
22
23     function EduModule(element) {
24       EduModule.__super__.constructor.call(this, 'edumodule', element);
25     }
26
27     return EduModule;
28
29   })(Binding);
30
31   Exercise = (function(_super) {
32     __extends(Exercise, _super);
33
34     function Exercise(element) {
35       var _this = this;
36       Exercise.__super__.constructor.call(this, 'exercise', element);
37       $(this.element).data("exercise-html", $(this.element).html());
38       $(".check", this.element).click(function(ev) {
39         _this.check();
40         $(".retry", _this.element).show();
41         return $(".check", _this.element).hide();
42       });
43       $(".retry", this.element).click(function(ev) {
44         return _this.retry();
45       });
46       $('.solutions', this.element).click(function() {
47         _this.show_solutions();
48         return $(".comment", _this.element).show();
49       });
50       $('.reset', this.element).click(function() {
51         return _this.reset();
52       });
53     }
54
55     Exercise.prototype.retry = function() {
56       $(".correct, .incorrect", this.element).removeClass("correct incorrect");
57       $(".check", this.element).show();
58       return $(".retry", this.element).hide();
59     };
60
61     Exercise.prototype.reset = function() {
62       $(this.element).html($(this.element).data('exercise-html'));
63       return exercise(this.element);
64     };
65
66     Exercise.prototype.piece_correct = function(qpiece) {
67       return $(qpiece).removeClass('incorrect').addClass('correct');
68     };
69
70     Exercise.prototype.piece_incorrect = function(qpiece) {
71       return $(qpiece).removeClass('correct').addClass('incorrect');
72     };
73
74     Exercise.prototype.check = function() {
75       var score, scores,
76         _this = this;
77       scores = [];
78       $(".question", this.element).each(function(i, question) {
79         return scores.push(_this.check_question(question));
80       });
81       score = [0, 0, 0];
82       $.each(scores, function(i, s) {
83         score[0] += s[0];
84         score[1] += s[1];
85         return score[2] += s[2];
86       });
87       return this.show_score(score);
88     };
89
90     Exercise.prototype.show_solutions = function() {
91       var _this = this;
92       this.reset();
93       return $(".question", this.element).each(function(i, question) {
94         return _this.solve_question(question);
95       });
96     };
97
98     Exercise.prototype.get_answers = function() {
99       var answers,
100         _this = this;
101       answers = [];
102       $('.question', this.element).each(function(i, question) {
103         return answers.push(_this.get_answer(question));
104       });
105       return answers;
106     };
107
108     Exercise.prototype.get_value_list = function(elem, data_key, numbers) {
109       var vl;
110       vl = $(elem).attr("data-" + data_key).split(/[ ,]+/).map($.trim);
111       if (numbers) {
112         vl = vl.map(function(x) {
113           return parseInt(x);
114         });
115       }
116       return vl;
117     };
118
119     Exercise.prototype.get_value_optional_list = function(elem, data_key) {
120       var mandat, opt, v, vals, _i, _len;
121       vals = this.get_value_list(elem, data_key);
122       mandat = [];
123       opt = [];
124       for (_i = 0, _len = vals.length; _i < _len; _i++) {
125         v = vals[_i];
126         if (v.slice(-1) === "?") {
127           opt.push(v.slice(0, -1));
128         } else {
129           mandat.push(v);
130         }
131       }
132       return [mandat, opt];
133     };
134
135     Exercise.prototype.show_score = function(score) {
136       var $msg;
137       $msg = $(".message", this.element);
138       $msg.text("Wynik: " + score[0] + " / " + score[2]);
139       if (score[0] >= score[2] && score[1] === 0) {
140         return $msg.addClass("maxscore");
141       } else {
142         return $msg.removeClass("maxscore");
143       }
144     };
145
146     Exercise.prototype.draggable_equal = function($draggable1, $draggable2) {
147       return false;
148     };
149
150     Exercise.prototype.draggable_accept = function($draggable, $droppable) {
151       var d, dropped, _i, _len;
152       dropped = $droppable.closest("ul, ol").find(".draggable");
153       for (_i = 0, _len = dropped.length; _i < _len; _i++) {
154         d = dropped[_i];
155         if (this.draggable_equal($draggable, $(d))) {
156           return false;
157         }
158       }
159       return true;
160     };
161
162     Exercise.prototype.draggable_move = function($draggable, $placeholder, ismultiple) {
163       var $added,
164         _this = this;
165       $added = $draggable.clone();
166       $added.data("original", $draggable.get(0));
167       if (!ismultiple) {
168         $draggable.addClass('disabled').draggable('disable');
169       }
170       $placeholder.after($added);
171       if (!$placeholder.hasClass('multiple')) {
172         $placeholder.hide();
173       }
174       if ($added.is(".add-li")) {
175         $added.wrap("<li/>");
176       }
177       $added.append('<span class="remove">x</span><div class="clr"></div>');
178       return $('.remove', $added).click(function(ev) {
179         _this.retry();
180         if (!ismultiple) {
181           $($added.data('original')).removeClass('disabled').draggable('enable');
182         }
183         if ($added.is(".add-li")) {
184           $added = $added.closest('li');
185         }
186         $added.prev(".placeholder:not(.multiple)").show();
187         return $added.remove();
188       });
189     };
190
191     Exercise.prototype.dragging = function(ismultiple, issortable) {
192       var _this = this;
193       return $(".question", this.element).each(function(i, question) {
194         var draggable_opts, self;
195         draggable_opts = {
196           revert: 'invalid',
197           helper: 'clone',
198           start: _this.retry
199         };
200         $(".draggable", question).draggable(draggable_opts);
201         self = _this;
202         return $(".placeholder", question).droppable({
203           accept: function(draggable) {
204             var $draggable, is_accepted;
205             $draggable = $(draggable);
206             is_accepted = true;
207             if (!$draggable.is(".draggable")) {
208               is_accepted = false;
209             }
210             if (is_accepted) {
211               is_accepted = self.draggable_accept($draggable, $(this));
212             }
213             if (is_accepted) {
214               $(this).addClass('accepting');
215             } else {
216               $(this).removeClass('accepting');
217             }
218             return is_accepted;
219           },
220           drop: function(ev, ui) {
221             $(ev.target).removeClass('accepting dragover');
222             return _this.draggable_move($(ui.draggable), $(ev.target), ismultiple);
223           },
224           over: function(ev, ui) {
225             return $(ev.target).addClass('dragover');
226           },
227           out: function(ev, ui) {
228             return $(ev.target).removeClass('dragover');
229           }
230         });
231       });
232     };
233
234     return Exercise;
235
236   })(Binding);
237
238   Wybor = (function(_super) {
239     __extends(Wybor, _super);
240
241     function Wybor(element) {
242       Wybor.__super__.constructor.call(this, element);
243       $(".question-piece input", element).change(this.retry);
244     }
245
246     Wybor.prototype.check_question = function(question) {
247       var all, bad, good, solution,
248         _this = this;
249       all = 0;
250       good = 0;
251       bad = 0;
252       solution = this.get_value_list(question, 'solution');
253       $(".question-piece", question).each(function(i, qpiece) {
254         var is_checked, piece_name, piece_no, should_be_checked;
255         piece_no = $(qpiece).attr('data-no');
256         piece_name = $(qpiece).attr('data-name');
257         if (piece_name) {
258           should_be_checked = solution.indexOf(piece_name) >= 0;
259         } else {
260           should_be_checked = solution.indexOf(piece_no) >= 0;
261         }
262         is_checked = $("input", qpiece).is(":checked");
263         if (should_be_checked) {
264           all += 1;
265         }
266         if (is_checked) {
267           if (should_be_checked) {
268             good += 1;
269             return _this.piece_correct(qpiece);
270           } else {
271             bad += 1;
272             return _this.piece_incorrect(qpiece);
273           }
274         } else {
275           return $(qpiece).removeClass("correct,incorrect");
276         }
277       });
278       return [good, bad, all];
279     };
280
281     Wybor.prototype.solve_question = function(question) {
282       var solution,
283         _this = this;
284       solution = this.get_value_list(question, 'solution');
285       return $(".question-piece", question).each(function(i, qpiece) {
286         var piece_name, piece_no, should_be_checked;
287         piece_no = $(qpiece).attr('data-no');
288         piece_name = $(qpiece).attr('data-name');
289         if (piece_name) {
290           should_be_checked = solution.indexOf(piece_name) >= 0;
291         } else {
292           should_be_checked = solution.indexOf(piece_no) >= 0;
293         }
294         console.log("check " + $("input[type=checkbox]", qpiece).attr("id") + " -> " + should_be_checked);
295         return $("input[type=checkbox],input[type=radio]", qpiece).prop('checked', should_be_checked);
296       });
297     };
298
299     Wybor.prototype.get_answer = function(question) {
300       var answer,
301         _this = this;
302       answer = [];
303       $('.question-piece', question).each(function(i, qpiece) {
304         var $qpiece;
305         $qpiece = $(qpiece);
306         if ($("input[type=checkbox],input[type=radio]", qpiece).is(':checked')) {
307           return answer.push($qpiece.attr('data-name'));
308         }
309       });
310       return answer;
311     };
312
313     return Wybor;
314
315   })(Exercise);
316
317   Uporzadkuj = (function(_super) {
318     __extends(Uporzadkuj, _super);
319
320     function Uporzadkuj(element) {
321       Uporzadkuj.__super__.constructor.call(this, element);
322       $('ol, ul', this.element).sortable({
323         items: "> li",
324         start: this.retry
325       });
326     }
327
328     Uporzadkuj.prototype.check_question = function(question) {
329       var all, bad, correct, pkt, pkts, positions, sorted, _i, _ref;
330       positions = this.get_value_list(question, 'original', true);
331       sorted = positions.sort();
332       pkts = $('.question-piece', question);
333       correct = 0;
334       bad = 0;
335       all = 0;
336       for (pkt = _i = 0, _ref = pkts.length; 0 <= _ref ? _i < _ref : _i > _ref; pkt = 0 <= _ref ? ++_i : --_i) {
337         all += 1;
338         if (pkts.eq(pkt).data('pos') === sorted[pkt]) {
339           correct += 1;
340           this.piece_correct(pkts.eq(pkt));
341         } else {
342           bad += 1;
343           this.piece_incorrect(pkts.eq(pkt));
344         }
345       }
346       return [correct, bad, all];
347     };
348
349     Uporzadkuj.prototype.solve_question = function(question) {
350       var p, parent, pkts, positions, sorted, _i, _len, _results;
351       positions = this.get_value_list(question, 'original', true);
352       sorted = positions.sort();
353       pkts = $('.question-piece', question);
354       pkts.sort(function(a, b) {
355         var q, w;
356         q = $(a).data('pos');
357         w = $(b).data('pos');
358         if (q < w) {
359           return 1;
360         }
361         if (q > w) {
362           return -1;
363         }
364         return 0;
365       });
366       parent = pkts.eq(0).parent();
367       _results = [];
368       for (_i = 0, _len = pkts.length; _i < _len; _i++) {
369         p = pkts[_i];
370         _results.push(parent.prepend(p));
371       }
372       return _results;
373     };
374
375     return Uporzadkuj;
376
377   })(Exercise);
378
379   Luki = (function(_super) {
380     __extends(Luki, _super);
381
382     function Luki(element) {
383       Luki.__super__.constructor.call(this, element);
384       this.dragging(false, false);
385     }
386
387     Luki.prototype.check = function() {
388       var all, bad, correct,
389         _this = this;
390       all = $(".placeholder", this.element).length;
391       correct = 0;
392       bad = 0;
393       $(".placeholder + .question-piece", this.element).each(function(i, qpiece) {
394         var $placeholder;
395         $placeholder = $(qpiece).prev(".placeholder");
396         if ($placeholder.data('solution') === $(qpiece).data('no')) {
397           _this.piece_correct(qpiece);
398           return correct += 1;
399         } else {
400           bad += 1;
401           return _this.piece_incorrect(qpiece);
402         }
403       });
404       return this.show_score([correct, bad, all]);
405     };
406
407     Luki.prototype.solve_question = function(question) {
408       var _this = this;
409       return $(".placeholder", question).each(function(i, placeholder) {
410         var $qp;
411         $qp = $(".question-piece[data-no=" + $(placeholder).data('solution') + "]", question);
412         return _this.draggable_move($qp, $(placeholder), false);
413       });
414     };
415
416     return Luki;
417
418   })(Exercise);
419
420   Zastap = (function(_super) {
421     __extends(Zastap, _super);
422
423     function Zastap(element) {
424       var _this = this;
425       Zastap.__super__.constructor.call(this, element);
426       $(".paragraph", this.element).each(function(i, par) {
427         return _this.wrap_words($(par), $('<span class="placeholder zastap"/>'));
428       });
429       this.dragging(false, false);
430     }
431
432     Zastap.prototype.check = function() {
433       var all, bad, correct,
434         _this = this;
435       all = 0;
436       correct = 0;
437       bad = 0;
438       $(".paragraph", this.element).each(function(i, par) {
439         return $(".placeholder", par).each(function(j, qpiece) {
440           var $dragged, $qp;
441           $qp = $(qpiece);
442           $dragged = $qp.next(".draggable");
443           if ($qp.data("solution")) {
444             if ($dragged && $qp.data("solution") === $dragged.data("no")) {
445               _this.piece_correct($dragged);
446               correct += 1;
447             }
448             return all += 1;
449           }
450         });
451       });
452       return this.show_score([correct, bad, all]);
453     };
454
455     Zastap.prototype.show_solutions = function() {
456       var _this = this;
457       this.reset();
458       return $(".paragraph", this.element).each(function(i, par) {
459         return $(".placeholder[data-solution]", par).each(function(j, qpiece) {
460           var $dr, $qp;
461           $qp = $(qpiece);
462           $dr = $(".draggable[data-no=" + $qp.data('solution') + "]", _this.element);
463           return _this.draggable_move($dr, $qp, false);
464         });
465       });
466     };
467
468     Zastap.prototype.wrap_words = function(element, wrapper) {
469       var chld, i, ignore, insertWrapped, j, len, space, wordb, _i, _ref, _results;
470       ignore = /^[ \t.,:;()]+/;
471       insertWrapped = function(txt, elem) {
472         var nw;
473         nw = wrapper.clone();
474         return $(document.createTextNode(txt)).wrap(nw).parent().attr("data-original", txt).insertBefore(elem);
475       };
476       _results = [];
477       for (j = _i = _ref = element.get(0).childNodes.length - 1; _ref <= 0 ? _i <= 0 : _i >= 0; j = _ref <= 0 ? ++_i : --_i) {
478         chld = element.get(0).childNodes[j];
479         if (chld.nodeType === document.TEXT_NODE) {
480           len = chld.textContent.length;
481           wordb = 0;
482           i = 0;
483           while (i < len) {
484             space = ignore.exec(chld.textContent.substr(i));
485             if (space != null) {
486               if (wordb < i) {
487                 insertWrapped(chld.textContent.substr(wordb, i - wordb), chld);
488               }
489               $(document.createTextNode(space[0])).insertBefore(chld);
490               i += space[0].length;
491               wordb = i;
492             } else {
493               i = i + 1;
494             }
495           }
496           if (wordb < len - 1) {
497             insertWrapped(chld.textContent.substr(wordb, len - 1 - wordb), chld);
498           }
499           _results.push($(chld).remove());
500         } else {
501           _results.push(void 0);
502         }
503       }
504       return _results;
505     };
506
507     return Zastap;
508
509   })(Exercise);
510
511   Przyporzadkuj = (function(_super) {
512     __extends(Przyporzadkuj, _super);
513
514     Przyporzadkuj.prototype.is_multiple = function() {
515       var qp, _i, _len, _ref;
516       _ref = $(".question-piece", this.element);
517       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
518         qp = _ref[_i];
519         if ($(qp).attr('data-solution').split(/[ ,]+/).length > 1) {
520           return true;
521         }
522       }
523       return false;
524     };
525
526     function Przyporzadkuj(element) {
527       Przyporzadkuj.__super__.constructor.call(this, element);
528       this.multiple = this.is_multiple();
529       this.dragging(this.multiple, true);
530     }
531
532     Przyporzadkuj.prototype.draggable_equal = function(d1, d2) {
533       return d1.data("no") === d2.data("no");
534     };
535
536     Przyporzadkuj.prototype.check_question = function(question) {
537       var all, bad_count, count, mandatory, minimum, optional, pn, pred, qp, self, v, _i, _j, _len, _len1, _ref, _ref1;
538       minimum = $(question).data("minimum");
539       count = 0;
540       bad_count = 0;
541       all = 0;
542       if (!minimum) {
543         self = this;
544         $(".subject .question-piece", question).each(function(i, el) {
545           var mandatory, v;
546           v = self.get_value_optional_list(el, 'solution');
547           mandatory = v[0];
548           return all += mandatory.length;
549         });
550       }
551       _ref = $(".predicate [data-predicate]", question);
552       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
553         pred = _ref[_i];
554         pn = $(pred).attr('data-predicate');
555         _ref1 = $(".question-piece", pred);
556         for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
557           qp = _ref1[_j];
558           v = this.get_value_optional_list(qp, 'solution');
559           mandatory = v[0];
560           optional = v[1];
561           if (mandatory.indexOf(pn) >= 0 || (minimum && optional.indexOf(pn) >= 0)) {
562             count += 1;
563             this.piece_correct(qp);
564           } else {
565             bad_count += 1;
566             this.piece_incorrect(qp);
567           }
568         }
569       }
570       return [count, bad_count, all];
571     };
572
573     Przyporzadkuj.prototype.solve_question = function(question) {
574       var $ph, $pr, draggables, m, mandatory, minimum, optional, qp, v, _i, _len, _ref, _results;
575       minimum = $(question).data("min");
576       _ref = $(".subject .question-piece", question);
577       _results = [];
578       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
579         qp = _ref[_i];
580         v = this.get_value_optional_list(qp, 'solution');
581         mandatory = v[0];
582         optional = v[1];
583         if (minimum) {
584           draggables = mandatory.count(optional).slice(0, minimum);
585         } else {
586           draggables = mandatory;
587         }
588         _results.push((function() {
589           var _j, _len1, _results1;
590           _results1 = [];
591           for (_j = 0, _len1 = draggables.length; _j < _len1; _j++) {
592             m = draggables[_j];
593             $pr = $(".predicate [data-predicate=" + m + "]", question);
594             $ph = $pr.find(".placeholder:visible");
595             _results1.push(this.draggable_move($(qp), $ph.eq(0), this.multiple));
596           }
597           return _results1;
598         }).call(this));
599       }
600       return _results;
601     };
602
603     return Przyporzadkuj;
604
605   })(Exercise);
606
607   PrawdaFalsz = (function(_super) {
608     __extends(PrawdaFalsz, _super);
609
610     function PrawdaFalsz(element) {
611       var qp, _i, _len, _ref,
612         _this = this;
613       PrawdaFalsz.__super__.constructor.call(this, element);
614       _ref = $(".question-piece", this.element);
615       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
616         qp = _ref[_i];
617         $(".true", qp).click(function(ev) {
618           ev.preventDefault();
619           _this.retry();
620           $(ev.target).closest(".question-piece").data("value", "true");
621           return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
622         });
623         $(".false", qp).click(function(ev) {
624           ev.preventDefault();
625           _this.retry();
626           $(ev.target).closest(".question-piece").data("value", "false");
627           return $(ev.target).addClass('chosen').siblings('a').removeClass('chosen');
628         });
629       }
630     }
631
632     PrawdaFalsz.prototype.check_question = function() {
633       var all, bad, good, qp, _i, _len, _ref;
634       all = 0;
635       good = 0;
636       bad = 0;
637       _ref = $(".question-piece", this.element);
638       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
639         qp = _ref[_i];
640         if ($(qp).data("solution").toString() === $(qp).data("value")) {
641           good += 1;
642           this.piece_correct(qp);
643         } else {
644           bad += 1;
645           this.piece_incorrect(qp);
646         }
647         all += 1;
648       }
649       return [good, bad, all];
650     };
651
652     PrawdaFalsz.prototype.show_solutions = function() {
653       var qp, _i, _len, _ref, _results;
654       this.reset();
655       _ref = $(".question-piece", this.element);
656       _results = [];
657       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
658         qp = _ref[_i];
659         if ($(qp).data('solution') === true) {
660           _results.push($(".true", qp).click());
661         } else {
662           _results.push($(".false", qp).click());
663         }
664       }
665       return _results;
666     };
667
668     return PrawdaFalsz;
669
670   })(Exercise);
671
672   exercise = function(ele) {
673     var cls, es;
674     es = {
675       wybor: Wybor,
676       uporzadkuj: Uporzadkuj,
677       luki: Luki,
678       zastap: Zastap,
679       przyporzadkuj: Przyporzadkuj,
680       prawdafalsz: PrawdaFalsz
681     };
682     cls = es[$(ele).attr('data-type')];
683     return new cls(ele);
684   };
685
686   window.edumed = {
687     'EduModule': EduModule
688   };
689
690   $(document).ready(function() {
691     new EduModule($("#book-text"));
692     return $(".exercise").each(function(i, el) {
693       return exercise(this);
694     });
695   });
696
697 }).call(this);