/**
* Created by YYp on 2016-05-07.
*/
(function($) {
$.fn.jquizzy = function(settings) {
var defaults = {
questions: null,
startImg: 'img/start.gif',
endText: '已结束!',
shortURL: null,
sendResultsURL: null,
jgx:60,
_data:null,
resultComments: {
perfect: '恭喜您合格!',
worst: '还需要继续努力!'
}
};
var config = $.extend(defaults, settings);
if (config.questions === null) {
$(this).html('
Failed to parse questions.
');
return;
}
var superContainer = $(this),
answers = [],
//introFob = ' ',
exitFob = '请选择一个选项!
',
contentFob = '',
questionsIteratorIndex,
answersIteratorIndex;
superContainer.addClass('main-quiz-holder');
for (questionsIteratorIndex = 0; questionsIteratorIndex < config.questions.length; questionsIteratorIndex++) {
contentFob += '' + (questionsIteratorIndex + 1) + '/' + config.questions.length + '
' + config.questions[questionsIteratorIndex].question + '
';
for (answersIteratorIndex = 0; answersIteratorIndex < config.questions[questionsIteratorIndex].answers.length; answersIteratorIndex++) {
_sb = "";
if(answersIteratorIndex == 0){
_sb = "A. "
}else if(answersIteratorIndex == 1){
_sb = "B. "
}else if(answersIteratorIndex == 2){
_sb = "C. "
}else{
_sb = "D. "
}
contentFob += '- ' +_sb+ config.questions[questionsIteratorIndex].answers[answersIteratorIndex] + '
';
}
contentFob += '
';
if (questionsIteratorIndex !== 0) {
contentFob += '
';
}
if (questionsIteratorIndex < config.questions.length - 1) {
contentFob += '
';
} else {
contentFob += '
';
}
contentFob += '
';
answers.push(config.questions[questionsIteratorIndex].correctAnswer);
}
superContainer.html(contentFob + exitFob);
var progress = superContainer.find('.progress'),
progressKeeper = superContainer.find('.progress-keeper'),
notice = superContainer.find('.notice'),
progressWidth = progressKeeper.width(),
userAnswers = [],
questionLength = config.questions.length,
slidesList = superContainer.find('.slide-container');
//判断答案
function checkAnswers() {
var resultArr = [];
for (i = 0; i < answers.length; i++) {
flag = false;
if (answers[i] == userAnswers[i]) {
flag = true;
}
resultArr.push(flag);
}
return resultArr;
}
/*判断分数*/
function judgeSkills(score) {
var returnString;
if (score >= config.jgx) return config.resultComments.perfect;
else return config.resultComments.worst;
}
progressKeeper.hide();
notice.hide();
slidesList.hide().first().fadeIn(500);
//点击选项
superContainer.find('li').click(function() {
var thisLi = $(this);
var _l = answers[flag] + "";
var MultiSelect = _l.split(",").length; //定义多选
//单选
/* if (thisLi.hasClass('selected')) {
thisLi.removeClass('selected');
} else {
thisLi.parents('.answers').children('li').removeClass('selected');
thisLi.addClass('selected');
}*/
if(MultiSelect >1){
//多选
if (!thisLi.hasClass('selected')) {
thisLi.addClass('selected');
}
else{
thisLi.removeClass('selected');
}
}else{
//单选
if (thisLi.hasClass('selected')) {
thisLi.removeClass('selected');
} else {
thisLi.parents('.answers').children('li').removeClass('selected');
thisLi.addClass('selected');
}
}
// 继续
$(".btns").click(function() {
$(".result").hide();
})
});
/*开始*/
superContainer.find('.nav-start').click(function() {
$(this).parents('.slide-container').fadeOut(500,
function() {
$(this).next().fadeIn(500);
progressKeeper.fadeIn(500);
});
return false;
});
/*下一题*/
var flag=0;//用于标记
superContainer.find('.next').click(function() {
flag++;
//未选中消息提醒
if ($(this).parents('.slide-container').find('li.selected').length === 0) {
notice.fadeIn(300);
}
//答题判断对错
// var $select=$(this).hasClass('selected');
var indexItems=[];
superContainer.find('ul[class="answers"]').eq(flag-1).children().each(function(index) {
if($(this).hasClass("selected")) {
var n = $(this).index() + 1;
if ($.inArray(n, indexItems) == -1) {
indexItems.push(n);
}
}
});
notice.hide();
$(this).parents('.slide-container').fadeOut(500,
function() {
$(this).next().fadeIn(500);
});
progress.animate({
width: progress.width() + Math.round(progressWidth / questionLength)
},
500);
return false;
});
/*上一题*/
superContainer.find('.prev').click(function() {
flag--;
notice.hide();
$(this).parents('.slide-container').fadeOut(500,
function() {
$(this).prev().fadeIn(500);
});
progress.animate({
width: progress.width() - Math.round(progressWidth / questionLength)
},
500);
return false;
});
/*完成*/
superContainer.find('.final').click(function() {
/*if ($(this).parents('.slide-container').find('li.selected').length === 0) {
notice.fadeIn(300);
return false;
}*/
clearInterval(timer);
superContainer.find('ul[class="answers"]').each(function(index) {
var indexItems=[];
$(this).children().each(function(s){
if($(this).hasClass("selected"))
{
var n=$(this).index()+1;
if($.inArray(n,indexItems)==-1)
{
indexItems.push($(this).html());
}
}
});
userAnswers.push(indexItems.join(","));
});
progressKeeper.hide();
var results = checkAnswers(),
resultSet = '',
trueCount = 0,
shareButton = '',
score,
url;
if (config.shortURL === null) {
config.shortURL = window.location
};
score = 0;
for (var i = 0,
toLoopTill = results.length; i < toLoopTill; i++) {
if (results[i] === true) {
trueCount++;
isCorrect = true;
}
if(results[i] === true){
score += config.questions[i].df;
}
resultSet += '' + (results[i] === true ? "
"+config.questions[i].question+ "【" + userAnswers[i] +"】
": "
"+config.questions[i].question + "【" +userAnswers[i] +"】
");
resultSet += '
';
resultSet += "
";
for (answersIteratorIndex = 0; answersIteratorIndex < config.questions[i].answers.length; answersIteratorIndex++) {
var classestoAdd = '';
var _s = config.questions[i].correctAnswer + "";
if(_s.indexOf(",") > 0){
var _s1 = config.questions[i].correctAnswer.split(",");
for(var x in _s1){
if(_s1[x] == answersIteratorIndex + 1){
classestoAdd += 'right';
}
}
}else{
if (config.questions[i].correctAnswer == answersIteratorIndex + 1) {
classestoAdd += 'right';
}
}
resultSet += '- ' + config.questions[i].answers[answersIteratorIndex] + '
';
}
resultSet += '
';
}
resultSet = ''+judgeSkills(score)+'
您的分数: ' + score + '
' + shareButton + '' + resultSet + '';
superContainer.find('.slide-container').hide();
superContainer.find('.results-container').show();
superContainer.find('.result-keeper').html(resultSet).show(500);
superContainer.find('.resultsview-qhover').hide();
superContainer.find('.result-row').hover(
function() {
$(this).find('.resultsview-qhover').show();
},
function() {
$(this).find('.resultsview-qhover').hide();
}
);
$(this).parents('.slide-container').fadeOut(500,
function() {
$(this).next().fadeIn(500);
}
);
if(config.sendResultsURL !== null){
if(config._data == null){
alert("网络异常!请重新扫码考试!");
return false;
}
$.ajax({
type: 'POST',
url: config.sendResultsURL,
data: {
name:config._data.name,
jobNumber:config._data.number,
phoneNum:config._data.phone,
dept:config._data.dept,
baseId:config._data.baseId,
regionId:config._data.regionId,
regionIds:config._data.regionIds,
regionVal:config._data.regionVal,
regionVals:config._data.regionVals,
paperId:config._data.paperId,
startTime:config._data.startTime,
examName:config._data.examName,
score:score,
jgx:config.jgx
}
});
}
return false;
});
};
})(jQuery);