Skip to content

Commit

Permalink
update structure
Browse files Browse the repository at this point in the history
  • Loading branch information
NayanKhedkar committed Sep 26, 2017
1 parent 1575e90 commit adc4c3a
Show file tree
Hide file tree
Showing 9 changed files with 1,899 additions and 583 deletions.
33 changes: 17 additions & 16 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "adapt-sentenceOrdering",
"version": "2.0.0",
"framework": "^2.0.0",
"authors": [
"Nayan Khedkar <[email protected]>"
],
"description": "A sentence Ordering question component.",
"main": "/js/adapt-sentenceOrdering.js",
"displayName" : "Sentence Ordering",
"component" : "sentenceOrdering",
"keywords": [
"adapt-plugin",
"adapt-component"
],
"license": "GPLv3"
}
"name": "adapt-sentenceOrdering",
"repository": "git://github.com/NayanKhedkar/adapt-sentenceOrdering.git",
"framework": "^2.0.0",
"version": "2.0.0",
"authors": [
"Nayan Khedkar <[email protected]>"
],
"description": "A sentence Ordering question component.",
"main": "/js/adapt-sentenceOrdering.js",
"displayName": "Sentence Ordering",
"component": "sentenceOrdering",
"keywords": [
"adapt-plugin",
"adapt-component"
],
"license": "GPLv3"
}
26 changes: 1 addition & 25 deletions example.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//sorting for sentence ordering
//sentence ordering
{
"_id": "c-100",
"_parentId": "b-100",
Expand All @@ -21,30 +21,6 @@
"_shouldScale":false,
"_defaultWidth": 860,
"_isPrefixTitle":false,
"_buttons": {
"_submit": {
"buttonText": "Submit",
"ariaLabel": "Select here to submit your answer."
},
"_reset": {
"buttonText": "Reset",
"ariaLabel": ""
},
"_showCorrectAnswer": {
"buttonText": "Correct Answer",
"ariaLabel": ""
},
"_hideCorrectAnswer": {
"buttonText": "My Answer",
"ariaLabel": ""
},
"_showFeedback": {
"buttonText": "Show feedback",
"ariaLabel": ""
},
"remainingAttemptsText": "attempts remaining",
"remainingAttemptText": "final attempt"
},
"_feedback": {
"correct": "This feedback will appear if you answered the question correctly.",
"_incorrect": {
Expand Down
192 changes: 6 additions & 186 deletions js/adapt-sentenceOrdering.js
Original file line number Diff line number Diff line change
@@ -1,190 +1,10 @@
define(function(require) {
var QuestionView = require('coreViews/questionView'),
Adapt = require('coreJS/adapt'),
SortableLib = require('components/adapt-sentenceOrdering/js/sortable');
var SentenceOrdering = QuestionView.extend({
events: {
},
setupQuestion: function() {
this.listenTo(Adapt, 'device:resize', this.resizeItems, 200);
this.setupRandomisation();
},
setupRandomisation: function() {
if (this.model.get('_isRandom')) {
this.model.set("_shuffleItems", _.shuffle(this.model.get("_items")));
} else {
this.model.set("_shuffleItems", this.model.get("_items"));
}
},
var Adapt = require('core/js/adapt');
var SentenceOrderingModel = require('./sentenceOrderingModel');
var SentenceOrderingView = require('./sentenceOrderingView');

onQuestionRendered: function() {
this.model.set('_sortableItems', this.$('#sortable').html());
this.setReadyStatus();
this.sortSentenceInitialize();
this.setHeight();
this.resizeItems();
if(!this.model.get('_isPrefixTitle'))this.$('.sentence-container').width('100%');
},

sortSentenceInitialize: function(event) {
var self = this;
if (event && event.preventDefault) {
event.preventDefault();
}
this.$("#sortable").sortable().on('sortable:activate', function(event, ui) {
$(ui.item).css({
'z-index': '1000'
});
}).on('sortable:deactivate', function(event, ui) {
$(ui.item).css({
'z-index': '0'
});
self.setDefaultHeight();
}).on('sortable:change', function(event, ui) {
$("li.placeholder").css('height', ui.item.height() + 'px');
});
},
setDefaultHeight: function() {
var sentenceOrderingContainers = this.$('.prefixTitle ,.sentenceSequence');
sentenceOrderingContainers.css({
'height': 'auto'
});
this.setHeight();
},
setHeight: function() {
var prefix = this.$('.prefixTitle'),
sentence = this.$('.sentenceSequence'),
prefixHeight,
sentenceHeight;
for (var i = 0, length = prefix.length; i < length; i++) {
prefixHeight = prefix.eq(i).height();
sentenceHeight = sentence.eq(i).height();
if (!(prefixHeight === sentenceHeight)) {
if (prefixHeight > sentenceHeight) {
sentence.eq(i).css({
'height': prefixHeight
});
} else {
prefix.eq(i).css({
'height': sentenceHeight
});
}
}
}
},
resizeItems: function() {
if (this.model.get("_shouldScale")) {
var $el = this.$("#innerWrapper"),
elHeight = $el.outerHeight(),
elWidth = $el.outerWidth(),
$wrapper = this.$('#scaleableWrapper');

function doResize(event, ui) {
var scale, heightFactor;
scale = Math.min(
ui.size.width / elWidth,
ui.size.height / elHeight
);
if (scale > 1) {
scale = 1;
}
$el.css({
'-ms-transform': 'scale(' + scale + ')',
'-moz-transform': 'scale(' + scale + ')',
'-webkit-transform': 'scale(' + scale + ')',
'-webkit-transform-style': 'preserve-3d',
'-webkit-transform': 'scale3d(' + scale + ',' + scale + ',' + scale + ')',
'transform': 'scale(' + scale + ')'
}).attr('zoom', scale);
$el.height(this.$('.sentence-container').height() * scale);
}
var starterData = {
size: {
width: $wrapper.width(),
height: $wrapper.height()
}
};
doResize(null, starterData);
} else {
this.setDefaultHeight();
}
},
disableQuestion: function() {
this.$("#sortable").sortable('disable');
},
enableQuestion: function() {
this.$("#sortable").sortable('enable');
},
canSubmit: function() {
return true;
},
onSubmitted: function() {
var numberOfIncorrectAnswers = this.model.get('_numberOfIncorrectAnswers');
var attemptsLeft = this.model.get('_attemptsLeft');
if (attemptsLeft !== 0 && numberOfIncorrectAnswers > 0)
this.$('.sentenceSequence').addClass('incorrect-resettable');
},
storeUserAnswer: function() {
var listElements = this.$(".sentenceSequence"),
userAnswer = [];
_.each(listElements, function(item, index) {
userAnswer.push({
'id': parseInt(item.id)
});
});
this.model.set({
'_userAnswerListElement': listElements,
'_userAnswer': userAnswer
});
},
showCorrectAnswer: function() {
var listElements = [],
cloneElement = this.$("#sortable li").clone();
cloneElement.sort(function(firstEle, secondEle) {
return parseInt(firstEle.id) - parseInt(secondEle.id);
}).each(function() {
listElements.push(this)
});
this.$('#sortable').html(listElements);
this.setDefaultHeight();
},
hideCorrectAnswer: function() {
this.$('#sortable').html(this.model.get('_userAnswerListElement'));
this.setDefaultHeight();
},
showMarking: function() {
var listElements = this.model.get('_userAnswer');
_.each(listElements, function(item, index) {
var $item = this.$('.sentenceSequence').eq(index);
$item.addClass(item._isCorrect ? 'correct' : 'incorrect')
}, this);
},
resetQuestion: function() {
this.model.set('_userAnswer', []);
this.$('#sortable').html(this.model.get('_sortableItems'));
},
isCorrect: function() {
var listElements = this.model.get('_userAnswer'),
items = this.model.get("_items"),
numberOfCorrectAnswers = 0,
numberOfIncorrectAnswers = 0,
isCorrect;
_.each(listElements, function(item, index) {
isCorrect = _.contains(items[index].position, item.id);
if (isCorrect) {
numberOfCorrectAnswers++;
item._isCorrect = true;
} else {
numberOfIncorrectAnswers++;
}
}, this);
this.model.set('_numberOfCorrectAnswers', numberOfCorrectAnswers);
this.model.set('_numberOfIncorrectAnswers', numberOfIncorrectAnswers);
// Check if correct answers matches correct items and there are no incorrect selections
var answeredCorrectly = (numberOfCorrectAnswers === items.length) && (numberOfIncorrectAnswers === 0);
return answeredCorrectly;
}
return Adapt.register("sentenceOrdering", {
view: SentenceOrderingView,
model: SentenceOrderingModel
});
Adapt.register("sentenceOrdering", SentenceOrdering);
return SentenceOrdering;
});
109 changes: 109 additions & 0 deletions js/sentenceOrderingModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
define(function(require) {
var QuestionModel = require('core/js/models/questionModel');

var SentenceOrderingModel = QuestionModel.extend({

init: function() {
QuestionModel.prototype.init.call(this);
},

restoreUserAnswers: function() {
if (!this.get("_isSubmitted")) return;
var userAnswer = this.get("_userAnswer");
var itemArray = [];
var items = _.sortBy(this.get("_items"));
_.each(userAnswer, function(item, index) {
itemArray[index] = items[item - 1];
}, this);
this.set("_items", itemArray);
this.setPrefixTitle();
this.setQuestionAsSubmitted();
this.markQuestion();
this.setScore();
this.setupFeedback();
},

setupRandomisation: function() {
this.setPrefixTitle();
if (this.get('_isRandom') && this.get('_isEnabled')) {
this.set("_items", _.shuffle(this.get("_items")));
}
},

setPrefixTitle: function() {
if (!this.get("_isPrefixTitle")) return;
var items = _.sortBy(this.get('_items'), 'id');
this.set("_prefixTitles", _.pluck(items, 'prefixTitle'));
},

// check if the user is allowed to submit the question
canSubmit: function() {
return true;

},

// This is important for returning or showing the users answer
// This should preserve the state of the users answers
storeUserAnswer: function() {
var userAnswer = [];
var tempArray = [];
var items = _.sortBy(this.get('_items'), 'id');
var userSortedList = this.get('_sentenceListJqueryObject').children();
this.set("userSortedList", userSortedList);
_.each(userSortedList, function(item, index) {
userAnswer.push(parseInt(item.dataset.itemid));
tempArray.push(items[parseInt(item.dataset.itemid) - 1]);
});
this.set({
'_items': tempArray,
'_userAnswer': userAnswer
});
},

isCorrect: function() {
var userAnswer = this.get('_userAnswer'),
itemsSorted = _.sortBy(this.get("_items"), 'id'),
items = this.get("_items"),
numberOfCorrectAnswers = 0,
numberOfIncorrectAnswers = 0,
isItemOnCorrectPlace = [];
_.each(userAnswer, function(item, index) {
if (_.contains(itemsSorted[index].position, item)) {
numberOfCorrectAnswers++;
isItemOnCorrectPlace.push(true);
} else {
numberOfIncorrectAnswers++;
isItemOnCorrectPlace.push(false);
}
}, this);
this.set('isItemOnCorrectPlace', isItemOnCorrectPlace);
this.set('_numberOfCorrectAnswers', numberOfCorrectAnswers);
this.set('_numberOfIncorrectAnswers', numberOfIncorrectAnswers);
// Check if correct answers matches correct items and there are no incorrect selections
var answeredCorrectly = (numberOfCorrectAnswers === items.length) && (numberOfIncorrectAnswers === 0);
return answeredCorrectly;
},

// Sets the score based upon the questionWeight
// Can be overwritten if the question needs to set the score in a different way
setScore: function() {
var questionWeight = this.get("_questionWeight");
var answeredCorrectly = this.get('_isCorrect');
var score = answeredCorrectly ? questionWeight : 0;
this.set('_score', score);
},
isPartlyCorrect: function() {
return this.get('_numberOfCorrectAnswers') >= this.get('_items').length / 2;
},
/*resetItems: function() {
if(!this.get('_previousItems')) return;
this.set('_items', this.get('_previousItems'));
},*/
resetUserAnswer: function() {
this.set({_userAnswer: []});
}
});

return SentenceOrderingModel;

});
Loading

0 comments on commit adc4c3a

Please sign in to comment.