| 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 |
|
| 157 |
if(jQuery('#watuTakerEmail').length) { |
| 158 |
var emailVal = jQuery('#watuTakerEmail').val(); |
| 159 |
if(emailVal == '' || emailVal.indexOf('@') < 0 || emailVal.indexOf('.') < 1) { |
| 160 |
alert(watu_i18n.email_required); |
| 161 |
jQuery('#watuTakerEmail').focus(); |
| 162 |
return false; |
| 163 |
} |
| 164 |
data['watu_taker_email'] = emailVal; |
| 165 |
} |
| 166 |
|
| 167 |
for(x=0; x<Watu.qArr.length; x++) { |
| 168 |
if(Watu.singlePage) { |
| 169 |
Watu.current_question = x+1; |
| 170 |
|
| 171 |
if(!Watu.isAnswered() && Watu.isRequired()) { |
| 172 |
alert(watu_i18n.missed_required_question); |
| 173 |
return false; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// qArr[x] is the question ID |
| 178 |
var ansgroup = '.answerof-'+Watu.qArr[x]; |
| 179 |
var fieldName = 'answer-'+Watu.qArr[x]; |
| 180 |
var ansvalues= Array(); |
| 181 |
var i=0; |
| 182 |
|
| 183 |
if(jQuery('#textarea_q_'+Watu.qArr[x]).length>0) { |
| 184 |
// open end question |
| 185 |
ansvalues[0]=jQuery('#textarea_q_'+Watu.qArr[x]).val(); |
| 186 |
} |
| 187 |
else { |
| 188 |
jQuery(ansgroup).each(function(){ |
| 189 |
if( jQuery(this).is(':checked') ) { |
| 190 |
ansvalues[i] = this.value; |
| 191 |
i++; |
| 192 |
} |
| 193 |
}); |
| 194 |
} |
| 195 |
|
| 196 |
data[fieldName+'[]'] = ansvalues; |
| 197 |
} |
| 198 |
|
| 199 |
data['post_id'] = Watu.post_id; |
| 200 |
data['start_time'] = jQuery('#watuStartTime').val(); |
| 201 |
data['h_app_id'] = Watu.hAppID; |
| 202 |
|
| 203 |
// no ajax? In this case only return true to allow submitting the form |
| 204 |
if(e && e.no_ajax && e.no_ajax.value == 1) return true; |
| 205 |
|
| 206 |
// if question captcha is available, add to data |
| 207 |
if(jQuery('#WatuTextCaptcha').length>0) { |
| 208 |
jQuery('#quiz-'+Watu.exam_id).show(); |
| 209 |
data['watu_text_captcha_answer'] = jQuery('#quiz-' + Watu.exam_id + ' input[name=watu_text_captcha_answer]').val(); |
| 210 |
data['watu_text_captcha_question'] = jQuery('#quiz-' + Watu.exam_id + ' input[name=watu_text_captcha_question]').val(); |
| 211 |
} |
| 212 |
|
| 213 |
// honeypot? show back form to wait for verification |
| 214 |
if(jQuery('#watuAppID' + Watu.exam_id).length > 0) { |
| 215 |
jQuery('#quiz-'+Watu.exam_id).show(); |
| 216 |
} |
| 217 |
|
| 218 |
if(!Watu.dontScroll) { |
| 219 |
jQuery('html, body').animate({ |
| 220 |
scrollTop: jQuery('#watu_quiz').offset().top - 50 |
| 221 |
}, 1000); |
| 222 |
} |
| 223 |
|
| 224 |
//jQuery('#watu_quiz').html("<p>Loading...</p>"); |
| 225 |
// change text and disable submit button |
| 226 |
jQuery("#action-button").val(watu_i18n.please_wait); |
| 227 |
jQuery("#action-button").attr("disabled", true); |
| 228 |
|
| 229 |
|
| 230 |
//var v=''; for(a in data) v+=data[a]+'\n'; alert(v); |
| 231 |
// don't do ajax call if no_ajax |
| 232 |
if(!e || !e.no_ajax || e.no_ajax.value != 1) { |
| 233 |
try{ |
| 234 |
// hide quiz, display loading |
| 235 |
jQuery('#watu_quiz').hide(); |
| 236 |
jQuery('#watu-loading-result').show(); |
| 237 |
jQuery.ajax({ type: 'POST', url: watuURL, data: data, success: Watu.success, error: Watu.error }); |
| 238 |
} catch(e) { alert(e) } |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
Watu.takingDetails = function(id, adminURL) { |
| 243 |
adminURL = adminURL || ""; |
| 244 |
tb_show("Taking Details", adminURL + "admin-ajax.php?action=watu_taking_details&id="+id, adminURL + "admin-ajax.php"); |
| 245 |
} |
| 246 |
|
| 247 |
Watu.success = function(r){ |
| 248 |
// first check for recaptcha error, if yes, do not replace the HTML |
| 249 |
// but display the error in alert and return false; |
| 250 |
if(r.indexOf('WATU_CAPTCHA:::')>-1) { |
| 251 |
parts = r.split(":::"); |
| 252 |
alert(parts[1]); |
| 253 |
jQuery("#action-button").val(watu_i18n.try_again); |
| 254 |
jQuery("#action-button").removeAttr("disabled"); |
| 255 |
jQuery('#watu_quiz').show(); |
| 256 |
jQuery('#watu-loading-result').hide(); |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
// redirect? |
| 261 |
if(r.indexOf('WATU_REDIRECT:::') > -1) { |
| 262 |
parts = r.split(":::"); |
| 263 |
window.location = parts[1]; |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
jQuery('#watu_quiz').html(r); |
| 268 |
|
| 269 |
// parse mathjax |
| 270 |
if (typeof MathJax != 'undefined') { |
| 271 |
if(typeof MathJax.Hub != 'undefined') MathJax.Hub.Queue(["Typeset",MathJax.Hub,"watupro_quiz"]); |
| 272 |
MathJax.typeset(); |
| 273 |
} |
| 274 |
|
| 275 |
jQuery('#watu_quiz').show(); |
| 276 |
jQuery('#watu-loading-result').hide(); |
| 277 |
} |
| 278 |
Watu.error = function(){ jQuery('#watu_quiz').html('Error Occured');} |
| 279 |
|
| 280 |
Watu.FBShare = function(url, title, desc, picture) { |
| 281 |
// compose quote |
| 282 |
var quote = ''; |
| 283 |
quote = decodeURI(title) + "\n" + decodeURIComponent(desc); |
| 284 |
|
| 285 |
FB.ui({ |
| 286 |
method: 'share', |
| 287 |
href: url, |
| 288 |
quote: quote, |
| 289 |
}, function(response){}); |
| 290 |
} |
| 291 |
|
| 292 |
Watu.initWatu = function() { |
| 293 |
jQuery("#question-1").show(); |
| 294 |
Watu.total_questions = jQuery(".watu-question").length; |
| 295 |
|
| 296 |
if(Watu.total_questions == 1) { |
| 297 |
jQuery("#action-button").show(); |
| 298 |
jQuery("#next-question").hide(); |
| 299 |
jQuery("#show-answer").hide(); |
| 300 |
|
| 301 |
} else { |
| 302 |
jQuery("#next-question").click(Watu.nextQuestion); |
| 303 |
jQuery("#show-answer").click(Watu.showAnswer); |
| 304 |
} |
| 305 |
if(!Watu.singlePage) jQuery("#action-button").click(Watu.nextQuestion); |
| 306 |
|
| 307 |
// give the honey if any |
| 308 |
quizID = Watu.exam_id; |
| 309 |
if(jQuery('#watuAppID' + quizID).length > 0) { |
| 310 |
Watu.hAppID = '_' + jQuery('#watuAppSourceID' + quizID).val(); |
| 311 |
jQuery('#watuAppID' + quizID).val(Watu.hAppID); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
jQuery(document).ready(Watu.initWatu); |
| 316 |
|