1
Fork 0
mirror of https://github.com/Steffo99/registro-fermi.git synced 2024-11-28 19:14:26 +00:00
registro-steffo/server/tests.js

353 lines
6 KiB
JavaScript
Raw Normal View History

2016-09-15 14:37:26 +00:00
/* Copyright © 2016 Stefano Pigozzi, Emiliano Maccaferri
* Released under the GPL v3 license
*/
var db = require("./connection");
var utils = require("./utils");
module.exports.addTest = function(sessionid, classname, subject, description, date, response, callback){
utils.checkProfSession(sessionid, function(row, response){
console.log("Controllando la sessione...");
if(response){
console.log("Sessione invalida!");
callback(null, response);
console.log(response)
return false;
}
if(row){
console.log("Sessione valida!");
var username = utils.rowToString(row, "username");
utils.checkIfProfInClass(username, classname, function(row, response){
if(response){
console.log("Non in questa classe!");
callback(null, response);
}
if(row){
db.query("INSERT INTO tests (class, subject, description, date) VALUES(?,?,?,?)", [classname, subject, description, date], function(err, success){
if(err){
console.log("Errore nell'aggiunta della verifica!");
throw err;
response = {
success: false,
errordesc: "fatal"
}
callback(null, response);
}
if(success){
response = {
success: true
}
callback(null, response);
console.log("Verifica aggiunta!");
}
});
}
});
}
});
}
module.exports.removeTest = function(sessionid, testid, response, callback){
utils.checkProfSession(sessionid, function(row, res){
if(res){
console.log("Sessione invalida!");
callback(null, res);
return false;
}
if(row){
console.log("Sessione valida!");
db.query("DELETE FROM tests WHERE id = ?", [testid], function(err, success){
if(err){
throw err;
response = {
success: false,
errordesc: "fatal"
}
callback(null, response);
return false;
}
response = {
success: true
}
callback(null, response);
});
}
});
}
module.exports.alterTest = function(sessionid, classname, testid, description, date, response, callback){
utils.checkProfSession(sessionid, function(row, response){
if(response){
console.log("Sessione invalida!");
callback(null, response);
return false;
}
if(row){
var username = utils.rowToString(row, "username");
utils.checkIfProfInClass(username, classname, function(row, response){
if(response){
console.log("Sessione invalida!");
callback(null, response);
return false;
}
if(row){
db.query("UPDATE tests SET description = ?,date = ? WHERE id = ?", [description, date, testid], function(err, success){
if(err){
throw err;
response = {
success: false,
errordesc: "fatal"
}
callback(null, response);
return false;
}
console.log("Ok, aggiornato!");
response = {
success: true,
newdesc: description,
newdate: date
}
callback(null, response);
});
}
});
}
});
}
module.exports.getTests = function(sessionid, classname, response, callback){
utils.checkSession(sessionid, function(row, response){
if(response){
console.log("Sessione invalida!");
callback(null, response);
return false;
}
if(row){
console.log("Creazione rank ed username");
var rank = utils.rowToString(row, "rank");
var username = utils.rowToString(row, "username");
console.log("r/u creati! \n", rank, username);
switch(rank){
case "prof":
console.log("è un prof!");
utils.checkIfProfInClass(username, classname, function(row, response){
if(response){
console.log("Non di questa classe");
callback(null, response);
return false;
}
if(row){
db.query("SELECT * FROM tests WHERE class = ?", [classname], function(err, rows){
if(err){
throw err;
response = {
success: false,
errordesc: "fatal"
}
callback(null, response);
return false;
}
if(rows.length > 0){
console.log("Verifiche trovate!");
response = {
success: true,
list: rows
}
callback(null, response);
}
if(rows.length == 0){
response = {
success: false,
errordesc: "no_tests_found"
}
callback(null, response);
return false;
}
});
}
});
break;
case "stud":
console.log("è uno studente!");
db.query("SELECT * FROM tests WHERE class = ?", [classname], function(err, rows){
if(err){
throw err;
response = {
success: false,
errordesc: "fatal"
}
callback(null, response);
return false;
}
if(rows.length > 0){
console.log("Verifiche trovate!");
response = {
success: true,
list: rows
}
callback(null, response);
}
if(rows.length == 0){
response = {
success: false,
errordesc: "no_tests_found"
}
callback(null, response);
return false;
}
});
break;
default:
break;
}
}
});
}