| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775 |
- $(document).ready(function () {
- window.history.forward();
- updateProcess();
- });
- function effectAnswerRight() {
- //Play sound right
- $("#audio-testing-answer-right")[0].load();
- $("#audio-testing-answer-right")[0].onloadeddata = function () {
- $("#audio-testing-answer-right")[0].play();
- };
- }
- function effectAnswerWrong() {
- //Play sound wrong
- $("#audio-testing-answer-wrong")[0].load();
- $("#audio-testing-answer-wrong")[0].onloadeddata = function () {
- $("#audio-testing-answer-wrong")[0].play();
- };
- }
- function playNormalTimeSound() {
- if ($("#normal-time-sound").length > 0) {
- $("#normal-time-sound")[0].load();
- $("#normal-time-sound")[0].onloadeddata = function () {
- $("#normal-time-sound")[0].play();
- };
- }
- }
- function playWarningTimeSound() {
- if ($("#warning-time-sound").length > 0) {
- $("#warning-time-sound")[0].load();
- $("#warning-time-sound")[0].onloadeddata = function () {
- $("#warning-time-sound")[0].play();
- };
- }
- }
- function updateProcess() {
- var total = parseInt($(".testing-theory-content").attr('total'));
- var indexCur = parseInt($(".testing-theory-content .testing-theory-item.item-current").attr('index'));
- $(".testing-theory-content .testing-theory-item.item-current").show();
- if (total > 0) {
- $(".testing-theory-process .testing-theory-process-bar").width((indexCur + 1) * 100 / total + '%');
- }
- $(".testing-theory-countdown svg circle.circle").css('stroke-dashoffset', '0px');
- $(".testing-theory-countdown-number").text(totalTime);
- var typeQuestionShow = $(".testing-theory-content .testing-theory-item.item-current").attr('typeQuestion');
- if (typeQuestionShow == 3) {
- if (!$('.testing-theory-content .testing-theory-item.item-current .testing-theory-pos-end').hasClass('ui-droppable')) {
- setDragAndDrop($(".testing-theory-content .testing-theory-item.item-current"));
- }
- }
- readyAnswer = 1;
- setTimeout(function () {
- runCountdown();
- }, 500);
- }
- var testData = new Array();
- var answerText = "";
- var answerTextTrue = "";
- var userAnswer = "";
- function nextQuestion(resultData, timeNext = 2000) {
- var answerTrue = $(".learning-theory-test-true").val();
- var qid = $(".learning-theory-test-true").attr('qid');
- var qtype = $(".learning-theory-test-true").attr('qtype');
- var ttype = $(".learning-theory-test-true").attr('ttype');
- testData.push({
- qid: qid,
- qtype: qtype,
- ttype: ttype,
- answerTrue: answerTrue,
- answer: userAnswer,
- answerText: answerText,
- answerTextTrue: answerTextTrue,
- time: Math.max(totalTime - second + 1, 1)
- });
- setTimeout(function () {
- questionNew = resultData['nextQuestion'];
- if (questionNew == "") {
- if (testData.length > 0) {
- /*$.blockUI(
- {
- message:'Vui lòng chờ trong giây lát',
- css: {
- border: 'none',
- padding: '15px',
- backgroundColor: '#000',
- '-webkit-border-radius': '2px',
- '-moz-border-radius': '2px',
- opacity: .5,
- color: '#fff'
- }}
- );*/
- $.ajaxSetup({
- headers: {
- 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
- }
- });
- $.ajax({
- url: $("#result-link").val(),
- type: "POST",
- data: {
- testData: testData
- },
- success: function (result) {
- console.log('link ' + $("#result-link").val());
- console.log('result ' + result);
- checkTest = 1;
- //if (result != 0) {
- // window.location.href = result;
- //} else {
- // window.location.href = location.protocol + "//" + location.host + '/';
- //}
- }
- });
- }
- }
- var divParent = $(".testing-theory-content");
- var divCurrent = $(".testing-theory-item.item-current");
- var divNext = $(divCurrent).next('.testing-theory-item');
- if (divNext.length == 0) {
- $(divParent).append(questionNew);
- divNext = $(divCurrent).next('.testing-theory-item');
- }
- if (divNext.length > 0) {
- $(divNext).addClass('item-current');
- $(divCurrent).removeClass('item-current');
- $(divCurrent).addClass('testing-theory-hide-question');
- $(divNext).addClass('testing-theory-show-question');
- updateProcess();
- setTimeout(function () {
- $(divCurrent).remove();
- }, 500);
- }
- }, timeNext);
- }
- // Time
- var t;
- var totalTime = 15;
- var second = totalTime;
- var readyRunCountdown = 1;
- var readyAnswer = 1;
- function runCountdown() {
- if (readyRunCountdown == 1 && readyAnswer == 1) {
- readyRunCountdown = 0;
- second = totalTime;
- var runCircle = 0;
- t = window.setInterval(function () {
- if (window.blurred) {
- } else {
- second = --second <= 0 ? 0 : second;
- if (runCircle == 0) {
- runCircle = 1;
- $(".testing-theory-countdown svg circle.circle").animate({
- "stroke-dashoffset": "-101px",
- }, (second) * 1000, "linear", function () {
- });
- }
- $(".testing-theory-countdown-number").text(second);
- if (second == 0) {
- window.clearInterval(t);
- endTime();
- return false;
- }
- if (second <= 5) {
- // $(".testing-theory-time-countdown").addClass("end-time");
- playWarningTimeSound();
- } else {
- playNormalTimeSound();
- }
- }
- }, 1000);
- }
- }
- function stopCountdown() {
- $(".testing-theory-countdown svg circle.circle").stop(true);
- window.clearInterval(t);
- readyRunCountdown = 1;
- readyAnswer = 0;
- }
- window.onblur = function () {
- window.blurred = true;
- $(".testing-theory-countdown svg circle.circle").stop(true);
- };
- window.onfocus = function () {
- window.blurred = false;
- if (readyRunCountdown == 0) {
- $(".testing-theory-countdown svg circle.circle").animate({ "stroke-dashoffset": "-101px" }, (second) * 1000, "linear");
- }
- };
- // END time
- function endTime() {
- // Get element
- //readyRunCountdown = 1;
- //var divParent = $('.testing-theory-content .testing-theory-item.item-current');
- // Check Show answer
- //if( !$(divParent).hasClass('show-answer') ){
- // Add class show answer
- //$(divParent).addClass('show-answer');
- // Get value for ajax
- //var token = $(divParent).attr('qToken');
- //var answer = '';
- //var typeQuestion = $(divParent).attr('typeQuestion');
- // Get answer true
- /*checkTestingAnswer(token, answer, function(answerTrue){
-
- effectAnswerWrong();
- // GUESSING
- if(typeQuestion == 2){
-
- // Effect answer
- $(divParent).find('.guessing-choice-item[val='+ answerTrue['answerTrue'] +']').addClass('choice-true');
- $(divParent).find(".guessing-choice-item.choice-true[hideAnswer] .guessing-text").text($(divParent).find(".guessing-choice-item.choice-true[hideAnswer]").attr('hideAnswer'));
- }
- nextQuestion(answerTrue);
-
- });*/
- //}
- }
- var answer = "";
- // CHECK ANSWER GUESSING
- $(document).on('click', '.testing-theory-item .guessing-choice-item', function () {
- // Get element
- var eleAnswer = $(this);
- var divParent = $(this).parents('.testing-theory-item');
- // Check Show answer
- if (!$(divParent).hasClass('show-answer')) {
- // Add class show answer
- $(divParent).addClass('show-answer');
- stopCountdown();
- // Get value for ajax
- var token = $(divParent).attr('qToken');
- userAnswer = $(eleAnswer).attr('val');
- answerText = $(eleAnswer).find('.guessing-text').text().trim();
- // Get answer true
- checkTestingAnswer(token, userAnswer, function (resultData) {
- divNextQuestion = resultData['nextQuestion'];
- //Check answer
- var checkAnswer = 0;
- if (userAnswer == resultData['answerTrue']) {
- checkAnswer = 1;
- }
- if (checkAnswer == 1) {
- effectAnswerRight();
- $(eleAnswer).addClass('choice-true');
- answerTextTrue = $(eleAnswer).find('.guessing-text').text().trim();
- if ($(eleAnswer).find(".guessing-choice-item").attr('mutilAnswer') != undefined) {
- answerText = $(eleAnswer).find(".guessing-choice-item").attr('mutilAnswer');
- answerTextTrue = $(eleAnswer).find(".guessing-choice-item").attr('mutilAnswer');
- }
- } else {
- effectAnswerWrong();
- $(eleAnswer).addClass('choice-false');
- $(divParent).find('.guessing-choice-item[val=' + resultData['answerTrue'] + ']').addClass('choice-true');
- var tEle = $(divParent).find('.guessing-choice-item[val=' + resultData['answerTrue'] + ']');
- answerTextTrue = $(tEle).find('.guessing-text').text().trim();
- if ($(tEle).find(".guessing-choice-item").attr('mutilAnswer') != undefined) {
- answerText = $(tEle).find(".guessing-choice-item").attr('mutilAnswer');
- answerTextTrue = $(tEle).find(".guessing-choice-item").attr('mutilAnswer');
- }
- }
- $(divParent).find(".guessing-choice-item.choice-true[hideAnswer] .guessing-text").text($(divParent).find(".guessing-choice-item.choice-true[hideAnswer]").attr('hideAnswer'));
- nextQuestion(resultData);
- });
- }
- });
- $(document).on('click', '.guessing-multi-choice-item', function () {
- $(this).toggleClass('choice-item-choose');
- var exeQuestion = $(this).parent('.guessing-exe-question');
- var check = 0;
- $(exeQuestion).children('.guessing-multi-choice-item').each(function () {
- if ($(this).hasClass('choice-item-choose')) {
- check = 1;
- }
- });
- if (check == 1) {
- $(exeQuestion).next('.exe-user-answer').find('.button-multi-check').addClass('button-active');
- } else {
- $(exeQuestion).next('.exe-user-answer').find('.button-multi-check').removeClass('button-active');
- }
- });
- // CHECH ANSWER ARRANGE
- $(document).on('click', '.testing-theory-item .exe-question-button-check.button-active, .button-multi-check.button-active', function () {
- $(this).removeClass('button-active');
- var button = $(this);
- var divParent = $('.testing-theory-item.item-current');
- var typeQuestion = $(divParent).attr('typeQuestion');
- if (!$(divParent).hasClass('show-answer')) {
- // Add class show answer
- $(divParent).addClass('show-answer');
- stopCountdown();
- var token = $(divParent).attr('qToken');
- if (typeQuestion == 3) {
- var answer = Array();
- answerTextTrue = '';
- $(divParent).find(".testing-theory-pos-end .pos-end-item").each(function (index, el) {
- answer.push($(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + index + "']").text().toLowerCase().trim());
- });
- userAnswer = answer;
- // Get answer true
- checkTestingAnswer(token, answer, function (resultData) {
- var listGoal = $(divParent).find(".testing-theory-pos-end .pos-end-item");
- var listTrue = resultData['answerTrue'];
- if (listGoal.length == listTrue.length) {
- var checkAnswer = 1;
- answerText = "";
- listGoal.each(function (index, el) {
- if (index == 0) {
- answerText += $(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + index + "']").text().toLowerCase().trim();
- } else {
- answerText += " + " + $(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + index + "']").text().toLowerCase().trim();
- }
- if ($(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + index + "']").text().toLowerCase().trim() != listTrue[index].toLowerCase().trim()) {
- $(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + index + "']").addClass('sort-wrong');
- checkAnswer = 0;
- } else {
- $(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + index + "']").addClass('sort-right');
- }
- });
- // Effect
- if (checkAnswer == 1) {
- effectAnswerRight();
- } else {
- effectAnswerWrong();
- }
- }
- nextQuestion(resultData);
- });
- } else if (typeQuestion == 4) {
- var exeQuestion = $(this).closest('.exe-user-answer').prev('.guessing-exe-question');
- var answer = "";
- $(exeQuestion).children('.guessing-multi-choice-item').each(function (index) {
- if ($(exeQuestion).children('.guessing-multi-choice-item[val="' + index + '"]').hasClass('choice-item-choose')) {
- answer += "1_";
- } else {
- answer += "0_";
- }
- });
- answer = answer.slice(0, -1);
- userAnswer = answer;
- // Get answer true
- checkTestingAnswer(token, answer, function (resultData) {
- //Check answer
- var checkAnswer = 0;
- if (userAnswer == resultData['answerTrue']) {
- checkAnswer = 1;
- }
- if (checkAnswer == 1) {
- effectAnswerRight();
- } else {
- effectAnswerWrong();
- }
- // Get answer
- var answerTrue = resultData['answerTrue'].split("_");
- var uAnswer = answer.split("_");
- for (var i = 0; i < answerTrue.length; i++) {
- if (parseInt(uAnswer[i]) === parseInt(answerTrue[i])) {
- if (parseInt(answerTrue[i]) == 1) {
- $(exeQuestion).children('.guessing-multi-choice-item[val="' + i + '"]').addClass('choice-answer-true');
- }
- } else {
- checkAnswer = 0;
- if (parseInt(answerTrue[i]) == 1) {
- $(exeQuestion).children('.guessing-multi-choice-item[val="' + i + '"]').addClass('item-answer-true');
- } else {
- $(exeQuestion).children('.guessing-multi-choice-item[val="' + i + '"]').addClass('choice-answer-false');
- }
- }
- }
- nextQuestion(resultData);
- });
- }
- }
- });
- // **********************************************
- //Sort word
- function setDragAndDrop(div) {
- var posCurrentTop = 0;
- var posCurrentLeft = 0;
- var checkClickItem = 0;
- //Check click and drag
- $(div).find(".testing-theory-pos-begin .word-item").draggable({
- start: function (event, ui) {
- $(this).css({ "transition": "none", "z-index": "2" });
- posCurrentTop = $(this).offset().top;
- posCurrentLeft = $(this).offset().left;
- checkClickItem = 1;
- setTimeout(function () {
- checkClickItem = 0;
- }, 200);
- },
- stop: function (event, ui) {
- $(this).css({ "transition": "all 0.5s ease", "z-index": "unset" });
- if ($(this).attr('pos') == undefined) {
- $(this).css({ "top": "0px", "left": "0px" });
- }
- if (Math.pow($(this).offset().left - posCurrentLeft, 2) + Math.pow($(this).offset().top - posCurrentTop, 2) < 400) { //if move < 20px
- if (checkClickItem == 1) {
- clickWord($(this));
- }
- }
- }
- });
- var checkDrop = 0;
- $(div).find(".testing-theory-pos-end .pos-end-item").droppable({
- drop: function (event, ui) {
- if ($(this).parents(".testing-theory-item").hasClass("item-current")) {
- checkDrop = 1;
- var element = ui.draggable;
- var divParent = $(element).parents(".testing-theory-item");
- var listgoal = divParent.find(".testing-theory-pos-end .pos-end-item");
- var listChildMoved = divParent.find(".testing-theory-pos-begin .word-item.word-item-moved");
- var pos = $(this).index();
- var box = listgoal[pos];
- var elementCurrent = divParent.find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + pos + "']");
- if (elementCurrent.length > 0 && !element.is(elementCurrent)) {
- var length = $(element).offset().left + $(element).width() / 2 - $(box).offset().left - $(box).width() / 2;
- if (length > 0) {
- pos++;
- box = listgoal[pos];
- }
- //check element next
- for (var i = listgoal.length - 1; i >= pos; i--) {
- if (listgoal[i + 1] != undefined && $(listgoal[i]).hasClass('word-div-haschild')) {
- var elCu = divParent.find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + i + "']");
- if (!$(listgoal[i + 1]).hasClass('word-div-haschild')) {
- $(listgoal[i + 1]).addClass('word-div-haschild');
- }
- $(listgoal[i]).removeClass('word-div-haschild');
- elCu.attr('pos', i + 1);
- $(listgoal[i + 1]).find(".word-div").width(elCu.width());
- }
- };
- }
- //add element to new box
- element.css("transition", "all 0.5s ease");
- if (!$(box).hasClass('word-div-haschild')) {
- $(box).addClass('word-div-haschild');
- }
- if (!element.hasClass('word-item-moved')) {
- element.addClass('word-item-moved');
- }
- element.attr("pos", pos);
- //edit position element
- adjustPositionWord(divParent);
- }
- },
- out: function (event, ui) {
- var element = ui.draggable;
- var pos = element.attr('pos');
- if (pos != undefined) {
- element.removeAttr("pos");
- element.removeClass('word-item-moved');
- var divParent = $(element).parents(".testing-theory-item");
- adjustPositionWord(divParent);
- }
- }
- });
- $(div).find(".testing-theory-pos-end").droppable({
- drop: function (event, ui) {
- if ($(this).parents(".testing-theory-item").hasClass("item-current")) {
- setTimeout(function () {
- if (checkDrop == 1) {
- checkDrop = 0;
- } else {
- var element = ui.draggable;
- var divParent = $(element).parents(".testing-theory-item");
- var listgoal = divParent.find(".testing-theory-pos-end .pos-end-item");
- listgoal.each(function (index) {
- if (!$(this).hasClass('word-div-haschild')) {
- element.addClass('word-item-moved');
- element.attr("pos", index);
- return false;
- }
- });
- //edit position element
- adjustPositionWord(divParent);
- }
- }, 50);
- }
- }
- });
- }
- $(document).on('click', '.testing-theory-pos-begin .pos-begin-item .word-item', function () {
- var element = $(this);
- clickWord(element);
- });
- function clickWord(element) {
- var divParent = $(element).parents(".testing-theory-item");
- var listgoal = divParent.find(".testing-theory-pos-end .pos-end-item");
- //Back
- if (element.hasClass("word-item-moved")) {
- element.removeAttr("pos");
- element.removeClass('word-item-moved');
- $(element).css({ "top": "0px", "left": "0px" });
- var divParent = $(element).parents(".testing-theory-item");
- adjustPositionWord(divParent);
- // Go
- } else {
- listgoal.each(function (index) {
- if (!$(this).hasClass('word-div-haschild')) {
- element.addClass('word-item-moved');
- element.attr("pos", index);
- return false;
- }
- });
- //edit position element
- adjustPositionWord(divParent);
- }
- }
- function adjustPositionWord(divParent) {
- var listChildMoved = $(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved");
- $(divParent).find(".testing-theory-pos-begin .pos-begin-item .word-item.sort-wrong").removeClass("sort-wrong");
- if (listChildMoved.length > 0) {
- //edit position of element moved
- var listgoal = $(divParent).find(".testing-theory-pos-end .pos-end-item");
- $(listgoal).removeClass('word-div-haschild');
- for (var i = 0; i < listgoal.length; i++) {
- if ($(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + i + "']").length > 0) {
- for (var j = 0; j < i; j++) {
- if ($(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + j + "']").length <= 0) {
- $(divParent).find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + i + "']").attr('pos', j);
- break;
- }
- }
- }
- }
- // Set width before set top, left
- listgoal.each(function (index, el) {
- var destination = $(el).find(".word-div");
- var current = divParent.find(".testing-theory-pos-begin .word-item.word-item-moved[pos='" + index + "']");
- if (current.length > 0) {
- $(destination).parent().addClass('word-div-haschild');
- $(destination).width($(current).width());
- } else {
- $(destination).width(0);
- }
- });
- listChildMoved.each(function (index, el) {
- var destination = divParent.find(".testing-theory-pos-end .pos-end-item:nth-of-type(" + (parseInt($(el).attr('pos')) + 1) + ") .word-div");
- var current = $(el).parent();
- var h = destination.offset().top - current.offset().top;
- var w = destination.offset().left - current.offset().left;
- $(el).css({ "top": h, "left": w });
- });
- //active button
- var button = $(divParent).find(".exe-question-button-check");
- if (listgoal.length == listChildMoved.length) {
- button.addClass("button-active");
- } else {
- button.removeClass("button-active");
- }
- // $(divParent).find(".testing-theory-question .button-reset").addClass('button-active');
- } else {
- // $(divParent).find(".testing-theory-question .button-reset").removeClass('button-active');
- }
- }
- function checkTestingAnswer(token, answer, getData) {
- $.ajaxSetup({
- headers: {
- 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
- }
- });
- var link = $('#answer-link').val();
- $.ajax({
- url: link,
- type: "POST",
- data: {
- 'token': token,
- 'user_answer': answer,
- 'time': Math.max(totalTime - second + 1, 1)
- },
- success: function (data) {
- if (data.length == 2) {
- if (data[0] == 'true') {
- getData(data[1]);
- } else if (data[0] == 'false') {
- window.location.href = data[1];
- }
- }
- },
- error: function (result) {
- }
- });
- }
|