// from: https://www.tornadoweb.org/en/stable/guide/security.html // with changes: removed datatype and callback from original postJSON function getCookie(name) { var r = document.cookie.match("\\b" + name + "=([^;]*)\\b"); return r ? r[1] : undefined; } jQuery.postJSON = function(url, args) { args._xsrf = getCookie("_xsrf"); $.ajax({url: url, data: $.param(args), type: "POST"}); }; // --------------------------------------------------------------------------- $(document).ready(function() { function button_handlers() { // button handlers (runs once) $("#allow_all").click( function() { $(":checkbox").prop("checked", true).trigger('change'); } ); $("#deny_all").click( function() { $(":checkbox").prop("checked", false).trigger('change'); } ); $("#reset_password").click( function () { $.postJSON("/admin", { "cmd": "reset_password", "value": $("#reset_number").val() }); } ); $("#inserir_novo_aluno").click( function () { $.postJSON("/admin", { "cmd": "insert_student", "value": JSON.stringify({ "number": $("#novo_numero").val(), "name": $("#novo_nome").val() }) }); } ); // authorization checkboxes in the students_table: $("tbody", "#students_table").on("change", "input", autorizeStudent); } // ---------------------------------------------------------------------- // checkbox handler to allow/deny students individually function autorizeStudent(e) { if (this.checked) { // $(this).parent().parent().addClass("table-primary"); // row class $.postJSON("/admin", {"cmd": "allow", "value": this.name}); } else { // $(this).parent().parent().removeClass("active"); $.postJSON("/admin", {"cmd": "deny", "value": this.name}); } } // ---------------------------------------------------------------------- function generate_grade_bar(grade) { var barcolor; if (grade < 10) barcolor = 'bg-danger'; else if (grade < 15) barcolor = 'bg-warning'; else barcolor = 'bg-success'; return '
' + grade + '
'; } // ---------------------------------------------------------------------- function populateHeader() { $.ajax({ url: "/admin", data: {"cmd": "test", "value": ""}, dataType: "json", success: function(data) { // fill jumbotron data $("#title").html(data['data']['title']); $("#ref").html(data['data']['ref']); $("#filename").html(data['data']['filename']); $("#database").html(data['data']['database']); $("#answers_dir").html(data['data']['answers_dir']); }, error: function() {alert("Servidor não responde.");} }); } // ---------------------------------------------------------------------- function populateStudentsTable() { var table = $('#students_table').DataTable({ columnDefs: [{ "searcheable": false, "orderable": false, "targets": 0 }], ordering: true, order: [[ 3, "asc"]], stateSave: false, paging: false, responsive: true, language: { search: "Pesquisar: ", info: "Visíveis _TOTAL_ alunos", infoEmpty: "Visíveis _TOTAL_ alunos", infoFiltered: "(filtrados de _MAX_ no total)", zeroRecords: "Não encontrado", emptyTable: "Não há alunos inscritos", }, ajax: { url: "admin", // students_table data: {"cmd": "students_table", "value": ""}, dataType: "json", dataSrc: function ( json ) { var t = []; for ( var i=0; i < json.data.length ; i++ ) { d = json.data[i]; var uid = d['uid']; var checked = d['allowed'] ? 'checked' : ''; var password_defined = d['password_defined'] ? ' ' : ''; var hora_inicio = d['start_time'] ? ' ' + d['start_time'].slice(11,19) + '': ''; var g = d['grades']; t[i] = []; t[i][0] = ''; //i+1; t[i][1] = ' '; t[i][2] = uid; t[i][3] = d['name']; t[i][4] = password_defined + hora_inicio; var gbar = ''; for (var j=0; j < g.length; j++) gbar += '
' + generate_grade_bar(g[j][0]) + '
'; t[i][5] = gbar; } return t; } }, }); table.on('order.dt search.dt', function () { table.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) { cell.innerHTML = i+1; }); }).draw(); setInterval( function () { table.ajax.reload(); }, 3000 ); } populateHeader(); // run once when the page is loaded populateStudentsTable(); button_handlers(); // assign handlers to buttons });