You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
12 KiB

10 months ago
/**
* 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('<div class="intro-container slide-container"><h2 class="qTitle">Failed to parse questions.</h2></div>');
return;
}
var superContainer = $(this),
answers = [],
//introFob = '<div class="intro-container slide-container"><a class="nav-start" href="#">请认真完成测试题。准备好了吗?<br/><br/><span><img src="'+config.startImg+'"></span></a></div> ',
exitFob = '<div class="question-number1"><div id="timer" style="color:red"></div><div id="warring" style="color:red"></div></div><div class="results-container slide-container"><div class="question-number">' + config.endText + '</div><div class="result-keeper" style="overflow: auto;"></div></div><div class="notice">请选择一个选项!</div><div class="progress-keeper" ><div class="progress"></div></div>',
contentFob = '',
questionsIteratorIndex,
answersIteratorIndex;
superContainer.addClass('main-quiz-holder');
for (questionsIteratorIndex = 0; questionsIteratorIndex < config.questions.length; questionsIteratorIndex++) {
contentFob += '<div class="slide-container"><div class="question-number">' + (questionsIteratorIndex + 1) + '/' + config.questions.length + '</div><div class="question">' + config.questions[questionsIteratorIndex].question + '</div><ul class="answers">';
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 += '<li>' +_sb+ config.questions[questionsIteratorIndex].answers[answersIteratorIndex] + '</li>';
}
contentFob += '</ul><div class="nav-container">';
if (questionsIteratorIndex !== 0) {
contentFob += '<div class="prev"><a class="nav-previous" href="#">&lt; 上一题</a></div>';
}
if (questionsIteratorIndex < config.questions.length - 1) {
contentFob += '<div class="next"><a class="nav-next" href="#">下一题 &gt;</a></div>';
} else {
contentFob += '<div class="next final"><a class="nav-show-result" href="#">完成</a></div>';
}
contentFob += '</div></div>';
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 += '<div class="result-row">' + (results[i] === true ? "<div style=\"text-align: left;\" class='correct'>&nbsp;&nbsp;&nbsp;&nbsp;"+config.questions[i].question+ "【" + userAnswers[i] +"】<span></span></div>": "<div class='wrong' style=\"text-align: left;\">&nbsp;&nbsp;&nbsp;&nbsp;"+config.questions[i].question + "【" +userAnswers[i] +"】<span></span></div>");
resultSet += '<div class="resultsview-qhover">';
resultSet += "<ul>";
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 += '<li class="' + classestoAdd + '">' + config.questions[i].answers[answersIteratorIndex] + '</li>';
}
resultSet += '</ul></div></div>';
}
resultSet = '<h2 class="qTitle">'+judgeSkills(score)+'<br/> 您的分数: ' + score + '</h2>' + shareButton + '<div class="jquizzy-clear"></div>' + resultSet + '<div class="jquizzy-clear"></div>';
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);