var courseArray = [];
$(document).ready(function () {
readRecords();
$('#send_email_btn').attr("disabled", true);
$('#send_email_btn').addClass('inactive');
$('#excl_btn').attr("disabled", true);
$('#excl_btn').addClass('inactive');
(function ($) {
$.fn.inputFilter = function (callback, errMsg) {
return this.on("input keydown keyup mousedown mouseup select contextmenu drop focusout", function (e) {
if (callback(this.value)) {
if (["keydown", "mousedown", "focusout"].indexOf(e.type) >= 0) {
$(this).removeClass("input-error");
this.setCustomValidity("");
}
this.oldValue = this.value;
this.oldSelectionStart = this.selectionStart;
this.oldSelectionEnd = this.selectionEnd;
} else if (this.hasOwnProperty("oldValue")) {
$(this).addClass("input-error");
this.setCustomValidity(errMsg);
this.reportValidity();
this.value = this.oldValue;
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
} else {
this.value = "";
}
});
};
}(jQuery));
$("#phone").inputFilter(function (value) {
return /^-?\d*$/.test(value);
}, "A megadott érték nem szám");
$('select').on('change', function () {
readRecords();
$('#excel_c_id').val($('#course').val());
let archive = $('option:selected', this).attr('data-archive');
if (archive == 1 && $(this).attr('id') == 'course') {
$('#file_input').hide();
$('#pelda_excel').parent().hide();
$('#h_div').hide();
$('#excl_btn').hide();
$('#modal-div').hide();
$('#email_btn').hide();
$('#archiv_text').show();
} else if (archive != 1 && $(this).attr('id') == 'course') {
$('#file_input').show();
$('#pelda_excel').parent().show();
$('#h_div').show();
$('#excl_btn').show();
$('#modal-div').show();
$('#email_btn').show();
$('#archiv_text').hide();
}
});
$('#search_all').change(function () {
readRecords();
});
$("#course option").each(function () {
courseArray.push($(this).val());
});
// Check all checkboxes
$("#checkAll").click(function () {
let selected = [];
$('input:checkbox').not(this).prop('checked', this.checked);
$.each($("input[name='isEmailSend']:checked"), function () {
selected.push($(this).val());
});
checkEmailCheckboxes();
});
$('#excl_file').on('change', function () {
if ($('#excl_file')[0].files.length === 0) {
$('#excl_btn').attr("disabled", true);
$('#excl_btn').addClass('inactive');
} else {
$('#excl_btn').attr("disabled", false);
$('#excl_btn').removeClass('inactive');
}
});
});
var isHun = isDomainHun();
function showAddModal() {
$('#addModal').modal("show");
}
function emailModal() {
readEmailRecords();
}
function isDomainHun() {
return window.location.hostname == 'www.archline.hu';
}
function checkCheckboxes() {
let selected = [];
let allcheckbox = [];
$.each($("input[name='isEmailSend']:checked"), function () {
selected.push($(this).val());
});
$.each($("input[name='isEmailSend']"), function () {
allcheckbox.push($(this).val());
});
if ($("#checkAll").is(':checked') && selected.length !== allcheckbox.length)
$("#checkAll").prop("checked", false);
else if (!$("#checkAll").is(':checked') && selected.length === allcheckbox.length)
$("#checkAll").prop("checked", true);
checkEmailCheckboxes();
}
function checkEmailCheckboxes() {
let selected = [];
$.each($("input[name='isEmailSend']:checked"), function () {
selected.push($(this).val());
});
if (selected.length > 0) {
$('#send_email_btn').attr("disabled", false);
$('#send_email_btn').removeClass('inactive');
} else {
$('#send_email_btn').attr("disabled", true);
$('#send_email_btn').addClass('inactive');
}
}
// Get users
function readRecords() {
let action = "readrecord";
let search_name = $('#search_name').val();
let course_id = $("#course").val();
let coursesData = $('#search_all').is(':checked') ? courseArray : [];
let json = JSON.stringify(coursesData);
$.ajax({
url: "process.php",
type: "post",
data: {
action: action,
course_id: course_id,
search_name: search_name,
courseArray: json
},
dataType: 'json',
success: function (data) {
createTablerow(data);
},
error: function () {
$('#loading_gif').hide();
}
});
}
function readEmailRecords() {
let action = "readEmailRecord";
let course_id = $("#course").val();
$.ajax({
url: "process.php",
type: "post",
data: {
action: action,
course_id: course_id
},
dataType: 'json',
success: function (data) {
createEmailrow(data);
},
error: function () {
$('#loading_gif').hide();
}
});
}
function sendEmail() {
let lengthError, emailError, emailSuccess;
let action = "sendEmail";
let selected = [];
let course_id = $("#course").val();
let school_id = $('#school_id').val();
$.each($("input[name='isEmailSend']:checked"), function () {
selected.push($(this).val());
});
let jsonString = JSON.stringify(selected);
if (isHun) {
lengthError = 'Legalább 1 diákot ki kell választania!';
emailError = 'Sikertelen email küldés';
emailSuccess = 'Az emailek sikeresen el lettek küldve!';
} else {
lengthError = 'You must select at least 1 student.';
emailError = 'Failed to send email';
emailSuccess = 'Emails sent successfully.';
}
if (selected.length === 0) {
alert(lengthError);
return false;
}
$.ajax({
url: "process.php",
type: "post",
data: {
action: action,
course_id: course_id,
school_id: school_id,
selected: jsonString
},
dataType: 'json',
success: function (data) {
let msg = '';
let dataLen = data.length;
for (let i = 0; i < dataLen; i++) {
if (i === 0)
msg += data[i].email;
else
msg += ', ' + data[i].email;
}
if (msg !== "")
alert(emailError + ": " + msg);
else
alert(emailSuccess);
readEmailRecords();
},
error: function () {
$('#loading_gif').hide();
}
});
}
function createEmailrow(data) {
let no_result;
let dataLen = data.length;
let counter = 0;
if (isHun)
no_result = 'Nincs találat!';
else
no_result = 'No result found.';
$("#emailToStudents tr:not(:first)").remove();
for (let i = 0; i < dataLen; i++) {
let name = data[i].name;
let email = data[i].email;
let student_id = data[i].student_id;
let email_sent = data[i].email_sent;
let status = data[i].status;
if (email_sent === 0 && status === 1) {
$("#emailToStudents").append("
");
$("#tr2_" + i).append("
");
$("#tr2_" + i).append("
" + name + "
");
$("#tr2_" + i).append("
" + email + "
");
counter++;
}
}
if (counter === 0)
$("#emailToStudents").append("
" + no_result + "
");
}
function createTablerow(data) {
let no_result, edit, delete_user;
let dataLen = data.length;
let counter = 1;
if (isHun) {
no_result = 'Nincs találat!';
edit = 'Szerkesztés';
delete_user = 'Törlés';
} else {
no_result = 'No result found.';
edit = 'Edit';
delete_user = 'Delete';
}
$("#student_table tr:not(:first)").remove();
if (data === "empty") {
$("#student_table").append("
" + no_result + "
");
$('#email_btn').attr("disabled", true);
$('#email_btn').addClass('inactive');
return;
}
$('#email_btn').attr("disabled", false);
$('#email_btn').removeClass('inactive');
$("#student_table").append("");
for (let i = 0; i < dataLen; i++) {
let name = data[i].name;
let email = data[i].email;
let phone = data[i].phone;
let student_id = data[i].student_id;
let prPass = data[i].prPass;
let status = data[i].status;
let statusStr = getStudentStatus(status);
let stud_name = "'" + name + "'";
$("#student_table").append("
");
$("#tr_" + i).append("
" + counter + "
");
$("#tr_" + i).append("
" + name + "
");
$("#tr_" + i).append("
" + email + "
");
$("#tr_" + i).append("
" + phone + "
");
$("#tr_" + i).append("
" + statusStr + "
");
$("#tr_" + i).append("
-" + prPass + "
");
$("#tr_" + i).append('
');
counter++;
}
$("#student_table").append("");
}
function getStudentStatus(status) {
let passive, active, exam_passed;
let statusStr = '';
if (isHun) {
passive = 'Passzív';
active = 'Aktív';
exam_passed = 'Levizsgázott';
} else {
passive = 'Passive';
active = 'Active';
exam_passed = 'Passed the exam';
}
switch (status) {
case 0:
statusStr = passive;
break;
case 1:
statusStr = active;
break;
case 2:
statusStr = exam_passed;
break;
default:
break;
}
return statusStr;
}
function clearInputs() {
$('#name').val('');
$('#email').val('');
$('#email2').val('');
$('#phone').val('');
}
function showError(element, message) {
$(element).show();
$(element).html(message);
}
function hideError(element) {
if ($(element).is(":visible")) {
setTimeout(function () {
$(element).fadeOut(1000);
}, 3000);
}
}
// Add new user
function addRecord() {
let name = $('#name').val();
let email = $('#email').val();
let phone = $('#phone').val();
let schoolid = $('#school_id').val();
let course_id = $("#course").val();
let action = "create";
if (checkInput(action) === false) {
hideError("#error_div");
return false;
}
$.ajax({
url: "process.php",
type: "post",
data: {
name: name,
email: email,
phone: phone,
schoolid: schoolid,
course_id: course_id,
action: action
},
success: function (data) {
if (data.length > 0) {
showError("#error_div", JSON.parse(data));
hideError("#error_div");
} else {
readRecords();
clearInputs();
$("#error_div").hide();
}
},
error: function () {
$('#loading_gif').hide();
}
});
}
// Get the selected user's details
function GetUserDetails(editid) {
$('#hidden_user_id').val(editid);
let action = "details";
$.post("process.php", {
editid: editid,
action: action
}, function (data, status) {
let user = JSON.parse(data);
$('#updatename').val(user.name);
$('#updateemail').val(user.email);
$('#updateemail2').val(user.email);
$('#updatephone').val(user.phone);
$('#courses').val(user.course_id);
$('#status').val(user.status);
}
);
$('#editModal').modal("show");
}
// Update the selected user
function updateUserDetails() {
let name = $('#updatename').val();
let email = $('#updateemail').val();
let phone = $('#updatephone').val();
let hidden_user_id = $('#hidden_user_id').val();
let course_id = $('#courses').val();
let status = $('#status').val();
let action = "update";
let isDelete = "N";
if (status == 2) {
if (!isHun)
msg = "Do you want " + name + " to be removed from the list?";
else
msg = "Szeretné-e, ha a listából törlődne " + name + "?";
let conf = confirm(msg);
if (conf === true)
isDelete = "Y";
}
if (checkInput(action) === false) {
hideError("#error_div2");
return false;
}
$.post("process.php", {
name: name,
email: email,
phone: phone,
hidden_user_id: hidden_user_id,
course_id: course_id,
status: status,
isDelete: isDelete,
action: action
}, function () {
$("#editModal").modal("hide");
$("#error_div2").hide();
readRecords();
}
);
}
// Delete the selected user
function DeleteUser(deletedid, name) {
let msg = "";
let action = "delete";
if (!isHun)
msg = "Are you sure you want to delete the selected student: " + name + "?";
else
msg = "Csak abban az esetben törölje, ha a hallgató félbe hagyta a tanulmányait. Biztosan törölni szeretné a következő diákot: " + name + "?";
let conf = confirm(msg);
if (conf === true) {
$.ajax({
url: "process.php",
type: "post",
data: {
deletedid: deletedid,
action: action
},
success: function () {
readRecords();
},
error: function () {
$('#loading_gif').hide();
}
});
}
}
function readExcel() {
let filename = $('input[type=file]').val().split('\\').pop();
let ext = filename.split('.').pop();
let action = "excel_import";
let schoolid = $('#school_id').val();
let course_id = $("#course").val();
let file_data = $('#excl_file').prop('files')[0];
let form_data = new FormData();
if (ext !== 'xlsx') {
if (isHun) {
alert("Nem megfelelő a file formátuma!");
} else {
alert("Incorrect file format.");
}
return false;
}
form_data.append('file', file_data);
form_data.append('schoolid', schoolid);
form_data.append('course_id', course_id);
form_data.append('action', action);
$('#loading_gif').show();
$.ajax({
url: "process.php",
type: "post",
cache: false,
contentType: false,
processData: false,
data: form_data,
success: function (data) {
data = JSON.parse(data);
if (data.length > 0) {
let msg = "";
for (let i = 0; i < data.length; i++) msg += data[i] + "\n";
alert(msg);
}
$('#loading_gif').hide();
readRecords();
},
error: function () {
$('#loading_gif').hide();
}
});
}
function checkInput(action) {
let name, email, email2, phone, element, nameError, emailError, email2Error, phoneError, email1_2Error;
if (isHun) {
nameError = 'A név mező nincs kitöltve!';
emailError = 'Az email mező nincs kitöltve!';
email2Error = 'Az email mező nincs megerősítve!';
phoneError = 'A telefonszám mező nincs kitöltve!';
email1_2Error = 'A két email nem egyezik!';
} else {
nameError = 'Empty field: name.';
emailError = 'Empty field: email.';
email2Error = 'Empty field: confirm email.';
phoneError = 'Empty field: telephone.';
email1_2Error = 'The two emails do not match.';
}
if (action === 'create') {
name = $('#name').val();
email = $('#email').val();
email2 = $('#email2').val();
phone = $('#phone').val();
element = "#error_div";
} else if (action === 'update') {
name = $('#updatename').val();
email = $('#updateemail').val();
email2 = $('#updateemail2').val();
phone = $('#updatephone').val();
element = "#error_div2";
}
if (name === '') {
showError(element, nameError);
return false;
}
if (email === '') {
showError(element, emailError);
return false;
}
if (email2 === '') {
showError(element, email2Error);
return false;
}
if (phone === '' && isHun) {
showError(element, phoneError);
return false;
}
if (email !== email2) {
showError(element, email1_2Error);
return false;
}
}