Skip to content

Instantly share code, notes, and snippets.

@easierbycode
Last active December 4, 2017 23:10
Show Gist options
  • Save easierbycode/b8ffb469fd45527b3683dc72bce755f7 to your computer and use it in GitHub Desktop.
Save easierbycode/b8ffb469fd45527b3683dc72bce755f7 to your computer and use it in GitHub Desktop.
Mindcrowd
// function getParameterByName( name ){
// name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
// var regexS = "[\\?&]"+name+"=([^&#]*)",
// regex = new RegExp( regexS ),
// results = regex.exec( window.location.href );
// if( results == null ){
// return "";
// } else{
// return decodeURIComponent(results[1].replace(/\+/g, " "));
// }
// }
// var current_language = function(lang){
// if (typeof lang != 'undefined') {
// current_language = lang;
// }
// return (current_language || getParameterByName('locale') || 'en');
// }
var _rounds;
var rounds = function(rounds) {
if (rounds != undefined){_rounds = rounds; }
return _rounds;
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var getQuestionsAndAnswers = function(roundNumber) {
var q_and_a = []
$(_rounds[roundNumber].questions).each(function(i, q){
// if (q.language == Mindcrowd.General.language() ) {
q_and_a.push([q.question, q.answer]);
// DRJ: instead of database objects I will be passing in nested arrays
// q_and_a.push(q);
// }
});
// DRJ: don't shuffle, order should always stay same
// return shuffle(q_and_a);
return q_and_a;
}
//Memory Test
function memoryTest() {
var words;
var test;
var counter;
var currentRound = 0;
var questionCount;
var total_right;
var total_wrong;
var questionCounter = 0;
var testCountdown;
var start_time;
var response_times;
var test_responses = {};
//Get groups of words for Memory Test page
$.ajax({
type: "get",
url: window.location.protocol + '//' + window.location.host + window.location.pathname + '.json' + window.location.search,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
console.log(data.test_rounds);
// Mindcrowd.Tests.rounds(data.test_rounds);
rounds(data.test_rounds);
showStudy();
}
});
function showStudy() {
wordCounter = 0;
questionCount = 0;
total_right = 0;
total_wrong = 0;
response_times = [];
test_responses[currentRound] = []
words = getQuestionsAndAnswers(currentRound);
display_words = shuffle(words.slice(0));
// roundTitle = Mindcrowd.Tests.rounds()[currentRound].title
roundTitle = rounds()[currentRound].title;
$('.word_input input').trigger('blur');
$('#test_holder').hide();
$('#study, .loading').fadeIn(1000);
$('body').removeClass('study_' + (currentRound - 1)).addClass('study_' + currentRound);
$('.round_title').text(roundTitle);
var wordInterval = setInterval(function() {
$('.loading').hide();
showQandA(display_words[wordCounter][0], display_words[wordCounter][1]);
wordCounter++;
if (wordCounter >= words.length) {
clearInterval(wordInterval);
setTimeout(function() {
showQuestion();
}, 3000);
}
}, 3000);
currentRound++;
}
function showQandA(item1, item2) {
// DRJ: image pair not implemented
// if ($('section#main').attr('data-question-type') == "image") {
// showImages(item1, item2);
// } else {
showWords(item1, item2);
// }
}
// function showImages(image1, image2) {
// $('#word1').hide().html("<img src='" + image1 + "'/>").fadeIn(1000);
// $('#word2').hide().html("<img src='" + image2 + "'/>").fadeIn(1000);
// }
function showWords(word1, word2) {
$('#word1').hide().text(word1).fadeIn(1000);
$('#word2').hide().text(word2).fadeIn(1000);
}
function showQuestion() {
var word = words[questionCount][0];
$('.word_input input[type="text"]').val('');
$('#study').hide();
$('#word1, #word2').text('');
// if ($('section#main').attr('data-question-type') == "image") {
// $('#test_holder .word').html("<img src='" + word + "'/>");
// $(document).mousedown(function(event) {
// event.preventDefault();
// switch (event.which) {
// case 1:
// clearInterval(testCountdown);
// var question = '';
// var answer = '';
// var memory_results = 'MemoryResults' + (currentRound - 1) + '';
// var response = [question, answer, 'left'];
// var response_array = null;
// test_responses[currentRound - 1].push(response);
// nextQuestion();
// break;
// case 2:
// break;
// case 3:
// clearInterval(testCountdown);
// var question = '';
// var answer = '';
// var memory_results = 'MemoryResults' + (currentRound - 1) + '';
// var response = [question, answer, 'right', response_times.pop()];
// var response_array = null;
// test_responses[currentRound - 1].push(response);
// nextQuestion();
// break;
// default:
// alert('You have a strange Mouse!');
// }
// });
// $('#test_holder').fadeIn(1, function() {
// var stop_time = new Date().getTime();
// var time = stop_time - start_time;
// response_times.push(time);
// });
// } else {
$('#test_holder .word').text(word);
$('#test_holder').fadeIn(1000, function() {
$('.word_input input[type="submit"]').removeAttr('disabled');
$('.word_input input[type="text"]').focus();
start_time = new Date().getTime();
//Record response time after first keypress
$('.word_input input').one('keypress', function() {
var stop_time = new Date().getTime();
var time = stop_time - start_time;
response_times.push(time);
});
});
// }
// DRJ: probably tried to stop browser from navigating back on accident
// $(document).keydown(function(e) {
// if (e.keyCode === 8 && !$('.word_input input[type="text"]').is(':focus')) {
// return false;
// }
// });
resetTimer();
}
function resetTimer() {
testCountdown = setInterval(function() {
questionCounter++;
if (questionCounter == 9) {
$('.word_input input[type="submit"]').trigger('click');
}
}, 1000);
}
function showResults() {
//$('#results .correct .amount').html(total_right);
//$('#results .wrong .amount').html(total_wrong);
$('#study, #test_holder').hide();
//$('#results').fadeIn(1500);
}
function showMessage() {
$('.word_input input').trigger('blur');
$('#words').hide();
$('.message').fadeIn(1000);
}
$('#study_button').click(function() {
$('#words').show();
$('.message').hide();
showStudy();
});
//Reset timer on keypress within text box
$('.word_input input').keypress(function() {
questionCounter = 0;
clearInterval(testCountdown);
resetTimer();
});
$('.word_input input').keypress(function(e) {
if (e.which == 13 && $('.word_input input').is(':focus')) {
$('.word_input input[type="submit"]').trigger('click');
return false;
}
});
$('.word_input input[type="submit"]').click(function(e) {
e.preventDefault();
clearInterval(testCountdown);
$(this).attr('disabled', 'disabled');
var question = words[questionCount][0].toLowerCase();
var answer = words[questionCount][1].toLowerCase();
var value = $('.word_input input[type="text"]').val().toLowerCase();
var memory_results = 'MemoryResults' + (currentRound - 1) + '';
var response = [question, answer, value, response_times.pop()];
var response_array = null;
test_responses[currentRound - 1].push(response);
if (value == answer) {
total_right++;
} else {
total_wrong++;
}
console.log('Total right: ' + total_right + ' - Total wrong: ' + total_wrong);
nextQuestion();
});
function nextQuestion() {
questionCount++;
//more questions
if (questionCount < words.length) {
// make progress bar move
$('#test_holder').hide();
showQuestion(); //next question
}
//no more questions, no more rounds
else if (questionCount >= words.length && currentRound == rounds().length) {
$.ajax({
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))
},
type: "POST",
url: $(location).attr('pathname') + '/result',
data: JSON.stringify({
result: {
number_correct: total_right,
test_responses: test_responses
}
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
var json = $.parseJSON(data.d);
if (data['redirect']) {
window.location.pathname = data['redirect']
}
}
});
}
//no more questions, has more rounds, round != 1???
else if (questionCount >= words.length && currentRound < rounds().length) {
if (typeof _gaq != 'undefined') {
_gaq.push(['_trackEvent', 'Rounds', 'Round ' + (currentRound - 1) + ' Completed']);
}
var bar_width = (currentRound / rounds().length) * 100;
$('#progress #progress_bar').animate({
width: bar_width + '%'
}, 500, 'easeOutQuart');
showResults();
showStudy(); //next round
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment