var relMdlEgVal; var selStmtDescEgVal; var selStmtEgVal; var relMdlLabVal; var selLabVal; var addSelBtnVal; var remSelBtnVal; var rstTxtArValBtnVal; var psfxSubSubmVal; var glErrMsg = 'Interní chyba.'; var lang = getUserLanguage(); setValues(); const REL_MDL = 'tst_r'; const SEL_STMT_DESC = 'tst_d_'; const SEL_STMT = 'tst_s_'; const REL_MDL_EG = '' + 'foo (bar (PK), bar2 (UK), bar3);\r\n' + 'foo2 (bar (PK, UK), bar2 (PK, UK2, FK do foo), bar3 (UK2, FK do foo[[bar2]]));\r\n' + 'foo3 (bar (PK, FK do foo2[[bar2, bar3]], FK2 do foo2[[bar2, bar3]]), bar2 (UK, UK2, FK do foo2, FK2 do foo2[[bar2, bar3]]), bar3 (UK2, FK do foo2, FK do foo2[[bar], [bar2, bar3]]));'; const SEL_STMT_DESC_EG = 'Vypsat všechny údaje z tabulky foo'; const SEL_STMT_EG = 'SELECT * FROM foo'; const S = ' '; const N = '
'; const BR = '' + '
' + '
' + '
'; const REL_MDL_LAB = '' + ''; const SEL_LAB = '' + ''; const SEL_NUM = '' + ''; const REL_MDL_TXT_AR = '' + ''; const SEL_STMT_DESC_TXT_AR = '' + ''; const SEL_STMT_TXT_AR = '' + ''; const ADD_SEL_BTN = '' + ''; const REM_SEL_BTN = '' + ''; const RST_TXT_AR_VAL_BTN = '' + ''; const CMT = '' + '
' + '
'; function getUserLanguage() { var pageURL = decodeURIComponent(window.location.search.substring(1)); var URLVars = pageURL.split(/;|&/); for (var i = 0; i < URLVars.length; i++) { var paramName = URLVars[i].split('='); var lang; if (paramName[0] === 'lang' && !!paramName[1]) { lang = paramName[1]; } } return lang; } function setValues() { if (lang === 'cs' || lang === undefined) { relMdlEgVal = 'Příklad zápisu relačního modelu'; selStmtDescEgVal = 'Příklad zápisu popisu příkazů SELECT'; selStmtEgVal = 'Příklad zápisu příkazů SELECT'; relMdlLabVal = 'Relační model'; selLabVal = 'Popisy příkazů SELECT a příkazy SELECT'; addSelBtnVal = 'Přidat SELECT'; remSelBtnVal = 'Odstranit'; rstTxtArValBtnVal = 'Restartovat'; psfxSubSubmVal = 'odevzdat'; } else { relMdlEgVal = 'Relational model example'; selStmtDescEgVal = 'SELECT statement description example'; selStmtEgVal = 'SELECT statement example'; relMdlLabVal = 'Relational model'; selLabVal = 'SELECT statement descriptions and SELECT statements'; addSelBtnVal = 'Add SELECT'; remSelBtnVal = 'Remove'; rstTxtArValBtnVal = 'Reset'; psfxSubSubmVal = 'submit'; glErrMsg = 'Internal error.'; } } String.prototype.contains = function (str) { return this.indexOf(str) > -1; }; function setFunctionName(arg) { fcnName = arg.callee.toString().match(/^[^(]+/)[0].replace(/function /, ''); } function logError(err) { var errMsg = err.stack; if (errMsg !== undefined) { if (!errMsg.contains(err.name)) { errMsg = err + ' at ' + errMsg; } } else { if (fcnName !== undefined) { err += ' at ' + fcnName; } errMsg = err; } if (!(typeof (console) === 'undefined' || typeof (console.error) === 'undefined')) { console.error(errMsg); } } window.onerror = function () { alert(glErrMsg); }; // The implementation below uses jQuery const ACC_TXT_ID = 'div#PB168RopotAccTxt'; const DEF_ERR_MSG_ID = 'div#PB168RopotDefErrMsg'; const ID = 'div#PB168Ropot'; const TST_STG = 'textarea[name^="tst_"][name$="_e_a_1"]'; const ANS = '' + 'textarea[name^="' + REL_MDL + '"],' + 'textarea[name^="' + SEL_STMT_DESC + '"],' + 'textarea[name^="' + SEL_STMT + '"]'; const MAX_SEL = 10; const STG_ID = ID.replace(/^span#/, ''); const STG_NUM = STG_ID + 'Num'; var user; var uco; var $extSrvTxtAr; var $tstForm; var evaldAns; var num = 1; var sprtStg = (typeof (Storage) !== 'undefined') ? true : false; var fcnName; var isChrome = !!window.chrome && !!window.chrome.webstore; var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; if (!(isChrome || isOpera)) { var newBtmPos; } function loadAccompanyingText() { setFunctionName(arguments); var relMdlEg = REL_MDL_EG.replace(/(\r?\n|\r)/g, N); var selStmtDescEg = SEL_STMT_DESC_EG.replace(/(\r?\n|\r)/g, N); var selStmtEg = SEL_STMT_EG.replace(/(\r?\n|\r)/g, N); $(ACC_TXT_ID).append('' + relMdlEgVal + ':' + BR + '' + relMdlEg + '' + BR + BR + selStmtDescEgVal + ':' + BR + '' + selStmtDescEg + '' + BR + BR + selStmtEgVal + ':' + BR + '' + selStmtEg + '' + ''); } function loadRopotContent() { setFunctionName(arguments); $(ID).append('' + REL_MDL_LAB + '
' + BR + REL_MDL_TXT_AR + '' + '' + RST_TXT_AR_VAL_BTN + BR + ADD_SEL_BTN + '' + '' + '
' + BR + BR + BR + ''); } function updateButtonContainerPosition() { setFunctionName(arguments); var isMozilla = typeof (InstallTrigger) !== 'undefined'; var isMSIE = false || !!document.documentMode; var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; var $btm = $(ID).find('span.btnContr'); var btmPos = $btm.css('bottom'); var posVal = btmPos.match(/-?\d+/); var posType = btmPos.replace(posVal, ''); if (posType === 'px') { var newPosVal; if (isMozilla) { newPosVal = posVal / 6; } else if (isMSIE) { newPosVal = posVal * (-1.25); } else if (isSafari) { newPosVal = posVal * 0.25; } newBtmPos = newPosVal + posType; $btm.css('bottom', newBtmPos); } } if (!sprtStg) { function getCookie(cName) { setFunctionName(arguments); var name = cName + '='; var encodedCookie = encodeURIComponent(document.cookie); var decodedCookie = decodeURIComponent(encodedCookie); var ca = decodedCookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) === ' ') { c = c.substring(1); } if (c.indexOf(name) === 0) { var cVal = c.substring(name.length, c.length); cVal = cVal.replace(/\#\.\#/g, ';'); cVal = cVal.replace(/\#\-\#/g, ','); cVal = cVal.replace(/\#\_\#/g, ' '); cVal = cVal.replace(/\#\^\#/g, '\r\n'); return cVal; } } return ''; } function setCookie(cName, cVal) { setFunctionName(arguments); if (typeof cVal === 'string') { cVal = cVal.replace(/;/g, '#.#'); cVal = cVal.replace(/,/g, '#-#'); cVal = cVal.replace(/ /g, '#_#'); cVal = cVal.replace(/(\r?\n|\r)/g, '#^#'); } var c = cName + '=' + cVal + ';'; var cSize = (document.cookie + c).length; var cCnt = document.cookie.split(';').length; if (cSize <= 4093 && cCnt < 50) { document.cookie = c; } } function deleteCookie(cName) { setFunctionName(arguments); document.cookie = cName + '=; expires=Tue, 28 Jan 1919 12:34:56 GMT;'; } } function getTextAreasSize() { setFunctionName(arguments); var txtArsSize = {}; $(ID).find('textarea').each(function () { var txtArSize = {}; txtArSize['w'] = $(this).outerWidth(); txtArSize['h'] = $(this).outerHeight(); var name = $(this).attr('name'); var ans = new RegExp('^(' + REL_MDL + '|' + SEL_STMT_DESC + '|' + SEL_STMT + ')'); if (name !== undefined && name.match(ans)) { var selStmtDesc = new RegExp('^' + SEL_STMT_DESC); if (name.match(selStmtDesc)) { txtArSize['w'] = '100%'; } txtArsSize['[name="' + name + '"]'] = txtArSize; } else { var id = $(this).attr('id'); if (id !== undefined) { txtArsSize['#' + id] = txtArSize; } else { var cls = $(this).attr('class'); if (cls !== undefined) { txtArsSize['[class="' + cls + '"]'] = txtArSize; } else if (name !== undefined) { txtArsSize['[name="' + name + '"]'] = txtArSize; } } } }); return txtArsSize; } function setTextAreasSize(txtArsSize) { setFunctionName(arguments); for (var attr in txtArsSize) { var txtArSize = txtArsSize[attr]; for (var key in txtArSize) { if (key === 'w' || key === 'h') { var dim = (key === 'w') ? 'width' : 'height'; var size = txtArSize[key]; $(ID).find('textarea' + attr).css(dim, size); } } } } function splitRelationalModel(relMdl) { setFunctionName(arguments); var relsArr = relMdl.split(';'); if (relsArr.length > 1 && relsArr.slice(-1)[0] === '') { relsArr.splice(-1, 1); } if (evaldAns !== undefined) { for (var evaldAwrType in evaldAns) { if (evaldAwrType === REL_MDL) { var evaldRels = evaldAns[evaldAwrType].val; break; } } } var awrRels = {}; var n = 1; for (var i = 0; i < relsArr.length; i++) { var relVal = relsArr[i]; var evald = false; var isOk = false; if (evaldAns !== undefined) { for (var evaldRelNum in evaldRels) { var evaldRelVal = evaldRels[evaldRelNum].awrRel; if (relVal.trim() === evaldRelVal.trim()) { evald = true; isOk = evaldRels[evaldRelNum].isOk; break; } } } var relNum = 'r' + n; var awrRel = {'evald': evald, 'isOk': isOk, 'awrRel': relVal}; awrRels[relNum] = awrRel; n++; } return awrRels; } $.fn.exists = function () { return $(this).length !== 0; }; function getComment(awrType, $awrCmt) { setFunctionName(arguments); var awrCmt; switch (awrType.replace(/\d+/, '')) { case SEL_STMT_DESC: var $selStmtDescCmt = $awrCmt.find('span.selStmtDescCmt'); awrCmt = $selStmtDescCmt.exists() ? $selStmtDescCmt.text() : ''; break; case SEL_STMT: var $selStmtCmt = $awrCmt.find('span.selStmtCmt'); awrCmt = $selStmtCmt.exists() ? $selStmtCmt.text() : ''; break; default: awrCmt = $awrCmt.exists() ? $awrCmt.text() : ''; break; } return awrCmt; } function setComment($awrType, awrType, awrCmt) { setFunctionName(arguments); $awrType.addClass('invalid'); if ($awrType.css('box-shadow') === 'none') { $awrType.removeClass('invalid'); $awrType.addClass('alternativeInvalid'); } var $awrContr = $awrType.closest('div#relMdlContr, div.selContr2'); var $awrCmt = $awrContr.find('div.cmt'); var hasCmt = true; if (!$awrCmt.exists()) { $awrContr.append('' + BR + CMT + ''); $awrCmt = $awrContr.find('div.cmt'); hasCmt = false; } switch (awrType.replace(/\d+/, '')) { case SEL_STMT_DESC: $awrCmt.prepend('' + '' + awrCmt + '' + (!hasCmt ? '' : N) + ''); break; case SEL_STMT: $awrCmt.append('' + (!hasCmt ? '' : N) + '' + awrCmt + '' + ''); break; default: $awrCmt.append('' + awrCmt + ''); break; } } function getAnswers() { setFunctionName(arguments); var ans = {}; $(ID).find(ANS).each(function () { var awrType = $(this).attr('name'); var awrVal = $(this).val(); var evald = false; if (evaldAns !== undefined) { for (var evaldAwrType in evaldAns) { var evaldAwrVal = evaldAns[evaldAwrType].val; if (awrType.substring(0, 5) === evaldAwrType.substring(0, 5)) { if (typeof (evaldAwrVal) === 'string') { if (awrVal.trim() === evaldAwrVal.trim()) { evald = true; break; } } } } } if (awrType === REL_MDL && evald === false) { awrVal = splitRelationalModel(awrVal); } var $awrCmt = $(this).closest('div#relMdlContr, div.selContr').find('div.cmt'); var awrCmt = evald ? getComment(awrType, $awrCmt) : ""; var awr = {'evald': evald, 'val': awrVal, 'cmt': awrCmt}; ans[awrType] = awr; }); return ans; } function setAnswers(ans) { setFunctionName(arguments); for (var awrType in ans) { var awrVal = ans[awrType].val; var awrCmt = ans[awrType].cmt; /***/var ramBool = Math.random() >= 0.5; awrCmt = ramBool ? 'ERROR: incorrect answer' : '';/***/ var $awrType = $(ID).find('textarea[name="' + awrType + '"]'); if (awrVal !== undefined) { if (typeof (awrVal) === 'string') { $awrType.val(awrVal); } else { var awrRelMdl = ''; var awrRels = awrVal; for (var relNum in awrRels) { var relVal = awrRels[relNum].awrRel; awrRelMdl += relVal + ';'; } if (awrRelMdl.length === 1) { awrRelMdl = ''; } $awrType.val(awrRelMdl); } } if (awrCmt !== undefined && awrCmt.length) { setComment($awrType, awrType, awrCmt); } } } function generateSelectContainers(ropotMtdtJSON) { setFunctionName(arguments); var ropotMtdt = JSON.parse(ropotMtdtJSON); var currNum = ropotMtdt[STG_NUM]; if (currNum > 1) { for (var i = 0; i < currNum - 1; i++) { addSelect(); } } setTextAreasSize(ropotMtdt.txtArsSize); } function loadRopotMetaData() { setFunctionName(arguments); var ropotMtdtJSON; if (sprtStg) { ropotMtdtJSON = sessionStorage.getItem('ropotMtdt'); } else { ropotMtdtJSON = getCookie('ropotMtdt'); deleteCookie('ropotMtdt'); } var is1stOpd = true; if (!(ropotMtdtJSON == null || ropotMtdtJSON === '')) { generateSelectContainers(ropotMtdtJSON); is1stOpd = false; } return is1stOpd; } function saveRopotMetaData() { setFunctionName(arguments); var ropotMtdt = {}; ropotMtdt[STG_NUM] = num; ropotMtdt['txtArsSize'] = getTextAreasSize(); var ropotMtdtJSON = JSON.stringify(ropotMtdt); if (sprtStg) { sessionStorage.setItem('ropotMtdt', ropotMtdtJSON); } else { setCookie('ropotMtdt', ropotMtdtJSON); } } function getDataFromExternalService(is1stOpd) { setFunctionName(arguments); /***//*var $extSrvAns = $tstForm.find('font').first(); if ($extSrvAns.exists()) { var extSrvAnsJSON = $extSrvAns.text(); try { var extSrvAns = JSON.parse(extSrvAnsJSON); if (is1stOpd) { var relMdlFromDB = extSrvAns.relMdl; if (relMdlFromDB !== undefined) { $(ID).find('textarea[name^="' + REL_MDL + '"]').val(relMdlFromDB); } } else { evaldAns = extSrvAns; if (evaldAns !== undefined) { setAnswers(evaldAns); } } $extSrvAns.remove(); } catch (err) { logError(err); } }*//***/ /***/if (sprtStg) { extSrvAnsJSON = sessionStorage.getItem('studAns'); } else { extSrvAnsJSON = getCookie('studAns'); deleteCookie('studAns'); } if (extSrvAnsJSON !== undefined) { try { var extSrvAns = JSON.parse(extSrvAnsJSON); if (is1stOpd) { var relMdlFromDB = extSrvAns.relMdl; if (relMdlFromDB !== undefined) { $(ID).find('textarea[name^="' + REL_MDL + '"]').val(relMdlFromDB); } } else { evaldAns = JSON.parse(extSrvAnsJSON).ans; if (evaldAns !== undefined) { setAnswers(evaldAns); } } } catch (err) { // not valid JSON string -- ignore it } }/***/ } function setDataForExternalService() { setFunctionName(arguments); var studAns = {'uco': uco}; studAns['name'] = user.match(/^[^(,]+/)[0].trim(); studAns['ans'] = getAnswers(); var studAnsJSON = JSON.stringify(studAns); $extSrvTxtAr.val(studAnsJSON); /***/if (sprtStg) { sessionStorage.setItem('studAns', studAnsJSON); } else { setCookie('studAns', studAnsJSON); }/***/ } function removeDefaultErrorMessage() { setFunctionName(arguments); $(DEF_ERR_MSG_ID).remove(); } function loadRopot() { setFunctionName(arguments); loadAccompanyingText(); loadRopotContent(); var is1stOpd = true; try { if (!(isChrome || isOpera)) { updateButtonContainerPosition(); } is1stOpd = loadRopotMetaData(); } catch (err) { logError(err); } getDataFromExternalService(is1stOpd); removeDefaultErrorMessage(); } function saveRopot() { setFunctionName(arguments); try { saveRopotMetaData(); } catch (err) { logError(err); } setDataForExternalService(); } function emptyRopot() { setFunctionName(arguments); $(ID).empty(); } function editSubmitButtonValue() { setFunctionName(arguments); var $subm = $tstForm.find(':submit').last(); var submVal = $subm.val(); if (!submVal.contains(psfxSubSubmVal)) { var prfxSubmVal = submVal.substring(0, submVal.indexOf(' ')); var psfxSubmVal = submVal.substring(submVal.indexOf(' '), submVal.length); var prfxSubSubmVal = (lang === 'cs' || lang === undefined) ? ' a ' : ' and '; if (submVal.contains(prfxSubSubmVal)) { prfxSubSubmVal = ', '; } var subSubmVal = prfxSubSubmVal + psfxSubSubmVal; var newSubmVal = prfxSubmVal + subSubmVal + psfxSubmVal; $subm.val(newSubmVal); } } function updateOrderAttributes() { setFunctionName(arguments); var $appdSel = $(ID).find('div.selContr').last(); var $selNum = $appdSel.find('label.sel'); var edOrd = $selNum.html().replace(/\d+/, num); if (num < 10) { edOrd = S + S + edOrd; } $selNum.html(edOrd); var $selStmtDesc = $appdSel.find('textarea.selStmtDesc'); var edName = $selStmtDesc.attr('name').replace(/\d+/, num); $selStmtDesc.attr('name', edName); var $selStmt = $appdSel.find('textarea.selStmt'); edName = $selStmt.attr('name').replace(/\d+/, num); $selStmt.attr('name', edName); var x = num % 3; if (x === 1) { x = ''; } else if (x === 0) { x = 3; } var edSelStmtDescPlHlr = $selStmtDesc.attr('placeholder') + x; var edSelStmtPlHlr = $selStmt.attr('placeholder') + x; $selStmtDesc.attr('placeholder', edSelStmtDescPlHlr); $selStmt.attr('placeholder', edSelStmtPlHlr); } function addSelect() { setFunctionName(arguments); try { if (num <= MAX_SEL) { $(ID).append('' + (num === 1 ? (SEL_LAB + BR) : '') + '
' + BR + SEL_NUM + '
' + '
' + SEL_STMT_DESC_TXT_AR + N + SEL_STMT_TXT_AR + '
' + '' + '' + RST_TXT_AR_VAL_BTN + BR + REM_SEL_BTN + '' + '' + '
' + BR + '
' + ''); updateOrderAttributes(); num++; } if (!(isChrome || isOpera) && newBtmPos !== undefined) { $(ID).find('span.btnContr').last().css('bottom', newBtmPos); } } catch (err) { logError(err); throw err; } } function resetStudentAnswer() { setFunctionName(arguments); try { $(this).closest('div#relMdlContr, div.selContr').find(ANS).val(''); } catch (err) { logError(err); throw err; } } function moveOrderAttributes($selToRem) { setFunctionName(arguments); var prevSelNum = $selToRem.find('label.sel').text().match(/\d+/); var prevSelStmtDescNum = $selToRem.find('textarea.selStmtDesc').attr('name').match(/\d+/); var prevSelStmtNum = $selToRem.find('textarea.selStmt').attr('name').match(/\d+/); $selToRem.nextAll().each(function () { var $selNum = $(this).find('label.sel'); var ord = $selNum.text(); var currNum = ord.match(/\d+/); if (prevSelNum.toString().length === currNum.toString().length) { var newNum = ord.replace(/\d+/, prevSelNum); $selNum.text(newNum); } else { ord = $selNum.html(); var newOrd = S + S + ord.replace(/\d+/, prevSelNum); $selNum.html(newOrd); } prevSelNum = currNum; var $selStmtDesc = $(this).find('textarea.selStmtDesc'); var name = $selStmtDesc.attr('name'); currNum = name.match(/\d+/); var newName = name.replace(/\d+/, prevSelStmtDescNum); $selStmtDesc.attr('name', newName); prevSelStmtDescNum = currNum; var $selStmt = $(this).find('textarea.selStmt'); name = $selStmt.attr('name'); currNum = name.match(/\d+/); newName = name.replace(/\d+/, prevSelStmtNum); $selStmt.attr('name', newName); prevSelStmtNum = currNum; }); } function removeSelect() { setFunctionName(arguments); try { var $selToRem = $(this).closest('div.selContr'); var isNotLast = $selToRem.next().exists(); var haveSels = $(ID).find('div.selContr').length - 1; if (haveSels) { if (isNotLast) { moveOrderAttributes($selToRem); } } else { $(ID).find('label#sel').remove(); } $selToRem.remove(); num--; } catch (err) { logError(err); throw err; } } $(document).ready(function () { try { // Get user's name and UCO user = $('div#id-uzivatel').text(); uco = user.match(/\d+/)[0]; // Hidden textarea -- e: question to store values for external service $extSrvTxtAr = $(ID).closest('table').find(TST_STG); try { // Load ROPOT for submitting relational models and queries loadRopot(); } catch (err) { // Empty ROPOT to have only error message in the content emptyRopot(); logError(err); } // IS MU ROPOT $tstForm = $(ID).closest('form[name="testform"]'); try { // Edit submit button value editSubmitButtonValue(); } catch (err) { logError(err); } // Responses to any buttons $(ID).find('button#addSel').click(addSelect); $(ID).on('click', 'button.remSel', removeSelect); $(ID).on('click', 'button.rstTxtArVal', resetStudentAnswer); $tstForm.submit(saveRopot); } catch (err) { logError(err); throw err; } });