Newer
Older
"use strict";
$(document).ready(() => {
let settings = getSettings();
const pathToSfeduAPIAdapter = $('#sync_api_url').html().trim();
let loadedDisciplines = {};
let allGroups = {};
let $groups = $('#json_groups');
let groupsRaw = $.parseJSON($groups.html() || '{}');
Object.values(groupsRaw).forEach((value) => {
value.groups.forEach((group) => {
let newGroup = {
groupNum: group.GroupNum,
groupID: group.ID,
gradeID: value.grade_id,
gradeNum: value.grade_num,
Anton Bagliy
committed
formID: group.FormID,
formName: group.FormName
};
allGroups[group.ID] = newGroup;
});
});
$groups.remove();
function customMenu(node) {
var items = {
export: {
label: "Выгрузить",
action: function (obj) {
const selected = $('#groupTree').jstree().get_selected();
Anton Bagliy
committed
function processDiscipline(selectedDiscipline, callback) {
let logDisciplineError = function (groupID, disciplineID, errorID, url) {
$.post(g_URLdir + url, {groupID: groupID, disciplineID: disciplineID, error: errorID}).success((disciplines) => {
}).fail(jqXHR => {
const status = parseInt(jqXHR.status);
let message = errorText;
try {
if (status === 400)
message = JSON.parse(jqXHR.responseText).message;
} catch (error) {
}
if (status !== 0)
Popup.error(message);
});
Anton Bagliy
committed
try {
if (selectedDiscipline.startsWith("discipline_")) {
const singleNumRegexp = /\w_(\d+)/;
const parseClass = function (str) {
singleNumRegexp.lastIndex = 0;
let foundMatch = singleNumRegexp.exec(str);
return foundMatch ? parseInt(foundMatch[1]) : 0;
};
const disciplineID = parseClass(selectedDiscipline);
const disciplineInfo = loadedDisciplines[disciplineID];
const parent = $('#groupTree').jstree().get_parent(selectedDiscipline);
const groupID = parseClass(parent);
const groupInfo = allGroups[groupID];
//console.log(groupInfo);
//console.log(disciplineInfo);
const planExtId = "000" + disciplineInfo.PlanExternalID;
const disciplineExtId = disciplineInfo.DisciplineExternalID;
$.get(pathToSfeduAPIAdapter + '/forms', {
accountid: User.accountid,
Anton Bagliy
committed
year: settings.year.toString() + "-" + (settings.year + 1).toString().substr(2, 2),
semester: settings.semesterNum,
plan: planExtId,
disciplineID: disciplineID,
Anton Bagliy
committed
discipline: disciplineExtId,
groupnum: groupInfo.groupNum,
groupid: groupInfo.groupID,
Anton Bagliy
committed
gradenum: groupInfo.gradeID,
Anton Bagliy
committed
study_form: groupInfo.formID,
Anton Bagliy
committed
faculty: settings.facultyID
}).success((response) => {
console.log(response);
let errorMessage = '';
if (response[1].Errors === '') {
Popup.success(`ведомость по дисциплине ${disciplineInfo.SubjectName} №${disciplineID} для ${groupInfo.groupNum} группы ${groupInfo.gradeNum} курса выгружена`);
let node = $('#groupTree').jstree(true).get_node(selectedDiscipline);
//node.type = "discipline_broken";
$('#groupTree').jstree(true).set_type(node, "discipline_exported");
$('#groupTree').jstree(true).redraw(true);
Anton Bagliy
committed
callback();
} else {
Popup.error(`не выгружена ведомость для "${disciplineInfo.SubjectName}" №${disciplineID} для ${groupInfo.groupNum} группы ${groupInfo.gradeNum} курса`);
let node = $('#groupTree').jstree(true).get_node(selectedDiscipline);
//node.type = "discipline_broken";
$('#groupTree').jstree(true).set_type(node, "discipline_broken");
$('#groupTree').jstree(true).redraw(true);
Anton Bagliy
committed
let errorCode = 1;
Anton Bagliy
committed
if (typeof response[1].Errors.Error[1] !== 'undefined') {
errorMessage = response[1].Errors.Error[1]['faultstring'];
errorCode = 2;
Anton Bagliy
committed
if (typeof errorMessage === 'undefined') {
errorMessage = response[1].Errors.Error[1]['Description']
errorCode = 3;
Anton Bagliy
committed
}
}
if (errorMessage !== '' && typeof errorMessage !== 'undefined') {
errorCode = 4;
Anton Bagliy
committed
Popup.error(errorMessage);
}
logDisciplineError(groupInfo.groupID, disciplineInfo.ID, errorCode, 'handler/discipline/setDisciplineErrorForGroup');
Anton Bagliy
committed
callback();
}
}).fail(jqXHR => {
const status = parseInt(jqXHR.status);
let message = "";
Anton Bagliy
committed
try {
if (status === 400)
message = JSON.parse(jqXHR.responseText).message;
} catch (error) {
logDisciplineError(groupInfo.groupID, disciplineInfo.ID, 5, 'handler/discipline/setDisciplineErrorForGroup');
Anton Bagliy
committed
}
if (status !== 0)
logDisciplineError(groupInfo.groupID, disciplineInfo.ID, 6, 'handler/discipline/setDisciplineErrorForGroup');
Anton Bagliy
committed
Popup.error(message);
callback();
});
}
} catch(e) {
callback();
}
Anton Bagliy
committed
let requests = selected.reduce((promiseChain, item) => {
return promiseChain.then(() => new Promise((resolve) => {
processDiscipline(item, resolve)
}));
}, Promise.resolve());
requests.then(() => Popup.success('Выгружены все выделенные дисциплины'))
}
},
};
if (!node.id.startsWith("discipline_")) {
// Delete the "delete" menu item
delete items.export;
}
return items;
}
$(function () {
$('#groupTree').jstree(
{
"core": {
// so that create works
"check_callback": true,
error: function (err) {
console.log(err);
}
},
"types": {
"discipline_broken": {
"icon": "../static/img/icons/delete.png"
},
"discipline_exported": {
},
"discipline_new": {
"icon": "../static/img/icons/tick.png"
},
"discipline_outdated": {
}
},
"plugins": ["contextmenu", "types"],
"contextmenu": {"items": customMenu},
},
);
}
);
$('#groupTree').on("changed.jstree", function (e, data) {
const singleNumRegexp = /\w_(\d+)/;
const parseClass = function (str) {
singleNumRegexp.lastIndex = 0;
let foundMatch = singleNumRegexp.exec(str);
return foundMatch ? parseInt(foundMatch[1]) : 0;
};
let getDisciplines = function (groupID, url, onSuccess, onFail, successText, errorText) {
$.post(g_URLdir + url, {groupID: groupID}).success((disciplines) => {
onSuccess(JSON.parse(JSON.parse(disciplines).response));
}).fail(jqXHR => {
onFail();
const status = parseInt(jqXHR.status);
let message = "";
try {
if (status === 400)
message = JSON.parse(jqXHR.responseText).message;
} catch (error) {
}
if (status !== 0)
Popup.error(message);
});
};
if (data.selected[0] && data.selected[0].startsWith("group_")) {
if (!$('#groupTree').jstree().is_leaf(data.selected)) return;
let groupID = parseClass(data.selected[0]);
if (!data.node.data.loading) {
data.node.data.loading = true;
getDisciplines(groupID, 'handler/discipline/getDisciplinesForGroup',
function (disciplines) {
jQuery.each(disciplines, function (i, val) {
loadedDisciplines[i] = val;
$('#groupTree').jstree().create_node(data.selected, {
"id": "discipline_" + val.ID + "_group_" + groupID,
// TODO: refactor:
"type": val.lastExportDate ? (val.lastExportError ? "discipline_broken" : (val.isOutdated ? "discipline_outdated" : "discipline_exported")) : "discipline_new",
"text": val.lastExportDate ? val.SubjectName + " №" + val.ID + ", последняя дата: " + val.lastExportDate : val.SubjectName + " №" + val.ID ,
}, "last");
});
data.node.data.loading = false;
$('#groupTree').jstree().open_node(data.selected);
},
function () {
data.node.data.loading = false;
}, "дисциплины загрузились", "ошибка");
}
} else if (data.selected[0] && data.selected[0].startsWith("discipline_")) {
}
})