| 1 |
--- |
| 2 |
texmf-dist/doc/latex/webquiz/www/js/webquiz.js | 428 ++++++++++++++++++++++++- |
| 3 |
1 file changed, 427 insertions(+), 1 deletion(-) |
| 4 |
|
| 5 |
--- texlive-extra-2019.20190506.orig/texmf-dist/doc/latex/webquiz/www/js/webquiz.js |
| 6 |
+++ texlive-extra-2019.20190506/texmf-dist/doc/latex/webquiz/www/js/webquiz.js |
| 7 |
@@ -1 +1,427 @@ |
| 8 |
-var currentB,currentQ,dTotal,quizindex_menu,qTotal,side_closed,side_menu,side_open,correct=[],buttonOrder=[],questionOrder=[],wrongAnswers=[],currentFeedback=null,QuizSpecifications=[],Discussion=[],onePage=!1,QuizTitles=[],blank={content:"",name:"blank"},cross={content:"✘",name:"cross"},star={content:"✭",name:"star"},tick={content:"✔",name:"tick"};function markAnswer(){"undefined"!=typeof Storage&&(sessionStorage.correct=JSON.stringify(correct),sessionStorage.wrongAnswers=JSON.stringify(wrongAnswers))}var quizindex_menu_not_created=!0;function create_quizindex_menu(){if(quizindex_menu_not_created){document.getElementById("quizzes-menu-icon").innerHTML=" ☰";var q,quiz_link,max=0,menu=document.createDocumentFragment();for(q=0;q<QuizTitles.length;q++)(quiz_link=document.createElement("li")).innerHTML='<a href="'+QuizTitles[q][1]+'">'+QuizTitles[q][0]+"</a>",menu.appendChild(quiz_link),max=Math.max(max,QuizTitles[q][0].length);quizindex_menu.style.width=Math.round(max)+"ex",quizindex_menu.appendChild(menu),quizindex_menu_not_created=!1}}function MenuEventListener(evnt){var menu_icon=document.getElementById("quzzes-menu-icon");quizindex_menu.contains(evnt.target)||("block"===quizindex_menu.style.display||menu_icon.contains(evnt.target))&&(evnt.stopPropagation(),toggle_quizindex_menu())}function toggle_quizindex_menu(){"block"===quizindex_menu.style.display?quizindex_menu.style.display="none":(quizindex_menu.style.display="block",window.addEventListener("click",MenuEventListener,!0))}function toggle_side_menu(){"block"===side_menu.style.display||""===side_menu.style.display?(side_menu.style.display="none",side_open.style.display="none",side_closed.style.display="block"):(side_menu.style.display="block",side_open.style.display="block",side_closed.style.display="none")}function showQuestion(newB,newQ){onePage||(newQ!=currentQ&&0!=currentQ&&(hideFeedback(),document.getElementById("question"+currentQ).style.display="none",currentB.classList.remove("button-selected")),document.getElementById("question"+newQ).style.display="table",document.getElementById("question-number").innerHTML=0<newQ?(document.getElementById("question-label").style.display="contents",String(newB)):(document.getElementById("question-label").style.display="none",Discussion[-newQ]),(currentB=document.getElementById("button"+newB)).classList.add("button-selected")),currentQ=newQ}function hideFeedback(){currentFeedback&&(currentFeedback.style.display="none")}function showFeedback(tag){hideFeedback(),(currentFeedback=document.getElementById(tag)).style.display="block"}function nextQuestion(increment){if(currentQ<0)gotoQuestion(1===increment?1:qTotal);else{for(var q,b=buttonOrder[currentQ];0===(b+=increment)?b=qTotal:qTotal<b&&(b=1),(q=questionOrder[b])!==currentQ&&correct[q];);b===currentB?alert("There are no more unanswered questions"):gotoQuestion(b)}}var buttons=["blank","cross","star","tick"];function updateQuestionMarker(bnum,qnum){if(0<qnum){var marker,button=document.getElementById("button"+bnum);marker=correct[qnum]?0===wrongAnswers[qnum]?star:tick:0<wrongAnswers[qnum]?cross:blank;for(var b=0;b<buttons.length;b++)button.classList.remove(buttons[b]);button.classList.add(marker.name),button.setAttribute("content",marker.content)}}function gotoQuestion(bnum){var qnum=0<bnum?questionOrder[bnum]:bnum;gotoQuestionHelper(qnum),history.pushState(qnum,null,null)}function gotoQuestionHelper(qnum){var bnum=0<qnum?buttonOrder[qnum]:qnum;updateQuestionMarker(bnum,qnum),showQuestion(bnum,qnum)}var compare={complex:function(ans,val){var a=math.complex(ans),b=math.complex(val);return a.re==b.re&&a.im==b.im},integer:function(ans,val){return parseInt(val)==parseInt(ans)},lowercase:function(ans,val){return val==String(ans).toLowerCase()},number:function(ans,val){return math.eval(val)==math.eval(ans)},string:function(ans,val){return val==String(ans)}};function checkAnswer(qnum){var i,question=QuizSpecifications[qnum],studentAnswer=document.forms["Q"+qnum+"Form"];if("input"==question.type){var answer=studentAnswer.elements[0].value;if(""==answer)return void alert("Please answer the question first!");try{correct[qnum]=compare[question.comparison](answer,question.value)}catch(err){correct[qnum]=False}correct[qnum]?showFeedback("q"+qnum+"true"):showFeedback("q"+qnum+"false")}else if("single"==question.type){var checkedAnswer=0;for(i=0;i<question.length;i++)if(studentAnswer.elements[i].checked){correct[qnum]=question[i],checkedAnswer=i+1;break}showFeedback("q"+qnum+"feedback"+checkedAnswer)}else{var badAnswers=[];for(i=0;i<question.length;i++)if(studentAnswer.elements[i].checked!==question[i]){badAnswers.push(i+1);break}0===badAnswers.length?(correct[qnum]=!0,showFeedback("q"+qnum+"feedback0")):(correct[qnum]=!1,showFeedback("q"+qnum+"feedback"+badAnswers[Math.floor(Math.random()*badAnswers.length)]))}correct[qnum]||(wrongAnswers[qnum]+=1),updateQuestionMarker(buttonOrder[qnum],qnum),markAnswer()}function shuffleQuestions(){var i,j,qi;for(i=questionOrder.length-1;0<i;i--)j=1+Math.floor(Math.random()*i),qi=questionOrder[i],questionOrder[i]=questionOrder[j],questionOrder[j]=qi;for(i=buttonOrder.length-1;0<i;i--)buttonOrder[questionOrder[i]]=i}function initSession(){for("undefined"!=typeof Storage&&(sessionStorage.correct&&(correct=JSON.parse(sessionStorage.correct)),sessionStorage.wrongAnswers&&(wrongAnswers=JSON.parse(sessionStorage.wrongAnswers))),i=0;i<qTotal+1;i++)updateQuestionMarker(buttonOrder[i],i);qnum=0<dTotal?-1:questionOrder[1],history.replaceState(qnum,null,null)}function WebQuizInit(questions,discussions,quizfile){window.addEventListener("popstate",function(e){gotoQuestionHelper(e.state)}),dTotal=discussions,0==(qTotal=questions)&&(document.getElementsByClassName("arrows")[0].style.display="none");var i;currentQ=currentB=0;for(i=0;i<qTotal+1;i++)wrongAnswers[i]=0,correct[i]=!1,questionOrder[i]=i,buttonOrder[i]=i;var script=document.createElement("script");script.src=quizfile+"/wq-"+quizfile+".js",script.type="text/javascript",document.head.appendChild(script),side_menu=document.getElementById("sidemenu"),side_open=document.getElementById("sidelabelopen"),side_closed=document.getElementById("sidelabelclosed"),quizindex_menu=document.getElementById("quizindex-menu"),0<QuizTitles.length&&quizindex_menu&&create_quizindex_menu()} |
| 9 |
\ No newline at end of file |
| 10 |
+/* ----------------------------------------------------------------------- |
| 11 |
+ * webquiz.js | javascript for controlling webquiz web pages |
| 12 |
+ * ----------------------------------------------------------------------- |
| 13 |
+ * |
| 14 |
+ * Copyright (C) Andrew Mathas and Donald Taylor, University of Sydney |
| 15 |
+ * |
| 16 |
+ * Distributed under the terms of the GNU General Public License (GPL) |
| 17 |
+ * http://www.gnu.org/licenses/ |
| 18 |
+ * |
| 19 |
+ * This file is part of the WebQuiz system. |
| 20 |
+ * |
| 21 |
+ * <[email protected]> |
| 22 |
+ * <[email protected]> |
| 23 |
+ * ---------------------------------------------------------------------- |
| 24 |
+ */ |
| 25 |
+ |
| 26 |
+// Global variables |
| 27 |
+var correct = []; // questions answered correctly |
| 28 |
+var buttonOrder = []; // map from button number to question number |
| 29 |
+var questionOrder = []; // map from question number to button number |
| 30 |
+var wrongAnswers = []; // questions answered incorrectly |
| 31 |
+ |
| 32 |
+ |
| 33 |
+var currentB; // current button number |
| 34 |
+var currentFeedback = null; // feedback currently being displayed |
| 35 |
+var currentQ; // current question number |
| 36 |
+var dTotal; // number of discussion items |
| 37 |
+var quizindex_menu; // handler for the drop-down menu, if it exists |
| 38 |
+var qTotal; // number of quiz questions |
| 39 |
+var side_closed; // handler for displaying sidelabelclosed |
| 40 |
+var side_menu; // handler to open and close side |
| 41 |
+var side_open; // handler for displaying sidelabelopen |
| 42 |
+//var theme_menu // handler for the theme_menu |
| 43 |
+ |
| 44 |
+// The following variables are redefined in the specifications file |
| 45 |
+var QuizSpecifications = []; |
| 46 |
+var Discussion = []; // Headings of discussion environments |
| 47 |
+var onePage = false; |
| 48 |
+ |
| 49 |
+// Defined and read from in quizindex.js if it exists |
| 50 |
+var QuizTitles = []; // Quiz titles from a quizindex environment |
| 51 |
+ |
| 52 |
+// Specification for the question buttons for use in updateQuestionMarker |
| 53 |
+var blank = { |
| 54 |
+ "content": "", |
| 55 |
+ "name": "blank" |
| 56 |
+}; |
| 57 |
+var cross = { |
| 58 |
+ "content": "\u2718", |
| 59 |
+ "name": "cross" |
| 60 |
+}; |
| 61 |
+var star = { |
| 62 |
+ "content": "\u272D", |
| 63 |
+ "name": "star" |
| 64 |
+}; |
| 65 |
+var tick = { |
| 66 |
+ "content": "\u2714", |
| 67 |
+ "name": "tick" |
| 68 |
+}; |
| 69 |
+ |
| 70 |
+function markAnswer() { |
| 71 |
+ if (typeof(Storage) !== "undefined") { |
| 72 |
+ sessionStorage.correct = JSON.stringify(correct); |
| 73 |
+ sessionStorage.wrongAnswers = JSON.stringify(wrongAnswers); |
| 74 |
+ } |
| 75 |
+} |
| 76 |
+ |
| 77 |
+// stop the dropdown menu from being created twice |
| 78 |
+var quizindex_menu_not_created = true; |
| 79 |
+ |
| 80 |
+// create the drop down menu dynamically using the QuizTitles array |
| 81 |
+function create_quizindex_menu() { |
| 82 |
+ if (quizindex_menu_not_created) { |
| 83 |
+ // add the menu icon for the quizzes menu - only called if there is at least one quiz |
| 84 |
+ document.getElementById("quizzes-menu-icon").innerHTML = " ☰"; |
| 85 |
+ |
| 86 |
+ var max = 0, q, quiz_link, menu = document.createDocumentFragment(); |
| 87 |
+ for (q = 0; q < QuizTitles.length; q++) { |
| 88 |
+ quiz_link = document.createElement("li"); |
| 89 |
+ quiz_link.innerHTML = '<a href="' + QuizTitles[q][1] + '">' + QuizTitles[q][0] + '</a>'; |
| 90 |
+ menu.appendChild(quiz_link); |
| 91 |
+ max = Math.max(max, QuizTitles[q][0].length); |
| 92 |
+ } |
| 93 |
+ quizindex_menu.style.width = Math.round(max) + "ex"; |
| 94 |
+ quizindex_menu.appendChild(menu); |
| 95 |
+ quizindex_menu_not_created = false; |
| 96 |
+ } |
| 97 |
+} |
| 98 |
+ |
| 99 |
+// create an event listener so that we can close the drop-down menu |
| 100 |
+// whenever some one clicks outside of it |
| 101 |
+function MenuEventListener(evnt) { |
| 102 |
+ var menu_icon = document.getElementById('quzzes-menu-icon'); |
| 103 |
+ if (quizindex_menu.contains(evnt.target)) { |
| 104 |
+ return; // inside the menu so just return |
| 105 |
+ } else { // outside the menu so check the number of menu_clicks |
| 106 |
+ if (quizindex_menu.style.display === 'block' || menu_icon.contains(evnt.target)) { |
| 107 |
+ evnt.stopPropagation(); |
| 108 |
+ toggle_quizindex_menu(); |
| 109 |
+ } |
| 110 |
+ //if (theme_menu.style.display === 'block' || menu_icon.contains(evnt.target)) { |
| 111 |
+ // evnt.stopPropagation(); |
| 112 |
+ // toggle_quizindex_menu(); |
| 113 |
+ //} |
| 114 |
+ } |
| 115 |
+} |
| 116 |
+ |
| 117 |
+function toggle_quizindex_menu() { |
| 118 |
+ if (quizindex_menu.style.display === 'block') { |
| 119 |
+ quizindex_menu.style.display = 'none'; |
| 120 |
+ } else { |
| 121 |
+ quizindex_menu.style.display = 'block'; |
| 122 |
+ window.addEventListener('click', MenuEventListener, true); |
| 123 |
+ } |
| 124 |
+} |
| 125 |
+ |
| 126 |
+// function toggle_theme_menu() {// unused |
| 127 |
+// if (theme_menu.style.display === 'block') { |
| 128 |
+// theme_menu.style.display = 'none'; |
| 129 |
+// } else { |
| 130 |
+// theme_menu.style.display = 'block'; |
| 131 |
+// window.addEventListener('click', MenuEventListener, true); |
| 132 |
+// } |
| 133 |
+// } |
| 134 |
+ |
| 135 |
+// toggle the display of the side menu and its many associated labels |
| 136 |
+function toggle_side_menu() { |
| 137 |
+ if (side_menu.style.display === "block" || side_menu.style.display === "") { |
| 138 |
+ side_menu.style.display = "none"; |
| 139 |
+ side_open.style.display = "none"; |
| 140 |
+ side_closed.style.display = "block"; |
| 141 |
+ } else { |
| 142 |
+ side_menu.style.display = "block"; |
| 143 |
+ side_open.style.display = "block"; |
| 144 |
+ side_closed.style.display = "none"; |
| 145 |
+ } |
| 146 |
+} |
| 147 |
+ |
| 148 |
+// Code to hide/show questions |
| 149 |
+function showQuestion(newB, newQ) { // newQ is an integer which is always in the correct range |
| 150 |
+ // alert('showing newB='+newB+', newQ='+newQ+', currentQ='+currentQ+'.'); |
| 151 |
+ if (!onePage) { |
| 152 |
+ // hide the current question and feedback |
| 153 |
+ if (newQ!=currentQ && currentQ!=0) { |
| 154 |
+ hideFeedback(); |
| 155 |
+ document.getElementById("question" + currentQ).style.display = "none"; |
| 156 |
+ // "de-select" the current button |
| 157 |
+ currentB.classList.remove("button-selected"); |
| 158 |
+ } |
| 159 |
+ // display the new question |
| 160 |
+ document.getElementById("question" + newQ).style.display = "table"; |
| 161 |
+ |
| 162 |
+ // update the question/discussion header and select question button |
| 163 |
+ if (newQ > 0) { |
| 164 |
+ document.getElementById("question-label").style.display = 'contents'; |
| 165 |
+ document.getElementById("question-number").innerHTML = String(newB); |
| 166 |
+ } else { |
| 167 |
+ document.getElementById("question-label").style.display = 'none'; |
| 168 |
+ document.getElementById("question-number").innerHTML = Discussion[-newQ]; |
| 169 |
+ } |
| 170 |
+ // set currentB = the current button and "select" the current button |
| 171 |
+ currentB = document.getElementById("button" + newB); |
| 172 |
+ currentB.classList.add("button-selected"); |
| 173 |
+ } |
| 174 |
+ |
| 175 |
+ // finally set currentQ = current question |
| 176 |
+ currentQ = newQ; |
| 177 |
+} |
| 178 |
+ |
| 179 |
+// Code to hide/show feedback |
| 180 |
+ |
| 181 |
+function hideFeedback() { |
| 182 |
+ if (currentFeedback) { |
| 183 |
+ currentFeedback.style.display = "none"; |
| 184 |
+ } |
| 185 |
+} |
| 186 |
+ |
| 187 |
+function showFeedback(tag) { |
| 188 |
+ // alert('Showing feedback for '+tag+'.'); |
| 189 |
+ hideFeedback(); // hide current feedback |
| 190 |
+ currentFeedback = document.getElementById(tag); |
| 191 |
+ currentFeedback.style.display = "block"; |
| 192 |
+} |
| 193 |
+ |
| 194 |
+// if increment==1 we find the next questions which has not |
| 195 |
+// been answered incorrectly and if increment==-1 we find the last |
| 196 |
+// such question |
| 197 |
+function nextQuestion(increment) { |
| 198 |
+ if (currentQ < 0) { // a discussion item => go to either first or last question |
| 199 |
+ if (increment === 1) { |
| 200 |
+ gotoQuestion(1); |
| 201 |
+ } else { |
| 202 |
+ gotoQuestion(qTotal); |
| 203 |
+ } |
| 204 |
+ } else { |
| 205 |
+ var b = buttonOrder[currentQ], q; |
| 206 |
+ do { |
| 207 |
+ b += increment; |
| 208 |
+ if (b === 0) { |
| 209 |
+ b = qTotal; |
| 210 |
+ } else if (b > qTotal) { |
| 211 |
+ b = 1; |
| 212 |
+ } |
| 213 |
+ q = questionOrder[b]; |
| 214 |
+ } while (q !== currentQ && correct[q]); |
| 215 |
+ if (b === currentB) { |
| 216 |
+ alert("There are no more unanswered questions"); |
| 217 |
+ } else { |
| 218 |
+ gotoQuestion(b); |
| 219 |
+ } |
| 220 |
+ } |
| 221 |
+} |
| 222 |
+ |
| 223 |
+ |
| 224 |
+var buttons = ['blank', 'cross', 'star', 'tick']; |
| 225 |
+function updateQuestionMarker(bnum, qnum) { |
| 226 |
+ // alert('updating bnum='+bnum+', qnum='+qnum+', currentQ='+currentQ+'.'); |
| 227 |
+ // here qnum is assumed to be the question number in the web form |
| 228 |
+ if (qnum > 0) { |
| 229 |
+ var marker; |
| 230 |
+ var button = document.getElementById('button'+bnum); |
| 231 |
+ if (correct[qnum]) { |
| 232 |
+ if (wrongAnswers[qnum] === 0) { |
| 233 |
+ marker = star; |
| 234 |
+ } else { |
| 235 |
+ marker = tick; |
| 236 |
+ } |
| 237 |
+ } else { |
| 238 |
+ if (wrongAnswers[qnum] > 0) { |
| 239 |
+ marker = cross; |
| 240 |
+ } else { |
| 241 |
+ marker = blank; |
| 242 |
+ } |
| 243 |
+ } |
| 244 |
+ for (var b = 0; b < buttons.length; b++) { |
| 245 |
+ button.classList.remove(buttons[b]); |
| 246 |
+ } |
| 247 |
+ button.classList.add(marker.name); |
| 248 |
+ button.setAttribute("content", marker.content); |
| 249 |
+ } |
| 250 |
+} |
| 251 |
+ |
| 252 |
+// jumps to a chosen question and pushes the number of this question to the browser history |
| 253 |
+function gotoQuestion(bnum) { |
| 254 |
+ // bnum is a button number so we need to convert to a question number |
| 255 |
+ |
| 256 |
+ var qnum = (bnum>0) ? questionOrder[bnum] : bnum; |
| 257 |
+ gotoQuestionHelper(qnum); |
| 258 |
+ history.pushState(qnum, null, null); |
| 259 |
+} |
| 260 |
+ |
| 261 |
+// jumps to the specified question (without pushing it to the browser history) |
| 262 |
+function gotoQuestionHelper(qnum) { |
| 263 |
+ var bnum = (qnum>0) ? buttonOrder[qnum] : qnum; |
| 264 |
+ updateQuestionMarker(bnum, qnum); |
| 265 |
+ showQuestion(bnum, qnum); |
| 266 |
+} |
| 267 |
+ |
| 268 |
+// dictionary of comparison methods for when question.type=='input' |
| 269 |
+// each function in the dictionary returns true or false |
| 270 |
+var compare = { |
| 271 |
+ 'complex': function(ans, val) {// check real and imaginary parts |
| 272 |
+ var a = math.complex(ans); |
| 273 |
+ var b = math.complex(val); |
| 274 |
+ return a.re==b.re && a.im==b.im; |
| 275 |
+ }, |
| 276 |
+ 'integer': function(ans, val) {// compare as integers |
| 277 |
+ return parseInt(val)==parseInt(ans); |
| 278 |
+ }, |
| 279 |
+ 'lowercase': function(ans, val) {//convert to lowercase string and compare |
| 280 |
+ return val==String(ans).toLowerCase(); |
| 281 |
+ }, |
| 282 |
+ 'number': function(ans, val) {// compare as numbers |
| 283 |
+ return math.eval(val)==math.eval(ans); |
| 284 |
+ }, |
| 285 |
+ 'string': function(ans, val) {// compare as strings |
| 286 |
+ return val==String(ans); |
| 287 |
+ } |
| 288 |
+}; |
| 289 |
+ |
| 290 |
+// check to see whether the answer is correct and update the markers accordingly |
| 291 |
+function checkAnswer(qnum) { |
| 292 |
+ // alert('checking qnum='+qnum+'.'); |
| 293 |
+ var question = QuizSpecifications[qnum]; |
| 294 |
+ var studentAnswer = document.forms["Q" + qnum + "Form"]; |
| 295 |
+ var i; |
| 296 |
+ if (question.type == "input") { |
| 297 |
+ var answer = studentAnswer.elements[0].value; |
| 298 |
+ if (answer=='') { //must have hit checkAnswer without answering, so ignore |
| 299 |
+ alert('Please answer the question first!'); |
| 300 |
+ return; |
| 301 |
+ } |
| 302 |
+ try { |
| 303 |
+ correct[qnum] = compare[question.comparison](answer, question.value); |
| 304 |
+ } catch(err) { |
| 305 |
+ correct[qnum] = False; |
| 306 |
+ } |
| 307 |
+ |
| 308 |
+ if (correct[qnum]) { |
| 309 |
+ showFeedback("q" + qnum + "true"); |
| 310 |
+ } else { |
| 311 |
+ showFeedback("q" + qnum + "false"); |
| 312 |
+ } |
| 313 |
+ } else if (question.type == "single") { |
| 314 |
+ var checkedAnswer = 0; |
| 315 |
+ for (i = 0; i < question.length; i++) { |
| 316 |
+ if (studentAnswer.elements[i].checked) { |
| 317 |
+ correct[qnum] = question[i]; |
| 318 |
+ checkedAnswer = i + 1; |
| 319 |
+ break; |
| 320 |
+ } |
| 321 |
+ } |
| 322 |
+ showFeedback("q" + qnum + "feedback" + checkedAnswer); |
| 323 |
+ } else { // type is "multiple" |
| 324 |
+ var badAnswers = []; |
| 325 |
+ for (i = 0; i < question.length; i++) { |
| 326 |
+ if (studentAnswer.elements[i].checked !== question[i]) { |
| 327 |
+ badAnswers.push(i + 1); |
| 328 |
+ break; |
| 329 |
+ } |
| 330 |
+ } |
| 331 |
+ // fully correct only if badAnswers == [] |
| 332 |
+ if (badAnswers.length === 0) { |
| 333 |
+ correct[qnum] = true; |
| 334 |
+ showFeedback("q" + qnum + "feedback0"); |
| 335 |
+ } else { |
| 336 |
+ // randomly display a feedback for one of incorrect choices |
| 337 |
+ correct[qnum] = false; |
| 338 |
+ showFeedback("q" + qnum + "feedback" + badAnswers[Math.floor(Math.random() * badAnswers.length)]); |
| 339 |
+ } |
| 340 |
+ } |
| 341 |
+ // |
| 342 |
+ if (!correct[qnum]) { |
| 343 |
+ wrongAnswers[qnum] += 1; |
| 344 |
+ } |
| 345 |
+ updateQuestionMarker(buttonOrder[qnum], qnum); |
| 346 |
+ markAnswer(); |
| 347 |
+} |
| 348 |
+ |
| 349 |
+/** |
| 350 |
+ * Shuffle the questionOrder array and make buttonOrder its inverse |
| 351 |
+ * Based on https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array |
| 352 |
+ */ |
| 353 |
+function shuffleQuestions() { |
| 354 |
+ var i, j, qi; |
| 355 |
+ for (i = questionOrder.length-1; i > 0; i--) { |
| 356 |
+ j = 1+Math.floor(Math.random() * i); |
| 357 |
+ qi = questionOrder[i]; |
| 358 |
+ questionOrder[i]=questionOrder[j]; |
| 359 |
+ questionOrder[j]=qi; |
| 360 |
+ } |
| 361 |
+ // ...and compute the inverse map |
| 362 |
+ for (i = buttonOrder.length - 1; i > 0; i--) { |
| 363 |
+ buttonOrder[questionOrder[i]] = i; |
| 364 |
+ } |
| 365 |
+} |
| 366 |
+ |
| 367 |
+// Restores the state of the question markers from the session storage |
| 368 |
+function initSession() { |
| 369 |
+ if (typeof(Storage) !== "undefined") { |
| 370 |
+ if (sessionStorage.correct) |
| 371 |
+ correct = JSON.parse(sessionStorage.correct); |
| 372 |
+ if (sessionStorage.wrongAnswers) |
| 373 |
+ wrongAnswers = JSON.parse(sessionStorage.wrongAnswers); |
| 374 |
+ } |
| 375 |
+ |
| 376 |
+ for (i = 0; i < qTotal+1; i++) { |
| 377 |
+ updateQuestionMarker(buttonOrder[i],i); |
| 378 |
+ } |
| 379 |
+ |
| 380 |
+ // make the browser history remember the first question |
| 381 |
+ qnum = (dTotal > 0) ? -1 : questionOrder[1]; |
| 382 |
+ history.replaceState(qnum, null, null); |
| 383 |
+} |
| 384 |
+ |
| 385 |
+// initialise the quiz, loading specifications and setting up the first question |
| 386 |
+function WebQuizInit(questions, discussions, quizfile) { |
| 387 |
+ // process init options |
| 388 |
+ |
| 389 |
+ // callback for browser history events |
| 390 |
+ window.addEventListener('popstate', function(e) { |
| 391 |
+ gotoQuestionHelper(e.state); |
| 392 |
+ }); |
| 393 |
+ |
| 394 |
+ qTotal = questions; |
| 395 |
+ dTotal = discussions; |
| 396 |
+ |
| 397 |
+ // remove question arrows when there are no questions |
| 398 |
+ if (qTotal==0) { |
| 399 |
+ document.getElementsByClassName('arrows')[0].style.display='none' |
| 400 |
+ } |
| 401 |
+ |
| 402 |
+ // display the first question or discussion item |
| 403 |
+ currentB = 0; |
| 404 |
+ currentQ = 0; |
| 405 |
+ var newQ = (dTotal > 0) ? -1 : 1; |
| 406 |
+ |
| 407 |
+ // set up arrays for tracking how many times the questions have been attempted |
| 408 |
+ var i; |
| 409 |
+ for (i = 0; i < qTotal+1; i++) { |
| 410 |
+ wrongAnswers[i] = 0; // the number of times the question has been attempted |
| 411 |
+ correct[i] = false; // whether or not the supplied answer is correct |
| 412 |
+ questionOrder[i] = i; // will determine the order of the questions |
| 413 |
+ buttonOrder[i] = i; // will determine the order of the buttons |
| 414 |
+ } |
| 415 |
+ |
| 416 |
+ // read the question specifications for the quiz |
| 417 |
+ // and then wait for the QuizSpecifications to load |
| 418 |
+ var script = document.createElement('script'); |
| 419 |
+ script.src = quizfile + "/wq-" + quizfile + ".js"; |
| 420 |
+ script.type = "text/javascript"; |
| 421 |
+ document.head.appendChild(script); |
| 422 |
+ |
| 423 |
+ // compute these only once |
| 424 |
+ side_menu = document.getElementById('sidemenu'); |
| 425 |
+ side_open = document.getElementById('sidelabelopen'); |
| 426 |
+ side_closed = document.getElementById('sidelabelclosed'); |
| 427 |
+ quizindex_menu = document.getElementById("quizindex-menu"); |
| 428 |
+ //theme_menu = document.getElementById('theme-menu'); |
| 429 |
+ |
| 430 |
+ // make the drop down menu if QuizTitles has some entries |
| 431 |
+ if (QuizTitles.length > 0 && quizindex_menu) { |
| 432 |
+ create_quizindex_menu(); |
| 433 |
+ } |
| 434 |
+} |
| 435 |
+ |
| 436 |
+ |