issue #526 implemented

parent 88f6058f
...@@ -29,7 +29,7 @@ module.exports = { ...@@ -29,7 +29,7 @@ module.exports = {
return res.badRequest(); return res.badRequest();
MetaInstruction.create({ MetaInstruction.create({
method: method.id, id_met: method.id,
name: req.param('name'), name: req.param('name'),
objective: req.param('objective'), objective: req.param('objective'),
}) })
...@@ -78,7 +78,7 @@ module.exports = { ...@@ -78,7 +78,7 @@ module.exports = {
if (error) if (error)
return res.serverError(); return res.serverError();
return res.ok(instruction); return res.ok(instruction);
} });
}) })
.catch(function () { .catch(function () {
return res.serverError(); return res.serverError();
......
...@@ -9,129 +9,7 @@ ...@@ -9,129 +9,7 @@
module.exports = { module.exports = {
/**
* Creates a new method for a student using a template (metamethod)
* @param {request} req
* {
* id_mmethod: metaMethodId,
* id_stu: studentId
* }
* @param {response} res
* {
* id: methodId,
* name: 'MetaMethod Name',
* description: 'MetaMethod Description',
* instructions: [
* {
* id: instructionId,
* name: 'Instruction Name',
* objective: 'Instruction Objective',
* method: methodId
* },
* ...
* ]
* }
*/
create: function(req, res) {
var params = req.allParams();
if (!params.id_mmethod) return res.badRequest("No meta method defined");
if (!params.id_stu) return res.badRequest("No student defined");
// Find meta method
MetaMethod.findOne({id: params.id_mmethod})
.then(mmethod => {
if (!mmethod)
throw new Error("Meta method not found");
// Create new method by meta method
return Method.create(
{
name: mmethod.name,
description: mmethod.description,
student: params.id_stu
})
.then(created => created)
.fail(err => {throw err});
})
.then(created => {
// Find meta instructions associated to meta method
MetaInstruction.find({id_met: params.id_mmethod})
.then(minstructions => {
if (!minstructions)
minstructions = [];
var l_ins = [];
// Every meta instruction is going to be created in 'Instruction'
// with .eachSeries the order of resulting array will be equal
// to the original array
async.eachSeries(minstructions, function(mins, next) {
Instruction.create({
method: created.id,
name: mins.name,
objective: mins.objective
})
.then(added => {
l_ins.push(added);
sails.log.debug("Instruction " + added.name + " added to method " + created.id);
})
.fail(err => {throw err})
.done(() => {next()});
},
function(err, results) {
if (err)
throw new Error("Error while looping through instructions");
return res.ok({
"name": created.name,
"description": created.description,
"id": created.id,
"student": created.id_stu,
"instructions": l_ins
});
});
})
.fail( err => {throw err});
})
.fail(err => {
return res.badRequest(err);
});
},
/** /**
* Creates a new method from scratch (without using a template)
* @param {request} req
* {
* id_stu: studentId,
* name: 'Method name'
* }
* @param {response} res
* {
* id: methodId,
* student: studentId,
* name: 'Method name'
* }
*/
newMethod: function (req, res) {
if (req.param('name') && req.param('id_stu')) {
Method.create({
name: req.param('name'),
student: req.param('id_stu')
}).then(function (method) {
if (!method) {
res.badRequest();
throw new Error('method not created');
}
res.ok(method);
})
.catch(function () {
res.serverError();
});
} else {
res.badRequest();
}
},
/**
* Return all the metamethods of a given supervisor (including the public metamethods) * Return all the metamethods of a given supervisor (including the public metamethods)
* not associated to a concrete supervisor. * not associated to a concrete supervisor.
* @param {request} req {} (with idSup specified as url parameter) * @param {request} req {} (with idSup specified as url parameter)
...@@ -159,89 +37,127 @@ module.exports = { ...@@ -159,89 +37,127 @@ module.exports = {
}); });
}, },
/** /**
* Return all the metamethods of a given supervisor he owns * Return all the metamethods of a given supervisor he owns
* @param {request} req {} (with idSup specified as url parameter) * @param {request} req {} (with idSup specified as url parameter)
* @param {response} res * @param {response} res
* [ * [
* { * {
* id: metamethodId * id: metamethodId
* name: 'Metamethod Name' * name: 'Metamethod Name'
* description: 'Metamethod description, which can be longer' * description: 'Metamethod description, which can be longer'
* }, * },
* ... * ...
* ] * ]
*/ */
supOwned: function (req, res) { supOwned: function (req, res) {
var params = req.allParams(); var params = req.allParams();
if (!params.id_sup) if (!params.id_sup)
return res.badRequest(); return res.badRequest();
MetaMethod.find({ supervisor: params.id_sup })
.populate('metainstructions')
.then(function (metaMethods) {
res.ok(metaMethods);
})
.catch(function (err) {
res.serverError(err);
});
},
//
// Saves a new meta method (template)
//
save: function(req, res){
var params = req.allParams();
if (!params.id_sup)
return res.badRequest("No supervisor defined");
MetaMethod.create({
"name": params.name,
"description": "",
"supervisor": params.id_sup
})
.then(function(createdMethod){
return res.ok(createdMethod);
})
.catch(function(err) {
sails.log.debug("Create Method template error: " + err);
return res.serverError("Error creating method template");
});
},
//
// Deletes a meta methdo
//
destroy: function(req, res){
var params = req.allParams();
if (!params.id_mmet) {
return res.badRequest("No meta method defined");
}
// Destroy instructions
MetaInstruction.destroy({ id_met: params.id_mmet }).exec(function(err, metainstructions) {
if (err)
return res.serverError(err);
// Destroy method
MetaMethod.destroy({ id: params.id_mmet }).exec(function(err, metamethod) {
if (err || !metamethod){
sails.log.debug("Destroy MetaMethod: " + err);
return res.serverError("No meta method found");
}
MetaMethod.find({ supervisor: params.id_sup })
.populate('metainstructions')
.then(function (metaMethods) {
res.ok(metaMethods);
})
.catch(function (err) {
res.serverError(err);
});
},
/**
* Creates a new method from scratch (without using a template)
* @param {request} req
* {
* id_stu: studentId,
* name: 'Method name'
* }
* @param {response} res
* {
* id: methodId,
* name: 'Method name'
* }
*/
create: function(req, res){
var params = req.allParams();
if (!params.id_sup)
return res.badRequest("No supervisor defined");
MetaMethod.create({
"name": params.name,
"description": "",
"supervisor": params.id_sup
})
.then(function(createdMethod){
return res.ok(createdMethod);
})
.catch(function(err) {
sails.log.debug("Create Method template error: " + err);
return res.serverError(err);
});
},
//
// Updates a method template (just method, no instructions)
//
update: function (req, res) {
var params = req.allParams();
if (!params.id)
return res.badRequest("No template id specified");
MetaMethod.findOne(params.id)
.then(function (method) {
if (!method)
return res.serverError("No template found");
method.name = params.name;
method.description = params.description;
delete method.metainstructions;
method.save(function (err) {
if (err)
return res.serverError(err);
return res.ok(method);
});
})
.catch(function (err) {
sails.log.debug("Error updating method" + err.msg);
return res.serverError("Error updating method");
});
},
//
// Deletes a meta methdo
//
destroy: function(req, res){
var params = req.allParams();
if (!params.id) {
return res.badRequest("No meta method defined");
}
// Destroy instructions
MetaInstruction.destroy({ id_met: params.id })
.then(function(metainstructions) {
MetaMethod.destroy({ id: params.id })
.then(function(metamethod) {
if (!metamethod){
sails.log.debug("Destroy MetaMethod: " + err);
return res.serverError("No meta method found");
}
return res.ok(metamethod); return res.ok(metamethod);
}); })
}); .catch(function (err) {
} return res.serverError(err);
});
})
.catch(function(err) {
return res.serverError(err);
});
}
}; };
...@@ -142,7 +142,9 @@ ...@@ -142,7 +142,9 @@
"minutes": "minutes", "minutes": "minutes",
"month_totals": "Month totals", "month_totals": "Month totals",
"name": "Name", "name": "Name",
"new_instruction": "New instruction",
"new_method": "New method", "new_method": "New method",
"new_objective": "New objective",
"new_session": "New session", "new_session": "New session",
"next_actions": "Next actions", "next_actions": "Next actions",
"next_sessions": "Next sessions", "next_sessions": "Next sessions",
...@@ -169,7 +171,7 @@ ...@@ -169,7 +171,7 @@
"office_not_updated": "Office not updated", "office_not_updated": "Office not updated",
"office_updated": "Office updated", "office_updated": "Office updated",
"offices": "Offices", "offices": "Offices",
"own_instructions": "Own method and instructions", "own_instructions": "Own method templates",
"own_labels": "Your labels", "own_labels": "Your labels",
"own_pictos": "Your pictograms", "own_pictos": "Your pictograms",
"pages": "Pages", "pages": "Pages",
......
...@@ -142,7 +142,9 @@ ...@@ -142,7 +142,9 @@
"minutes": "minutos", "minutes": "minutos",
"month_totals": "Totales mes", "month_totals": "Totales mes",
"name": "Nombre", "name": "Nombre",
"new_instruction": "Nueva instrucción",
"new_method": "Nuevo método", "new_method": "Nuevo método",
"new_objective": "Nuevo objetivo",
"new_session": "Nueva sesión", "new_session": "Nueva sesión",
"next_actions": "Acciones posteriores", "next_actions": "Acciones posteriores",
"next_sessions": "Sesiones posteriores", "next_sessions": "Sesiones posteriores",
...@@ -169,7 +171,7 @@ ...@@ -169,7 +171,7 @@
"office_not_updated": "El gabinete no se ha podido actualizar", "office_not_updated": "El gabinete no se ha podido actualizar",
"office_updated": "Gabinete actualizado", "office_updated": "Gabinete actualizado",
"offices": "Gabinetes", "offices": "Gabinetes",
"own_instructions": "Métodos e instrucciones propias", "own_instructions": "Plantillas de métodos propias",
"own_labels": "Propias", "own_labels": "Propias",
"own_pictos": "Propios", "own_pictos": "Propios",
"pages": "Páginas", "pages": "Páginas",
......
...@@ -22,7 +22,7 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr ...@@ -22,7 +22,7 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr
// Query to meta_methods to fill the select fill with precharged methods // Query to meta_methods to fill the select fill with precharged methods
// and supervisor template methods // and supervisor template methods
$http $http
.get(config.backend+'/metamethods/' + $scope.user.id) .get(config.backend+'/method/template/' + $scope.user.id)
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
// Add to list // Add to list
$scope.methods_available = data; $scope.methods_available = data;
...@@ -217,7 +217,6 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr ...@@ -217,7 +217,6 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr
// Add instruction // Add instruction
$scope.add_instruction = function(method){ $scope.add_instruction = function(method){
$http $http
.post(config.backend+'/instruction', { method: method.id } ) .post(config.backend+'/instruction', { method: method.id } )
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
......
...@@ -16,17 +16,14 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s ...@@ -16,17 +16,14 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
// //
$scope.methods_available = []; $scope.methods_available = [];
// Query to meta_methods to fill the select fill with precharged methods // Query to meta_methods to fill the select fill with precharged methods
// and supervisor template methods // and supervisor template methods
$http $http
.get(config.backend+'/method/templates/owned/' + $scope.user.id) .get(config.backend+'/method/template/owned/' + $scope.user.id)
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
// Add to list // Add to list
$scope.methods = data; $scope.methods = data;
console.log("Meta Methods charged:");
console.log(JSON.stringify($scope.methods_available));
// Option to add new methods
$scope.methods_available.push({ id: 0, name: $translate.instant('new_method') });
}) })
.error(function(data, status, headers, config) { .error(function(data, status, headers, config) {
console.log("Error from API: " + data.error); console.log("Error from API: " + data.error);
...@@ -39,39 +36,35 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s ...@@ -39,39 +36,35 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
// Add new method template (metamethod) selected to the student // Add new method template (metamethod) selected to the student
$scope.add_method = function(){ $scope.add_method = function(){
var data = { var new_data = {
name: $translate.instant("new_method"); name: $translate.instant("new_method"),
id_sup: $scope.user.id
}; };
$scope.method.unshift(data);
$http $http
.post(config.backend+'/method/template/save', {'id_sup': $scope.user.id }) .post(config.backend+'/method/template', new_data)
.then( //success .success(function(data, status, headers, config) {
function(data, status, headers, config) { data.metainstructions = [];
ngToast.success($translate.instant('method_saved', {method_name: method.name})); $scope.methods.unshift(data);
} })
,function(data, status, headers, config) { //error .error(function(data, status, headers, config) { //error
ngToast.success($translate.instant('method_name_duplicated', {method_name: method.name})), ngToast.warning($translate.instant("method_name_duplicated", {method_name: new_data.name}));
} });
);
}; };
// Delete method template // Delete method template
$scope.delete_method = function(method){ $scope.delete_method = function(method){
newconfirm($translate.instant('confirmation')).then(function() { newconfirm($translate.instant('confirmation'))
.then(function() {
$http $http
.delete(config.backend+'/method/template/' + $scope.method.id) .delete(config.backend+'/method/template/' + method.id)
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
console.log('Delete Method Template and its Instructions'); console.log('Delete Method Template and its Instructions');
ngToast.success($translate.instant('template_deleted'));
ngToast.success({content: $translate.instant('template_deleted')});
// Delete in select
for(var i=0; i<$scope.methods.length; i++) { for(var i=0; i<$scope.methods.length; i++) {
if($scope.methods[i].id == $scope.method_selected.id){ if($scope.methods[i].id == method.id){
$scope.methods.splice(i,1); $scope.methods.splice(i,1);
$scope.method_selected = null;
break; break;
} }
} }
...@@ -79,7 +72,7 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s ...@@ -79,7 +72,7 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
.error(function(data, status, headers, config) { .error(function(data, status, headers, config) {
console.log("Error from API: " + data.error); console.log("Error from API: " + data.error);
}); });
} });
}; };
// Update method template // Update method template
...@@ -88,7 +81,7 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s ...@@ -88,7 +81,7 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
// Remove instructions as we only update title or description // Remove instructions as we only update title or description
var method_to_save = {}; var method_to_save = {};
Object.assign(method_to_save, method); Object.assign(method_to_save, method);
delete method_to_save.instructions; delete method_to_save.metainstructions;
$http $http
.put(config.backend+'/method/template/' + method.id, method_to_save) .put(config.backend+'/method/template/' + method.id, method_to_save)
...@@ -103,12 +96,18 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s ...@@ -103,12 +96,18 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
// Add instruction template // Add instruction template
$scope.add_instruction = function(method){ $scope.add_instruction = function(method){
var new_instruction = {
method: method.id,
name: $translate.instant("new_instruction"),
objective: $translate.instant("new_objective")
};
$http $http
.post(config.backend+'/instruction/template/', { method: method.id } ) .post(config.backend+'/instruction/template/', new_instruction)
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
console.log('Added instruction:' + JSON.stringify(data)); console.log('Added instruction:' + JSON.stringify(data));
// Add in view // Add in view
method.instructions.push(data); method.metainstructions.push(data);
}) })
.error(function(data, status, headers, config) { .error(function(data, status, headers, config) {
console.log("Error from API: " + data.error); console.log("Error from API: " + data.error);
...@@ -137,11 +136,11 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s ...@@ -137,11 +136,11 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
// Buscar method // Buscar method
for(var i=0; i<$scope.methods.length; i++) { for(var i=0; i<$scope.methods.length; i++) {
var m = $scope.methods[i]; var m = $scope.methods[i];
if(ins.method == m.id){ if(ins.id_met == m.id){
// Buscar la instrucción y eliminar de la vista // Buscar la instrucción y eliminar de la vista
for(var j=0; j<m.instructions.length; j++) { for(var j=0; j<m.metainstructions.length; j++) {
if(ins.id == m.instructions[j].id){ if(ins.id == m.metainstructions[j].id){
$scope.methods[i].instructions.splice(j,1); $scope.methods[i].metainstructions.splice(j,1);
break; break;
} }
} }
......
...@@ -26,11 +26,9 @@ ...@@ -26,11 +26,9 @@
<textarea class="editable" ng-model="m.description " placeholder="{{'description' | translate}}" ng-blur="update_method(m)"></textarea> <textarea class="editable" ng-model="m.description " placeholder="{{'description' | translate}}" ng-blur="update_method(m)"></textarea>
<!-- Tabla método --> <!-- Tabla método -->
<table class="table_instructions table"> <table class="table_instructions table">
<tr> <tr>
<th></th>
<th translate>instruction</th> <th translate>instruction</th>
<th translate>objetive</th> <th translate>objetive</th>
<th></th> <th></th>
......
...@@ -45,9 +45,9 @@ module.exports.routes = { ...@@ -45,9 +45,9 @@ module.exports.routes = {
'POST /method/save': 'MethodController.save', 'POST /method/save': 'MethodController.save',
'DELETE /method/:id': 'MethodController.destroy', 'DELETE /method/:id': 'MethodController.destroy',
'GET /method/templates/:id_sup': 'MetaMethodController.supVisible', 'GET /method/template/:id_sup': 'MetaMethodController.supVisible',
'GET /method/templates/owned/:id_sup': 'MetaMethodController.supOwned', 'GET /method/template/owned/:id_sup': 'MetaMethodController.supOwned',
'POST /method/template/new': 'MetaMethodController.create', 'POST /method/template': 'MetaMethodController.create',
'PUT /method/template/:id': 'MetaMethodController.update', 'PUT /method/template/:id': 'MetaMethodController.update',
'DELETE /method/template/:id': 'MetaMethodController.destroy', 'DELETE /method/template/:id': 'MetaMethodController.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