Exercises: Fix draggable shuffle answer order BT#17725

pull/4101/head
Julio Montoya 5 years ago
parent a7d243b8ee
commit 5074fa6f6f
  1. 46
      main/template/default/exercise/submit.js.tpl
  2. 41
      main/template/rainbow/exercise/submit.js.tpl

@ -222,33 +222,35 @@ jsPlumb.ready(function () {
} }
}); });
$(function () { function shuffleArray(array) {
DraggableAnswer.init( for (var i = array.length - 1; i > 0; i--) {
$(".exercise-draggable-answer"), var j = Math.floor(Math.random() * (i + 1));
$(".droppable") var temp = array[i];
); array[i] = array[j];
array[j] = temp;
}
}
$(function () {
// if shuffle answers // if shuffle answers
if ('{{ shuffle_answers }}' == '1') { if ('{{ shuffle_answers }}' == '1') {
$('.exercise-draggable-answer').each(function(){ $('.exercise-draggable-answer').each(function(){
// get current ul // get current ul
var $ul = $(this); var $ul = $(this);
// get array of list items in current ul var li = $ul.find('.touch-items');
var $liArr = $ul.children('li'); var liContents = [];
// sort array of list items in current ul randomly li.each(function() {
$liArr.sort(function(a,b){ liContents.push($(this));
// Get a random number between 0 and 10 });
var temp = parseInt( Math.random()*100 ); shuffleArray(liContents);
// Get 1 or 0, whether temp is odd or even $ul.empty().html(liContents);
var isOddOrEven = temp%2; });
// Get +1 or -1, whether temp greater or smaller than 5 }
var isPosOrNeg = temp>5 ? 1 : -1;
// Return -1, 0, or +1 DraggableAnswer.init(
return( isOddOrEven*isPosOrNeg ); $(".exercise-draggable-answer"),
}) $(".droppable")
// append list items to ul );
.appendTo($ul);
});
}
}); });
</script> </script>

@ -222,33 +222,34 @@ jsPlumb.ready(function () {
} }
}); });
$(function () { function shuffleArray(array) {
DraggableAnswer.init( for (var i = array.length - 1; i > 0; i--) {
$(".exercise-draggable-answer"), var j = Math.floor(Math.random() * (i + 1));
$(".droppable") var temp = array[i];
); array[i] = array[j];
array[j] = temp;
}
}
$(function () {
// if shuffle answers // if shuffle answers
if ('{{ shuffle_answers }}' == '1') { if ('{{ shuffle_answers }}' == '1') {
$('.exercise-draggable-answer').each(function(){ $('.exercise-draggable-answer').each(function(){
// get current ul // get current ul
var $ul = $(this); var $ul = $(this);
// get array of list items in current ul var li = $ul.find('.touch-items');
var $liArr = $ul.children('li'); var liContents = [];
// sort array of list items in current ul randomly li.each(function() {
$liArr.sort(function(a,b){ liContents.push($(this));
// Get a random number between 0 and 10 });
var temp = parseInt( Math.random()*100 ); shuffleArray(liContents);
// Get 1 or 0, whether temp is odd or even $ul.empty().html(liContents);
var isOddOrEven = temp%2;
// Get +1 or -1, whether temp greater or smaller than 5
var isPosOrNeg = temp>5 ? 1 : -1;
// Return -1, 0, or +1
return( isOddOrEven*isPosOrNeg );
})
// append list items to ul
.appendTo($ul);
}); });
} }
DraggableAnswer.init(
$(".exercise-draggable-answer"),
$(".droppable")
);
}); });
</script> </script>

Loading…
Cancel
Save