Skip to content

Commit

Permalink
Merge pull request #5582 from christianbeeznest/ofaj-21775
Browse files Browse the repository at this point in the history
Exercise: Fix ordering question shuffle issue in draggable questions - refs BT#21775
  • Loading branch information
christianbeeznest authored Jun 10, 2024
2 parents 17c27c9 + fcff639 commit e8df95b
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions public/main/template/default/exercise/submit.js.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -224,32 +224,38 @@ jsPlumb.ready(function () {
});
$(function () {
DraggableAnswer.init(
$(".exercise-draggable-answer"),
$(".droppable")
);
// if shuffle answers
if ('{{ shuffle_answers }}' == '1') {
$('.exercise-draggable-answer').each(function(){
// get current ul
var $ul = $(this);
// get array of list items in current ul
var $liArr = $ul.children('li');
// sort array of list items in current ul randomly
$liArr.sort(function(a,b){
// Get a random number between 0 and 10
var temp = parseInt( Math.random()*100 );
// Get 1 or 0, whether temp is odd or even
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")
);
// if shuffle answers
if ('{{ shuffle_answers }}' == '1') {
$('.exercise-draggable-answer').each(function() {
var $ul = $(this);
var $liArr = $ul.children('li').toArray();
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
var previousOrder = $liArr.map(item => item.id).join();
var newOrder;
do {
shuffle($liArr);
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>

0 comments on commit e8df95b

Please sign in to comment.