Merge pull request #5582 from christianbeeznest/ofaj-21775

Exercise: Fix ordering question shuffle issue in draggable questions - refs BT#21775
pull/5584/head
christianbeeznest 1 year ago committed by GitHub
commit e8df95b26e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 60
      public/main/template/default/exercise/submit.js.html.twig

@ -224,32 +224,38 @@ jsPlumb.ready(function () {
}); });
$(function () { $(function () {
DraggableAnswer.init( DraggableAnswer.init(
$(".exercise-draggable-answer"), $(".exercise-draggable-answer"),
$(".droppable") $(".droppable")
); );
// 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 var $ul = $(this);
var $ul = $(this); var $liArr = $ul.children('li').toArray();
// get array of list items in current ul
var $liArr = $ul.children('li'); function shuffle(array) {
// sort array of list items in current ul randomly for (let i = array.length - 1; i > 0; i--) {
$liArr.sort(function(a,b){ const j = Math.floor(Math.random() * (i + 1));
// Get a random number between 0 and 10 [array[i], array[j]] = [array[j], array[i]];
var temp = parseInt( Math.random()*100 ); }
// Get 1 or 0, whether temp is odd or even return array;
var isOddOrEven = temp%2; }
// Get +1 or -1, whether temp greater or smaller than 5
var isPosOrNeg = temp>5 ? 1 : -1; var previousOrder = $liArr.map(item => item.id).join();
// Return -1, 0, or +1 var newOrder;
return( isOddOrEven*isPosOrNeg );
}) do {
// append list items to ul shuffle($liArr);
.appendTo($ul); newOrder = $liArr.map(item => item.id).join();
}); } while (newOrder === previousOrder);
}
// Detach and append list items to preserve event handlers
$.each($liArr, function(index, item) {
$ul.append($(item).detach());
});
});
}
}); });
</script> </script>

Loading…
Cancel
Save