/**
* Scrambled List iDevice
*
* Released under Attribution-ShareAlike 4.0 International License.
* Author: Ignacio Gros (http://gros.es/) for http://exelearning.net/
*
* License: http://creativecommons.org/licenses/by-sa/4.0/
*
* It includes the HTML5 Sortable jQuery Plugin, released under the MIT license (details below)
*/
var $exeScrambledList = {
idevicePath:'',
init: function () {
this.idevicePath = (typeof ($exeAuthoring) != 'undefined') ? "/scripts/idevices/scrambled-list/export/" : "";
$(".exe-sortableList").each(function (instance) {
if ($('body').hasClass("exe-epub3")) {
$(this).prepend('
' + $exe_i18n.epubDisabled + '
' + $exe_i18n.solution + ':
')
} else {
$exeScrambledList.enableList(this, instance);
}
});
},
enableList: function (activity, instance) {
var lists = $("ul.exe-sortableList-list", activity);
var btn = $("p.exe-sortableList-buttonText", activity);
var evaluationIDC = $('div.exe-sortableList-evaluationID', activity);
var evaluationID = '';
var ideviceIDC = $('div.exe-sortableList-ideviceID', activity);
var ideviceID = false;
var evaluationMessagesC = $('div.exe-sortableList-evaluationMessages', activity);
var evaluationMessages = '###'
if (ideviceIDC.length == 1 && ideviceIDC.text() != "") {
ideviceID = ideviceIDC.text();
}
if (evaluationIDC.length == 1 && evaluationIDC.text() != "") {
evaluationID = evaluationIDC.text();
}
if (evaluationMessagesC.length == 1 && evaluationMessagesC.text() != "") {
evaluationMessages = evaluationMessagesC.text();
}
if (lists.length == 1 && btn.length == 1) {
lists.css("visibility", "hidden");
lists.each(
function () {
var lis = [];
$("LI", this).each(function () {
lis.push(this.innerHTML);
});
lis = $exeScrambledList.randomizeArray(lis);
$exeScrambledList.getListHTML(activity, lis, this, instance, btn, evaluationID, ideviceID, evaluationMessages);
}
);
$exeScrambledList.updateEvaluationIcon(instance, evaluationID, ideviceID, evaluationMessages)
}
},
getLinksHTML: function (i, listOrder) {
return '' + (i + 1) + ' → ' + i + '' + (i + 1) + ' → ' + (i + 2) + '';
},
getListLinks: function (listOrder) {
var ul = $("#exe-sortableList-" + listOrder);
var lis = $("li", ul);
$("SPAN", ul).remove();
lis.each(function (i) {
this.className = "";
if (i == 0) this.className = "first";
if ((i + 1) == lis.length) this.className = "last";
this.innerHTML += $exeScrambledList.getLinksHTML(i, listOrder);
});
// No inline JavaScript (onclick, etc.)
$('a.exe-sortableList-sorter', ul).each(function () {
var c = this.className;
c = c.split("exe-sortableList-sort-");
if (c.length == 2) {
c = c[1].split('_');
if (c.length == 3) {
this.onclick = function () {
$exeScrambledList.sortList(this, parseInt(c[0]), parseInt(c[1]), parseInt(c[2]));
return false;
}
}
}
});
},
sortList: function (e, a, b, listOrder) { // LI - FROM - TO
var list = $("#exe-sortableList-" + listOrder);
list.sortable("destroy");
var lis = $("LI", list);
if (b < 0 || b > (lis.length - 1)) return false;
var newList = [];
var li, prev, current, next;
for (var i = 0; i < lis.length; i++) {
li = lis[i].innerHTML.split("")[0].split("")[0];
newList.push(li);
if (i == (a - 1)) prev = li;
else if (i == a) current = li;
else if (i == (a + 1)) next = li;
}
newList[b] = current;
if (b < a) { // Up
newList[a] = prev;
} else { // Down
newList[a] = next;
}
list.html($exeScrambledList.getULhtml(newList, listOrder)).sortable();
},
getULhtml: function (lis, listOrder) {
html = '';
for (var i = 0; i < lis.length; i++) {
html += '
' + lis[i] + '
';
}
$("#exe-sortableList-" + listOrder).html(html).sortable();
$exeScrambledList.getListLinks(listOrder);
},
getListHTML: function (activity, lis, list, listOrder, btn, evaluationID, ideviceID, messages) {
var html = '
';
for (var i = 0; i < lis.length; i++) {
html += '
' + lis[i] + '
';
}
html += "
";
html += '';
html += '';
$(list).hide().attr("id", "exe-sortableListResults-" + listOrder).before(html);
$("#exe-sortableList-" + listOrder).sortable().bind('sortupdate', function (e, ui) {
$exeScrambledList.getListLinks(listOrder);
});
$exeScrambledList.getListLinks(listOrder);
// Event handlers
$("#exe-sortableListButton-" + listOrder).click(function () {
$exeScrambledList.check(this, listOrder, evaluationID, ideviceID, messages);
});
// / Event handlers
},
check: function (e, listOrder, evaluationID, ideviceID, messages) {
// No more sorting
$("#exe-sortableListButton-" + listOrder).hide();
var list = $("#exe-sortableList-" + listOrder);
$("a", list).hide();
list.sortable("destroy");
// Check the answers
var activity = $(e).parents(".exe-sortableList");
var right = true;
var userList = $("#exe-sortableList-" + listOrder);
var rightAnswers = $("#exe-sortableListResults-" + listOrder);
var rightAnswersLis = $("li", rightAnswers);
$("LI", userList).each(function (i) {
var currentText = $(this).html().split("")[0].split("")[0];
if (currentText != rightAnswersLis.eq(i).html()) right = false;
});
var feedback = $('#exe-sortableList-' + listOrder + '-feedback');
var score = '0.00'
if (right) {
feedback.html("