PluginProbe
Watu Quiz / trunk
Watu Quiz vtrunk
3.4.8 trunk 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.5.3 3.4.6 3.4.7
watu / script.js

script.js in Watu Quiz trunk, at script.js

317 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // wrap in object to avoid polluting the javascript namespace
2 var Watu={};
3
4 Watu.current_question = 1;
5 Watu.dontScroll = 0;
6 Watu.total_questions = 0;
7 Watu.mode = "show";
8
9 Watu.isAnswered = function() {
10 if(jQuery('#questionType' + Watu.current_question).val() == 'textarea') {
11 if(jQuery('.watu-textarea-'+Watu.current_question).val()!='') return true;
12 else return false;
13 }
14
15 var answered = false;
16
17 jQuery("#question-" + Watu.current_question + " .answer").each(function(i) {
18 if(this.checked) {
19 answered = true;
20 return true;
21 }
22 });
23
24 return answered;
25 }
26
27 Watu.isRequired = function() {
28 if(jQuery('#questionType'+ Watu.current_question).attr('class') == 'required') return true;
29
30 return false;
31 }
32
33 Watu.checkAnswer = function(e) {
34 if(!Watu.isAnswered()) {
35 if(Watu.isRequired()) {
36 alert(watu_i18n.missed_required_question);
37 return false;
38 }
39
40 // not required, so ask
41 if(!Watu.noAlertUnanswered && !confirm(watu_i18n.nothing_selected)) {
42 e.preventDefault();
43 e.stopPropagation();
44 return false;
45 }
46 }
47 return true;
48 }
49
50 Watu.nextQuestion = function(e, dir) {
51 dir = dir || 'next'; // next or previous
52 if(dir == 'next' && !Watu.checkAnswer(e)) return;
53
54 // change the displayed question number
55 var numQ = jQuery('#numQ').html();
56 if(dir == 'next') numQ++;
57 else numQ--;
58 jQuery('#numQ').html(numQ);
59
60 jQuery("#question-" + Watu.current_question).hide();
61 if(dir == 'next') Watu.current_question++;
62 else Watu.current_question--;
63 jQuery("#question-" + Watu.current_question).show();
64
65 if(Watu.total_questions <= Watu.current_question) {
66 jQuery("#next-question").hide();
67 jQuery("#action-button").show();
68 if(jQuery('#WatuTextCaptcha').length) jQuery('#WatuTextCaptcha').show();
69 }
70 else {
71 jQuery("#next-question").show();
72 if(jQuery('#WatuTextCaptcha').length) jQuery('#WatuTextCaptcha').hide();
73 }
74
75 // show / hide prev button if any
76 if(jQuery('#prev-question').length) {
77 if(Watu.current_question <= 1) jQuery('#prev-question').hide();
78 else jQuery('#prev-question').show();
79 }
80
81 if(!Watu.dontScroll && jQuery(document).scrollTop() > 250) {
82 jQuery('html, body').animate({
83 scrollTop: jQuery('#watu_quiz').offset().top -100
84 }, 100);
85 }
86
87 }
88
89 // This part is used only if the answers are show on a per question basis.
90 Watu.showAnswer = function(e) {
91 if(!Watu.checkAnswer(e)) return;
92
93 if(Watu.mode == "next") {
94 Watu.mode = "show";
95
96 jQuery("#question-" + Watu.current_question).hide();
97 Watu.current_question++;
98 jQuery("#question-" + Watu.current_question).show();
99
100 jQuery("#show-answer").val(watu_i18n.show_answer);
101
102 if(!Watu.dontScroll && jQuery(document).scrollTop() > 250) {
103 jQuery('html, body').animate({
104 scrollTop: jQuery('#watu_quiz').offset().top -100
105 }, 100);
106 }
107 return;
108 }
109
110 Watu.mode = "next";
111
112 jQuery(".php-answer-label.label-"+Watu.current_question).addClass("correct-answer");
113 jQuery("input.answer-"+Watu.current_question).attr("disabled",true);
114 jQuery(".answer-"+Watu.current_question).each(function(i) {
115 if(this.checked && this.className.match(/js\-answer/)) {
116 var number = this.id.toString().replace(/\D/g,"");
117 if(number) {
118 jQuery("#answer-label-"+number).addClass("user-answer");
119 }
120 }
121 });
122
123 if(jQuery('#watuQuestionFeedback-' + Watu.current_question).length) {
124 jQuery('#watuQuestionFeedback-' + Watu.current_question).show();
125 }
126
127 if(Watu.total_questions <= Watu.current_question) {
128 jQuery("#show-answer").hide();
129 jQuery("#action-button").show();
130 } else {
131 jQuery("#show-answer").val(watu_i18n.next_q);
132 }
133
134 if(!Watu.dontScroll && jQuery(document).scrollTop() > 250) {
135 jQuery('html, body').animate({
136 scrollTop: jQuery('#watu_quiz').offset().top -100
137 }, 100);
138 }
139 }
140
141 Watu.submitResult = function(e) {
142 var answer_ids = [];
143 jQuery('#quiz-' + this.exam_id + ' .watu-answer-ids').each(function(index, value){
144 answer_ids.push(this.value);
145 });
146
147 // if text captcha is there we have to make sure it's shown
148 if(jQuery('#WatuTextCaptcha').length && !jQuery('#WatuTextCaptcha').is(':visible')) {
149 alert(watu_i18n.complete_text_captcha);
150 jQuery('#WatuTextCaptcha').show();
151 return false;
152 }
153
154 var data = {action:'watu_submit', 'do': 'show_exam_result', quiz_id: exam_id,
155 'question_id[]': Watu.qArr, 'answer_ids[]' : answer_ids,
156 'watu_nonce': (typeof watu_i18n !== 'undefined' && watu_i18n.watu_submit_nonce) ? watu_i18n.watu_submit_nonce : (jQuery('#quiz-' + this.exam_id + ' input[name=watu_nonce]').val() || jQuery('input[name=watu_nonce]').val()) };
157
158 if(jQuery('#watuTakerEmail').length) {
159 var emailVal = jQuery('#watuTakerEmail').val();
160 if(emailVal == '' || emailVal.indexOf('@') < 0 || emailVal.indexOf('.') < 1) {
161 alert(watu_i18n.email_required);
162 jQuery('#watuTakerEmail').focus();
163 return false;
164 }
165 data['watu_taker_email'] = emailVal;
166 }
167
168 for(x=0; x<Watu.qArr.length; x++) {
169 if(Watu.singlePage) {
170 Watu.current_question = x+1;
171
172 if(!Watu.isAnswered() && Watu.isRequired()) {
173 alert(watu_i18n.missed_required_question);
174 return false;
175 }
176 }
177
178 // qArr[x] is the question ID
179 var ansgroup = '.answerof-'+Watu.qArr[x];
180 var fieldName = 'answer-'+Watu.qArr[x];
181 var ansvalues= Array();
182 var i=0;
183
184 if(jQuery('#textarea_q_'+Watu.qArr[x]).length>0) {
185 // open end question
186 ansvalues[0]=jQuery('#textarea_q_'+Watu.qArr[x]).val();
187 }
188 else {
189 jQuery(ansgroup).each(function(){
190 if( jQuery(this).is(':checked') ) {
191 ansvalues[i] = this.value;
192 i++;
193 }
194 });
195 }
196
197 data[fieldName+'[]'] = ansvalues;
198 }
199
200 data['post_id'] = Watu.post_id;
201 data['start_time'] = jQuery('#watuStartTime').val();
202 data['h_app_id'] = Watu.hAppID;
203
204 // no ajax? In this case only return true to allow submitting the form
205 if(e && e.no_ajax && e.no_ajax.value == 1) return true;
206
207 // if question captcha is available, add to data
208 if(jQuery('#WatuTextCaptcha').length>0) {
209 jQuery('#quiz-'+Watu.exam_id).show();
210 data['watu_text_captcha_answer'] = jQuery('#quiz-' + Watu.exam_id + ' input[name=watu_text_captcha_answer]').val();
211 data['watu_text_captcha_question'] = jQuery('#quiz-' + Watu.exam_id + ' input[name=watu_text_captcha_question]').val();
212 }
213
214 // honeypot? show back form to wait for verification
215 if(jQuery('#watuAppID' + Watu.exam_id).length > 0) {
216 jQuery('#quiz-'+Watu.exam_id).show();
217 }
218
219 if(!Watu.dontScroll) {
220 jQuery('html, body').animate({
221 scrollTop: jQuery('#watu_quiz').offset().top - 50
222 }, 1000);
223 }
224
225 //jQuery('#watu_quiz').html("<p>Loading...</p>");
226 // change text and disable submit button
227 jQuery("#action-button").val(watu_i18n.please_wait);
228 jQuery("#action-button").attr("disabled", true);
229
230
231 //var v=''; for(a in data) v+=data[a]+'\n'; alert(v);
232 // don't do ajax call if no_ajax
233 if(!e || !e.no_ajax || e.no_ajax.value != 1) {
234 try{
235 // hide quiz, display loading
236 jQuery('#watu_quiz').hide();
237 jQuery('#watu-loading-result').show();
238 jQuery.ajax({ type: 'POST', url: watuURL, data: data, success: Watu.success, error: Watu.error });
239 } catch(e) { alert(e) }
240 }
241 }
242
243 Watu.takingDetails = function(id, adminURL) {
244 adminURL = adminURL || "";
245 tb_show("Taking Details", adminURL + "admin-ajax.php?action=watu_taking_details&id="+id, adminURL + "admin-ajax.php");
246 }
247
248 Watu.success = function(r){
249 // first check for recaptcha error, if yes, do not replace the HTML
250 // but display the error in alert and return false;
251 if(r.indexOf('WATU_CAPTCHA:::')>-1) {
252 parts = r.split(":::");
253 alert(parts[1]);
254 jQuery("#action-button").val(watu_i18n.try_again);
255 jQuery("#action-button").removeAttr("disabled");
256 jQuery('#watu_quiz').show();
257 jQuery('#watu-loading-result').hide();
258 return false;
259 }
260
261 // redirect?
262 if(r.indexOf('WATU_REDIRECT:::') > -1) {
263 parts = r.split(":::");
264 window.location = parts[1];
265 return true;
266 }
267
268 jQuery('#watu_quiz').html(r);
269
270 // parse mathjax
271 if (typeof MathJax != 'undefined') {
272 if(typeof MathJax.Hub != 'undefined') MathJax.Hub.Queue(["Typeset",MathJax.Hub,"watupro_quiz"]);
273 MathJax.typeset();
274 }
275
276 jQuery('#watu_quiz').show();
277 jQuery('#watu-loading-result').hide();
278 }
279 Watu.error = function(){ jQuery('#watu_quiz').html('Error Occured');}
280
281 Watu.FBShare = function(url, title, desc, picture) {
282 // compose quote
283 var quote = '';
284 quote = decodeURI(title) + "\n" + decodeURIComponent(desc);
285
286 FB.ui({
287 method: 'share',
288 href: url,
289 quote: quote,
290 }, function(response){});
291 }
292
293 Watu.initWatu = function() {
294 jQuery("#question-1").show();
295 Watu.total_questions = jQuery(".watu-question").length;
296
297 if(Watu.total_questions == 1) {
298 jQuery("#action-button").show();
299 jQuery("#next-question").hide();
300 jQuery("#show-answer").hide();
301
302 } else {
303 jQuery("#next-question").click(Watu.nextQuestion);
304 jQuery("#show-answer").click(Watu.showAnswer);
305 }
306 if(!Watu.singlePage) jQuery("#action-button").click(Watu.nextQuestion);
307
308 // give the honey if any
309 quizID = Watu.exam_id;
310 if(jQuery('#watuAppID' + quizID).length > 0) {
311 Watu.hAppID = '_' + jQuery('#watuAppSourceID' + quizID).val();
312 jQuery('#watuAppID' + quizID).val(Watu.hAppID);
313 }
314 }
315
316 jQuery(document).ready(Watu.initWatu);
317