MediaWiki:Quiz/code.js

(function($) {   'use strict';    function msg(name) {        if (quizLang in translations && name in translations[quizLang]) {            return translations[quizLang][name];        }        return translations.en[name];    }

var score = 0, currentQuestion = 0, secondsSince = 0, minutesSince = 0, questionsAmount = 0, startCounterVar; $.fn.randomize = function(selector) { (selector ? this.find(selector) : this).parent.each(function {           $(this).children(selector).sort(function { return Math.random - 0.5; }).detach.appendTo(this);       }); return this; };

$(function {       if ($('#quizQ').length > 0) {            importArticle({ type: 'style', article: 'u:dev:MediaWiki:Quiz/code.css' });           $('div#quizQ').append('          ');            startCounter;            floatHourglass;            summonBigClouds;            summonSmallClouds;            $('#quizQ #quizName').text(quizName);            questionsAmount = questions.length;            if (questionsAmount > 0) {                $('#quizQ #nxtBtn').on('click', function { $('#quizQ #nxtBtn').removeClass('nxtBtnActive'); if ($('#quizQ #questionForm input:checked').length > 0 || currentQuestion === -1) { updateScore; $('#quizQ #questionBlock').fadeOut(200, updateForm); $('#quizQ #questionBlock').fadeIn(200); }               });                $('#quizQ #questionCounter').text(currentQuestion + 1 + '/' + questionsAmount);                $('#quizQ #nxtBtn').text(msg('nxtBtn'));                createform;                $('#quizQ #questionBlock').on('click', function { if ($('#quizQ #questionForm input:checked').length > 0) { $('#quizQ #nxtBtn').addClass('#quizQ nxtBtnActive'); }               });            } else {                $('#quizQ #question').html(' ' + mw.html.escape(msg('noQuestionsHeader')) + ' ');            }        }    });

function createform { var currentQuestionArray = questions[currentQuestion]; $('#quizQ #question').text(currentQuestionArray[0]); $.each(currentQuestionArray, function(i, v) {           if (i < 1) {                return true;            }            $('#quizQ #questionForm').append(' ' + mw.html.escape(String(v)) + ' ');        }); $('#answerWrapper').randomize; }

function updateScore { if (currentQuestion !== -1) { var submittedAnswer = $('#quizQ #questionForm input:checked + label').text, currentQuestionArray = questions[currentQuestion], correctAnswer = currentQuestionArray[1]; if (correctAnswer === submittedAnswer) { score++; }       } else { score = 0; }   }

function updateForm { currentQuestion++; if (currentQuestion < questionsAmount) { $('#quizQ #questionForm').empty; createform; $('#quizQ #questionCounter').text(currentQuestion + 1 + '/' + questionsAmount); }       if (currentQuestion === questionsAmount) { currentQuestion = -1; clearTimeout(startCounterVar); $('#quizQ #questionForm').empty; $('#quizQ #questionCounter').text(''); $('#quizQ #question').html(' ' + mw.html.escape(msg('resultsHeader')) + ': ' + Math.round(score / questionsAmount * 100) + '% (' + score + '/' + questionsAmount + ') '); var finalScore = score / questionsAmount, resultsFraction = 1 / resultsTextArray.length, resultsText; $.each(resultsTextArray, function(i, v) {               if (finalScore >= resultsFraction * i) {                    resultsText = v;                }            }); $('#quizQ #questionBlock').append(' ' + mw.html.escape(resultsText) + ' '); $('#quizQ #nxtBtn').text(msg('rplBtn')).addClass('nxtBtnActive'); }       if (currentQuestion === 0) { secondsSince = 0; minutesSince = 0; startCounter; $('#quizQ #resultsText').remove; $('#quizQ #nxtBtn').text(msg('nxtBtn')).removeClass('nxtBtnActive'); $('#quizQ #questionCounter').text(currentQuestion + 1 + '/' + questionsAmount); }   }

function startCounter { if (secondsSince > 59) { secondsSince = 0; minutesSince += 1; }

function checkTime(i) { if (i < 10) { i = '0' + i;           } return i;       } var mAdjusted = checkTime(minutesSince), sAdjusted = checkTime(secondsSince); $('#quizQ #quiztimer').text(mAdjusted + ':' + sAdjusted); secondsSince += 1; startCounterVar = setTimeout(function {           startCounter;        }, 1000); }

function floatHourglass { $('#quizQ #quizhourglass').animate({           top: '-=20px'        }, 1000).animate({            top: '+=20px'        }, 1000); setTimeout(function {           floatHourglass;        }, 2000); }

function summonBigClouds { var randomHeight = Math.random * 500 - 70; $(' ').css('top', randomHeight).appendTo('#quizQ').animate({           right: '+=850px'        }, 20000, function {            $(this).remove;        }); setTimeout(function {           summonBigClouds;        }, 4000); }

function summonSmallClouds { var randomHeight = Math.random * 500 - 35; $(' ').css('top', randomHeight).appendTo('#quizQ').animate({           right: '+=800px'        }, 30000, function {            $(this).remove;        }); setTimeout(function {           summonSmallClouds;        }, 5000); } })(jQuery);