1
Fork 0
mirror of https://github.com/Steffo99/registro-fermi.git synced 2024-11-22 08:04:28 +00:00
registro-steffo/server/marks.js

514 lines
10 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.addMark = function(sessionID, username, mark, subject, desc, term, response, callback){
utils.checkProfSession(sessionID, function(row, response){
if(response){
console.log("Ops!")
callback(null, response);
}
if(row){
var user = utils.rowToString(sessid, "username");
console.log("SessID found for " + user + "!");
var getClasses = "SELECT classes FROM prof_classes WHERE username = ?";
db.query(getClasses, [user], function(err, classes){
if(classes.length > 1){
console.log("WTF?");
response = {
success: false,
error: "generic"
}
callback(null, response);
}
if(classes.length == 1){
console.log("Classi trovate!");
var classes = utils.rowToString(classes, "classes");
var cArr = classes.split(",");
var getrawClass = "SELECT class FROM users WHERE username = ? AND rank = 'stud'";
db.query(getrawClass, [username], function(err, classn){
var rawClass = utils.rowToString(classn, "class");
console.log(jClass);
if(err){throw err; response = {success: false, errordesc: "fatal"}}
if(classn.length == 0){
console.log("Studente inesistente");
response = {
success: false,
error: "username_does_not_exist"
}
callback(null, response);
}
if(classn.length == 1){
if(cArr.contains(rawClass)){
console.log("Autorizzato!");
var unixdate = Math.round((new Date()).getTime() / 1000);
var addMarkQuery = "INSERT INTO global_marks (username, class, mark, subject, description, mark_date, term) VALUES(?,?,?,?,?,?,?)";
db.query(addMarkQuery, [username, rawClass, mark, subject, desc, unixdate, term], function(err, res){
if(err){ throw err; response = { success: false, errordesc: "fatal" } }
console.log("Voto aggiunto!");
response = {
success: true,
student: username,
class: rawClass,
"mark": mark,
"subject": subject,
description: desc,
date: unixdate,
term: term
}
callback(null, response);
});
}else{
console.log("Non autorizzato");
response = {
success: false,
error: "Unauthorized"
}
callback(null, response);
}
}
});
}
});
}
});
}
module.exports.getMarks = function(sessionid, student, response, callback){
utils.checkSession(sessionid, function(row, response){
if(response){
console.log("Ops!");
callback(null, response);
}
if(row){
var rank = utils.rowToString(row, "rank");
var username = utils.rowToString(row, "username");
console.log(rank, username);
switch (rank) {
case 'prof':
console.log("è un prof!");
db.query("SELECT class FROM users WHERE username = ?", [student], function(err, row){
if(row.length == 0){
console.log("Nessuno studente con quell'username");
response = {
success: false,
errordesc: "no_student_found"
}
callback(null, response);
}
if(err){
throw err;
response = {
success: false,
errordesc: "fatal"
}
callback(null, response);
return false;
}
if(row.length == 1){
console.log("Classe dello studente trovata");
var rawClass = utils.rowToString(row, "class");
db.query("SELECT classes FROM prof_classes WHERE username = ?", [username], function(err, row){
if(row.length == 0){
console.log("Nessun prof con quell'username");
response = {
success: false,
errordesc: "no_prof_found"
}
callback(null, response);
}
if(err){
throw err;
response = {
success: false,
errordesc: "fatal"
}
callback(null, response);
return false;
}
if(row.length == 1){
var rawClasses = utils.rowToString(row, "classes");
var classesArray = rawClasses.split(",");
console.log(classesArray);
if(classesArray.contains(rawClass)){
console.log("Professore autorizzato a leggere i voti dello studente " + student);
db.query("SELECT * FROM global_marks WHERE username = ?", [student], function(err, marks){
console.log("Query per le info dei voti eseguita!");
if(marks.length == 0){
if(err) callback(err, null);
console.log("Nessun voto.");
response = {
success: false,
error: 'no_marks_found'
}
callback(null, response);
}
if(marks.length > 0){
console.log("Voto/i trovato/i per " + username);
response = {
success: true,
list: marks
}
callback(null, response);
}
});
}else{
console.log("Professore non autorizzato!");
response = {
success: false,
errordesc: "not_of_this_class"
}
callback(null, response);
return false;
}
}
});
}
});
break;
case 'stud':
console.log("è uno studente!");
db.query("SELECT * FROM global_marks WHERE username = ?", [student], function(err, marks){
console.log("Query per le info dei voti eseguita!");
if(marks.length == 0){
if(err) callback(err, null);
console.log("Nessun voto.");
response = {
success: false,
error: 'no_marks_found'
}
callback(null, response);
}
if(marks.length > 0){
console.log("Voto/i trovato/i per " + username);
response = {
success: true,
list: marks
}
callback(null, response);
}
});
break;
default:
break;
}
}
});
}
module.exports.alterMark = function(sessionid, newmark, newdesc, markid, response, callback){
db.query("SELECT username FROM sessions WHERE session_id = ? AND rank = 'prof'", [sessionid], function(err, row){
if(err){
throw err;
response = {
success: false,
error: "fatal"
}
callback(null, response);
return false;
}
if(row.length == 0){
response = {
success: false,
error: "invalid_sessionid"
}
callback(null, response);
}
if(row.length == 1){
var username = utils.rowToString(row, "username");
db.query("SELECT class FROM global_marks WHERE id = ?", [markid], function(err, row){
if(err){
throw err;
response = {
success: false,
error: "fatal"
}
callback(null, response);
return false;
}
if(row.length == 0){
response = {
success: false,
error: "no_id_found"
}
callback(null, response);
}
if(row.length == 1){
var rawClass = utils.rowToString(row, "class");
db.query("SELECT classes FROM prof_classes WHERE username = ?", [username], function(err, row){
if(row.length == 0){
console.log("Nessun prof con quell'username");
response = {
success: false,
errordesc: "no_prof_found"
}
callback(null, response);
}
if(err){
throw err;
response = {
success: false,
errordesc: "fatal"
}
callback(null, response);
return false;
}
if(row.length == 1){
var rawClasses = utils.rowToString(row, "classes");
var classesArray = rawClasses.split(",");
if(classesArray.contains(rawClass)){
console.log("Professore autorizzato a modificare il voto");
db.query("UPDATE global_marks SET mark = ?, description = ? WHERE id = ?", [newmark, newdesc, markid], function(err, success){
if(err){
throw err;
response = {
success: false,
errordesc: "fatal"
}
callback(null, response);
return false;
}
response = {
success: true,
newmark: newmark,
newdesc: newdesc
}
callback(null, response);
});
}else{
console.log("Professore non autorizzato!");
response = {
success: false,
errordesc: "not_of_this_class"
}
callback(null, response);
return false;
}
}
});
}
});
}
});
}