Commit 8d0f2c3d by Jose Antonio

Working - Duplicate scene

parent a763dfc5
...@@ -117,6 +117,50 @@ module.exports = { ...@@ -117,6 +117,50 @@ module.exports = {
}); });
}, },
/**
* Copies a scene with its stu_pictos
* @param {request} req {} (id of source scene)
*/
duplicate: function(req,res){
Scene.findOne({id:req.params.id})
.populate('stuPictos').then(function(scene){
Scene.create({
name: scene.name,
active: false,
categories: scene.categories,
supervisor: scene.supervisor,
student: scene.student
}).then(scene=>{
async.forEach(scene.stuPictos, function (stuPicto, cb) {
StuPicto.create({
student: stuPicto.student,
picto: stuPicto.picto,
scene: scene.id,
attributes: stuPicto.attributes
}).catch(function (err){
console.log("Error creating stu_picto "+err.details);
sails.log.error(err.details);
});
},
function (err) { // function called when loop is done
if (err) {
console.log(err.details);
sails.log.error(err.details);
return res.json({
'error': err.details
});
} else
return res.ok(scene);
});
}).catch(function (err){
return res.serverError("Error creating scene: " + err);
});
});
},
// //
// Logs a scene action and broadcast to anyone subscribed to this student // Logs a scene action and broadcast to anyone subscribed to this student
scene: function (req, res) { scene: function (req, res) {
......
...@@ -892,8 +892,9 @@ module.exports = { ...@@ -892,8 +892,9 @@ module.exports = {
/** /**
* Add an existing picto to the student's collection * Add an existing picto to the student's collection
* @param {request} req (with id_stu and id_picto as url parameters) * @param {request} req (with id_scene,id_stu and id_picto as url parameters)
* { * {
* id_scene,
* id_stu, * id_stu,
* id_picto, * id_picto,
* attributes: { @see StuPicto.getValidAttributes() } * attributes: { @see StuPicto.getValidAttributes() }
...@@ -928,7 +929,7 @@ module.exports = { ...@@ -928,7 +929,7 @@ module.exports = {
add_picto: function (req, res) { add_picto: function (req, res) {
var params = req.allParams(); var params = req.allParams();
StuPicto.find({id_pic: params.id_picto, id_stu: params.id_stu}) StuPicto.find({id_pic: params.id_picto, id_stu: params.id_stu, id_scene:params.id_scene})
.then((entries) => { .then((entries) => {
if (entries && entries.length > 0) { if (entries && entries.length > 0) {
var err = new Error("Picto already in student's vocabulary"); var err = new Error("Picto already in student's vocabulary");
......
...@@ -122,6 +122,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -122,6 +122,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
// get active scene // get active scene
$scope.showActiveScene = function (scene) { $scope.showActiveScene = function (scene) {
$scope.loadingPictos = true;
$scope.freeCategoryPictos = $scope.freeCategoryPictos || generateGrid(); $scope.freeCategoryPictos = $scope.freeCategoryPictos || generateGrid();
$scope.studentPictos[$scope.getCategoryId($scope.selectedCategory)] = $scope.studentPictos[$scope.getCategoryId($scope.selectedCategory)] =
$scope.studentPictos[$scope.getCategoryId($scope.selectedCategory)] || generateGrid(); $scope.studentPictos[$scope.getCategoryId($scope.selectedCategory)] || generateGrid();
...@@ -143,7 +144,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -143,7 +144,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
// get active scene // get active scene
$scope.showScene = function (idScene) { $scope.showScene = function (idScene) {
$scope.loadingPictos = true;
$scope.freeCategoryPictos = $scope.freeCategoryPictos || generateGrid(); $scope.freeCategoryPictos = $scope.freeCategoryPictos || generateGrid();
$scope.studentPictos[$scope.getCategoryId($scope.selectedCategory)] = $scope.studentPictos[$scope.getCategoryId($scope.selectedCategory)] =
$scope.studentPictos[$scope.getCategoryId($scope.selectedCategory)] || generateGrid(); $scope.studentPictos[$scope.getCategoryId($scope.selectedCategory)] || generateGrid();
...@@ -283,6 +284,25 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -283,6 +284,25 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
}; };
// Duplicate viewing scene
$scope.copy_scene = function () {
$http.get(config.backend + '/scene/'+$scope.viewingScene.id+'/copy' )
.success(function (newScene) {
io.socket.post('/scene', {
action: 'add',
data: newScene
}, function () {});
$scope.loadScenesList();
$translate('scene_duplicated').then(function (translation) {
ngToast.success({ content: translation });
});
}).error(function () {});
};
$scope.deleteFreePicto = function (studentPicto) { $scope.deleteFreePicto = function (studentPicto) {
$translate('confirmation').then(t => { $translate('confirmation').then(t => {
if ($window.confirm(t)) { if ($window.confirm(t)) {
......
...@@ -84,6 +84,7 @@ module.exports.policies = { ...@@ -84,6 +84,7 @@ module.exports.policies = {
create: ['tokenAuth', 'isSupervisorOfStudent'], create: ['tokenAuth', 'isSupervisorOfStudent'],
update: ['tokenAuth', 'isSupervisorOfStudent'], update: ['tokenAuth', 'isSupervisorOfStudent'],
destroy: ['tokenAuth', 'isSupervisorOfStudent'], destroy: ['tokenAuth', 'isSupervisorOfStudent'],
duplicate: ['tokenAuth', 'isSupervisorOfStudent'],
getScene: ['tokenAuth'], getScene: ['tokenAuth'],
getStudentScenes: ['tokenAuth'], getStudentScenes: ['tokenAuth'],
scene: true scene: true
......
...@@ -77,6 +77,7 @@ module.exports.routes = { ...@@ -77,6 +77,7 @@ module.exports.routes = {
'POST /scene': 'SceneController.scene', 'POST /scene': 'SceneController.scene',
'GET /scene/:id': 'SceneController.getScene', 'GET /scene/:id': 'SceneController.getScene',
'GET /scene/:id/copy': 'SceneController.duplicate',
'POST /scene/:id': 'SceneController.create', 'POST /scene/:id': 'SceneController.create',
'PUT /scene/:id': 'SceneController.update', 'PUT /scene/:id': 'SceneController.update',
'DELETE /scene/:id': 'SceneController.destroy', 'DELETE /scene/:id': 'SceneController.destroy',
......
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