var QuestionRanking = Class.create({
sQuestionType: 'RANKING',
TEXT: 0,
IMAGE: 1,
NONE: 0,
LETTERS: 1,
NUMBERS: 2,
//System settings
sListIdPrefix: 'ranking',
sIdDelimiter: '_',
sLabelSuffix: ') ',
iListHeightAdjust: -17, //17,//10
iListItemPadding: 10, //10,//5
//User settings
iChoiceWidth: 110,
iChoiceMaxHeight: 145,
labelType: 0,
//Variables
//sChoices: new Array(),
//iChoicesPos: new Array(),
//choicesInitPos: new Array(),
//sGoodAnswerFeedback: new Array(),
//sWrongAnswerFeedback: new Array(),
questionNb: 0,
oList: null,
someSortableId: '',
widthSortableFeedback: 0,
tableWidthSortableFeedback: 0,
tableHeightSortableFeedback: 0,
currentScore: 0,
ponderation: 0,
triesCount: 0,
status:'',
//Mandatory functions
initialize: function(quiz, page, questionNb){
this.quiz = quiz;
this.page = page;
this.questionNb = questionNb;
this.sChoices = new Array();
this.iChoicesPos = new Array();
this.choicesInitPos = new Array();
this.sGoodAnswerFeedback = new Array();
this.sWrongAnswerFeedback = new Array();
this.status = this.quiz.statusToDo;
},
display: function(){
//Lists
var oListsWrapper = nq4_buildHTMLElement('table',{cellpadding:'0',cellspacing:'0',border:'0'});
//oListsWrapper.width = (this.iChoiceWidth * 2);
var oCurrRow = oListsWrapper.insertRow(0);
var oLabelsWrapper = oCurrRow.insertCell(0);
var oChoicesWrapper = oCurrRow.insertCell(1);
oLabelsWrapper.vAlign = "top";
this.oList = this.buildRankingSortable();
this.oList.id = this.sListIdPrefix + this.sIdDelimiter + this.questionNb
oChoicesWrapper.appendChild(this.oList);
if(this.labelType != this.NONE){
var oLabelsList = this.buildRankingLabels();
oLabelsList.id = this.sListIdPrefix + this.sIdDelimiter + 'labels' + this.sIdDelimiter + this.questionNb
oLabelsWrapper.appendChild(oLabelsList);
}
$('question').update(oListsWrapper);
Position.includeScrollOffsets = true;
Sortable.create(this.oList.id, {scroll:window});
if(this.labelType != this.NONE){
this.balanceLists(this.oList,oLabelsList);
}
},
save: function(){
this.sSaveValue = '';
var iIndex = null;
var sId = null;
var iPos = 0;
for(var i = 0;i < this.oList.childNodes.length;i++){
if(this.oList.childNodes[i].tagName == 'LI'){
sId = this.oList.childNodes[i].id;
iIndex = sId.substring(sId.lastIndexOf(this.sIdDelimiter) + 1);
this.iChoicesPos[iIndex] = iPos;
iPos++;
}
}
},
validate: function(){
this.save();
this.triesCount++;
var feedbackHTML = '';
var goodAnswerCount = 0;
var wrongAnswerCount = 0;
var heightToPut;
var widthToPut;
var label = '';
var choice = '';
var bulletImage = '';
var feedback = '';
for(var i = 0;i < this.sChoices.length; i++){
var theCols = '
| ';
switch(this.labelType){
case this.LETTERS:
label = getLetterLabel(i + 1).toUpperCase() + this.sLabelSuffix;
break;
case this.NUMBERS:
label = (i + 1) + this.sLabelSuffix;
break;
}
currentChoiceId = this.iChoicesPos.indexOf(i);
choice = this.sChoices[currentChoiceId];
if(this.iChoicesPos[i] == i){
goodAnswerCount++;
bulletImage = 'bullet_green.jpg';
feedback = this.sGoodAnswerFeedback[currentChoiceId];
}else{
wrongAnswerCount++;
bulletImage = 'bullet_red.jpg';
feedback = this.sWrongAnswerFeedback[currentChoiceId]
}
heightToPut = document.getElementById(this.someSortableId).style.height;
feedbackHTML += '';
feedbackHTML += '';
feedbackHTML += ' | ';
if (this.labelType > 0){
feedbackHTML += ' | ';
theCols = theCols + ' | ';
}
if(choice[1] == this.TEXT){
feedbackHTML += ' | ';
feedbackHTML += '
' + theCols + '' + feedback + ' | ';
}else{
var temp = new Element('div');
temp.appendChild(nq4_buildImageObject(choice[0],this.iChoiceWidth,this.iChoiceMaxHeight,this.quiz.mediasFolder));
heightToPut = "100%";
feedbackHTML += ' | ';
feedbackHTML += '
' + theCols + '' + feedback + ' | ';
}
feedbackHTML += '
';
if (feedback != ''){
feedbackHTML += '' + theCols + ' |
';
}
feedbackHTML += '
';
}
this.currentScore = goodAnswerCount / this.sChoices.length * this.ponderation;
this.status = this.quiz.statusToRedo;
if(this.currentScore == this.ponderation){
feedbackHTML = '' + this.quiz.goodAnswerLabel + '
' + feedbackHTML;
this.status = this.quiz.statusCompleted;
}else{
if(wrongAnswerCount > 0)
feedbackHTML = '' + this.quiz.wrongAnswerLabel + '
' + feedbackHTML;
else
feedbackHTML = '' + this.quiz.incompleteAnswerLabel + '
' + feedbackHTML;
}
setFeedback(feedbackHTML);
openFeedback();
return this.currentScore;
},
showSolution: function(){
var label = '';
var solutionHTML = this.quiz.solutionLabel + '
';
var heightToPut;
for(var i = 0;i < this.sChoices.length;i++){
switch(this.labelType){
case this.LETTERS:
label = getLetterLabel(i + 1).toUpperCase() + this.sLabelSuffix;
break;
case this.NUMBERS:
label = (i + 1) + this.sLabelSuffix;
break;
}
choice = this.sChoices[i];
heightToPut = document.getElementById(this.someSortableId).style.height;
solutionHTML += '';
solutionHTML += '';
if (this.labelType > 0)
solutionHTML += ' | ';
if(choice[1] == this.TEXT){
solutionHTML += ' | ';
}else{
var temp = new Element('div');
temp.appendChild(nq4_buildImageObject(choice[0],this.iChoiceWidth,this.iChoiceMaxHeight,this.quiz.mediasFolder));
solutionHTML += ' | ';
}
solutionHTML += '
';
solutionHTML += '
';
}
setFeedback(solutionHTML);
openFeedback();
},
redo:function(whereFrom){
this.iChoicesPos = this.choicesInitPos.clone();
this.currentScore = 0;
this.status = this.quiz.statusToRedo;
closeFeedback();
this.display();
},
redoQuiz: function(){
this.iChoicesPos = this.choicesInitPos.clone();
this.currentScore = 0;
this.status = this.quiz.statusToRedo;
},
isAnswered: function() {
return true;
},
setLabelType: function(i){
this.labelType = i;
},
//Question specific functions
addChoice: function(sChoice,iChoiceType,sGoodAnswerFeedback,sWrongAnswerFeedback,position){
if (position == 0){
this.sChoices[this.sChoices.length] = [sChoice, iChoiceType];
if(iChoiceType == this.IMAGE){
this.quiz.imgPreloader.addImage(sChoice);
}
this.iChoicesPos[this.iChoicesPos.length] = this.iChoicesPos.length;
this.choicesInitPos[this.choicesInitPos.length] = this.choicesInitPos.length;
this.sGoodAnswerFeedback[this.sGoodAnswerFeedback.length] = sGoodAnswerFeedback;
this.sWrongAnswerFeedback[this.sWrongAnswerFeedback.length] = sWrongAnswerFeedback;
}
else{
position = position - 1;
this.sChoices[this.sChoices.length] = [sChoice, iChoiceType];
if(iChoiceType == this.IMAGE){
this.quiz.imgPreloader.addImage(sChoice);
}
this.iChoicesPos[this.iChoicesPos.length] = position;
this.choicesInitPos[this.choicesInitPos.length] = position;
this.sGoodAnswerFeedback[this.sGoodAnswerFeedback.length] = sGoodAnswerFeedback;
this.sWrongAnswerFeedback[this.sWrongAnswerFeedback.length] = sWrongAnswerFeedback;
}
},
setChoicePos: function(iID,iPos){
this.iChoicesPos[iID] = iPos;
},
buildRankingSortable: function(){
var oListItem = null;
var iCurrIndex = null;
var oList = nq4_buildHTMLElement('ul',{});
oList.style.listStyleType = 'none';
oList.style.padding = '0';
oList.style.marginLeft = '0';
oList.style.marginTop = '-3px'; //MONTE DE 10px POUR ALIGNEMENT LUCIE.
for(var i = 0;i < this.sChoices.length;i++){
oListItem = nq4_buildHTMLElement('li',{});
oListItem.style.marginBottom = '15px'; //20px
oListItem.style.border = 'solid 1px #CBC8C8';
oListItem.style.padding = this.iListItemPadding + 'px';
oListItem.style.cursor = 'move';
for(var j = 0;j < this.iChoicesPos.length;j++){
if(this.iChoicesPos[j] == i){
iCurrIndex = j;
break;
}
}
if(this.sChoices[iCurrIndex][1] == this.TEXT){
oListItem.innerHTML = this.sChoices[iCurrIndex][0];
}else{
var oContentLI = nq4_buildHTMLElement('table',{cellpadding:'0',cellspacing:'0',border:'0'});
var oCurrRowLI = oContentLI.insertRow(0);
var oCellLI = oCurrRowLI.insertCell(0);
oCellLI.id = "tdLI" + i;
oListItem.appendChild(oContentLI);
oCellLI.appendChild(nq4_buildImageObject(this.sChoices[iCurrIndex][0],this.iChoiceWidth,this.iChoiceMaxHeight,this.quiz.mediasFolder));
}
oListItem.id = this.sListIdPrefix + this.sIdDelimiter + this.questionNb + this.sIdDelimiter + iCurrIndex;
this.someSortableId = this.sListIdPrefix + this.sIdDelimiter + this.questionNb + this.sIdDelimiter + iCurrIndex;
oList.appendChild(oListItem);
}
return oList;
},
buildRankingLabels: function(){
var sLabel = '';
var oListItem = null;
var iCurrIndex = null;
var oList = nq4_buildHTMLElement('ul',{});
oList.style.listStyleType = 'none';
oList.style.padding = '0';
oList.style.marginLeft = '0';
oList.style.marginTop = '-3px'; //MONTE DE 10px POUR ALIGNEMENT LUCIE.
for(var i = 0;i < this.sChoices.length;i++){
oListItem = nq4_buildHTMLElement('li',{});
oListItem.style.marginBottom = '15px';
oListItem.style.paddingTop = this.iListItemPadding + 'px';
/*if(this.sChoices[i][1] == this.IMAGE){
alert("ici");
if (i == 0){
oListItem.style.paddingTop = '0px';
}
else{
oListItem.style.paddingTop = '10px';
}
}
else{
oListItem.style.paddingTop = '10px';
}*/
oListItem.style.paddingBottom = this.iListItemPadding + 'px';
oListItem.style.paddingRight = '6px';
if(this.labelType == this.LETTERS){
sLabel = getLetterLabel(i + 1).toUpperCase() + this.sLabelSuffix;
}else if(this.labelType == this.NUMBERS){
sLabel = (i + 1) + this.sLabelSuffix;
}
oListItem.innerHTML = sLabel;
oList.appendChild(oListItem);
}
return oList;
},
balanceLists: function(oListA,oListB){
var iMaxWidth = 0;
var iMaxHeight = 0;
for(var i = 0;i < oListA.childNodes.length;i++){
if(oListA.childNodes[i].tagName == 'LI'){
iMaxHeight = Math.max(iMaxHeight,getElementPos(oListA.childNodes[i]).height);
iMaxWidth = Math.max(iMaxWidth,getElementPos(oListA.childNodes[i]).width);
}
}
for(var i = 0;i < oListB.childNodes.length;i++){
if(oListB.childNodes[i].tagName == 'LI'){
iMaxHeight = Math.max(iMaxHeight,getElementPos(oListB.childNodes[i]).height);
}
}
iMaxHeight += this.iListHeightAdjust;
iMaxWidth = iMaxWidth - (this.iListItemPadding * 2) - 2;
for(var i = 0;i < oListA.childNodes.length;i++){
if(oListA.childNodes[i].tagName == 'LI'){
oListA.childNodes[i].style.height = iMaxHeight + 'px';
}
}
for(var i = 0;i < oListB.childNodes.length;i++){
if(oListB.childNodes[i].tagName == 'LI'){
oListB.childNodes[i].style.height = (iMaxHeight + 2) + 'px';
}
}
for(var i = 0;i < oListA.childNodes.length;i++){
if(oListA.childNodes[i].tagName == 'LI'){
var iCurrIndex;
for(var j = 0;j < this.iChoicesPos.length;j++){
if(this.iChoicesPos[j] == i){
iCurrIndex = j;
break;
}
}
this.widthSortableFeedback = iMaxWidth + 'px';
if(this.sChoices[iCurrIndex][1] == this.IMAGE){
var theID = "tdLI" + i;
$(theID).width = iMaxWidth + 'px';
$(theID).height = iMaxHeight + 'px';
this.tableWidthSortableFeedback = iMaxWidth + 'px';
this.tableHeightSortableFeedback = iMaxHeight + 'px';
document.getElementById(theID).align = 'center';
document.getElementById(theID).vAlign = 'middle';
}
}
}
},
shuffle: function(){
this.iChoicesPos = getShuffledOrder(this.sChoices.length);
this.choicesInitPos = this.iChoicesPos.clone();
},
getConsigne: function(){
return this.quiz.consigneRanking;
}
});