1initialize_test() 2function initialize_test() { 3 if (jQuery(".test-widget-container").length) { 4 jQuery(".test-widget-container").each(function () { 5 let selectedQuestionId = null 6 let selectedOption = null 7 let gridCorrectAnswer = null 8 9 jQuery(this).find(".grid-poll-answer-container-overlay").hide() 10 11 const testId = jQuery(this).find('input[name="test-id"]').val() 12 const testContent = JSON.parse(jQuery(this).find('input[name="test-content"]').val()) 13 const cookieName = `585720315${testId}` 14 15 const widgetContainer = jQuery(this) 16 17 const shadeColor = widgetContainer.attr("data-shade-color") 18 const multiQuestionPoll = widgetContainer.attr("data-multi-question-poll") 19 const showResults = widgetContainer.attr("data-show-results") 20 const unixDateTime = widgetContainer.attr("data-unix-datetime") 21 22 let questionIndex = 1 23 let userAnswers = [] 24 25 if (unixDateTime && Math.floor(Date.now() / 1000) > unixDateTime) { 26 widgetContainer.find(".intro-screen").fadeOut(400, function () { 27 widgetContainer.find(".complete-screen").show() 28 }) 29 hide_loader() 30 } else { 31 checkTestStatus() 32 } 33 34 function checkTestQuestion(question) { 35 let correct = true 36 const correctAnswer = question.answers.find(function (a) { 37 return a.weight != 0 38 }) 39 const foundAnswer = userAnswers.find(function (a) { 40 return a.question_id == question.id 41 }) 42 if (!foundAnswer) { 43 correct = false 44 } else { 45 const foundMatchedAnswer = question.answers.find(function (a) { 46 return a.id == foundAnswer.answer_id 47 }) 48 if (!foundMatchedAnswer) correct = false 49 if (foundMatchedAnswer.weight != 1) correct = false 50 } 51 52 return { 53 correctAnswer: correctAnswer, 54 userAnswer: foundAnswer, 55 isCorrect: correct, 56 } 57 } 58 59 widgetContainer.on("click", ".start-button", function (e) { 60 e.preventDefault() 61 widgetContainer.find(".intro-screen").fadeOut(400, function () { 62 widgetContainer.find(".test-header-img-container").hide(400) 63 widgetContainer.find(".question-screen-" + testContent[0].id).fadeIn([400, function () {}]) 64 }) 65 }) 66 67 widgetContainer.on("click", ".retake-test", function (e) { 68 e.preventDefault() 69 eraseCookie(cookieName) 70 71 widgetContainer.find(".finish-screen").fadeOut(400, function () { 72 jQuery(".intro-screen").fadeIn(400, function () {}) 73 }) 74 75 selectedQuestionId = null 76 selectedOption = null 77 78 questionIndex = 1 79 userAnswers = [] 80 81 checkTestStatus() 82 }) 83 84 function getQuestionPercentages(questionId, answerId) { 85 var data = { 86 action: "sno_test_get_percentage", 87 question_id: questionId, 88 correct_answer_id: answerId, 89 } 90 return jQuery.post(sno_polls_ajax_object.ajax_url, data, null, "json") 91 } 92 function getTestResults(questionId) { 93 const data = { 94 action: "sno_test_results", 95 test_id: questionId, 96 } 97 return jQuery.get(sno_polls_ajax_object.ajax_url, data, null, "json") 98 } 99 100 widgetContainer.on("click", ".results-button", async function (e) { 101 e.preventDefault() 102 var answerData = checkTestQuestion(testContent[0]) 103 104 const nextQuestionScreen = widgetContainer.find(".question-review-screen-" + testContent[0].id) 105 106 let percentageCorrect = 0 107 108 if (multiQuestionPoll == 1) { 109 const response = await getTestResults(answerData.userAnswer.question_id) 110 111 if (response.success === true) { 112 showMultiQuestionPollResults(response.data.results, nextQuestionScreen) 113 } else { 114 console.log("error getting test results", response) 115 } 116 } else { 117 if (answerData.correctAnswer === undefined) { 118 var data_backup = new Array("1") 119 answerData.correctAnswer = data_backup 120 } 121 const response = await getQuestionPercentages( 122 answerData.correctAnswer.question_id, 123 answerData.correctAnswer.id, 124 ) 125 if (response.success === true) { 126 percentageCorrect = (response.data.correct / response.data.total) * 100 127 } else { 128 console.log("err", response) 129 } 130 } 131 132 widgetContainer.find(".test-header-img-container").hide(200) 133 134 if (answerData.correctAnswer != undefined) { 135 const gridCorrectAnswer = nextQuestionScreen.find( 136 '.grid-test-review-answer-container[data-answer-id="' + answerData.correctAnswer.id + '"]', 137 ) 138 } 139 140 if (gridCorrectAnswer != null && gridCorrectAnswer.length && multiQuestionPoll != 1) { 141 if (answerData.isCorrect) { 142 gridCorrectAnswer 143 .find(".grid-poll-answer-container-overlay") 144 .css("border", "1px solid green") 145 .show() 146 } else { 147 const selectedAnswer = gridCorrectAnswer.find( 148 '.test-review-answer-container[data-answer-id="' + answerData.userAnswer.answer_id + '"]', 149 ) 150 selectedAnswer.find(".grid-poll-answer-container-overlay").show() 151 } 152 } 153 154 // correctAnswer.find('img').removeClass('hidden') 155 if (multiQuestionPoll != 1) { 156 const correctAnswer = nextQuestionScreen.find( 157 '.test-review-answer-container[data-answer-id="' + answerData.correctAnswer.id + '"]', 158 ) 159 correctAnswer.css("border", "2px solid green") 160 } 161 162 if (multiQuestionPoll != 1) { 163 if (answerData.isCorrect) { 164 if (showResults == 1) { 165 nextQuestionScreen 166 .find(".correct-answer p") 167 .text(`Correct! ${Math.round(percentageCorrect)}% answered this question correctly`) 168 } else { 169 nextQuestionScreen.find(".correct-answer p").text(`Correct!`) 170 } 171 nextQuestionScreen.find(".incorrect-answer").addClass("hidden") 172 nextQuestionScreen.find(".correct-answer").removeClass("hidden") 173 } else { 174 if (showResults == 1 && multiQuestionPoll != 1) { 175 nextQuestionScreen
176 .find(".incorrect-answer p") 177 .text(`Incorrect. ${Math.round(percentageCorrect)}% answered this question correctly`) 178 } else { 179 nextQuestionScreen.find(".incorrect-answer p").text("Incorrect") 180 } 181 nextQuestionScreen.find(".incorrect-answer").removeClass("hidden") 182 nextQuestionScreen.find(".correct-answer").addClass("hidden") 183 184 const selectedAnswer = nextQuestionScreen.find( 185 '.test-review-answer-container[data-answer-id="' + answerData.userAnswer.answer_id + '"]', 186 ) 187 // selectedAnswer.find('.your-answer').removeClass('hidden') 188 if (multiQuestionPoll != 1) { 189 selectedAnswer.css("border", "2px solid " + shadeColor) 190 } 191 } 192 } 193 194 widgetContainer.find(".finish-screen").fadeOut(400, function () { 195 nextQuestionScreen.fadeIn(400, function () {}) 196 }) 197 }) 198 199 widgetContainer.on("click", ".next-button", async function (e) { 200 e.preventDefault() 201 const currentQuestionScreen = widgetContainer.find( 202 ".question-review-screen-" + testContent[questionIndex - 1].id, 203 ) 204 if (questionIndex > testContent.length - 1) { 205 currentQuestionScreen.fadeOut(400, function () { 206 widgetContainer.find(".finish-screen").fadeIn(400, function () {}) 207 questionIndex = 1 208 }) 209 return 210 } 211 const nextQuestionScreen = widgetContainer.find( 212 ".question-review-screen-" + testContent[questionIndex].id, 213 ) 214 var answerData = checkTestQuestion(testContent[questionIndex]) 215 let percentageCorrect = 0 216 217 if (multiQuestionPoll == 1) { 218 const response = await getTestResults(answerData.userAnswer.question_id) 219 220 if (response.success === true) { 221 showMultiQuestionPollResults(response.data.results, nextQuestionScreen) 222 } else { 223 console.log("error getting test results", response) 224 displayError() 225 } 226 } else { 227 if (answerData.correctAnswer === undefined) { 228 var data_backup = new Array("1") 229 answerData.correctAnswer = data_backup 230 } 231 const response = await getQuestionPercentages( 232 answerData.correctAnswer.question_id, 233 answerData.correctAnswer.id, 234 ) 235 if (response.success === true) { 236 percentageCorrect = (response.data.correct / response.data.total) * 100 237 } else { 238 console.log("err", response) 239 } 240 } 241 242 // correctAnswer.find('img').removeClass('hidden') 243 if (multiQuestionPoll != 1) { 244 const correctAnswer = nextQuestionScreen.find( 245 '.test-review-answer-container[data-answer-id="' + answerData.correctAnswer.id + '"]', 246 ) 247 correctAnswer.css("border", "2px solid green") 248 } 249 250 if (multiQuestionPoll != 1) { 251 const gridCorrectAnswer = nextQuestionScreen.find( 252 '.grid-test-review-answer-container[data-answer-id="' + answerData.correctAnswer.id + '"]', 253 ) 254 } 255 256 if (gridCorrectAnswer != null && gridCorrectAnswer.length && multiQuestionPoll != 1) { 257 gridCorrectAnswer 258 .find(".grid-poll-answer-container-overlay") 259 .css("border", "4px solid green") 260 .show() 261 262 if (answerData.isCorrect) { 263 // gridCorrectAnswer.find('.grid-poll-answer-container-overlay').clone().show() 264 // nextQuestionScreen.append(gridCorrectAnswer) 265 } else { 266 const selectedAnswer = nextQuestionScreen.find( 267 '.grid-test-review-answer-container[data-answer-id="' + 268 answerData.userAnswer.answer_id + 269 '"]', 270 ) 271 selectedAnswer.find(".grid-poll-answer-container-overlay").show() 272 } 273 } 274 275 if (multiQuestionPoll != 1) { 276 if (answerData.isCorrect) { 277 if (showResults == 1 && multiQuestionPoll != 1) { 278 nextQuestionScreen 279 .find(".correct-answer p") 280 .text(`Correct! ${Math.round(percentageCorrect)}% answered this question correctly`) 281 } else { 282 nextQuestionScreen.find(".correct-answer p").text(`Correct!`) 283 } 284 nextQuestionScreen.find(".incorrect-answer").addClass("hidden") 285 nextQuestionScreen.find(".correct-answer").removeClass("hidden") 286 } else { 287 if (showResults == 1 && multiQuestionPoll != 1) { 288 nextQuestionScreen
289 .find(".incorrect-answer p") 290 .text(`Incorrect. ${Math.round(percentageCorrect)}% answered this question correctly`) 291 } else { 292 nextQuestionScreen.find(".incorrect-answer p").text("Incorrect") 293 } 294 nextQuestionScreen.find(".incorrect-answer").removeClass("hidden") 295 nextQuestionScreen.find(".correct-answer").addClass("hidden") 296 const selectedAnswer = nextQuestionScreen.find( 297 '.test-review-answer-container[data-answer-id="' + answerData.userAnswer.answer_id + '"]', 298 ) 299 // selectedAnswer.find('.your-answer').removeClass('hidden') 300 if (multiQuestionPoll != 1) { 301 selectedAnswer.css("border", "2px solid " + shadeColor) 302 } 303 } 304 } 305 306 currentQuestionScreen.fadeOut(400, function () { 307 nextQuestionScreen.fadeIn(400, function () {}) 308 questionIndex++ 309 }) 310 }) 311 312 function showMultiQuestionPollResults(results, cScreen) { 313 const totalVotes = results.reduce(function (accum, curr) { 314 return curr.total ? accum + Number(curr.total) : accum 315 }, 0) 316 317 for (let result of results) { 318 const votePercentage = 319 totalVotes != 0 && totalVotes != undefined ? Number(result.total) / totalVotes : 0 320 321 const el = cScreen.find(`div[data-answer-id="${result.id}"]`) 322 323 el.find(`input[data-answer-id="${result.id}"]`).hide() 324 325 el.find(`.result-text`).text(`${Math.round(votePercentage * 100) || 0}%`) 326 el.find(`.result-shading`).css({ width: `${votePercentage * 100 || 0}%` }) 327 el.addClass("results") 328 widgetContainer.find(".grid-color-info").hide() 329 } 330 hide_loader() 331 } 332 333 widgetContainer.find(".question-screen").on("click", ".submit-button", async function (e) { 334 e.preventDefault() 335 336 widgetContainer.find(".submit-button").prop("disabled", true) 337 widgetContainer.find(".submit-button").addClass("disabled-button") 338 339 let questionId, answerId 340 341 if (selectedQuestionId && selectedOption) { 342 questionId = selectedQuestionId 343 answerId = selectedOption 344 345 selectedQuestionId = null 346 selectedOption = null 347 } else { 348 questionId = jQuery(this).closest(".question-screen").attr("data-question-id") 349 answerId = widgetContainer.find(".question-screen").find('input[name="test-answer"]:checked').val() 350 } 351 352 const res = await sendTestVote(questionId, answerId) 353 354 if (res.success === true) { 355 userAnswers.push(res.data) 356 updateCookieState(res.data) 357 } else { 358 console.log("err", res) 359 displayError() 360 } 361 console.log(testContent, 'hello') 362 widgetContainer.find(".question-screen-" + testContent[questionIndex - 1].id).fadeOut(400, function () { 363 if (questionIndex >= testContent.length) { 364 const results = testContent 365 .map(function (t) { 366 return checkTestQuestion(t) 367 }) 368 .filter(function (a) { 369 return a.isCorrect 370 }) 371 372 if (multiQuestionPoll != 1) { 373 widgetContainer 374 .find(".finish-screen") 375 .find(".number-correct") 376 .text(`${results.length} / ${testContent.length} questions correct`) 377 } 378 379 widgetContainer.find(".finish-screen").fadeIn(400) 380 questionIndex = 1 381 } else { 382 widgetContainer 383 .find(".question-screen-" + testContent[questionIndex].id.toString()) 384 .fadeIn(400, function () { 385 questionIndex++ 386 }) 387 } 388 }) 389 }) 390 391 widgetContainer.find(".question-screen").on("click", 'input[name="test-answer"]', function (e) { 392 if (jQuery(this).prop("checked") === true) { 393 widgetContainer.find(".question-screen").find(".submit-button").prop("disabled", false) 394 widgetContainer.find(".question-screen").find(".submit-button").removeClass("disabled-button") 395 } 396 }) 397 398 widgetContainer.on("click", ".grid-test-answer-container", handleAnswerClick) 399 400 function handleAnswerClick(e) { 401 e.preventDefault() 402 // get vote 403 selectedQuestionId = jQuery(this).closest(".question-screen").attr("data-question-id") 404 const answerId = jQuery(this).find('input[name="test-answer"]').val() 405 406 selectedOption = answerId 407 408 jQuery('input[name="test-answer"]').each(function (index, el) { 409 const id = jQuery(this).val() 410 411 if (id === answerId) { 412 jQuery(this).prev(".grid-poll-answer-container-overlay").show() 413 } else { 414 jQuery(this).prev(".grid-poll-answer-container-overlay").hide() 415 } 416 }) 417 418 widgetContainer.find(".question-screen").find(".submit-button").prop("disabled", false) 419 widgetContainer.find(".question-screen").find(".submit-button").removeClass("disabled-button") 420 } 421 422 function sendTestVote(questionId, answerId) { 423 var data = { 424 action: "sno_test_vote", 425 question_id: questionId, 426 answer_id: answerId, 427 } 428 return jQuery.post(sno_polls_ajax_object.ajax_url, data, null, "json") 429 } 430 431 function checkTestStatus() { 432 show_loader()
433 const testCookie = readCookie(cookieName) 434 const parsedCookie = JSON.parse(testCookie) 435 436 if (testCookie) { 437 for (let answer of parsedCookie) { 438 userAnswers.push(answer) 439 } 440 441 if (parsedCookie.length && parsedCookie.length >= testContent.length) { 442 handleAlreadyVoted() 443 } else if (parsedCookie.length && parsedCookie.length < testContent.length) { 444 questionIndex = parsedCookie.length + 1 445 handlePartiallyVoted() 446 } 447 } 448 hide_loader() 449 } 450 451 function updateCookieState(userAnswer) { 452 const testCookie = readCookie(cookieName) 453 if (!testCookie) { 454 const cookieData = [userAnswer] 455 createCookie(cookieName, JSON.stringify(cookieData), 30) 456 } else { 457 const cookieData = [...JSON.parse(testCookie), userAnswer] 458 eraseCookie(cookieName) 459 createCookie(cookieName, JSON.stringify(cookieData), 30) 460 } 461 } 462 463 function handleAlreadyVoted() { 464 const results = testContent 465 .map(function (t) { 466 return checkTestQuestion(t) 467 }) 468 .filter(function (a) { 469 return a.isCorrect 470 }) 471 472 if (multiQuestionPoll != 1) { 473 widgetContainer 474 .find(".finish-screen") 475 .find(".number-correct") 476 .text(`${results.length} / ${testContent.length} questions correct`) 477 } 478 479 widgetContainer.find(".intro-screen").fadeOut(400, function () { 480 jQuery(".finish-screen").fadeIn(400, function () {}) 481 }) 482 } 483 484 function handlePartiallyVoted() { 485 widgetContainer.find(".intro-screen").fadeOut(400, function () { 486 widgetContainer.find(".test-header-img-container").hide(400) 487 widgetContainer.find(".question-screen-" + testContent[questionIndex - 1].id).show(400) 488 }) 489 } 490 491 function show_loader() { 492 widgetContainer.find(".test-content").fadeOut(400, function () {}) 493 widgetContainer.find(".test-loader").fadeIn(400, function () {}) 494 } 495 496 function hide_loader() { 497 widgetContainer.find(".test-loader").fadeOut(400, function () {}) 498 widgetContainer.find(".test-content").fadeIn(400, function () {}) 499 } 500 501 function displayError() { 502 widgetContainer.find(".test-loader").fadeOut(400, function () {}) 503 widgetContainer.find(".poll-answers-container").fadeOut(400, function () {}) 504 widgetContainer.find(".error-container").fadeIn(400, function () {}) 505 } 506 }) 507 } 508} 509 510function createCookie(name, value, days) { 511 let expires 512 513 if (days) { 514 const date = new Date() 515 date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000) 516 expires = "; expires=" + date.toGMTString() 517 } else { 518 expires = "" 519 } 520 document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/" 521} 522 523function readCookie(cname) { 524 let name = encodeURIComponent(cname) + "=" 525 let ca = document.cookie.split(";") 526 for (let i = 0; i < ca.length; i++) { 527 let c = ca[i] 528 while (c.charAt(0) === " ") c = c.substring(1, c.length) 529 if (c.indexOf(name) === 0) return decodeURIComponent(c.substring(name.length, c.length)) 530 } 531 return null 532} 533 534function eraseCookie(name) { 535 createCookie(name, "", -1) 536}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.