-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.js
212 lines (209 loc) · 9.86 KB
/
quiz.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
(function($) {
$.fn.jquizzy = function(settings) {
var defaults = {
questions: null,
startImg: 'images/start.gif',
endText: '已结束!',
shortURL: null,
sendResultsURL: null,
resultComments: {
perfect: '你是爱因斯坦么?',
excellent: '非常优秀!',
good: '很好,发挥不错!',
average: '一般般了。',
bad: '太可怜了!',
poor: '好可怕啊!',
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="results-container slide-container"><div class="question-number">' + config.endText + '</div><div class="result-keeper"></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++) {
contentFob += '<li>' + config.questions[questionsIteratorIndex].answers[answersIteratorIndex] + '</li>';
}
contentFob += '</ul><div class="nav-container">';
if (questionsIteratorIndex !== 0) {
contentFob += '<div class="prev"><a class="nav-previous" href="#">< 上一题</a></div>';
}
if (questionsIteratorIndex < config.questions.length - 1) {
contentFob += '<div class="next"><a class="nav-next" href="#">下一题 ></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(introFob + 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 = [],
flag = false;
for (i = 0; i < answers.length; i++) {
if (answers[i] == userAnswers[i]) {
flag = true;
} else {
flag = false;
}
resultArr.push(flag);
}
return resultArr;
}
function roundReloaded(num, dec) {
var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
return result;
}
var FinshScore;
function judgeSkills(score) {
var returnString;
FinshScore = score;
var id = prompt("请输入学号:");
var name = prompt("请输入姓名:");
var phone = prompt("请输入手机号:");
var school = prompt("请输入学院:");
$.get("proc.php?id="+id+"&name="+name+"&school="+school+"&phone="+phone+"&score="+score,"");
if (score === 100) return config.resultComments.perfect;
else if (score > 90) return config.resultComments.excellent;
else if (score > 70) return config.resultComments.good;
else if (score > 50) return config.resultComments.average;
else if (score > 35) return config.resultComments.bad;
else if (score > 20) return config.resultComments.poor;
else return config.resultComments.worst;
}
progressKeeper.hide();
notice.hide();
slidesList.hide().first().fadeIn(500);
superContainer.find('li').click(function() {
var thisLi = $(this);
if (thisLi.hasClass('selected')) {
thisLi.removeClass('selected');
} else {
thisLi.parents('.answers').children('li').removeClass('selected');
thisLi.addClass('selected');
}
});
superContainer.find('.nav-start').click(function() {
$(this).parents('.slide-container').fadeOut(500,
function() {
$(this).next().fadeIn(500);
progressKeeper.fadeIn(500);
});
return false;
});
superContainer.find('.next').click(function() {
if ($(this).parents('.slide-container').find('li.selected').length === 0) {
notice.fadeIn(300);
return false;
}
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() {
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;
}
superContainer.find('li.selected').each(function(index) {
userAnswers.push($(this).parents('.answers').children('li').index($(this).parents('.answers').find('li.selected')) + 1);
});
if (config.sendResultsURL !== null) {
var collate = [];
for (r = 0; r < userAnswers.length; r++) {
collate.push('{"questionNumber":"' + parseInt(r + 1, 10) + '", "userAnswer":"' + userAnswers[r] + '"}');
}
$.ajax({
type: 'POST',
url: config.sendResultsURL,
data: '{"answers": [' + collate.join(",") + ']}',
complete: function() {
console.log("OH HAI");
}
});
}
progressKeeper.hide();
var results = checkAnswers(),
resultSet = '',
trueCount = 0,
shareButton = '',
score,
url;
if (config.shortURL === null) {
config.shortURL = window.location
};
for (var i = 0,
toLoopTill = results.length; i < toLoopTill; i++) {
if (results[i] === true) {
trueCount++;
isCorrect = true;
}
resultSet += '<div class="result-row">' + (results[i] === true ? "<div class='correct'>#"+(i + 1)+"<span></span></div>": "<div class='wrong'>#"+(i + 1)+"<span></span></div>");
resultSet += '<div class="resultsview-qhover">' + config.questions[i].question;
resultSet += "<ul>";
for (answersIteratorIndex = 0; answersIteratorIndex < config.questions[i].answers.length; answersIteratorIndex++) {
var classestoAdd = '';
if (config.questions[i].correctAnswer == answersIteratorIndex + 1) {
classestoAdd += 'right';
}
if (userAnswers[i] == answersIteratorIndex + 1) {
classestoAdd += ' selected';
}
resultSet += '<li class="' + classestoAdd + '">' + config.questions[i].answers[answersIteratorIndex] + '</li>';
}
resultSet += '</ul></div></div>';
}
score = roundReloaded(trueCount / questionLength * 100, 2);
resultSet = '<h2 class="qTitle">' + judgeSkills(score) + '<br/> 您的分数: ' + score + '</h2>' + shareButton + '<div class="jquizzy-clear"></div>' + resultSet + '<div class="jquizzy-clear"></div>';
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);
});
return false;
});
};
})(jQuery);