// Set up some variables var Poll, voteButton, pollName, pollId, userIp, userEmail, timeStamp, adminUrl; adminUrl = window.location.protocol + "//" + window.location.host + "/wp-admin/admin-ajax.php"; jQuery(document).ready(function(){ Poll = jQuery('#onnet-polls'); voteButton = Poll.find('.answer-button'); // If a poll is found, initialize and load our event handlers if(Poll.length > 0) { opInitializePoll(); } }); function opInitializePoll() { pollName = jQuery('input[name="poll_name"]').val(); pollId = jQuery('input[name="poll_id"]').val(); userIp = jQuery('input[name="user_ip"]').val(); userEmail = jQuery('input[name="user_email_address"]').val(); timeStamp = jQuery('input[name="timestamp"]').val(); Poll.find('.poll--progress').hide(); // Check if user has voted already var voted = opCheckIfVoted(pollId); if(voted != null) { opShowResults(); } else { voteButton.on('click', submitVote); } } // Checks if the user has voted already function opCheckIfVoted(pollId) { return localStorage.getItem("Poll voted - "+pollId); } function submitVote() { // Check if user has voted already var voted = opCheckIfVoted(pollId); if(voted != null) { } else { var vote = jQuery(this).find('.poll--caption').text(); jQuery(this).find('.poll--checkbox').trigger('votedPoll::success'); storeUserVote(vote, pollId, pollName, userIp, userEmail, timeStamp); } } // Shows the results for the poll function opShowResults() { var answers = Poll.find('ul.answers'); answers.fadeOut(); Poll.addClass('results'); fetchPollResults(pollId); answers.fadeIn({complete: showPollProgressBar}); } function showPollProgressBar() { Poll.find('.poll--progress').fadeIn(); } function storeUserVote( vote, pollId, pollName, userIp, userEmail, timeStamp ) { jQuery.ajax({ type: "GET", data: { action: 'store_poll_vote', userVote: vote, pollID: pollId, pollName: pollName, userIP: userIp, userEmail: userEmail, timeStamp: timeStamp }, dataType: "html", url: adminUrl, beforeSend: function () { }, success: function (data) { localStorage.setItem("Poll voted - "+pollId, JSON.stringify("Poll voted - "+pollId)); opShowResults(); } }); } function fetchPollResults( pollId ) { jQuery.ajax({ type: "GET", data: { action: 'fetch_poll_results', pollID: pollId }, dataType: "html", url: adminUrl, beforeSend: function () { }, success: function (data) { $data = jQuery(jQuery.parseJSON(data)); if($data.length > 0) { var totalVotes = 0; var totalVotesArr = []; $data.each(function(i, e) { var caption = e.user_vote; caption = caption.stripSlashes(); Poll.find('.poll--caption:contains('+caption+')').parent().find('.poll-vote-count').text(e.vote_count); totalVotes += parseInt(e.vote_count); totalVotesArr[i] = parseInt(e.vote_count); }); var max = Math.max.apply(null, totalVotesArr); Poll.find('.answer-button').each(function(i, e) { var count = jQuery(e).find('.poll-vote-count').text(); var percentage = Math.round((count / totalVotes) * 100).toFixed(0)+"%"; if (count == "" && percentage == "0%") { jQuery(e).find('.poll-vote-count').text('0'); } jQuery(e).find('.poll-votes').text(percentage); jQuery(e).find('.poll-bar').css('width', percentage); if(count == max) { jQuery(e).addClass('most-voted'); } }); } else { jQuery('#onnet-polls .answer-button').each(function(i, e) { jQuery(e).find('.poll-votes').text("0%"); jQuery(e).find('.poll-bar').css('width', '0'); }); } } }); } String.prototype.stripSlashes = function(){ return this.replace(/\\(.)/mg, "$1"); } function populatePollResults() { jQuery('.knowledge-quiz-result').each(function(i, e) { weight_from = jQuery(this).attr('data-from'); weight_to = jQuery(this).attr('data-to'); last_weight = jQuery(this).attr('data-last'); if((weight_from <= quiz_score) && ((quiz_score < weight_to) || (quiz_score <= last_weight))) { jQuery(this).css({ 'display': 'block' }).addClass('final-result').find('.quiz-percentage').text(Math.round((quiz_score / question_count) * 100).toFixed(0)+"%"); } }); } /* playback timings (ms): LoadShardBlock: 1005.845 (3) esindex: 0.009 captures_list: 1033.905 CDXLines.iter: 16.09 (3) PetaboxLoader3.datanode: 817.123 (5) exclusion.robots: 0.183 exclusion.robots.policy: 0.169 RedisCDXSource: 7.977 PetaboxLoader3.resolve: 1370.45 (2) load_resource: 1807.672 */