new controllers added

parent bfc040f7
/* global Instruction, Method */
/**
* MetaInstructionController manages the requests related to the MetaInstruction model.
* Read it's documentation for further information.
* @type {Object}
*/
module.exports = {
/**
* Create a new Instruction, which is associated to the given method
* @param {request} req
* {
* method: metaMethodId,
* name: 'New instruction' (optional)
* objective: 'This instruction has an objective... description' (optional)
* }
* @param {response} res
* {
* id (instructionId)
* method
* name
* objective
* }
*/
create: function (req, res) {
MetaMethod.findOne({ id: req.param('method') }).then(function (method) {
if (!method)
return res.badRequest();
MetaInstruction.create({
method: method.id,
name: req.param('name'),
objective: req.param('objective'),
})
.then(function (instruction) {
if (instruction)
return res.ok(instruction);
return res.badRequest();
})
.catch(function (err) {
return res.serverError(err);
});
})
.catch(function (err) {
return res.serverError(err);
});
},
/**
* Update an existing Instruction template, known by it's ID
* @param {request} req (with instruction id as url parameter)
* {
* method: methodId, (optional)
* name: 'New instruction' (optional)
* objective: 'This instruction has an objective... description' (optional)
* }
* @param {response} res
* {
* id
* method
* name
* objective
* }
*/
update: function (req, res) {
if (!req.params.id)
return res.badRequest();
MetaInstruction.findOne({ id: req.params.id })
.then(function (instruction) {
if (!instruction)
return res.badRequest();
instruction.method = req.param('method') || instruction.method;
instruction.name = req.param('name') || instruction.name;
instruction.objective = req.param('objective') || instruction.objective;
instruction.save(function (error) {
if (error)
return res.serverError();
return res.ok(instruction);
}
})
.catch(function () {
return res.serverError();
});
},
/**
* Delete an instruction template by its ID
* @param {request} req {} (with instructionId as url parameter)
* @param {response} res {}
*/
destroy: function (req, res) {
if (!req.params.id)
return res.badRequest();
MetaInstruction.destroy({ id: req.params.id }).exec(function (error) {
if (error)
return res.badRequest();
else
return res.ok();
});
}
};
/* global sails, Method, MetaMethod */
/**
* MethodController
*
* @description :: Server-side logic for managing methods
* @help :: See http://links.sailsjs.org/docs/controllers
*/
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)
* @param {response} res
* [
* {
* id: metamethodId
* name: 'Metamethod Name'
* description: 'Metamethod description, which can be longer'
* },
* ...
* ]
*/
supVisible: function (req, res) {
var params = req.allParams();
MetaMethod.find({ or: [
{ supervisor: null },
{ supervisor: params.id_sup }
] }).then(function (metaMethods) {
res.ok(metaMethods);
})
.catch(function (err) {
res.serverError(err);
});
},
/**
* Return all the metamethods of a given supervisor he owns
* @param {request} req {} (with idSup specified as url parameter)
* @param {response} res
* [
* {
* id: metamethodId
* name: 'Metamethod Name'
* description: 'Metamethod description, which can be longer'
* },
* ...
* ]
*/
supOwned: function (req, res) {
var params = req.allParams();
if (!params.id_sup)
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");
}
return res.ok(metamethod);
});
});
}
};
...@@ -131,35 +131,11 @@ module.exports = { ...@@ -131,35 +131,11 @@ module.exports = {
} }
}, },
/**
* 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)
* @param {response} res
* [
* {
* id: metamethodId
* name: 'Metamethod Name'
* description: 'Metamethod description, which can be longer'
* },
* ...
* ]
*/
meta_methods: function (req, res) {
var params = req.allParams();
MetaMethod.find({ or: [
{ supervisor: null },
{ supervisor: params.id_sup }
] }).then(function (metaMethods) {
res.ok(metaMethods);
})
.catch(function () {
res.serverError();
});
},
// FERNANDO: avoid method name duplicates // FERNANDO: avoid method name duplicates
//
// Saves a method as a template
//
save: function(req, res){ save: function(req, res){
var params = req.allParams(); var params = req.allParams();
...@@ -173,8 +149,6 @@ module.exports = { ...@@ -173,8 +149,6 @@ module.exports = {
return res.json(500, {error: "No method found"}); // empty array return res.json(500, {error: "No method found"}); // empty array
} }
MetaMethod.create({ MetaMethod.create({
"name": method.name, "name": method.name,
"description": method.description, "description": method.description,
...@@ -230,68 +204,27 @@ module.exports = { ...@@ -230,68 +204,27 @@ module.exports = {
var params = req.allParams(); var params = req.allParams();
if (!params.id) { if (!params.id)
return res.json(500, {error: "No method defined"}); return res.badRquest("No method defined");
}
// Destroy instructions // Destroy instructions
Instruction.destroy({ method: params.id }).exec(function(err, instructions) { Instruction.destroy({ method: params.id }).exec(function(err, instructions) {
if (err || !instructions){ if (err || !instructions){
sails.log.debug("Destroy Instructions: " + err); sails.log.debug("Destroy Instructions: " + err);
return res.json(500, {error: "Cannot delete instructions"}); return res.serverError("Cannot delete instructions");
} }
// Destroy method // Destroy method
Method.destroy({ id: params.id }).exec(function(err, method) { Method.destroy({ id: params.id }).exec(function(err, method) {
if (err || !method){
sails.log.debug("Destroy Method: " + err);
return res.serverError("No method found");
}
return res.ok(method);
});
});
if (err || !method){
sails.log.debug("Destroy Method: " + err);
return res.json(500, {error: "No method found"});
}
return res.json(method);
});
});
},
destroyTemplate: function(req, res){
var params = req.allParams();
if (!params.id_mmet) {
return res.json(500, {error: "No meta method defined"});
}
// Destroy instructions
MetaInstruction.destroy({ id_met: params.id_mmet }).exec(function(err, metainstructions) {
if (err || !metainstructions){
sails.log.debug("Destroy MetaInstructions: " + err);
return res.json(500, {error: "No meta instructions found"});
}
// Destroy method
MetaMethod.destroy({ id: params.id_mmet }).exec(function(err, metamethod) {
if (err || !metamethod){
sails.log.debug("Destroy MetaMethod: " + err);
return res.json(500, {error: "No meta method found"});
}
return res.json(metamethod);
});
});
} }
}; };
...@@ -595,4 +595,5 @@ module.exports = { ...@@ -595,4 +595,5 @@ module.exports = {
res.badRequest(); res.badRequest();
} }
} }
}; };
...@@ -142,6 +142,7 @@ ...@@ -142,6 +142,7 @@
"minutes": "minutes", "minutes": "minutes",
"month_totals": "Month totals", "month_totals": "Month totals",
"name": "Name", "name": "Name",
"new_method": "New method",
"new_session": "New session", "new_session": "New session",
"next_actions": "Next actions", "next_actions": "Next actions",
"next_sessions": "Next sessions", "next_sessions": "Next sessions",
...@@ -168,6 +169,7 @@ ...@@ -168,6 +169,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_labels": "Your labels", "own_labels": "Your labels",
"own_pictos": "Your pictograms", "own_pictos": "Your pictograms",
"pages": "Pages", "pages": "Pages",
......
...@@ -142,6 +142,7 @@ ...@@ -142,6 +142,7 @@
"minutes": "minutos", "minutes": "minutos",
"month_totals": "Totales mes", "month_totals": "Totales mes",
"name": "Nombre", "name": "Nombre",
"new_method": "Nuevo método",
"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",
...@@ -168,6 +169,7 @@ ...@@ -168,6 +169,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_labels": "Propias", "own_labels": "Propias",
"own_pictos": "Propios", "own_pictos": "Propios",
"pages": "Páginas", "pages": "Páginas",
......
...@@ -94,6 +94,12 @@ dashboardApp.config(function ($stateProvider, $urlRouterProvider) { ...@@ -94,6 +94,12 @@ dashboardApp.config(function ($stateProvider, $urlRouterProvider) {
templateUrl: 'modules/supervisor/views/setup.html', templateUrl: 'modules/supervisor/views/setup.html',
controller: 'SetupCtrl', controller: 'SetupCtrl',
}) })
.state('instructions', {
url: '/instructions',
parent: 'supervisor',
templateUrl: 'modules/supervisor/views/instructions.html',
controller: 'InstructionsCtrl',
})
// Abstract page for student // Abstract page for student
.state('student', { .state('student', {
templateUrl: 'modules/student/views/student.html', templateUrl: 'modules/student/views/student.html',
......
...@@ -29,7 +29,7 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr ...@@ -29,7 +29,7 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr
console.log("Meta Methods charged:"); console.log("Meta Methods charged:");
console.log(JSON.stringify($scope.methods_available)); console.log(JSON.stringify($scope.methods_available));
// Option to add new methods // Option to add new methods
$scope.methods_available.push({ id: 0, name: "Nuevo método" }); $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);
...@@ -99,7 +99,7 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr ...@@ -99,7 +99,7 @@ dashboardControllers.controller('StudentInstructionsCtrl', function StudentInstr
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
console.log('Created Method and Instructions'); console.log('Created Method and Instructions');
console.log('Method (with Instructions):' + JSON.stringify(data)); console.log('Method (with Instructions):' + JSON.stringify(data));
$scope.methods.push(data); $scope.methods.unshift(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);
......
...@@ -27,6 +27,18 @@ ...@@ -27,6 +27,18 @@
</div> </div>
<ul class="dropdown-menu" role="menu"> <ul class="dropdown-menu" role="menu">
<li> <li>
<a class="pointer" role="menuitem" tabindex="0" href="/app/#/students">
<i class="glyphicon glyphicon-user" aria-hidden="true"></i>
{{ 'students' | translate }}
</a>
</li>
<li>
<a class="pointer" role="menuitem" tabindex="0" href="/app/#/instructions">
<i class="glyphicon glyphicon-tasks" aria-hidden="true"></i>
{{ 'instructions' | translate }}
</a>
</li>
<li>
<a class="pointer" role="menuitem" tabindex="0" href="/app/#/setup"> <a class="pointer" role="menuitem" tabindex="0" href="/app/#/setup">
<i class="glyphicon glyphicon-cog" aria-hidden="true"></i> <i class="glyphicon glyphicon-cog" aria-hidden="true"></i>
{{ 'setup' | translate }} {{ 'setup' | translate }}
......
<!-- InstructionsCtrl controls here, see app.js --> <!-- InstructionsCtrl controls here, see app.js -->
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title" translate>students</h3></div> <div class="panel-heading"><h3 class="panel-title" translate>own_instructions</h3></div>
<div class="panel-body"> <div class="panel-body">
<!-- Select to add new method --> <!-- Select to add new method -->
<div class="form-group"> <div class="form-group">
<select class="form-control" name="method_select" id="method_select" ng-model="method_selected" ng-options="ma.name for ma in methods_available">
<option value="" translate>select_method</option>
</select>
<!-- Botón añadir método --> <!-- Botón añadir método -->
<button ng-click="add_method()" class="btn btn-success btn-sm" popover="{{ 'add' | translate}}" popover-trigger="mouseenter" ng-disabled="method_selected.id == undefined"> <button ng-click="add_method()" class="btn btn-success btn-sm" popover="{{ 'add' | translate}}" popover-trigger="mouseenter">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> <span translate>new_method</span>
</button> </button>
<!-- Botón eliminar plantilla -->
<button ng-click="delete_template()" class="btn btn-danger btn-sm" popover="{{ 'delete_template' | translate}}" popover-trigger="mouseenter" ng-show="method_selected.supervisor == user.id">
<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
</button>
</div> </div>
<!-- END select to add new method --> <!-- END select to add new method -->
...@@ -28,8 +21,6 @@ ...@@ -28,8 +21,6 @@
<input type="text" class="editable title" ng-model="m.name " ng-blur="update_method(m)"/> <input type="text" class="editable title" ng-model="m.name " ng-blur="update_method(m)"/>
<div class="options"> <div class="options">
<a ng-click="save_as_template(m)" popover="{{ 'save_as_template' | translate}}" popover-trigger="mouseenter"><span class="text_medium color_black glyphicon glyphicon-floppy-disk" aria-hidden="true"></span></a>
<a ng-click="delete_method(m)" popover="{{ 'delete' | translate}}" popover-trigger="mouseenter"><span class="text_medium delete color_red glyphicon glyphicon-remove-circle" aria-hidden="true"></span></a> <a ng-click="delete_method(m)" popover="{{ 'delete' | translate}}" popover-trigger="mouseenter"><span class="text_medium delete color_red glyphicon glyphicon-remove-circle" aria-hidden="true"></span></a>
</div> </div>
...@@ -42,40 +33,11 @@ ...@@ -42,40 +33,11 @@
<th></th> <th></th>
<th translate>instruction</th> <th translate>instruction</th>
<th translate>objetive</th> <th translate>objetive</th>
<th class="text-center" translate>instruction_begin</th>
<th class="text-center" translate>instruction_end</th>
<th class="text-center" translate>state</th>
<th></th> <th></th>
</tr> </tr>
<tr ng-repeat="i in m.instructions"> <tr ng-repeat="i in m.metainstructions">
<td>
<a ng-click="open_instruction(i)"><span class="glyphicon glyphicon-file text_medium" aria-hidden="true"></span></a>
</td>
<td><input class="editable" type="text" ng-model="i.name" ng-blur="update_instruction(i)" /></td> <td><input class="editable" type="text" ng-model="i.name" ng-blur="update_instruction(i)" /></td>
<td><input class="elipsis editable" type="text" ng-model="i.objective" ng-blur="update_instruction(i)" /></td> <td><input class="elipsis editable" type="text" ng-model="i.objective" ng-blur="update_instruction(i)" /></td>
<td class="editable">
<div class="text-center">
<span class="color_blue">{{ i.begin | date:"dd-MM-yyyy" }}</span>
<br />
{{ i.begin | date:"HH:mm:ss" }}
</div>
</td>
<td class="editable">
<div class="text-center" ng-class="{ color_green : i.id == m.last_ins }">
<span ng-class="{ color_green : i.id == m.last_ins, color_blue : i.id != m.last_ins }">{{ i.end | date:"dd-MM-yyyy" }}</span>
<br/>
{{ i.end | date:"HH:mm:ss" }}
</div>
</td>
<td class="editable_status">
<span class="pointer text_medium glyphicon" ng-class="{
'color_green': i.status == 'finished',
'glyphicon-check': i.status == 'finished',
'color_blue': i.status == 'started',
'glyphicon-edit': i.status == 'started',
'glyphicon-minus': i.status == null
}" aria-hidden="true" popover="{{(i.status || 'nobegin') | translate}}" popover-trigger="mouseenter" ng-click="change_status(i)"></span>
</td>
<td><a confirmed-click="delete_instruction(i);" ng-confirm-click="{{ 'confirmation' | translate}}" class="delete_instruction"><span class="text_medium delete color_red glyphicon glyphicon-remove-circle" aria-hidden="true"></span></a></td> <td><a confirmed-click="delete_instruction(i);" ng-confirm-click="{{ 'confirmation' | translate}}" class="delete_instruction"><span class="text_medium delete color_red glyphicon glyphicon-remove-circle" aria-hidden="true"></span></a></td>
</tr> </tr>
</table> </table>
......
...@@ -37,14 +37,26 @@ module.exports.policies = { ...@@ -37,14 +37,26 @@ module.exports.policies = {
destroy: ['tokenAuth'] destroy: ['tokenAuth']
}, },
MetaInstructionController: {
update: ['tokenAuth'],
create: ['tokenAuth'],
destroy: ['tokenAuth']
},
MetaMethodController: {
supVisible: ['tokenAuth'],
supOwned: ['tokenAuth'],
create: ['tokenAuth'],
update: ['tokenAuth'],
destroy: ['tokenAuth']
},
MethodController: { MethodController: {
update: ['tokenAuth'], update: ['tokenAuth'],
create: ['tokenAuth'], create: ['tokenAuth'],
newMethod: ['tokenAuth'], newMethod: ['tokenAuth'],
save: ['tokenAuth'], save: ['tokenAuth'],
destroy: ['tokenAuth'], destroy: ['tokenAuth'],
destroyTemplate: ['tokenAuth'],
meta_methods: ['tokenAuth']
}, },
OfficeController: { OfficeController: {
......
...@@ -35,14 +35,21 @@ module.exports.routes = { ...@@ -35,14 +35,21 @@ module.exports.routes = {
'POST /instruction': 'InstructionController.create', 'POST /instruction': 'InstructionController.create',
'DELETE /instruction/:id': 'InstructionController.destroy', 'DELETE /instruction/:id': 'InstructionController.destroy',
'GET /metamethods/:id_sup': 'MethodController.meta_methods', 'PUT /instruction/template/:id': 'MetaInstructionController.update',
'POST /instruction/template': 'MetaInstructionController.create',
'DELETE /instruction/template/:id': 'MetaInstructionController.destroy',
'PUT /method/:id': 'MethodController.update', 'PUT /method/:id': 'MethodController.update',
'POST /method': 'MethodController.create', 'POST /method': 'MethodController.create',
'POST /method/new': 'MethodController.newMethod', 'POST /method/new': 'MethodController.newMethod',
'POST /method/save': 'MethodController.save', 'POST /method/save': 'MethodController.save',
'DELETE /method/:id': 'MethodController.destroy', 'DELETE /method/:id': 'MethodController.destroy',
'DELETE /method/template/:id_mmet': 'MethodController.destroyTemplate',
'GET /method/templates/:id_sup': 'MetaMethodController.supVisible',
'GET /method/templates/owned/:id_sup': 'MetaMethodController.supOwned',
'POST /method/template/new': 'MetaMethodController.create',
'PUT /method/template/:id': 'MetaMethodController.update',
'DELETE /method/template/:id': 'MetaMethodController.destroy',
'GET /office/get_all': 'OfficeController.getAll', 'GET /office/get_all': 'OfficeController.getAll',
'GET /office/get/:id': 'OfficeController.get', 'GET /office/get/:id': 'OfficeController.get',
......
...@@ -44,6 +44,7 @@ module.exports = function (grunt) { ...@@ -44,6 +44,7 @@ module.exports = function (grunt) {
'assets/scripts/modules/supervisor/controllers/supervisor.js', 'assets/scripts/modules/supervisor/controllers/supervisor.js',
'assets/scripts/modules/supervisor/controllers/students.js', 'assets/scripts/modules/supervisor/controllers/students.js',
'assets/scripts/modules/supervisor/controllers/setup.js', 'assets/scripts/modules/supervisor/controllers/setup.js',
'assets/scripts/modules/supervisor/controllers/instructions.js',
'assets/scripts/modules/student/controllers/setup.js', 'assets/scripts/modules/student/controllers/setup.js',
'assets/scripts/modules/student/controllers/student.js', 'assets/scripts/modules/student/controllers/student.js',
'assets/scripts/modules/student/controllers/session.js', 'assets/scripts/modules/student/controllers/session.js',
......
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