Commit 7c718270 by Jose Antonio

Working on scene

parent 426ea672
...@@ -31,6 +31,8 @@ sails/src/config/ssl/**/*.crt ...@@ -31,6 +31,8 @@ sails/src/config/ssl/**/*.crt
sails/src/config/ssl/**/*.csr sails/src/config/ssl/**/*.csr
sails/src/config/ssl/**/*.pem sails/src/config/ssl/**/*.pem
sails/src/config/local.js sails/src/config/local.js
sails/src/config/local.js
sails/src/assets/scripts/config.js
sails/src/node_modules sails/src/node_modules
sails/src/certbot.log sails/src/certbot.log
sails/.vagrant sails/.vagrant
......
/* global Student, PictoCore, VStuLastInstruction, StuPicto, StuSup, sailsTokenAuth, sails,
Picto */
/**
/* StudentController
*
* @description :: Server-side logic for managing students
* @help :: See http://links.sailsjs.org/docs/controllers
*/
module.exports = {
//
// Adds a new scene into the database
//
create: function (req, res) {
var params = req.params.all();
Scene.create({
name: params.name,
active: false,
categories: params.categories,
supervisor: supervisor.id,
student: student.id
}).then(scene=>{
return res.ok(scene);
}).catch(function (err){
return res.serverError("Error creating scene: " + err);
});
},
//
// Update a scene data
//
update: function (req, res) {
Scene.findOne({ id: req.params.id }).then(function (scene) {
if (!scene) {
res.badRequest();
throw new Error('Scene not found');
}
delete scene.categories;//To avoid update these fields
delete scene.supervisor;
delete scene.student;
scene.name = req.param('name') || instruction.name;
scene.active = req.param('active') || instruction.active;
scene.save(function (error) {
if (error) {
res.serverError();
} else {
res.ok(scene);
}
});
})
.catch(function () {
res.serverError();
});
},
/**
* Delete a scene by its ID
* @param {request} req {} (with sceneId as url parameter)
* @param {response} res {}
*/
destroy: function (req, res) {
Scene.destroy({ id: req.params.id }).exec(function (error) {
if (error)
return res.badRequest();
else
return res.ok();
});
},
/**
* Return a scene with all StuPicto
* @param {request} req {} (with sceneId as url parameter)
* @param {response} res {}
*/
getScene: function(req, res){
Scene.findOne({id: req.params.id})
.populate('stuPictos').then(function(scene){
if(!scene)
return res.badRequest();
else
console.log(scene);
return res.ok(scene);
}).catch(function (err){
return res.serverError("Error finding scene "+err);
});
},
/**
* Return all the scenes of a student
* @param {request} req {} (studentId)
* @param {response} res {}
*/
getScene: function(req, res){
Scene.find({ student: req.params.id_stu})
.populate('stuPictos').then(function(scenes){
if(!scenes)
return res.badRequest();
else
return res.ok(scenes);
}).catch(function (err){
return res.serverError("Error retrieving scenes "+err);
});
}
};
...@@ -818,6 +818,33 @@ module.exports = { ...@@ -818,6 +818,33 @@ module.exports = {
}); });
}, },
/**
* Return the active scene of a student with all StuPicto
* @param {request} req {} (with studentId as url parameter)
* @param {response} res {}
*/
getActiveScene: function(req, res){
Scene.findOne({ student: req.params.id_stu, active: true})
.populate('stuPictos').then(function(scene){
if(!scene)
return res.badRequest();
else
console.log(scene);
Scene.pictos(scene.id, function(err, pictos){
if (err){
return res.serverError("Error obtaining pictos: "+ err);
}
scene.pictos=pictos;
console.log(scene);
return res.ok(scene);
});
}).catch(function (err){
return res.serverError("Error finding scene "+err);
});
},
// //
// Returns all working sessions for the given student // Returns all working sessions for the given student
// //
......
/**
* scene.js
*
* @description :: TODO: Write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
tableName : 'scene',
migrate : 'safe',
schema : true,
autoPK : false,
autoCreatedAt : false,
autoUpdatedAt : false,
attributes: {
id: {
type: "integer",
autoIncrement: true,
primaryKey: true,
unique: true
},
name: {
required: true,
type: "string",
size: 100
},
active: {
type: "boolean"
},
categories: {
type: "boolean"
},
supervisor: { //FK de supervisor 1 a NN
columnName: "id_sup",
required: true,
type: "integer",
model: "Supervisor"
},
student: { // FK de student 1 a N
columnName: "id_stu",
required: true,
type: "integer",
model: "Student"
},
// Relacion con Stu_picto
stuPictos:{
collection: "StuPicto",
via: "scene"
}
},
//
// Class method for getting the list of pictos associated to a given
// scene
pictos: function(id_scene, callback) {
var l = [];
var fs = require('fs');
console.log("metodo pictos scene");
Scene.findOne(id_scene)
.then((scene) => {
if (!scene)
throw new Error("No scene found");
var stuPictos = StuPicto.find({scene: id_scene})
.populate('picto')
.then((stuPictos) => {
if (!stuPictos || stuPictos.length == 0)
return [];
console.log(stuPictos);
return stuPictos;
})
.catch((err) => {
throw err;
});
var student = Student.findOne(scene.student).then(student=>{
if (!student)
return [];
return student;
}).catch( (err) => {
throw err;
});
return [scene, stuPictos, student];
})
.spread((scene, stuPictos, student) => {
console.log(stuPictos);
async.eachSeries(stuPictos, function(stuPicto, next_cb) {
// Populate expressions to get it with the picto
Picto.findOne(stuPicto.picto.id)
.populate('expressions', {lang: student.lang})
.populate('tags', {lang: student.lang})
.then((picto) => {
// check picto has expressions associated in student language
if (picto.expressions.length == 0 || picto.expressions[0].text.length == 0)
return next_cb();
// check picto image is available
picto.imageFileExists(function(found) {
if (found) {
// Now we have everything, so we add the picto to the list
stuPicto.attributes.expression = stuPicto.attributes.expression ? stuPicto.attributes.expression : picto.expressions[0].text;
var stuPictoToAdd = {
"id": stuPicto.id,
"picto": stuPicto.picto,
"attributes": stuPicto.attributes,
"tags": picto.tags ? picto.tags : []
};
l.push(stuPictoToAdd);
next_cb();
} else {
next_cb();
}
});
})
.catch((err) => {
next_cb(err);
});
},
function (err) { // loop has end
callback(err, l);
}); // end async.eachSeries
})
.catch((err) => {
callback(err, l);
}); // end Student.findOne
}
};
...@@ -32,6 +32,11 @@ module.exports = { ...@@ -32,6 +32,11 @@ module.exports = {
type: 'integer', type: 'integer',
model: 'Picto' model: 'Picto'
}, },
scene:{ //FK de Scene 1 a N
columName: "id_scene",
type: "integer",
model: "Scene"
},
/** /**
* This "extra" property allow us to adapt the server to the student needs * This "extra" property allow us to adapt the server to the student needs
......
...@@ -80,6 +80,14 @@ module.exports.policies = { ...@@ -80,6 +80,14 @@ module.exports.policies = {
fromSearch: ['tokenAuth'] fromSearch: ['tokenAuth']
}, },
SceneController:{
create: ['tokenAuth', 'isSupervisorOfStudent'],
update: ['tokenAuth', 'isSupervisorOfStudent'],
destroy: ['tokenAuth', 'isSupervisorOfStudent'],
getScene: ['tokenAuth'],
getStudentScenes: ['tokenAuth']
},
ServerController: { ServerController: {
ping: true, ping: true,
ping_session: ['tokenAuth'] ping_session: ['tokenAuth']
...@@ -115,7 +123,8 @@ module.exports.policies = { ...@@ -115,7 +123,8 @@ module.exports.policies = {
actions_batch: ['tokenAuth'], actions_batch: ['tokenAuth'],
delete: ['tokenAuth', 'isSupAdmin'], delete: ['tokenAuth', 'isSupAdmin'],
unlink_supervisor: ['tokenAuth', 'isSupAdmin'], unlink_supervisor: ['tokenAuth', 'isSupAdmin'],
delete_picto: ['tokenAuth', 'isSupervisorOfStudent'] delete_picto: ['tokenAuth', 'isSupervisorOfStudent'],
getActiveScene: ['tokenAuth']
}, },
LicenseController: { LicenseController: {
......
...@@ -73,6 +73,12 @@ module.exports.routes = { ...@@ -73,6 +73,12 @@ module.exports.routes = {
'DELETE /picto/:id': 'PictoController.destroy', 'DELETE /picto/:id': 'PictoController.destroy',
'DELETE /picto/:id_sup/tag/:id_tag': 'PictoController.del_tag', 'DELETE /picto/:id_sup/tag/:id_tag': 'PictoController.del_tag',
'GET /scene/:id': 'SceneController.getScene',
'GET /stu/:id_stu/scenes': 'SceneController.getStudentScenes',
'POST /scene/:id': 'SceneController.create',
'PUT /scene/:id': 'SceneController.update',
'DELETE /scene/:id': 'SceneController.destroy',
'GET /server/ping': 'ServerController.ping', 'GET /server/ping': 'ServerController.ping',
'GET /server/ping_session': 'ServerController.ping_session', 'GET /server/ping_session': 'ServerController.ping_session',
...@@ -82,6 +88,7 @@ module.exports.routes = { ...@@ -82,6 +88,7 @@ module.exports.routes = {
'GET /stu/:id_stu/tutors': 'StudentController.tutors', 'GET /stu/:id_stu/tutors': 'StudentController.tutors',
'POST /stu/:id_stu/sup/:id_sup': 'StudentController.link_supervisor', 'POST /stu/:id_stu/sup/:id_sup': 'StudentController.link_supervisor',
'GET /stu/:id_stu/pictos': 'StudentController.pictos', 'GET /stu/:id_stu/pictos': 'StudentController.pictos',
'GET /stu/:id_stu/activeScene': 'StudentController.getActiveScene',
'GET /stu/:id_stu/methods': 'StudentController.methods', 'GET /stu/:id_stu/methods': 'StudentController.methods',
'GET /stu/:id_stu/lasttries': 'StudentController.lasttries', 'GET /stu/:id_stu/lasttries': 'StudentController.lasttries',
......
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