shuffle answers in exercise order + fix answer numbering
authorJan Szejko <janek37@gmail.com>
Mon, 17 Jul 2017 15:33:58 +0000 (17:33 +0200)
committerJan Szejko <janek37@gmail.com>
Mon, 17 Jul 2017 15:33:58 +0000 (17:33 +0200)
src/editor/plugins/core/edumed/order/element.js
src/editor/plugins/core/edumed/order/view.html
src/editor/plugins/core/edumed/order/view.js

index 724f2ec..4fb7507 100644 (file)
@@ -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) {
index 561de89..25f3bf2 100644 (file)
@@ -5,6 +5,7 @@
         <li class="active" mode="initial"><a href="#"><%= gettext('Initial') %></a></li>
         <li mode="correct"><a href="#"><%= gettext('Solution') %></a></li>
     </ul>
+    <button class="shuffle"><%= gettext('Shuffle') %></button>
     <div class="placeholder placeholder-top"></div>
     <ol></ol>
 </div>
\ No newline at end of file
index 3dc9a81..4b470e6 100644 (file)
@@ -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) {