testing.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. $(document).ready(function () {
  2. window.history.forward();
  3. //exit
  4. $('.button-exit').click(function () {
  5. var msg = $('#back-link').attr('msg');
  6. var link = $('#back-link').val();
  7. if (confirm(msg)) {
  8. window.location.href = link;
  9. }
  10. });
  11. $('.btn-ready').click(function () {
  12. runCountdown();
  13. $('.testing-rule').fadeOut(function () {
  14. updateProcess();
  15. });
  16. });
  17. });
  18. var parent = $('#hidden-parent').val();
  19. var parentSlug = $('#hidden-parent-name').val();
  20. var topicSlug = $('#hidden-topic-name').val();
  21. var dataAnswer = {};
  22. dataAnswer.topicId = $('#hidden-topicid').val();
  23. dataAnswer.total = parseInt($('.testing-body-cover').attr('total'));
  24. dataAnswer.answerGuess = [];
  25. dataAnswer.answerWrite = [];
  26. var rightAnswers;
  27. var listAnswers = [];
  28. var listElement = [];
  29. // CHECK ANSWER GUESSING
  30. $(document).on('click', '.testing-exercise-answer .answer-guessing', function () {
  31. // stop all
  32. var divParent = $(this).parents('.testing-exercise');
  33. var correctAnswer = $(divParent).attr('correctAnswer');
  34. var questionId = $(divParent).attr('questionId');
  35. // Get element
  36. var eleAnswer = $(this);
  37. var answer = $(eleAnswer).attr('val');
  38. // Add class show answer
  39. $(eleAnswer).addClass('choose-answer');
  40. // check number of answer right
  41. console.log("correctAnswer " + correctAnswer);
  42. rightAnswers = correctAnswer.split(",");
  43. var next = false;
  44. if (listAnswers.indexOf($(eleAnswer).attr('val')) === -1) {
  45. listAnswers.push($(eleAnswer).attr('val'));
  46. listElement.push(eleAnswer);
  47. }
  48. if (rightAnswers.length == 1) { next = true }
  49. else if (listAnswers.length < rightAnswers.length) {
  50. next = false;
  51. } else {
  52. next = true;
  53. }
  54. console.log("listAnswers " + listAnswers);
  55. console.log("rightAnswers " + rightAnswers);
  56. if (next == true) {
  57. //$('.iframe-testing-video').pause();
  58. //this.contentWindow.postMessage('{"event":"command","func":"stopVideo","args":""}', '*')
  59. console.log("stop video")
  60. //var frame = document.getElementById("iframe-testing-video");
  61. //frame.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
  62. //var iframe = document.getElementsByTagName("iframe-testing-video")[0].contentWindow;
  63. //iframe.postMessage('{"event":"command","func":"' + 'stopVideo' + '","args":""}', '*');
  64. //var reSrc = $('.iframe-testing-video').attr("src");
  65. //console.log("reSrc ", reSrc);
  66. //$('.iframe-testing-video').attr("src", reSrc)
  67. //$('.iframe-testing-video')[0].contentWindow.postMessage('{"event":"command","func":"' + 'pauseVideo' + '","args":""}', '*');
  68. const iframes = document.querySelectorAll('.iframe-testing-video');
  69. Array.from(iframes).forEach((iframe) => {
  70. iframe.src = iframe.getAttribute('src')
  71. });
  72. //document.getElementsByClassName('iframe-testing-video').pause();
  73. //document.getElementsByClassName('iframe-testing-video').currentTime = 0;
  74. //$(".iframe-testing-video")[0].pause();
  75. //$("#iframe-testing-video").css('display', 'none');
  76. //$("#iframe-testing-video").css('display', 'block');
  77. stopCountdown();
  78. // Check Show answer
  79. if (!$(divParent).hasClass('show-answer')) {
  80. // Add class show answer
  81. $(divParent).addClass('show-answer');
  82. // Get value for ajax
  83. var token = $(divParent).attr('token');
  84. // Get answer true
  85. //checkTestingAnswer(listAnswers, rightAnswers, function (result) {
  86. checkTestingAnswer(listAnswers, function (result) {
  87. console.log(result);
  88. var check = 0;
  89. if (result.correct == 1) {
  90. check = 1;
  91. }
  92. // Check answer
  93. var k;
  94. if (check == 1) {
  95. for (k = 0; k < listElement.length; k++) {
  96. listElement[k].addClass('choose-right');
  97. }
  98. soundAnswerRight();
  99. } else {
  100. for (k = 0; k < listElement.length; k++) {
  101. listElement[k].addClass('choose-wrong');
  102. }
  103. //$(eleAnswer).addClass('choose-wrong');
  104. for (k = 0; k < result.result.length; k++) {
  105. $(divParent).find('.testing-exercise-answer .answer-guessing[val="' + result.result[k] + '"]').addClass('choose-right');
  106. }
  107. soundAnswerWrong();
  108. }
  109. if ($(divParent).find('.testing-exercise-answer .answer-guessing.choose-right[hideAnswer] .guessing-text').length > 0) {
  110. $(divParent).find('.testing-exercise-answer .answer-guessing.choose-right[hideAnswer] .guessing-text').text($(divParent).find('.testing-exercise-answer .answer-guessing.choose-right[hideAnswer]').attr('hideAnswer'));
  111. }
  112. dataAnswer.answerGuess.push({
  113. pos: answer,
  114. questionId: $('.exercise-current').attr('questionId'),
  115. card: $('.exercise-current').attr('index'),
  116. word: $('.exercise-current .testing-exercise-result').attr('word'),
  117. synset: $('.exercise-current .testing-exercise-result').attr('synset'),
  118. answerA: $('.exercise-current').find('.answer-guessing[val="1"] .guessing-text').text().trim(),
  119. answerB: $('.exercise-current').find('.answer-guessing[val="2"] .guessing-text').text().trim(),
  120. answerC: $('.exercise-current').find('.answer-guessing[val="3"] .guessing-text').text().trim(),
  121. answerD: $('.exercise-current').find('.answer-guessing[val="4"]').attr('hideanswer').trim(),
  122. time: Math.max(totalTime - second + 1, 1),
  123. right: result[1],
  124. isRight: check
  125. });
  126. console.log(dataAnswer);
  127. // Show answer
  128. console.log("Class: " + $(eleAnswer).attr('class'));
  129. let typeCourse = $(".testing-exercise-answer").attr("typeCourse");
  130. console.log("att: " + typeCourse);
  131. if (!$(eleAnswer).hasClass('no-answer')) {
  132. console.log("Not has no answer");
  133. if (typeCourse == "LISTEN") {
  134. showAnswerWithoutFlash(divParent);
  135. } else
  136. showAnswer(divParent);
  137. } else {
  138. console.log("Goto next");
  139. gotoNext(divParent);
  140. }
  141. });
  142. }
  143. listAnswers = [];
  144. }
  145. });
  146. // CHECK ANSWER WRITING
  147. $(document).on('click', '.testing-exercise-answer .answer-check.check-active', function () {
  148. var divParent = $(this).parents(".testing-exercise");
  149. var correctAnswer = $(divParent).attr('correctAnswer');
  150. var button = $(this);
  151. stopCountdown();
  152. if (!$(divParent).hasClass('show-answer')) {
  153. $(divParent).addClass('show-answer');
  154. $(button).removeClass('check-active');
  155. var inputAnswer = $(divParent).find('.testing-exercise-answer .answer-writing');
  156. var userAnswer = $(inputAnswer).val().trim().toLowerCase();
  157. var token = $(divParent).attr('token');
  158. $(inputAnswer).trigger('blur');
  159. // Get answer true
  160. checkTestingAnswer(userAnswer, function (result) {
  161. trueAnswer = String(result).trim().toLowerCase();
  162. var check = 0;
  163. // RIGHT
  164. if (result.correct == 1) {
  165. check = 1;
  166. soundAnswerRight();
  167. $(inputAnswer).addClass('answer-right');
  168. // WRONG
  169. } else {
  170. soundAnswerWrong();
  171. $(inputAnswer).addClass('answer-wrong');
  172. $(inputAnswer).val(result.result);
  173. }
  174. dataAnswer.answerWrite.push({ answer: userAnswer, card: $('.exercise-current').attr('index'), word: $('.exercise-current .testing-exercise-result').attr('word'), synset: $('.exercise-current .testing-exercise-result').attr('synset'), time: Math.max(totalTime - second + 1, 1), right: result[1], isRight: check });
  175. // Show answer
  176. showAnswer(divParent);
  177. });
  178. }
  179. });
  180. $(document).on('keyup', '.testing-exercise-answer .answer-writing', function (e) {
  181. if (e.keyCode == 13) {
  182. $(this).next(".answer-check").trigger('click');
  183. } else {
  184. if ($(this).val().length > 0) {
  185. $(this).next(".answer-check").addClass('check-active');
  186. } else {
  187. $(this).next(".answer-check").removeClass('check-active');
  188. }
  189. }
  190. });
  191. $(document).on('keydown', '.testing-exercise-answer .answer-writing', function (e) {
  192. if (e.keyCode == 13) {
  193. return false;
  194. }
  195. });
  196. function endTime() {
  197. readyRunCountdown = 1;
  198. var divParent = $(".testing-exercise.exercise-current");
  199. var correctAnswer = $(divParent).attr('correctAnswer');
  200. var typeQuestion = $(divParent).attr('typeQuestion');
  201. var token = $(divParent).attr('token');
  202. if (!$(divParent).hasClass('show-answer')) {
  203. $(divParent).addClass('show-answer');
  204. // GUESSING
  205. if (typeQuestion == "guessing") {
  206. checkTestingAnswer(0, function (result) {
  207. $(divParent).find('.testing-exercise-answer .answer-guessing[val="' + result.result + '"]').addClass('choose-right');
  208. soundAnswerWrong();
  209. if ($(divParent).find('.testing-exercise-answer .answer-guessing.choose-right[hideAnswer] .guessing-text').length > 0) {
  210. $(divParent).find('.testing-exercise-answer .answer-guessing.choose-right[hideAnswer] .guessing-text').text($(divParent).find('.testing-exercise-answer .answer-guessing.choose-right[hideAnswer]').attr('hideAnswer'));
  211. }
  212. //console.log("END TIME " + $('.exercise-current').attr('questionId'))
  213. dataAnswer.answerGuess.push({
  214. pos: 0,
  215. questionId: $('.exercise-current').attr('questionId'),
  216. card: $('.exercise-current').attr('index'),
  217. word: $('.exercise-current .testing-exercise-result').attr('word'),
  218. synset: $('.exercise-current .testing-exercise-result').attr('synset'),
  219. answerA: $('.exercise-current').find('.answer-guessing[val="1"] .guessing-text').text().trim(),
  220. answerB: $('.exercise-current').find('.answer-guessing[val="2"] .guessing-text').text().trim(),
  221. answerC: $('.exercise-current').find('.answer-guessing[val="3"] .guessing-text').text().trim(),
  222. answerD: $('.exercise-current').find('.answer-guessing[val="4"]').attr('hideanswer').trim(),
  223. time: totalTime,
  224. right: result[1],
  225. isRight: 0
  226. });
  227. // Show answer
  228. showAnswer(divParent);
  229. });
  230. // WRITING
  231. } else {
  232. var inputAnswer = $(divParent).find('.testing-exercise-answer .answer-writing');
  233. $(inputAnswer).trigger('blur');
  234. // Get answer true
  235. checkTestingAnswer("no answer", function (result) {
  236. soundAnswerWrong();
  237. $(inputAnswer).addClass('answer-wrong');
  238. $(inputAnswer).val(result.result);
  239. dataAnswer.answerWrite.push({ answer: "no answer", card: $('.exercise-current').attr('index'), word: $('.exercise-current .testing-exercise-result').attr('word'), synset: $('.exercise-current .testing-exercise-result').attr('synset'), time: totalTime, right: result[1], isRight: 0 });
  240. // Show answer
  241. showAnswer(divParent);
  242. });
  243. }
  244. }
  245. }
  246. function showAnswer(divParent) {
  247. setTimeout(function () {
  248. $(divParent).find('.flash-card-box').boxSlider('showSlide', 1);
  249. $('.flash-card-box').boxSlider('option', 'onafter', function ($previousSlide, $currentSlide, currIndex, nextIndex) {
  250. if (nextIndex == 1) {
  251. if ($(divParent).find('.testing-exercise-result .result-content-sound').length > 0) {
  252. playSound($(divParent).find('.testing-exercise-result .result-content-sound'), function () {
  253. //nextQuestion();
  254. });
  255. setTimeout(function () {
  256. nextQuestion();
  257. }, 2000);
  258. } else {
  259. nextQuestion();
  260. }
  261. }
  262. });
  263. }, 1000);
  264. setTimeout(function () {
  265. // $("#testing-countdown").fadeOut(400);
  266. }, 500);
  267. }
  268. function showAnswerWithoutFlash(divParent) {
  269. //setTimeout(function () {
  270. // $(divParent).find('.flash-card-box').boxSlider('showSlide', 1);
  271. // $('.flash-card-box').boxSlider('option', 'onafter', function ($previousSlide, $currentSlide, currIndex, nextIndex) {
  272. // if (nextIndex == 1) {
  273. // if ($(divParent).find('.testing-exercise-result .result-content-sound').length > 0) {
  274. // playSound($(divParent).find('.testing-exercise-result .result-content-sound'), function () {
  275. // });
  276. // setTimeout(function () {
  277. // nextQuestion();
  278. // }, 2000);
  279. // } else {
  280. // nextQuestion();
  281. // }
  282. // }
  283. // });
  284. //}, 1000);
  285. setTimeout(function () {
  286. nextQuestion();
  287. }, 1000);
  288. }
  289. function gotoNext(divParent) {
  290. setTimeout(function () {
  291. if ($(divParent).find('.testing-exercise-result .result-content-sound').length > 0) {
  292. playSound($(divParent).find('.testing-exercise-result .result-content-sound'), function () {
  293. //nextQuestion();
  294. });
  295. setTimeout(function () {
  296. nextQuestion();
  297. }, 1000);
  298. } else {
  299. nextQuestion();
  300. }
  301. }, 2000);
  302. setTimeout(function () {
  303. // $("#testing-countdown").fadeOut(400);
  304. }, 500);
  305. }
  306. function nextQuestion() {
  307. console.log("Next question");
  308. setTimeout(function () {
  309. var divCurrent = $(".testing-exercise.exercise-current");
  310. var indexCur = parseInt($(divCurrent).attr('index'));
  311. var divNext = $(".testing-exercise[index='" + (indexCur + 1) + "']");
  312. if (divNext.length > 0) {
  313. $(".testing-exercise.exercise-current").removeClass('exercise-current');
  314. $(divNext).addClass('exercise-current');
  315. $(divCurrent).addClass('learning-hide-item');
  316. $(divNext).addClass('learning-show-item');
  317. setTimeout(function () {
  318. $(divCurrent).css('display', 'none');
  319. }, 500);
  320. updateProcess();
  321. } else {
  322. $.blockUI(
  323. {
  324. message: "Please wait...",
  325. css: {
  326. border: 'none',
  327. padding: '15px',
  328. backgroundColor: '#000',
  329. '-webkit-border-radius': '2px',
  330. '-moz-border-radius': '2px',
  331. opacity: .5,
  332. color: '#fff'
  333. }
  334. }
  335. );
  336. document.cookie = 'userActive=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
  337. dataAnswer = JSON.stringify(dataAnswer);
  338. console.log(dataAnswer);
  339. $.ajax({
  340. url: urlConfig('/Common/EditTestResult'),
  341. data: {
  342. dataAnswer: dataAnswer
  343. },
  344. dataType: 'text',
  345. type: "POST",
  346. //async: false
  347. }).success(function (result) {
  348. console.log("result: " + result);
  349. if (result != "1" && result != 1) {
  350. console.log("reload");
  351. location.reload();
  352. } else {
  353. console.log("urlConfig");
  354. console.log(urlConfig(parentSlug + '/' + topicSlug));
  355. //alert(1);
  356. window.location.href = urlConfig(parentSlug + '/' + topicSlug);
  357. window.onbeforeunload = null;
  358. }
  359. }).error(function (jqXHR, textStatus, errorThrown) {
  360. });
  361. // setTimeout(function() {
  362. // //show charts for test
  363. // window.location.href = urlConfig(parentSlug + '/' + topicSlug);
  364. // window.onbeforeunload = null;
  365. // }, 3000);
  366. }
  367. }, 0);
  368. }
  369. function updateProcess() {
  370. console.log("update process");
  371. $('.testing-answer-group').fadeOut();
  372. var total = parseInt($(".testing-body-cover").attr('total'));
  373. var indexCur = parseInt($(".testing-exercise.exercise-current").attr('index'));
  374. if (total > 0) {
  375. $(".testing-process-bar").width((indexCur + 1) * 100 / total + '%');
  376. }
  377. totalTime = parseInt($('.exercise-current').attr('time'));
  378. $(".testing-countdown svg circle.circle").css('stroke-dashoffset', '0px');
  379. $(".testing-countdown-number").text(totalTime);
  380. $(".testing-exercise.exercise-current").css('display', 'block');;
  381. readyAnswer = 1;
  382. if ($(".testing-exercise.exercise-current .testing-exercise-answer .answer-writing").length > 0) {
  383. $(".testing-exercise.exercise-current .testing-exercise-answer .answer-writing").focus();
  384. }
  385. setTimeout(function () {
  386. // $("#testing-countdown").fadeIn(400);
  387. if ($(".testing-exercise.exercise-current .testing-exercise-quesiton .question-sound.main-sound-play-auto").length > 0) {
  388. playSound($('.testing-exercise.exercise-current .testing-exercise-quesiton .question-sound'), function () {
  389. runCountdown();
  390. });
  391. } else {
  392. runCountdown();
  393. }
  394. }, 500);
  395. updateImage(indexCur);
  396. }
  397. // Time
  398. var t;
  399. var totalTime = 15;
  400. var second = totalTime;
  401. var readyRunCountdown = 1;
  402. var readyAnswer = 1;
  403. function runCountdown() {
  404. console.log("update process " + readyRunCountdown);
  405. if (readyRunCountdown == 1 && readyAnswer == 1) {
  406. readyRunCountdown = 0;
  407. second = totalTime;
  408. var runCircle = 0;
  409. t = window.setInterval(function () {
  410. if (window.blurred) {
  411. } else {
  412. second = --second <= 0 ? 0 : second;
  413. if (runCircle == 0) {
  414. runCircle = 1;
  415. $(".testing-countdown svg circle.circle").animate({
  416. "stroke-dashoffset": "-101px",
  417. }, (second) * 1000, "linear", function () {
  418. });
  419. }
  420. $(".testing-countdown-number").text(second);
  421. if (second == 0) {
  422. window.clearInterval(t);
  423. endTime();
  424. return false;
  425. }
  426. if (second <= 5) {
  427. // $(".testing-theory-time-countdown").addClass("end-time");
  428. playWarningTimeSound();
  429. } else {
  430. playNormalTimeSound();
  431. }
  432. }
  433. }, 1000);
  434. }
  435. }
  436. function stopCountdown() {
  437. $(".testing-countdown svg circle.circle").stop(true);
  438. window.clearInterval(t);
  439. readyRunCountdown = 1;
  440. readyAnswer = 0;
  441. }
  442. window.onblur = function () {
  443. window.blurred = true;
  444. $(".testing-countdown svg circle.circle").stop(true);
  445. };
  446. window.onfocus = function () {
  447. window.blurred = false;
  448. if (readyRunCountdown == 0) {
  449. $(".testing-countdown svg circle.circle").animate({ "stroke-dashoffset": "-101px" }, (second) * 1000, "linear");
  450. }
  451. };
  452. function playNormalTimeSound() {
  453. if ($("#normal-time-sound").length > 0) {
  454. $("#normal-time-sound")[0].load();
  455. $("#normal-time-sound")[0].onloadeddata = function () {
  456. $("#normal-time-sound")[0].play();
  457. };
  458. }
  459. }
  460. function playWarningTimeSound() {
  461. if ($("#warning-time-sound").length > 0) {
  462. $("#warning-time-sound")[0].load();
  463. $("#warning-time-sound")[0].onloadeddata = function () {
  464. $("#warning-time-sound")[0].play();
  465. };
  466. }
  467. }
  468. function checkTestingAnswer(answer, getAnswer) {
  469. //console.log("Check answer: " + answer + ", questionId: " + questionId);
  470. $.ajax({
  471. //url: urlConfig('check-answer'),
  472. url: '/Common/CheckAnswer',
  473. data: {
  474. 'questionId': $('.exercise-current').attr('questionId'),
  475. 'customerAnswer': answer.toString(),
  476. //'correctAnswer': correctAnswer.toString()
  477. },
  478. type: "POST"
  479. }).success(function (result) {
  480. getAnswer(result);
  481. });
  482. }
  483. function updateImage(indexCurrent) {
  484. var next_1 = $(".testing-exercise[index='" + indexCurrent + "']");
  485. var next_2 = $(".testing-exercise[index='" + (indexCurrent + 1) + "']");
  486. if (next_1.length > 0) {
  487. if ($(next_1).find('.testing-exercise-result .result-img img').attr('src') == '' || $(next_1).find('.testing-exercise-result .result-img img').attr('src') == undefined) {
  488. $(next_1).find('.testing-exercise-result .result-img img').attr('src', $(next_1).find(".testing-exercise-result").attr('img_url'));
  489. }
  490. }
  491. if (next_2.length > 0) {
  492. if ($(next_2).find('.testing-exercise-result .result-img img').attr('src') == '' || $(next_2).find('.testing-exercise-result .result-img img').attr('src') == undefined) {
  493. $(next_2).find('.testing-exercise-result .result-img img').attr('src', $(next_2).find(".testing-exercise-result").attr('img_url'));
  494. }
  495. }
  496. }
  497. $(document).ready(function () {
  498. $(".testing-exercise-flashcard .flash-card-box").each(function () {
  499. var index = $(this).attr('index');
  500. var defaultOptions = {
  501. speed: 600
  502. , timeout: 300
  503. , next: '#next'
  504. , prev: '#prev'
  505. , pause: '#pause'
  506. };
  507. $("#learning-theory-box-" + (index)).boxSlider(defaultOptions);
  508. });
  509. });