issue #526 implemented

parent 88f6058f
......@@ -29,7 +29,7 @@ module.exports = {
return res.badRequest();
MetaInstruction.create({
method: method.id,
id_met: method.id,
name: req.param('name'),
objective: req.param('objective'),
})
......@@ -78,7 +78,7 @@ module.exports = {
if (error)
return res.serverError();
return res.ok(instruction);
}
});
})
.catch(function () {
return res.serverError();
......
......@@ -10,128 +10,6 @@
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)
* not associated to a concrete supervisor.
* @param {request} req {} (with idSup specified as url parameter)
......@@ -188,11 +66,20 @@ module.exports = {
});
},
//
// Saves a new meta method (template)
//
save: function(req, res){
/**
* 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();
......@@ -209,7 +96,35 @@ module.exports = {
})
.catch(function(err) {
sails.log.debug("Create Method template error: " + err);
return res.serverError("Error creating method template");
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");
});
},
......@@ -220,26 +135,27 @@ module.exports = {
var params = req.allParams();
if (!params.id_mmet) {
if (!params.id) {
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){
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);
})
.catch(function (err) {
return res.serverError(err);
});
})
.catch(function(err) {
return res.serverError(err);
});
}
......
......@@ -142,7 +142,9 @@
"minutes": "minutes",
"month_totals": "Month totals",
"name": "Name",
"new_instruction": "New instruction",
"new_method": "New method",
"new_objective": "New objective",
"new_session": "New session",
"next_actions": "Next actions",
"next_sessions": "Next sessions",
......@@ -169,7 +171,7 @@
"office_not_updated": "Office not updated",
"office_updated": "Office updated",
"offices": "Offices",
"own_instructions": "Own method and instructions",
"own_instructions": "Own method templates",
"own_labels": "Your labels",
"own_pictos": "Your pictograms",
"pages": "Pages",
......
......@@ -142,7 +142,9 @@
"minutes": "minutos",
"month_totals": "Totales mes",
"name": "Nombre",
"new_instruction": "Nueva instrucción",
"new_method": "Nuevo método",
"new_objective": "Nuevo objetivo",
"new_session": "Nueva sesión",
"next_actions": "Acciones posteriores",
"next_sessions": "Sesiones posteriores",
......@@ -169,7 +171,7 @@
"office_not_updated": "El gabinete no se ha podido actualizar",
"office_updated": "Gabinete actualizado",
"offices": "Gabinetes",
"own_instructions": "Métodos e instrucciones propias",
"own_instructions": "Plantillas de métodos propias",
"own_labels": "Propias",
"own_pictos": "Propios",
"pages": "Páginas",
......
......@@ -22,7 +22,7 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr
// Query to meta_methods to fill the select fill with precharged methods
// and supervisor template methods
$http
.get(config.backend+'/metamethods/' + $scope.user.id)
.get(config.backend+'/method/template/' + $scope.user.id)
.success(function(data, status, headers, config) {
// Add to list
$scope.methods_available = data;
......@@ -217,7 +217,6 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr
// Add instruction
$scope.add_instruction = function(method){
$http
.post(config.backend+'/instruction', { method: method.id } )
.success(function(data, status, headers, config) {
......
......@@ -16,17 +16,14 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
//
$scope.methods_available = [];
// Query to meta_methods to fill the select fill with precharged methods
// and supervisor template methods
$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) {
// Add to list
$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) {
console.log("Error from API: " + data.error);
......@@ -39,39 +36,35 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
// Add new method template (metamethod) selected to the student
$scope.add_method = function(){
var data = {
name: $translate.instant("new_method");
var new_data = {
name: $translate.instant("new_method"),
id_sup: $scope.user.id
};
$scope.method.unshift(data);
$http
.post(config.backend+'/method/template/save', {'id_sup': $scope.user.id })
.then( //success
function(data, status, headers, config) {
ngToast.success($translate.instant('method_saved', {method_name: method.name}));
}
,function(data, status, headers, config) { //error
ngToast.success($translate.instant('method_name_duplicated', {method_name: method.name})),
}
);
.post(config.backend+'/method/template', new_data)
.success(function(data, status, headers, config) {
data.metainstructions = [];
$scope.methods.unshift(data);
})
.error(function(data, status, headers, config) { //error
ngToast.warning($translate.instant("method_name_duplicated", {method_name: new_data.name}));
});
};
// Delete method template
$scope.delete_method = function(method){
newconfirm($translate.instant('confirmation')).then(function() {
newconfirm($translate.instant('confirmation'))
.then(function() {
$http
.delete(config.backend+'/method/template/' + $scope.method.id)
.delete(config.backend+'/method/template/' + method.id)
.success(function(data, status, headers, config) {
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++) {
if($scope.methods[i].id == $scope.method_selected.id){
if($scope.methods[i].id == method.id){
$scope.methods.splice(i,1);
$scope.method_selected = null;
break;
}
}
......@@ -79,7 +72,7 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
.error(function(data, status, headers, config) {
console.log("Error from API: " + data.error);
});
}
});
};
// Update method template
......@@ -88,7 +81,7 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
// Remove instructions as we only update title or description
var method_to_save = {};
Object.assign(method_to_save, method);
delete method_to_save.instructions;
delete method_to_save.metainstructions;
$http
.put(config.backend+'/method/template/' + method.id, method_to_save)
......@@ -103,12 +96,18 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
// Add instruction template
$scope.add_instruction = function(method){
var new_instruction = {
method: method.id,
name: $translate.instant("new_instruction"),
objective: $translate.instant("new_objective")
};
$http
.post(config.backend+'/instruction/template/', { method: method.id } )
.post(config.backend+'/instruction/template/', new_instruction)
.success(function(data, status, headers, config) {
console.log('Added instruction:' + JSON.stringify(data));
// Add in view
method.instructions.push(data);
method.metainstructions.push(data);
})
.error(function(data, status, headers, config) {
console.log("Error from API: " + data.error);
......@@ -137,11 +136,11 @@ dashboardControllers.controller('InstructionsCtrl', function InstructionsCtrl($s
// Buscar method
for(var i=0; i<$scope.methods.length; 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
for(var j=0; j<m.instructions.length; j++) {
if(ins.id == m.instructions[j].id){
$scope.methods[i].instructions.splice(j,1);
for(var j=0; j<m.metainstructions.length; j++) {
if(ins.id == m.metainstructions[j].id){
$scope.methods[i].metainstructions.splice(j,1);
break;
}
}
......
......@@ -26,11 +26,9 @@
<textarea class="editable" ng-model="m.description " placeholder="{{'description' | translate}}" ng-blur="update_method(m)"></textarea>
<!-- Tabla método -->
<table class="table_instructions table">
<tr>
<th></th>
<th translate>instruction</th>
<th translate>objetive</th>
<th></th>
......
......@@ -45,9 +45,9 @@ module.exports.routes = {
'POST /method/save': 'MethodController.save',
'DELETE /method/:id': 'MethodController.destroy',
'GET /method/templates/:id_sup': 'MetaMethodController.supVisible',
'GET /method/templates/owned/:id_sup': 'MetaMethodController.supOwned',
'POST /method/template/new': 'MetaMethodController.create',
'GET /method/template/:id_sup': 'MetaMethodController.supVisible',
'GET /method/template/owned/:id_sup': 'MetaMethodController.supOwned',
'POST /method/template': 'MetaMethodController.create',
'PUT /method/template/:id': 'MetaMethodController.update',
'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