From: Jan Szejko Date: Mon, 17 Jul 2017 15:33:58 +0000 (+0200) Subject: shuffle answers in exercise order + fix answer numbering X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/2f9850da2c6ad0258f87c3d8972689da6aa8011d?ds=inline shuffle answers in exercise order + fix answer numbering --- diff --git a/src/editor/plugins/core/edumed/order/element.js b/src/editor/plugins/core/edumed/order/element.js index 724f2ec..4fb7507 100644 --- a/src/editor/plugins/core/edumed/order/element.js +++ b/src/editor/plugins/core/edumed/order/element.js @@ -53,6 +53,16 @@ _.extend(OrderExerciceElement, { }.bind(this)); + this.view.on('shuffleItems', function () { + this.wlxmlNode.document.transaction(function() { + this.shuffleItems(); + }.bind(this), { + metadata: { + description: gettext('Set random order') + } + }); + }.bind(this)); + var exerciseNode = this.wlxmlNode; this.createContainer(this.wlxmlNode.object.getDescription(), { resetBackground: true, @@ -83,6 +93,7 @@ _.extend(OrderExerciceElement, { var node = event.meta.node; if(node.is('item.answer') && event.meta.parent && event.meta.parent.is('list.orderable')) { this.reloadView(); + this.updateNumbers(); } }, reloadView: function() { @@ -91,6 +102,23 @@ _.extend(OrderExerciceElement, { this.view.addItem(item); }.bind(this)); }, + updateNumbers: function () { + var answer = 1; + this.view.sortedItemViews().forEach(function (itemView) { + itemView.item.setAnswer(answer); + answer++; + }) + }, + shuffleItems: function () { + var items = this.wlxmlNode.object.getItems(); + var currentIndex = items.length, randomIndex; + while (0 !== currentIndex) { + randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex -= 1; + items[currentIndex].node.after(items[randomIndex].node); + } + this.reloadView(); + }, getVerticallyFirstTextElement: function() { var toret; this.containers.some(function(container) { diff --git a/src/editor/plugins/core/edumed/order/view.html b/src/editor/plugins/core/edumed/order/view.html index 561de89..25f3bf2 100644 --- a/src/editor/plugins/core/edumed/order/view.html +++ b/src/editor/plugins/core/edumed/order/view.html @@ -5,6 +5,7 @@
  • <%= gettext('Initial') %>
  • <%= gettext('Solution') %>
  • +
      \ No newline at end of file diff --git a/src/editor/plugins/core/edumed/order/view.js b/src/editor/plugins/core/edumed/order/view.js index 3dc9a81..4b470e6 100644 --- a/src/editor/plugins/core/edumed/order/view.js +++ b/src/editor/plugins/core/edumed/order/view.js @@ -15,6 +15,7 @@ var OrderExerciseView = function(element) { this.modePills = this.dom.find('.modePills'); this.list = this.dom.find('ol'); this.description = this.dom.find('.description'); + this.shuffleButton = this.dom.find('.shuffle'); this.itemViews = []; this.modePills.find('a').on('click', function(e) { @@ -23,6 +24,12 @@ var OrderExerciseView = function(element) { this.setMode($(e.target).parent().attr('mode')); }.bind(this)); + this.shuffleButton.on('click', function(e) { + e.stopPropagation(); + e.preventDefault(); + this.trigger('shuffleItems'); + }.bind(this)); + this.mode = 'initial'; var dropTargets = this.dom.find('.placeholder-top'); @@ -34,13 +41,7 @@ var OrderExerciseView = function(element) { dropTargets.on('dragenter', function() { var first = this.itemViews[0]; if(this.mode === 'correct') { - first = this.itemViews.slice(0) - .sort(function(view1, view2) { - if(view1.item.getAnswer() > view2.item.getAnswer()) { - return 1; - } - return -1; - })[0]; + first = this.sortedItemViews()[0]; } if(!this.allowDropAt(first, true)) { return; @@ -63,13 +64,7 @@ var OrderExerciseView = function(element) { var first = this.itemViews[0]; if(this.mode === 'correct') { - first = this.itemViews.slice(0) - .sort(function(view1, view2) { - if(view1.item.getAnswer() > view2.item.getAnswer()) { - return 1; - } - return -1; - })[0]; + first = this.sortedItemViews()[0]; } this.trigger(this.mode === 'initial' ? 'moveItem' : 'moveAnswer', droppedItem.item, first.item, 'before'); @@ -110,19 +105,24 @@ _.extend(OrderExerciseView.prototype, Backbone.Events, { this.itemViews.forEach(function(itemView) { this.list.append(itemView.dom); }.bind(this)); + this.shuffleButton.show(); } else { - this.itemViews.slice(0) - .sort(function(view1, view2) { - if(view1.item.getAnswer() > view2.item.getAnswer()) { - return 1; - } - return -1; - }) + this.sortedItemViews() .forEach(function(itemView) { this.list.append(itemView.dom); }.bind(this)); + this.shuffleButton.hide(); } }, + sortedItemViews: function () { + return this.itemViews.slice(0) + .sort(function(view1, view2) { + if(view1.item.getAnswer() > view2.item.getAnswer()) { + return 1; + } + return -1; + }); + }, allowDropAt: function(view, up) { var arr = [this.draggedView.dom[0]]; if(!up) {