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 = 'relations';
const SELECTS = 'selects';
const SEL_STMT_DESC = 'description';
const SEL_STMT = 'statement';
const SCHEMATA_ARRAY = [];
const LOGIN_S = 'login';
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 LOG_EG = 'např. xdohnal';
const S = ' ';
const N = ' ';
const BR = ''
+ '
'
// + BR
// debug + MAGICKY_TLACITKO
// + MAGICKY_REVERSE
+ '');
}
function loadComments() {
setFunctionName(arguments);
var odkaz = {'uco': uco};
odkaz['name'] = user.match(/^[^(,]+/)[0].trim();
odkaz['hash'] = "blableblu";
var odkazJSON = JSON.stringify(odkaz);
var odkazEncoded = encodeURI(odkazJSON);
var urlC = "https://disa.fi.muni.cz/lectures/ropots?zobrazeni=p&t=pb168:fixed:relmodel&s="+ odkazEncoded;
$.post( urlC, function( data ) {
var dataC = data.match(/\{.*/); //eliminuje nok nebo ok... vse po {
if (dataC != '') {
var dataCJSON = JSON.parse(dataC);
var relaceC = '';
var rComment = dataCJSON.ans.relations.comment;
if (rComment != '' && rComment != undefined ) {
var nlrComment = rComment.replace('#^#', ' ');
$(ID).find('div#relMdlContr').append(
BR
+ '
'
+ nlrComment
+ '
'
);
}
var selectsC = dataCJSON.ans.selects;
var selectsCNum = selectsC.length;
for (var i = 0; i < selectsCNum; i++) {
sComment = selectsC[i].comment;
if (sComment != '' && sComment != undefined ) {
snlComment = sComment.replace('#^#', ' ');
$(ID).find('textarea[name^="' + SEL_STMT + (i+1) + '"]').after(
BR
+ '
'
+ snlComment
+ '
'
);
}
}
}
});
}
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);
}
}
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; // toto podchytit vsude! TODO, uz to tam neni
var awrRel = {'schema': relVal, 'evald': evald, 'isOk': isOk};
awrRels.push(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 = {};
var s_array = [];
$(ID).find(ANSW).each(function () { // toto by relativne mohlo bejt jen pro REL
var awrType = $(this).attr('name'); //tady se to posere.-D
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) {
var awrVal = awrVal.trim();
awrVal = splitRelationalModel(awrVal);
var $awrCmt = $(this).closest('div#relMdlContr, div.selContr').find('div.cmt');
var awrCmt = evald ? getComment(awrType, $awrCmt) : "";
var awr = {'schemata': awrVal, 'comment': awrCmt, 'evald': evald};
ans[awrType] = awr;
}
else {
var desc = $(this).val();
var state = $(this).closest('div#relMdlContr, div.selContr').find('textarea.selStmt').val();
var $awrCmt = $(this).closest('div#relMdlContr, div.selContr').find('div.cmt');
var awrCmt = evald ? getComment(awrType, $awrCmt) : "";
var selekt = {'description': desc, 'statement': state, 'evald': evald, 'comment': awrCmt};
s_array.push(selekt);
awrType = 'selects';
ans[awrType] = s_array;
}
});
return ans;
}
function setAnswers(ans) {
setFunctionName(arguments);
for (var awrType in ans) {
var awrVal = ans[awrType].val;
var awrCmt = ans[awrType].cmt;
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 - 2; i++) {
addSelect();
}
}
setTextAreasSize(ropotMtdt.txtArsSize);
}
function loadRopotMetaData() {
setFunctionName(arguments);
var ropotMtdtJSON;
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 magicButton() {
setFunctionName(arguments);
var studAns = {'uco': uco};
studAns['login'] = login;
studAns['name'] = user.match(/^[^(,]+/)[0].trim();
studAns['ans'] = getAnswers();
studAns['percentage'] = 0;
var studAnsJSON = JSON.stringify(studAns);
$extSrvTxtAr.val(studAnsJSON);
}
function magicReverse() {
setFunctionName(arguments);
var zkama = $extSrvTxtAr.val();
if (zkama != '') {
var zkamaJSON = JSON.parse(zkama);
var schemata = '';
var schemataANS = zkamaJSON.ans.relations.schemata;
for (var i = 0; i < schemataANS.length; i++) {
schemata += schemataANS[i].schema + ';';
}
$(ID).find('textarea[name^="' + REL_MDL + '"]').val(schemata);
if (zkamaJSON.login != 'undefined') {
login = zkamaJSON.login;
// console.log(login);
$(LOG).find('input[name="login0"]').val(login);
}
var selectsANS = zkamaJSON.ans.selects;
var selectsNum = selectsANS.length;
//console.log(selectsNum);
for (var i = 0; i < selectsNum; i++) {
if (i > 0) {
addSelect();
}
$(ID).find('textarea[name^="' + SEL_STMT_DESC + (i+1) + '"]').val(selectsANS[i].description);
$(ID).find('textarea[name^="' + SEL_STMT + (i+1) + '"]').val(selectsANS[i].statement);
}
}
}
function getLogin() {
setFunctionName(arguments);
login = $(LOG).find('input[name="login0"]').val();
//tady je kontrola loginu z fadminu
var flogin = 'x' + uco;
var fNamelogin = login.substr(1);
fNamelogin = fNamelogin.replace(/[0-9]/g, '');
//console.log(fNamelogin);
var editName = user.match(/^[^(,]+/)[0].trim();
var lowName = editName.toLowerCase();
var normalizedName = lowName.normalize('NFD').replace(/[\u0300-\u036f]/g, ""); //bez diakritiky
var validLogin = normalizedName.includes(fNamelogin);
// console.log(validLogin);
if (login.length < 3 || login.length > 10 || login.charAt(0) !== 'x' || (login != flogin && validLogin != true)) {
//upravit aby to bralo vsechny podminky!
return "nope";
} else {
return login;
}
}
function loginButtonF() {
setFunctionName(arguments);
var result = getLogin();
if (result !== "nope")
{
var link = {'uco': uco};
link['login'] = login;
link['name'] = user.match(/^[^(,]+/)[0].trim();
link['hash'] = "blableblu";
var linkJSON = JSON.stringify(link);
var linkEncoded = encodeURI(linkJSON);
var url = "https://disa.fi.muni.cz/lectures/ropots?zobrazeni=p&t=pb168:fixed:readmodel&s="+ linkEncoded;
// console.log(url);
$.post( url, function( data ) {
var dataW = data.substring(3);
$(ID).find('textarea[name="relations"]').val(dataW);
if ($(LOG).find('div.frontError').val() !== undefined) {
$(LOG).find('div.frontError').remove();
}
});
} else if ($(LOG).find('div.frontError').val() == undefined) {
$(LOG).find('div.studentLogin').append(
BR
+ '
'
+ 'Chybný login nebo žádné relace. Případně jste nenastavil(a) potřebná přístupová práva k relaci.'
+ '
'
);
}
}
function setDataForExternalService() {
setFunctionName(arguments);
var studAns = {'uco': uco};
studAns['login'] = login;
studAns['name'] = user.match(/^[^(,]+/)[0].trim();
studAns['ans'] = getAnswers();
studAns['percentage'] = 0;
var studAnsJSON = JSON.stringify(studAns);
$extSrvTxtAr.val(studAnsJSON);
}
function removeDefaultErrorMessage() {
setFunctionName(arguments);
$(DEF_ERR_MSG_ID).remove();
}
function loadRopot() {
setFunctionName(arguments);
loadLogin();
loadAccompanyingText();
loadRopotContent();
loadRopotButton();
var is1stOpd = true;
var $extSrvTxtAr = $(ID).closest('table').find(TST_STG);
try {
if (!(isChrome || isOpera)) {
updateButtonContainerPosition();
}
is1stOpd = loadRopotMetaData();
} catch (err) {
logError(err);
}
magicReverse();
loadComments();
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).find('div.selekty').append(''
+ '
'
+ BR
+ SEL_NUM + '
'
+ '
'
+ SEL_STMT_DESC_TXT_AR + N
+ SEL_STMT_TXT_AR
+ '
' + ''
+ ''
+ REM_SEL_BTN
+ ''
+ ''
+ '
'
+ '
'
+ '');
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 $popisHodnota = $(this).closest('div#relMdlContr, div.selContr').find('textarea.selStmtDesc').val();
var $selektHodnota = $(this).closest('div#relMdlContr, div.selContr').find('textarea.selStmt').val();
if ($popisHodnota == '' && $selektHodnota == '' ) {
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--;
} else if ($(ID).find('div.front1Error').val() == undefined) {
$(this).closest('div#relMdlContr, div.selContr2').append(
BR
+ '
'
+ 'Smažte prvně všechny texty z této odpovědi.'
+ '
'
);
// alert($popisHodnota + ' ' + $selektHodnota);
}
} catch (err) {
logError(err);
throw err;
}
}
$(document).ready(function () {
try {
var elementExists = document.getElementById("id-uzivatel"); // kontrola existence elementu
if (elementExists === null) { //kdyz tam neni
// Get user's name and UCO novej design
user = $('a.jmeno.show-for-medium').text();
uco = user.match(/\d+/)[0];
// alert(uco);
} else {
// Get user's name and UCO starej design
user = $('div#id-uzivatel').text();
uco = user.match(/\d+/)[0];
// alert(uco);
}
// Hidden textarea -- e: question to store values for external service
$extSrvTxtAr = $(ID).closest('table').find(TST_STG);
// if ($extSrvTxtAr =! null) {
// magicReverse();
// }
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(); nechapu na co a nefunguje mu to
} catch (err) {
logError(err);
}
// Responses to any buttons
$(LOG).find('button#loginButton').click(loginButtonF);
$(ID).find('button#addSel').click(addSelect);
$(ID).on('click', 'button.remSel', removeSelect);
$(ID).on('click', 'button.rstTxtArVal', resetStudentAnswer);
// $(ID).find('button#magie').click(magicButton);
// $(ID).find('button#magieR').click(magicReverse);
$tstForm.submit(saveRopot);
} catch (err) {
logError(err);
throw err;
}
});