admin.js
5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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 '<div class="progress"><div class="progress-bar ' + barcolor
+ '" role="progressbar" aria-valuenow="' + grade
+ '" aria-valuemin="0" aria-valuemax="20" style="min-width: 2em; width: '
+ (5*grade) + '%;">' + grade + '</div></div>';
}
// ----------------------------------------------------------------------
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 < json.data.length ; i++ ) {
d = json.data[i];
var uid = d['uid'];
var checked = d['allowed'] ? 'checked' : '';
var password_defined = d['password_defined'] ? ' <span class="badge badge-secondary"><i class="fa fa-key" aria-hidden="true"></i></span>' : '';
var hora_inicio = d['start_time'] ? ' <span class="badge badge-success">' + d['start_time'].slice(11,19) + '</span>': '';
var g = d['grades'];
t[i] = [];
t[i][0] = i+1;
t[i][1] = '<input type="checkbox" name="' + uid + '" value="true"' + checked + '> ';
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 += '<div data-toggle="tooltip" data-placement="top" title="' + g[j][1].slice(0,19) + '"><a href="review?test_id=' + g[j][2] + '">' + generate_grade_bar(g[j][0]) + '</a></div>';
t[i][5] = gbar;
}
return t;
}
},
});
setInterval( function () {
table.ajax.reload();
}, 3000 );
}
populateHeader(); // run once when the page is loaded
populateStudentsTable();
button_handlers(); // assign handlers to buttons
});