$(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 () { $.ajax({ type: "POST", url: "/admin", data: { "cmd": "reset_password", "value": $("#reset_number").val() } }); } ); $("#inserir_novo_aluno").click( function () { $.ajax({ type: "POST", url: "/admin", data: { "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 $.ajax({ type: "POST", url: "/admin", data: {"cmd": "allow", "value": this.name} }); } else { // $(this).parent().parent().removeClass("active"); $.ajax({ type: "POST", url: "/admin", data: {"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({ stateSave: true, 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' : ''; var hora_inicio = d['start_time'] ? ' ' + d['start_time'].slice(11,19) + '': ''; var g = d['grades']; t[i] = []; t[i][0] = ' ' + uid; t[i][1] = d['name']; t[i][2] = password_defined + hora_inicio; var gbar = ''; for (var j=0; j < g.length; j++) gbar += '
' + generate_grade_bar(g[j][0]) + '
'; t[i][3] = gbar; } return t; } }, }); setInterval( function () { table.ajax.reload(); }, 3000 ); } populateHeader(); // run once when the page is loaded populateStudentsTable(); button_handlers(); // assign handlers to buttons });