MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 8.0.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/demo.imensosoftware.com/matrix/js/common.js
var app = {};

app.document = { docId: "", rowId: "", assignment_no: "" };

app.location = window.location.origin + "/matrix/";

if (window.location.hostname == "sh018.hostgator.tempwebhost.net") {
    app.location = "http://sh018.hostgator.tempwebhost.net/~a16102wt/matrix/";
}

$(document).ready(function () {

    // Get assignment_no safely
    const urlParams = new URLSearchParams(window.location.search);
    app.document.assignment_no = urlParams.get('assignment_no');

    app.getData();
});


/* ================= CLICK ROW ================= */

$(document).on('click', '#row_link', function () {

    app.document.docId = $(this).data('doc_id');
    app.document.rowId = $(this).data('row_id');

    $.ajax({
        url: app.location + "post_data.php",
        type: "POST",
        data: {
            get_detail: true,
            assignment_no: app.document.assignment_no,
            docId: app.document.docId
        },
        dataType: "json",
        success: function (result) {

            if (!result) return;

            $("#title").val(result.doc_title || "");
            $("#date").val(result.doc_date || "");
            $("#description").val(result.doc_description || "");

        },
        error: function (err) {
            console.log(err);
        }
    });

    app.clickToDocId();
});


/* ================= SUBMIT BUTTON ================= */

$(document).on('click', '#submit_button', function (e) {
    e.preventDefault();
    app.handleForm();
});


/* ================= GET DATA ================= */

app.getData = function () {

    var default_length = $('ul.list-group > li.default').length;
    var done_length = $('ul.list-group > li.done').length;
    var doubt_length = $('ul.list-group > li.doubt').length;

    $('#doc_default').html(default_length);
    $('#doc_done').html(done_length);
    $('#doc_doubt').html(doubt_length);
    $('#total_doc').html(default_length + done_length);

    if (default_length > 0) {
        $('ul.list-group > li.default:first>a').trigger('click');
        $('ul.list-group > li.default:first').addClass('active');
    }
};


/* ================= CLICK TO DOC ================= */

app.clickToDocId = function () {

    if (!app.document.assignment_no || !app.document.docId) return;

    $("#row_iframe").attr(
        "src",
        "docs/" + app.document.assignment_no + "/" + app.document.docId + ".pdf"
    );

    $("#rowId").val(app.document.rowId);
    $("#docId").val(app.document.docId);

    $('ul.list-group > li.active').removeClass('active');

    $('ul')
        .find('[data-doc_id="' + app.document.docId + '"]')
        .closest('li')
        .addClass('active');
};


/* ================= HANDLE FORM ================= */

app.handleForm = function () {
    app.addForm();
    app.submitForm();
};


/* ================= ADD FORM ================= */

app.addForm = function () {

    app.formData = {
        assignment_no: app.document.assignment_no,
        docId: app.document.docId,
        rowId: app.document.rowId,
        title: $('#title').val().trim(),
        date: $('#date').val().trim(),
        description: $('#description').val().trim()
    };
};


/* ================= VALIDATION ================= */

app.validateForm = function () {

    var error = [];

    if (app.formData.title === "") {
        error.push("Please enter title");
    }

    if (app.formData.date === "") {
        error.push("Please enter start date");
    }

    if (app.formData.description === "") {
        error.push("Please enter description");
    }

    return error;
};


/* ================= SUBMIT FORM ================= */

app.submitForm = function () {

    var errors = app.validateForm();

    if (errors.length > 0) {
        alert(errors.join("\n"));
        return false;
    }

    $.ajax({
        url: app.location + "post_data.php",
        type: "POST",
        data: { formData: app.formData },
        dataType: "json",

        success: function (result) {

            if (!result) return;

            var activeItem = $('ul.list-group > li.active');

            if (result.status === "default") {

                if (activeItem.find('a').data('doc_id') == app.document.docId) {
                    activeItem
                        .addClass('default')
                        .removeClass('done doubt');
                }

            } else {

                if (activeItem.find('a').data('doc_id') == app.document.docId) {

                    if (result.status === "doubt") {
                        activeItem.addClass('doubt');
                    }

                    activeItem
                        .addClass('done')
                        .removeClass('default');
                }

                activeItem.removeClass('active');

                var default_length = $('ul.list-group > li.default').length;

                if (default_length > 0) {
                    $('ul.list-group > li.default:first>a').trigger('click');
                    $('ul.list-group > li.default:first').addClass('active');
                }

                $('#formAddRow')[0].reset();

                app.getData();

                if ($('ul.list-group > li.done').length === $('ul.list-group > li').length) {
                    app.myFunction();
                }
            }
        },

        error: function (err) {
            console.log(err);
        }
    });
};


/* ================= FINAL SUBMIT ================= */

app.myFunction = function () {

    var confirmSubmit = confirm("Do you want to submit the file!");

    if (!confirmSubmit) return;

    $.ajax({
        url: app.location + "post_data.php",
        type: "POST",
        data: {
            assignment_no: app.document.assignment_no,
            assignment_status_id: 2
        },
        dataType: "json",

        success: function (result) {

            if (!result) return;

            if (result.status === "fail") {
                alert(result.message);
            } else {
                window.location.href = app.location;
            }
        },

        error: function (err) {
            console.log(err);
        }
    });
};