var questions = questions || null; //Array.prototype.shuffle = function(local){ // var a = this; // var newArray = typeof local === "boolean" && local ? this : []; // for (var i = 0, newIdx, curr, next; i < a.length; i++){ // newIdx = Math.floor(Math.random()*i); // curr = a[i]; // next = a[newIdx]; // newArray[i] = next; // newArray[newIdx] = curr; // } // // return newArray; //}; function shuffleArray(d) { for (var c = d.length - 1; c > 0; c--) { var b = Math.floor(Math.random() * (c + 1)); var a = d[c]; d[c] = d[b]; d[b] = a; } return d }; function initPage(evt) { if (questions != null ) { initQuestions(); } initPopups(); } function initQuestions() { for (var key in questions) { var container = document.getElementById(key); var question = questions[key]; if (!container || !question ) continue; initQuestion(question); var form = getNextVariants(question); container.querySelector(".content").innerHTML = form; } } function initQuestion(question) { if (question.variants) question.variants = shuffleArray(question.variants); question.position = -1; } function getNextVariants(question) { question.correct = []; question.correctAnswers = []; var out = []; for (var i = 0; i < question.count; i++) { var index = -1; var variant = ""; if (question.variants) { index = (++question.position % question.variants.length); variant = question.variants[index]; } if (question.type == "v" ) { var elems = variant.match(/\{([^}]+)\}/g); for (var j = 0; j < elems.length; j++) { var elem = elems[j] var vals = elem.substring(1, elem.length-1).trim().split(/\s*[,\|]\s*/); var text = ""; variant = variant.replace(elem, text); } if (variant.indexOf("|") > 0) { var vals2 = variant.split("|"); question.feedback = vals2[1]; variant = vals2[0]; } out.push("
"); } if (question.type == "vf" ) { // roletka s feedbackem var elems = variant.match(/\{([^}]+)\}/g); for (var j = 0; j < elems.length; j++) { var elem = elems[j] var vals = elem.substring(1, elem.length-1).trim().split(/\s*[,\|]\s*/); var text = ""; variant = variant.replace(elem, text); } out.push("
"); } if (question.type == "vc" ) { // vyber z roletky s danym seznamem spolecnym pro vsechny varianty var text = "" + variant[0] +" – "; out.push("
"); } if (question.type == "select_word" ) { // vyber slova ve vete, vyber z roletky s danym seznamem spolecnym pro vsechny varianty var words = variant.split(/\s+/g); var sentence = [] for (var j = 0; j < words.length; j++) { var word = words[j]; var affix = ""; if (endsWith( word, ".,;?!")) { affix = word.charAt(word.length-1); word = word.substring(0, word.length-1); } if (word.startsWith("{")) { var parts = word.replace(/[\{\}]/g, "").split(/\|/) parts.push(j); // pidani indexu slova - na pozici 3 parts[1] = parts[1].split("/"); question.correct.push(parts); console.log(parts[0] + " - " + parts[2] + " - " + question.options[parts[2]]) question.correctAnswers.push(question.options[parts[2]]); word = parts[0]; } else { question.correct.push(""); question.correctAnswers.push(""); } if (word == "-" && word == "–") { sentence.push(word); } else { sentence.push("" + word + "" + affix); } } var text = "
"); } if (question.type == "th" ) { var elems = variant.match(/\{([^}]+)\}/g); for (var j = 0; j < elems.length; j++) { var elem = elems[j] var vals = elem.substring(1, elem.length-1).trim().split(/\s*\|\s*/); var textLength = "" + Math.ceil(vals[1].length*1) + "em"; var text = "(" + vals[0]+ ") "; var cors = vals[1].split(/\//); trimValues(cors); question.correct.push(cors); text += ""; variant = variant.replace(elem, text); } out.push("
"); } if (question.type == "thf" ) { var elems = variant.match(/\{([^}]+)\}/g); for (var j = 0; j < elems.length; j++) { var elem = elems[j] var vals = elem.substring(1, elem.length-1).trim().split(/\s*\|\s*/); var textLength = "" + Math.ceil(vals[1].length*0.65) + "em"; var text = "(" + vals[0]+ ") "; var cors = vals[1].split(/\//); trimValues(cors); question.correct.push(cors); question.feedback = vals[2]; text += ""; variant = variant.replace(elem, text) + "
"; } out.push(""); } if (question.type == "tf" ) { var elems = variant.match(/\{([^}]+)\}/g); for (var j = 0; j < elems.length; j++) { var elem = elems[j] var vals = elem.substring(1, elem.length-1).trim().split(/\s*\/\s*/); var textLength = "" + Math.ceil(vals[0].length*0.65) + "em"; if (question.textLength) { textLength = question.textLength; } var text = " "; trimValues(vals); question.correct.push(vals); text += ""; variant = variant.replace(elem, text); } out.push("
"); } if (question.type == "tff" ) { // fill text + feedback (examples etc.) var parts = variant.split(/\s*\|\s*/); if (parts.length < 2) break; question.feedback = parts[1]; variant = parts[0]; var elems = variant.match(/\{([^}]+)\}/g); for (var j = 0; j < elems.length; j++) { var elem = elems[j] var vals = elem.substring(1, elem.length-1).trim().split(/\s*\/\s*/); var textLength = "" + Math.ceil(vals[0].length*0.65) + "em"; if (question.textLength) { textLength = question.textLength; } if (textLength == "0em") textLength = "8em"; var text = " "; trimValues(vals); question.correct.push(vals); text += ""; variant = variant.replace(elem, text); } out.push("
"); } if (question.type == "fillin" ) { var rndIndex = Math.floor(variant.length*Math.random()); if (variant[rndIndex][0] == "-") { rndIndex += (Math.random() > 0.5) ? -1 : 1; } var text = question.template; for (var i = 0; i < variant.length; i++ ) { var item = variant[i].join("/") if (i != rndIndex) { item = ""; question.correctAnswers.push(variant[i]); } text = text.replace("{T" + (i+1) + "}", item); } out.push("
"); } if (question.type == "translate" ) { var elems = variant.match(/\{([^}]+)\}/g); for (var j = 0; j < elems.length; j++) { var elem = elems[j] var vals = elem.substring(1, elem.length-1).trim().split(/\s*[\|]\s*/); trimValues(vals); var text = "
" + vals[0] +"
"); } if (question.type == "division" ) { var divider = templates.divider; var sels = [""] for (var j = 0; j < question.options.length; j++) { var qopt = question.options[j]; var opt = templates.divOption.replace("{TEXT}", qopt).replace("{VALUE}", qopt); sels.push(opt); } divider = divider.replace("{OPTIONS}", sels.join("")); out.push("
"); } if (question.type == "sortf" ) { // tahaci trideni + feedback out.push("
"); } if (question.type == "dragf" ) { // tahaci vkladani do textu out.push("
" + variant2 + "
") } } out.push(""); } } return out.join(""); } function endsWith(text, endChars) { for (var i = 0; i < endChars.length; i++) { if (text.endsWith(endChars.charAt(i)) ) { return true; } } return false; } function toggleWord(elem) { var index = elem.getAttribute("data-index"); if (elem.classList.contains("selected")) { var table = elem.parentNode.parentNode.querySelector("table[data-index=\"" + index + "\"]"); if (table) { table.parentNode.removeChild(table); } } else { var results = elem.parentNode.parentNode.querySelector(".results"); if (results) { var qid = elem.parentNode.parentNode.parentNode.parentNode.id; var options = questions[qid].options; var sel = "" var text = "
Slovo: | " + elem.innerHTML +" | |
---|---|---|
1. p. j. č.: | ||
Род и тип | " + sel +" |
Slovo: | " + cor[0] +" |
---|---|
1. p. j. č.: | " + cor[1] + " |
Род и тип | " + question.correctAnswers[i] +" |
" + answer[1] + " | " var start = answer[0]; if (i < question.correctAnswers.length-1) { row2 += "" + text.substring(answer[0], question.correctAnswers[i+1][0]) + " | " } else { row2 += "" + text.substring(answer[0]) + " | " } } out += row1 + "
" + question.feedback + "
"; var fb = form.querySelector(".feedback"); if (fb) fb.innerHTML = out; break; case "dragf": var drops = form.querySelectorAll(".droparea-fill"); for (var i = 0; i < drops.length; i++) { var drop = drops[i]; drop.ondragend = null; var item = drop.querySelector(".dragitem"); if (item) { item.ondrag = null; checkText(item, question.correctAnswers[i]); } } break; case "sortf": var drops = form.querySelectorAll(".droparea"); isEmpty = true; for (var i = 0; i < drops.length; i++) { if (drops[i].querySelectorAll(".dragitem").length > 0) { isEmpty = false; } } if (isEmpty) { break; } for (var i = 0; i < drops.length; i++) { var items = drops[i].querySelectorAll(".dragitem"); for (var j = 0; j < items.length; j++) { var item = items[j]; var iid = item.id; var fb = item.querySelector(".feedback"); var fb_content = ""; if (i == question.items[iid].group) { fb_content = "" item.classList.add("correct"); } else { fb_content = ""; item.classList.add("wrong"); } fb_content += " " + question.items[iid].feedback + "" fb.innerHTML = fb_content; } } } var toolbar = form.querySelector(".toolbar"); if (toolbar) { toolbar.innerHTML = templates.nexter; } } function checkText(elem, correct) { console.log(elem) var input = elem.querySelector("input"); var val = ""; if (input) { val = input.value.trim(); val = val.replace(/\s+/g, " ") } if (elem.classList.contains("dragitem")) { input = elem.firstChild; val = input.nodeValue.trim(); } if (val == "") return; var fb = elem.querySelector(".feedback"); //input.disabled = true; fb.innerHTML = ""; elem.classList.remove("correct"); elem.classList.remove("wrong"); var isCorrect = false; for (var j = 0; j < correct.length; j++) { if (val == correct[j]) { isCorrect = true; } } var feedback = ""; if (isCorrect) { feedback = "" + feedback elem.classList.add("correct"); } else { feedback = " (*" + correct.join("/") + ")" elem.classList.add("wrong"); } fb.innerHTML = feedback; } function checkSelect(elem, correct, correctAnswer) { var sel = elem.querySelector("select"); var fb = elem.querySelector(".feedback"); //sel.disabled = true; fb.innerHTML = ""; elem.classList.remove("correct"); elem.classList.remove("wrong"); //if (sel.value == "---") return; var fbComment = ""; if (sel.value == correct) { fb.innerHTML = ""; elem.classList.add("correct"); } else { fb.innerHTML = " (*" + correctAnswer + ") " + fbComment; elem.classList.add("wrong"); } } function createNextForm(nexter) { var content = nexter.parentNode.parentNode; var form = content.parentNode; var question = questions[form.id]; content.innerHTML = getNextVariants(question); } function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("text", ev.currentTarget.id); } function drop(ev) { ev.preventDefault(); var data = ev.dataTransfer.getData("text"); ev.currentTarget.appendChild(document.getElementById(data)); } function togglePopup (evt) { var icon = evt.currentTarget; var popup = icon.parentNode; if (popup.classList.contains("shown")) { popup.setAttribute("title", "Close popup") } else { popup.setAttribute("title", "Show popup") } popup.classList.toggle("shown") } function closePopup(evt) { var popup = evt.currentTarget.parentNode.parentNode; popup.classList.remove("shown"); } function initPopups() { var pops = document.querySelectorAll(".popup"); for (var i = 0; i < pops.length; i++) { var pop = pops[i]; var content = pop.innerHTML; pop.innerHTML = ""; var icon = pop.appendChild(document.createElement("span")); icon.className = "popup-icon"; icon.addEventListener("click", togglePopup); var cont = pop.appendChild(document.createElement("span")); cont.className = "popup-content"; cont.innerHTML = content; var closer = cont.appendChild(document.createElement("span")); closer.className = "closer"; closer.innerHTML = "×"; closer.addEventListener("click", closePopup); } } function addChild(parent, childName, childClass) { var child = parent.appendChild(document.createElement(childName)); if (childClass != undefined) { child.className = childClass; } return child; } window.addEventListener("load", initPage);