$(document).ready(function() { // button handlers (runs once) function define_buttons_handlers() { $("#allow_all").click( function() { $(":checkbox").prop("checked", true).trigger('change'); } ); $("#deny_all").click( function() { $(":checkbox").prop("checked", false).trigger('change'); } ); $("#reset_password").click( function () { var number = $("#reset_number").val(); $.ajax({ type: "POST", url: "/adminwebservice", data: {"cmd": "reset", "name": number} }); } ); $("#inserir_novo_aluno").click( function () { $.ajax({ type: "POST", url: "/adminwebservice", data: { "cmd": "insert", "number": $("#novo_numero").val(), "name": $("#novo_nome").val() } }); } ); } // ---------------------------------------------------------------------- // checkbox handler to allow/deny students individually function autorizeStudent(e) { $.ajax({ type: "POST", url: "/adminwebservice", data: {"cmd": "allow", "name": this.name, "value": this.checked} }); if (this.checked) $(this).parent().parent().addClass("active"); else $(this).parent().parent().removeClass("active"); } // ---------------------------------------------------------------------- function populateOnlineTable(students) { var rows = ""; // make list of online students var active = []; $.each(students, function(i, r) { if (r['start_time'] != '') { active.push([r['uid'], r['name'], r['start_time'], r['ip_address'], r['user_agent'], r['focus']]); } }); // sort by start time active.sort(function(a,b){return a[2] < b[2] ? -1 : (a[2] == b[2] ? 0 : 1);}); n = active.length; for(var i = 0; i < n; i++) { rows += '\ ' + active[i][0] + '\ ' + active[i][1] + '\ ' + active[i][2].slice(11,19) + '\
' + active[i][3] + '
\ ' + (active[i][5]? '' : 'unfocus') + '\ '; } $("#online_students").html(rows); $("#online-header").html(n + " Activo(s)"); } // ---------------------------------------------------------------------- function generate_grade_bar(grade) { var barcolor; if (grade < 10) { barcolor = 'progress-bar-danger'; } else if (grade < 15) { barcolor = 'progress-bar-warning'; } else { barcolor = 'progress-bar-success'; } var bar = '
' + grade + '
'; return bar } // ---------------------------------------------------------------------- function populateStudentsTable(students) { var n = students.length; $("#students-header").html(n + " Inscritos") var rows = ""; $.each(students, function(i, d) { var uid = d['uid']; if (d['start_time'] != '') // test rows += ''; else if (d['online']) // online rows += ''; else if (d['allowed']) // allowed rows += ''; else // offline rows += ''; rows += '\ ' + (d['start_time']=='' ? '' : ' teste') + // (d['online'] ? 'online' : '') + '\ ' + uid + '\ ' + d['name'] + (d['password_defined'] ? ' pw' : '') +'\ '; var g = d['grades']; var glength = g.length; for (var i=0; i < glength; i++) { rows += '
' + generate_grade_bar(g[i][0]) + '
'; } rows += ''; }); $("#students").html(rows); $('[data-toggle="tooltip"]').tooltip(); } // ---------------------------------------------------------------------- function populate() { $.ajax({ url: "/adminwebservice", dataType: "json", success: function(data) { // show clock on upper left corner var t = new Date(); $('#currenttime').html(t.getHours() + (t.getMinutes() < 10 ? ':0' : ':') + t.getMinutes()); // fill jumbotron data $("#title").html(data['test']['title']); $("#ref").html(data['test']['ref']); $("#filename").html(data['test']['filename']); $("#database").html(data['test']['database']); $("#answers_dir").html(data['test']['answers_dir']); // fill online and student tables populateOnlineTable(data["students"]); populateStudentsTable(data["students"]) // add event handlers $('input[type="checkbox"]').change(autorizeStudent); }, error: function() {alert("Servidor não responde.");} }); } populate(); // run once when the page is loaded define_buttons_handlers(); setInterval(populate, 5000); // poll server on 5s interval });