<commit_before>/******************************************************************************
*App -> Container object. 
******************************************************************************/
var App = {

}

/******************************************************************************
*	Views -> More ParentViews, see lift.js
******************************************************************************/

var ChoiceView = BaseView.extend({
	template: "#choice-template",
	checkAnswer: function() {
		var guess = this.$el.find("input").is(":checked");
		var store = {
			guess: ""
		};
		store.result = (guess === this.model.get("is_correct"));
		if(guess) {
			store.guess = this.model.get("text");
		}
		return store;
	}
});

var BasicAttemptView = BaseView.extend({
	tagName: "tr",
	template: "#quiz-basic-attempt-template"
});

var BasicAttemptListView = ListView.extend({
	SingleView: BasicAttemptView
});

var QuizView = BaseView.extend({
	template: "#single-quiz-template",
	events: {
		"click .quiz-submit": "checkAnswer",
		"keyup .quiz-guess": "submitAnswerOnEnter"
	},
	render: function() {
		BaseView.prototype.render.call(this);
		if(this.model.get("isMCQ")) {
			this.renderChoices(this.model.choices.models);
		}
		this.attemptListView = new BasicAttemptListView({
			collection: this.model.attempts,
			el: this.$el.find("tbody")
		});
		this.attemptListView.render();
	},
	afterRender: function() {
		this.$el.find(".quiz-guess").focus();
		window.scrollTo(0, 0);
	},
	renderChoices:function(choices) {
		this.choices = [];
		var self = this;
		choices.forEach(function(c) {
			var view = new ChoiceView({model: c});
			view.render();
			self.choices.push(view);
			self.$el.find(".choices").append(view.$el);
		});
	},
	checkAnswer: function(evt) {
		var i;
		var attempt = {};
		if(!this.model.get("isMCQ")) {
			attempt.guess = $.trim(this.$el.find(".quiz-guess").val());
			attempt.result = (attempt.guess === String(this.model.get("answer")));
		}
		else {
			attempt = this.checkMCQAnswer();
		}
		console.log(attempt);
		this.model.addAttempt(attempt);
		if(attempt.result) {
			this.options.parent.showNext(this.model);	
		}
	},

	checkMCQAnswer: function() {
		var result = true;
		var guesses = "";
		var data;
		for(i = 0; i<this.choices.length; i++) {
			data = this.choices[i].checkAnswer();
			if(data.guess) {
				guesses += data.guess + ";";
			}
			if(!data.result) {
				result = false;
			}
		}
		return {
			result: result,
			guess: guesses
		}

	},
	submitAnswerOnEnter: function(evt) {
		if(evt.keyCode == 13) {
			this.checkAnswer();
		}
	}

});<commit_msg>Changed MCQ guess delimiter to ','<commit_after>/******************************************************************************
*App -> Container object. 
******************************************************************************/
var App = {

}

/******************************************************************************
*	Views -> More ParentViews, see lift.js
******************************************************************************/

var ChoiceView = BaseView.extend({
	template: "#choice-template",
	checkAnswer: function() {
		var guess = this.$el.find("input").is(":checked");
		var store = {
			guess: ""
		};
		store.result = (guess === this.model.get("is_correct"));
		if(guess) {
			store.guess = this.model.get("text");
		}
		return store;
	}
});

var BasicAttemptView = BaseView.extend({
	tagName: "tr",
	template: "#quiz-basic-attempt-template"
});

var BasicAttemptListView = ListView.extend({
	SingleView: BasicAttemptView
});

var QuizView = BaseView.extend({
	template: "#single-quiz-template",
	events: {
		"click .quiz-submit": "checkAnswer",
		"keyup .quiz-guess": "submitAnswerOnEnter"
	},
	render: function() {
		BaseView.prototype.render.call(this);
		if(this.model.get("isMCQ")) {
			this.renderChoices(this.model.choices.models);
		}
		this.attemptListView = new BasicAttemptListView({
			collection: this.model.attempts,
			el: this.$el.find("tbody")
		});
		this.attemptListView.render();
	},
	afterRender: function() {
		this.$el.find(".quiz-guess").focus();
		window.scrollTo(0, 0);
	},
	renderChoices:function(choices) {
		this.choices = [];
		var self = this;
		choices.forEach(function(c) {
			var view = new ChoiceView({model: c});
			view.render();
			self.choices.push(view);
			self.$el.find(".choices").append(view.$el);
		});
	},
	checkAnswer: function(evt) {
		var i;
		var attempt = {};
		if(!this.model.get("isMCQ")) {
			attempt.guess = $.trim(this.$el.find(".quiz-guess").val());
			attempt.result = (attempt.guess === String(this.model.get("answer")));
		}
		else {
			attempt = this.checkMCQAnswer();
		}
		console.log(attempt);
		this.model.addAttempt(attempt);
		if(attempt.result) {
			this.options.parent.showNext(this.model);	
		}
	},

	checkMCQAnswer: function() {
		var result = true;
		var guesses = "";
		var data;
		var length = this.choices.length;
		for(i = 0; i<length; i++) {
			data = this.choices[i].checkAnswer();
			if(data.guess) {
				guesses += data.guess + ", ";
			}
			if(!data.result) {
				result = false;
			}
		}
		guesses = guesses.slice(0, guesses.length - 2);
		return {
			result: result,
			guess: guesses
		}

	},
	submitAnswerOnEnter: function(evt) {
		if(evt.keyCode == 13) {
			this.checkAnswer();
		}
	}

});
{'fraction_non_alphanumeric': 0.283323933070126, 'fraction_numerical': 0.0022560631697687537, 'mean_word_length': 4.205479452054795, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5477242', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1744, 'n_words': 288}