Solving problems with sessions and timestamps

parent 77b7ae86
...@@ -43,7 +43,7 @@ public abstract class Action { ...@@ -43,7 +43,7 @@ public abstract class Action {
final String param_id_dev="id_dev"; final String param_id_dev="id_dev";
final String param_timestamp="timestamp"; final String param_timestamp="timestamp";
final Date currentTime = new Date(); final Date currentTime = new Date();
SimpleDateFormat datetime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); SimpleDateFormat datetime = new SimpleDateFormat("yyyy-MM-ddTHH:mm:ss.SSSZ");
try { try {
JSONObject jsonObject = new JSONObject() JSONObject jsonObject = new JSONObject()
.put(param_id_stu, PCBcontext.getPcbdb().getCurrentUser().get_id_stu()) .put(param_id_stu, PCBcontext.getPcbdb().getCurrentUser().get_id_stu())
......
...@@ -196,7 +196,7 @@ END ...@@ -196,7 +196,7 @@ END
CREATE TRIGGER trg_insert_users_detail CREATE TRIGGER trg_insert_users_detail
INSTEAD OF INSERT ON users_detail INSTEAD OF INSERT ON users_detail
FOR EACH ROW FOR EACH ROW
WHEN NEW.pwd_stu NOT IS NULL WHEN NEW.pwd_stu IS NOT NULL
BEGIN BEGIN
INSERT OR REPLACE INTO student VALUES (NEW.id_stu, NEW.nickname_stu, NEW.pwd_stu, NEW.name_stu, NEW.surname_stu, NEW.url_img_stu, NEW.gender_stu, NEW.lang_stu, NEW.attributes_stu); INSERT OR REPLACE INTO student VALUES (NEW.id_stu, NEW.nickname_stu, NEW.pwd_stu, NEW.name_stu, NEW.surname_stu, NEW.url_img_stu, NEW.gender_stu, NEW.lang_stu, NEW.attributes_stu);
INSERT OR REPLACE INTO supervisor VALUES (NEW.id_sup, NEW.email_sup, NEW.pwd_sup, NEW.name_sup, NEW.surname_sup, NEW.url_img_sup, NEW.gender_sup, NEW.lang_sup, NEW.tts_engine_sup); INSERT OR REPLACE INTO supervisor VALUES (NEW.id_sup, NEW.email_sup, NEW.pwd_sup, NEW.name_sup, NEW.surname_sup, NEW.url_img_sup, NEW.gender_sup, NEW.lang_sup, NEW.tts_engine_sup);
......
...@@ -27,11 +27,11 @@ SET time_zone = "+00:00"; ...@@ -27,11 +27,11 @@ SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `action` ( CREATE TABLE IF NOT EXISTS `action` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(20) COLLATE utf8_unicode_ci NOT NULL CHECK (type IN ('Add','Select','Delete','Show','Unshow','Pause','tryinit','tryend','initsession','endsession','pausesession','resumesession')), `type` varchar(20) COLLATE utf8_unicode_ci NOT NULL CHECK (type IN ('Add','Select','Delete','Show','Unshow','Pause','tryinit','tryend','initsession','endsession','pausesession','resumesession')),
`timestamp` timestamp DEFAULT CURRENT_TIMESTAMP, `timestamp` timestamp(3) DEFAULT CURRENT_TIMESTAMP,
`id_sup` int(11) DEFAULT NULL, `id_sup` int(11) DEFAULT NULL,
`id_stu` int(11) NOT NULL, `id_stu` int(11) NOT NULL,
`id_try` int(11) DEFAULT NULL, `id_try` int(11) DEFAULT NULL,
`description` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'serialization of properties of the picto at the time of the action', `description` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'serialization of properties of the picto at the time of the action',
`gps_lat` float DEFAULT NULL, `gps_lat` float DEFAULT NULL,
`gps_lon` float DEFAULT NULL, `gps_lon` float DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
...@@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS `action` ( ...@@ -42,7 +42,7 @@ CREATE TABLE IF NOT EXISTS `action` (
KEY `fk_stu_act` (`id_stu`), KEY `fk_stu_act` (`id_stu`),
KEY `id_try` (`id_try`) KEY `id_try` (`id_try`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1
COMMENT="This table registers and action performed by a user at a given time, all information of the action is in JSON format in the 'description' column and the operation performed is one of the possible for the 'type' column"; COMMENT="This table registers and action performed by a user at a given time, all information of the action is in JSON format in the 'description' column and the operation performed is one of the possible for the 'type' column. NOTE: timestamps must support fractional seconds, so MySQL versions >= 5.6.4 are required.";
-- -------------------------------------------------------- -- --------------------------------------------------------
...@@ -70,10 +70,10 @@ COMMENT="Stores the expressions available in several languages for a given categ ...@@ -70,10 +70,10 @@ COMMENT="Stores the expressions available in several languages for a given categ
CREATE TABLE IF NOT EXISTS `method` ( CREATE TABLE IF NOT EXISTS `method` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) COLLATE utf8_unicode_ci NOT NULL, `name` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL, `description` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_stu` int(11) NOT NULL, `id_stu` int(11) NOT NULL,
`registration` date DEFAULT NULL, `registration` date DEFAULT NULL,
`notes` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL, `notes` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `fk_stu_met` (`id_stu`) KEY `fk_stu_met` (`id_stu`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1
...@@ -91,8 +91,8 @@ CREATE TABLE IF NOT EXISTS `instruction` ( ...@@ -91,8 +91,8 @@ CREATE TABLE IF NOT EXISTS `instruction` (
`name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `name` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`objective` varchar(512) COLLATE utf8_unicode_ci DEFAULT NULL, `objective` varchar(512) COLLATE utf8_unicode_ci DEFAULT NULL,
`status` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL CHECK (status IN ('started','finished')), `status` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL CHECK (status IN ('started','finished')),
`begin` timestamp NULL, `begin` timestamp(3) NULL,
`end` timestamp NULL, `end` timestamp(3) NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1
COMMENT="An instruction is a 'phase' in a method for learning AAC"; COMMENT="An instruction is a 'phase' in a method for learning AAC";
...@@ -122,7 +122,7 @@ COMMENT="One in a set of instructions predefined or stored by users. They are re ...@@ -122,7 +122,7 @@ COMMENT="One in a set of instructions predefined or stored by users. They are re
CREATE TABLE IF NOT EXISTS `meta_method` ( CREATE TABLE IF NOT EXISTS `meta_method` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT, `id` tinyint(4) NOT NULL AUTO_INCREMENT,
`name` varchar(40) COLLATE utf8_unicode_ci NOT NULL, `name` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL, `description` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL,
`id_sup` INT( 11 ) DEFAULT NULL, `id_sup` INT( 11 ) DEFAULT NULL,
UNIQUE(name,id_sup), UNIQUE(name,id_sup),
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
...@@ -195,7 +195,7 @@ CREATE TABLE IF NOT EXISTS `picto_acl` ( ...@@ -195,7 +195,7 @@ CREATE TABLE IF NOT EXISTS `picto_acl` (
`id_sup` int(11) NOT NULL DEFAULT '0', `id_sup` int(11) NOT NULL DEFAULT '0',
`id_pic` int(11) NOT NULL DEFAULT '0', `id_pic` int(11) NOT NULL DEFAULT '0',
`privilege` varchar(1) COLLATE utf8_unicode_ci NOT NULL, `privilege` varchar(1) COLLATE utf8_unicode_ci NOT NULL,
`timestamp` datetime, `timestamp` timestamp(3),
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `k_pic_aclp` (`id_pic`), KEY `k_pic_aclp` (`id_pic`),
KEY `k_sup_aclp` (`id_sup`) KEY `k_sup_aclp` (`id_sup`)
...@@ -264,7 +264,7 @@ COMMENT="Labels assigned to pictos by default or by supervisors"; ...@@ -264,7 +264,7 @@ COMMENT="Labels assigned to pictos by default or by supervisors";
CREATE TABLE IF NOT EXISTS `source` ( CREATE TABLE IF NOT EXISTS `source` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT, `id` tinyint(4) NOT NULL AUTO_INCREMENT,
`name` varchar(40) COLLATE utf8_unicode_ci NOT NULL, `name` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL, `description` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2
COMMENT="Contains all possible sources of pictos"; COMMENT="Contains all possible sources of pictos";
...@@ -285,9 +285,9 @@ CREATE TABLE IF NOT EXISTS `student` ( ...@@ -285,9 +285,9 @@ CREATE TABLE IF NOT EXISTS `student` (
`gender` char(1) COLLATE utf8_unicode_ci NOT NULL, `gender` char(1) COLLATE utf8_unicode_ci NOT NULL,
`country` char(2) COLLATE utf8_unicode_ci NOT NULL, `country` char(2) COLLATE utf8_unicode_ci NOT NULL,
`pic` varchar(255) COLLATE utf8_unicode_ci DEFAULT 'defaultAvatar.jpg', `pic` varchar(255) COLLATE utf8_unicode_ci DEFAULT 'defaultAvatar.jpg',
`notes` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL, `notes` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL,
`lang` varchar(5) COLLATE utf8_unicode_ci NOT NULL, `lang` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
`attributes` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Attributes describing student along with his/her configuration', `attributes` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Attributes describing student along with his/her configuration',
`id_off` int(11) DEFAULT NULL, `id_off` int(11) DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`), UNIQUE KEY `username` (`username`),
...@@ -366,10 +366,10 @@ COMMENT="Supervisors information"; ...@@ -366,10 +366,10 @@ COMMENT="Supervisors information";
CREATE TABLE IF NOT EXISTS `try` ( CREATE TABLE IF NOT EXISTS `try` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`id_ws` int(11) NOT NULL, `id_ws` int(11) NOT NULL,
`begin` timestamp DEFAULT CURRENT_TIMESTAMP, `begin` timestamp(3) DEFAULT CURRENT_TIMESTAMP,
`end` timestamp NULL, `end` timestamp(3) NULL,
`result` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, `result` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`description` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL, `description` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `id_ws` (`id_ws`) KEY `id_ws` (`id_ws`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1
...@@ -388,10 +388,10 @@ CREATE TABLE IF NOT EXISTS `working_session` ( ...@@ -388,10 +388,10 @@ CREATE TABLE IF NOT EXISTS `working_session` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`id_sup` int(11) NOT NULL, `id_sup` int(11) NOT NULL,
`id_ins` int(11) NOT NULL, `id_ins` int(11) NOT NULL,
`begin` timestamp DEFAULT CURRENT_TIMESTAMP, `begin` timestamp(3) DEFAULT CURRENT_TIMESTAMP,
`end` timestamp NULL, `end` timestamp(3) NULL,
`current` boolean NULL DEFAULT 1, `current` boolean NULL DEFAULT 1,
`description` varchar(1024) COLLATE utf8_unicode_ci DEFAULT NULL, `description` varchar(4096) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
KEY `id_sup` (`id_sup`), KEY `id_sup` (`id_sup`),
KEY `id_ins` (`id_ins`), KEY `id_ins` (`id_ins`),
......
...@@ -42,7 +42,7 @@ module.exports = { ...@@ -42,7 +42,7 @@ module.exports = {
if (!params.type) return res.json(500, {error: "No type of action defined"}); if (!params.type) return res.json(500, {error: "No type of action defined"});
// Optional params // Optional params
var timestamp = null; if(params.timestamp) timestamp = params.timestamp; var timestamp = null; if(params.timestamp) timestamp = params.timestamp.toISOString();
var sup = null; if(params.supervisor) sup = params.supervisor; var sup = null; if(params.supervisor) sup = params.supervisor;
var _try = null; if(params._try) _try = params._try; var _try = null; if(params._try) _try = params._try;
var description = null; if(params.description) description = params.description; var description = null; if(params.description) description = params.description;
...@@ -59,18 +59,20 @@ module.exports = { ...@@ -59,18 +59,20 @@ module.exports = {
"description": description, "description": description,
"gpsLat": gpsLat, "gpsLat": gpsLat,
"gpsLon": gpsLon "gpsLon": gpsLon
}).exec(function(err, created){
if(err || !created) return res.json(500, { error: "Error saving an action "+ params.type +": " + err });
if(created) {
StuOpenTry.findOne({id_stu : params.student}).exec(function(err, data){
if(err || !data){
sails.log.error("Error finding try for student "+ params.student + ' when creating action '+ params.type);
return res.json(500, {error: 'Action '+ params.type +' not created'});
}
sails.log.debug("Try for student "+params.student+ ":"+JSON.stringify(data));
return res.json(200,{open_try: data.openTry });
}) })
} .then(function(created){
if (!created)
throw new Error("Action.create returned NULL");
StuOpenTry.findOne({id_stu : params.student})
.then(function(data){
if(!data)
throw new Error("Error finding try for student "+ params.student + ' when creating action '+ params.type);
return res.ok({open_try: data.openTry });
})
})
.fail(function(err) {
res.serverError("Error saving an action "+ params.type +": " + err);
}); });
}, },
...@@ -100,7 +102,7 @@ module.exports = { ...@@ -100,7 +102,7 @@ module.exports = {
Action.create({ Action.create({
type: a.action, type: a.action,
timestamp: a.attributes.timestamp, timestamp: a.attributes.timestamp.toISOString(),
supervisor: sup, supervisor: sup,
student: params.student, student: params.student,
description: desc description: desc
......
...@@ -271,7 +271,8 @@ module.exports = { ...@@ -271,7 +271,8 @@ module.exports = {
* { * {
* id: pictoId, * id: pictoId,
* source: 1, * source: 1,
* owner: supervisorId * owner: supervisorId,
* uri: 'https://path/to/picto/in/the/web'
* } * }
*/ */
upload: function (req, res) { upload: function (req, res) {
......
...@@ -83,17 +83,17 @@ module.exports = { ...@@ -83,17 +83,17 @@ module.exports = {
res.serverError(); res.serverError();
}); });
} else { } else {
res.badRequest('Invalid user or password'); res.badRequest('Invalid password');
} }
} else { } else {
res.badRequest('Invalid user or password'); res.badRequest('Invalid user');
} }
}) })
.catch(function () { .catch(function () {
res.badRequest('Invalid user or password'); res.badRequest('Invalid user or password');
}); });
} else { } else {
res.badRequest('Invalid user or password'); res.badRequest('No email or or password');
} }
}, },
......
...@@ -7,13 +7,17 @@ ...@@ -7,13 +7,17 @@
module.exports = { module.exports = {
// /**
//close action * Closes a working session
// close a WS * @param req
// * {
* id_ws: <working session ID>,
* }
*/
close: function(req, res) { close: function(req, res) {
if (!req.params.id_ws) return res.json(500, {error: "No working session defined"}); if (!req.params.id_ws)
res.badRequest("No working session defined");
StuOpenTry.findOne( { id_ws : req.params.id_ws } ).exec(function(err, t) { StuOpenTry.findOne( { id_ws : req.params.id_ws } ).exec(function(err, t) {
if (err) { if (err) {
...@@ -38,16 +42,27 @@ module.exports = { ...@@ -38,16 +42,27 @@ module.exports = {
}); });
}, },
// create action /**
// adds a working session and a InitSession action * Creates a new working session
// * @param {request} req
// @TODO 357 * {
id_sup: <supervisor ID>
id_stu: <student ID>
id_ins: <instruction ID>
* }
* @param {response} res
* {
* // TODO: specify return values
* }
*/
create: function(req, res) { create: function(req, res) {
var params = req.allParams(); var params = req.allParams();
if (!params.id_sup) return res.json(500, {error: "No supervisor defined"}); console.log(JSON.stringify(params));
if (!params.id_ins) return res.json(500, {error: "No instruction defined"});
if (!params.id_stu) return res.json(500, {error: "No student defined"}); if (!params.id_sup) return res.badRequest("No supervisor defined");
if (!params.id_ins) return res.badRequest("No instruction defined");
if (!params.id_stu) return res.badRequest("No student defined");
if (!params.desc) params.desc = ""; if (!params.desc) params.desc = "";
var data = {}; var data = {};
...@@ -55,100 +70,165 @@ module.exports = { ...@@ -55,100 +70,165 @@ module.exports = {
data.instruction = params.id_ins; data.instruction = params.id_ins;
data.description = params.desc; data.description = params.desc;
StuOpenTry.findOne({or: [ // pending open try?
StuOpenTry.findOne( {or: [
{ id_sup : params.id_sup }, { id_sup : params.id_sup },
{ id_stu : params.id_stu } { id_stu : params.id_stu }
]} ).exec(function(err, t) { ]})
if (err) { .then(function(t) {
sails.log.error("Error Recovering from ws "+err); var ws = null;
return res.json(500, {error: 'recovering from ws:'+err}); if (t && t.openTry) { // YES, so WS not closed, we update to close it
} var ws_end = t.end ? t.end : t.begin;
if(!t || t.openTry==null) //WS closed correctly ws = WorkingSession.update({id: t.id_ws}, {end: ws_end.toISOString()})
WorkingSession.create(data).exec(function(err, ws){ .then(function(ws) {
if(err || !ws){ return ws;
sails.log.error("Creating new Working Sesion error "+err); });
return res.json(500, {error: 'Working Session not created'});
} }
return ws;
})
.then(function(oldws) { // now we can create new working session
return WorkingSession.create(data)
.then(function(ws) {
return ws;
});
})
.then(function(ws) { // now we have a new working session
if (!ws)
throw new Error("Error when calling WorkingSession.create");
// Create the InitSession Action // Create the InitSession Action
Action.create({ // This creates, also, a new try (see triggers-session-constraints.sql)
var action = Action.create({
"type": "initsession", "type": "initsession",
"timestamp": ws.begin, "timestamp": ws.begin.toISOString(),
"supervisor": ws.supervisor, "supervisor": ws.supervisor,
"student": params.id_stu "student": params.id_stu
}).exec(function(err, action){
if(err || !action) {
sails.log.error("Creating initial action for new Working Sesion. Error "+err);
return res.json(500, {error: 'Working Session not created'});
}
StuOpenTry.findOne( {id_stu : params.id_stu } ).exec(function(err, t) {
if (err) {
sails.log.error("Error Recovering from ws "+err);
return res.json(500, {error: 'recovering from ws:'+err});
}
// Return the working session and try created
sails.log.debug("Initial action for new Working Sesion "+JSON.stringify(ws)+". Action:"+JSON.stringify(action));
return res.json({
"id": ws.id,
"first_try_id":t.openTry,
"recovered_ws": null
})
}) })
.then(function(action) {
if(!action)
throw new Error("Error when creating action 'initsession'");
return action;
}); });
}); return [action, ws];
else { //Not closed previous WS must be recovered and closed }).spread(function(action, ws) {
sails.log.debug("Recovering WS required for student "+params.id_stu); StuOpenTry.findOne({id_stu : params.id_stu })
var ws_end; .then(function(opentry) {
if (t.end==null) ws_end = t.begin; if (!opentry)
else ws_end = t.end; throw new Error("Error when looking for open try");
WorkingSession.update({id: t.id_ws}, {end: ws_end.toISOString()}).exec(function(err,u) {
if (err) {
sails.log.error("Error updating no closed WS " + t.id_ws);
return res.json(500, {error: 'Working Session not created'});
}
WorkingSession.create(data).exec(function(err, ws){
if(err || !ws){
sails.log.error("Creating new Working Sesion error "+err);
return res.json(500, {error: 'Working Session not created'});
}
// Create the InitSession Action
Action.create({
"type": "initsession",
"timestamp": ws.begin,
"supervisor": ws.supervisor,
"student": params.id_stu
}).exec(function(err, action){
if(err || !action) {
sails.log.error("Creating initial action for new Working Sesion. Error "+err);
return res.json(500, {error: 'Working Session not created'});
}
StuOpenTry.findOne( {id_stu : params.id_stu} ).exec(function(err, t) {
if (err) {
sails.log.error("Error Recovering from ws "+err);
return res.json(500, {error: 'recovering from ws:'+err});
}
// Return the working session and try created // Return the working session and try created
sails.log.debug("Initial action for new Working Sesion "+JSON.stringify(action)); sails.log.debug("Initial action for new Working Sesion "+JSON.stringify(ws)+". Action:"+JSON.stringify(action));
return res.json({
return res.ok({
"id": ws.id, "id": ws.id,
"first_try_id":t.openTry, "first_try_id":opentry.id,
"recovered_ws": null "recovered_ws": null
})
})
}); });
}); });
})
.fail(function(err) {
res.serverError("Session not created: " + err);
}); });
}
}); // ---
//
// StuOpenTry.findOne( {or: [
// { id_sup : params.id_sup },
// { id_stu : params.id_stu }
// ]} ).exec(function(err, t) {
// if (err)
// res.serverError("Recovering from ws: "+err);
//
// if(t || t.openTry==null) { //WS closed correctly
// sails.log.debug("No open try, let's create session");
//
// WorkingSession.create(data).exec(function(err, ws){
// if(err || !ws)
// return res.serverError("Creating new Working Sesion error "+err);
//
// // Create the InitSession Action
// // This creates, also, a new try (see triggers-session-constraints.sql)
// Action.create({
// "type": "initsession",
// "timestamp": ws.begin,
// "supervisor": ws.supervisor,
// "student": params.id_stu
// }).exec(function(err, action){
// if(err || !action)
// res.serverError("Creating initial action for new Working Sesion. Error "+err);
//
// StuOpenTry.findOne( {id_stu : params.id_stu } ).exec(function(err, t) {
// if (err)
// res.serverError('Could not open try for new session: '+err);
//
// // Return the working session and try created
// sails.log.debug("Initial action for new Working Sesion "+JSON.stringify(ws)+". Action:"+JSON.stringify(action));
// res.ok({
// "id": ws.id,
// "first_try_id":t.openTry,
// "recovered_ws": null
// })
// })
// });
// });
// }
// else { //Not closed previous WS must be recovered and closed
// sails.log.debug("Open try found, recovering WS for student "+params.id_stu);
//
// var ws_end;
// if (t.end==null)
// ws_end = t.begin;
// else
// ws_end = t.end;
// WorkingSession.update({id: t.id_ws}, {end: ws_end.toISOString()}).exec(function(err,u) {
// if (err)
// res.serverError("Error updating non closed WS " + t.id_ws);
//
// WorkingSession.create(data).exec(function(err, ws){
// if(err || !ws)
// res.serverError("Creating new Working Sesion error "+err);
//
// // Create the InitSession Action
// Action.create({
// "type": "initsession",
// "timestamp": ws.begin,
// "supervisor": ws.supervisor,
// "student": params.id_stu
// }).exec(function(err, action){
// if(err || !action)
// res.serverError("Creating initial action for new Working Sesion. Error " + err);
//
// StuOpenTry.findOne( {id_stu : params.id_stu} ).exec(function(err, t) {
// if (err)
// res.serverError("Error Recovering from ws "+err);
//
// // Return the working session and try created
// res.ok({
// "id": ws.id,
// "first_try_id":t.openTry,
// "recovered_ws": null
// })
// })
// });
// });
// });
// }
// });
}, },
// update action /**
// ends a working session and a creates an EndSession action * Updates working session information and a creates an EndSession action if
// * update info is to end session
// @TODO 357 * @param {request} req
* {
* // TODO: specify parameters
* }
* @param {response} res
* {
* // TODO: specify return values
* }
*/
update: function(req, res) { update: function(req, res) {
var params = req.allParams(); var params = req.allParams();
console.log(JSON.stringify(params));
WorkingSession.update({id:params.id}, params).exec(function(err, ws){ WorkingSession.update({id:params.id}, params).exec(function(err, ws){
if(err || !ws){ if(err || !ws){
...@@ -162,7 +242,7 @@ module.exports = { ...@@ -162,7 +242,7 @@ module.exports = {
// Create the EndSession Action // Create the EndSession Action
Action.create({ Action.create({
"type": "endsession", "type": "endsession",
"timestamp": ws[0].end, "timestamp": ws[0].end.toISOString(),
"supervisor": ws[0].supervisor, "supervisor": ws[0].supervisor,
"student": params.id_stu "student": params.id_stu
}).exec(function(err, action){ }).exec(function(err, action){
......
...@@ -52,6 +52,35 @@ module.exports = { ...@@ -52,6 +52,35 @@ module.exports = {
via: "workingSession" via: "workingSession"
} }
}, },
/**
* Checks a previous session is not opened for that supervisor. If so,
* session is closed (current is set to 0)
* @param {Object} attrs All session properties to be stored
* @param {Function} next Function to be executed when the check process
* has been completed (an error object will be passed
* to the function if necesary)
*/
beforeCreate: function (attrs, next) {
WorkingSession.find({id_sup: attrs.supervisor, current: 1})
.then(function(wss) {
if (wss) {
async.each(wss, function(ws, cb) {
ws.current = 0;
WorkingSession.update(ws.id, ws, function(err, update) {
if (err) throw new Error("Error when udpating open sessions");
cb();
});
});
}
next();
})
.fail(function(err) {
next(err);
})
},
// //
// Returns the number of working sessions per year (REPORTS) // Returns the number of working sessions per year (REPORTS)
// //
......
...@@ -80,6 +80,7 @@ ...@@ -80,6 +80,7 @@
"enlarge": "Enlarge", "enlarge": "Enlarge",
"enormous": "Enormous", "enormous": "Enormous",
"error_adding_picto": "Error adding picto", "error_adding_picto": "Error adding picto",
"error_creating_session": "Error when creating session",
"error_deleting_picto": "Error deleting picto", "error_deleting_picto": "Error deleting picto",
"error_downloading_supervisors": "Error downloading supervisors", "error_downloading_supervisors": "Error downloading supervisors",
"error_downloading_offices": "Error downloading offices", "error_downloading_offices": "Error downloading offices",
......
...@@ -83,6 +83,7 @@ ...@@ -83,6 +83,7 @@
"expand_navigation": "Desplegar navegación", "expand_navigation": "Desplegar navegación",
"expression": "Expresión:", "expression": "Expresión:",
"error_adding_picto": "Error al añadir el picto", "error_adding_picto": "Error al añadir el picto",
"error_creating_session": "Error al crear sesión",
"error_deleting_picto": "Error borrando el picto", "error_deleting_picto": "Error borrando el picto",
"error_downloading_supervisors": "Error al descargar los supervisores", "error_downloading_supervisors": "Error al descargar los supervisores",
"error_downloading_offices": "Error al descargar las oficinas", "error_downloading_offices": "Error al descargar las oficinas",
......
...@@ -3,7 +3,14 @@ ...@@ -3,7 +3,14 @@
//----------------------- //-----------------------
// Student Session Controller // Student Session Controller
//----------------------- //-----------------------
dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtrl($scope, $stateParams, $http, config, $window) { dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtrl(
$scope,
$stateParams,
$http,
config,
$window,
$translate,
ngToast) {
...@@ -206,7 +213,7 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr ...@@ -206,7 +213,7 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr
.post(config.backend+'/ws', { .post(config.backend+'/ws', {
"id_sup": $scope.user.id, "id_sup": $scope.user.id,
"id_stu": $scope.studentData.id, "id_stu": $scope.studentData.id,
"id_ins": $scope.selectedIns "id_ins": $scope.selectedIns.id
}) })
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
...@@ -229,7 +236,9 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr ...@@ -229,7 +236,9 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr
$scope.studentData.current_instruction=$scope.selectedIns.name; $scope.studentData.current_instruction=$scope.selectedIns.name;
}) })
.error(function(data, status, headers, config) { .error(function(data, status, headers, config) {
$translate('error_creating_session').then(function (translation) {
ngToast.danger({ content: translation });
});
}); });
}; };
...@@ -237,12 +246,10 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr ...@@ -237,12 +246,10 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr
// update the description of a given working session // update the description of a given working session
// //
$scope.update_ws = function (ws){ $scope.update_ws = function (ws){
$http $http
.put(config.backend+'/ws/' + ws.id, { "description" : ws.description }) .put(config.backend+'/ws/' + ws.id, { "description" : ws.description })
.then(function(data, status, headers, config) { .then(function(data, status, headers, config) {
// TODO notify update?
}) })
,function(data, status, headers, config) { ,function(data, status, headers, config) {
...@@ -376,26 +383,22 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr ...@@ -376,26 +383,22 @@ dashboardControllers.controller('StudentSessionCtrl', function StudentSessionCtr
// what data has updated, and refresh what needs to be refreshed. // what data has updated, and refresh what needs to be refreshed.
if($scope.ws){ if($scope.ws){
console.log(data.action);
switch(data.action){ switch(data.action){
case 'Add': case 'Add':
$scope.actual_try.actions.push(data); $scope.actual_try.actions.push(data);
break; break;
case 'Delete': case 'Delete':
$scope.actual_try.actions.push(data); $scope.actual_try.actions.push(data);
break; break;
case 'Select': case 'Select':
$scope.actual_try.actions.push(data); $scope.actual_try.actions.push(data);
break; break;
case 'Show': case 'Show':
$scope.load_tries($scope.selectedIns); $scope.load_tries($scope.selectedIns);
// Empty actual try and push the first action of next try // Empty actual try and push the first action of next try
$scope.actual_try.actions = []; $scope.actual_try.actions = [];
$scope.actual_try.actions.push({ action: 'tryinit' }); $scope.actual_try.actions.push({ action: 'tryinit' });
break; break;
} }
} }
......
...@@ -44,10 +44,10 @@ ...@@ -44,10 +44,10 @@
<img ng-src="{{ a.attributes.picto.picto.uri }}" /> <img ng-src="{{ a.attributes.picto.picto.uri }}" />
<div class="action-type"> <div class="action-type">
<span ng-if="a.action == 'add'" class="glyphicon glyphicon-plus color_green" aria-hidden="true"></span> <span ng-if="a.action == 'add'" class="glyphicon glyphicon-plus color_green" aria-hidden="true"></span>
<span ng-if="a.action == 'select'" class="glyphicon glyphicon-hand-up color_blue" aria-hidden="true"></span> <span ng-if="a.action == 'Select'" class="glyphicon glyphicon-hand-up color_blue" aria-hidden="true"></span>
<span ng-if="a.action == 'delete'" class="glyphicon glyphicon-remove color_red" aria-hidden="true"></span> <span ng-if="a.action == 'Delete'" class="glyphicon glyphicon-remove color_red" aria-hidden="true"></span>
<span ng-if="a.action == 'show'" class="glyphicon glyphicon-eye-open color_blue" aria-hidden="true"></span> <span ng-if="a.action == 'Show'" class="glyphicon glyphicon-eye-open color_blue" aria-hidden="true"></span>
<span ng-if="a.action == 'unshow'" class="glyphicon glyphicon-edit color_blue" aria-hidden="true"></span> <span ng-if="a.action == 'Unshow'" class="glyphicon glyphicon-edit color_blue" aria-hidden="true"></span>
<span ng-if="a.action == 'tryinit'" class="glyphicon glyphicon-log-in" aria-hidden="true"></span> <span ng-if="a.action == 'tryinit'" class="glyphicon glyphicon-log-in" aria-hidden="true"></span>
<span ng-if="a.action == 'tryend'" class="glyphicon glyphicon-log-out" aria-hidden="true"></span> <span ng-if="a.action == 'tryend'" class="glyphicon glyphicon-log-out" aria-hidden="true"></span>
<span ng-if="a.action == 'pausesession'" class="glyphicon glyphicon-pause" aria-hidden="true"></span> <span ng-if="a.action == 'pausesession'" class="glyphicon glyphicon-pause" aria-hidden="true"></span>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment