issue #815 closed: responses for update and add in picto homogenized

parent 33d65db5
...@@ -786,12 +786,6 @@ module.exports = { ...@@ -786,12 +786,6 @@ module.exports = {
* source: 1 @TODO Other sources * source: 1 @TODO Other sources
* owner: supervisorId or null * owner: supervisorId or null
* }, * },
* expression: {
* id: expressionId,
* lang: 'es-es',
* text: 'Picto Expression',
* picto: pictoId
* },
* attributes: { @see StuPicto.getValidAttributes() } * attributes: { @see StuPicto.getValidAttributes() }
* }, * },
* ... * ...
...@@ -852,7 +846,6 @@ module.exports = { ...@@ -852,7 +846,6 @@ module.exports = {
* student: studentId (speficied as url parameter) * student: studentId (speficied as url parameter)
* picto: { @see Picto model} * picto: { @see Picto model}
* attributes: { @see StuPicto.getValidAttributes() } * attributes: { @see StuPicto.getValidAttributes() }
* expression: { @see Picto model }
* } * }
*/ */
add_picto: function (req, res) { add_picto: function (req, res) {
...@@ -892,12 +885,9 @@ module.exports = { ...@@ -892,12 +885,9 @@ module.exports = {
if (!stuPicto) if (!stuPicto)
throw new Error("stu_picto not created"); throw new Error("stu_picto not created");
sails.log.debug("->>" + JSON.stringify(picto)); sails.log.debug("->>" + JSON.stringify(picto));
return res.ok({ Student.pictoInfo(stuPicto.id, function(err, resp) {
id: stuPicto.id, if (err) throw err;
student: params.id_stu, return res.ok(resp);
attributes: stuPicto.attributes,
picto: picto,
expression: picto.expressions[0]
}); });
}) })
.catch(err => res.serverError("Error adding picto: " + err)); .catch(err => res.serverError("Error adding picto: " + err));
...@@ -944,14 +934,11 @@ module.exports = { ...@@ -944,14 +934,11 @@ module.exports = {
if (!updated) if (!updated)
throw new Error ("error on update"); throw new Error ("error on update");
// return res.json(updated[0]); Student.pictoInfo(updated[0].id, function(err, resp) {
return res.ok({ if (err) throw err;
id: updated[0].id, // id of stu_picto return res.ok(resp);
attributes: updated[0].attributes, // picto attributes for student
picto: {
id: updated[0].picto // picto information
}
}); });
}) })
.catch(err => { .catch(err => {
return res.serverError('Unable to update picto for student: ' + err); return res.serverError('Unable to update picto for student: ' + err);
...@@ -1007,7 +994,10 @@ module.exports = { ...@@ -1007,7 +994,10 @@ module.exports = {
sps[i].save(); sps[i].save();
} }
} }
return res.ok(sp); Student.pictoInfo(sp.id, function(err, resp) {
if (err) throw err;
return res.ok(resp);
});
}) })
.catch(err => {throw err}); .catch(err => {throw err});
}); });
...@@ -1148,8 +1138,6 @@ module.exports = { ...@@ -1148,8 +1138,6 @@ module.exports = {
var roomName = 'studentRoom' + attributes.id_stu; var roomName = 'studentRoom' + attributes.id_stu;
sails.log.debug("Inside vocabulary");
if (req.isSocket) { if (req.isSocket) {
sails.log.debug("Inside vocabulary - isSocket"); sails.log.debug("Inside vocabulary - isSocket");
......
...@@ -130,6 +130,22 @@ module.exports = function eventsHook(sails) { ...@@ -130,6 +130,22 @@ module.exports = function eventsHook(sails) {
attributes: attributes attributes: attributes
} }
}; };
},
/**
* Vocabulary is updated
* @param {action} type of the action
* @param {attributes} attributes of the action (id_stu, stu_picto)
* @return {Object} {name, data}
*/
vocabulary: function (action, attributes) {
return {
name: 'vocabulary',
data: {
action: action,
attributes: attributes
}
};
} }
}; };
}; };
...@@ -90,7 +90,6 @@ module.exports = { ...@@ -90,7 +90,6 @@ module.exports = {
color: null, color: null,
expression: null, expression: null,
legend: 'none', legend: 'none',
delete_strip_after_delivery: true
}; };
if (typeof attributes === 'object') { if (typeof attributes === 'object') {
...@@ -129,10 +128,7 @@ module.exports = { ...@@ -129,10 +128,7 @@ module.exports = {
} }
if (!((/^(none|normal|full)$/).test(validAttributes.legend))) { if (!((/^(none|normal|full)$/).test(validAttributes.legend))) {
delete validAttributes.legend; delete validAttributes.legend;
} }
if (typeof validAttributes.delete_strip_after_delivery !== 'boolean') {
delete validAttributes.delete_strip_after_delivery;
}
if (typeof validAttributes.highlight !== 'boolean') { if (typeof validAttributes.highlight !== 'boolean') {
delete validAttributes.highlight; delete validAttributes.highlight;
} }
......
...@@ -378,10 +378,10 @@ module.exports = { ...@@ -378,10 +378,10 @@ module.exports = {
picto.imageFileExists(function(found) { picto.imageFileExists(function(found) {
if (found) { if (found) {
// Now we have everything, so we add the picto to the list // 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 = { var stuPictoToAdd = {
"id": stuPicto.id, "id": stuPicto.id,
"picto": stuPicto.picto, "picto": stuPicto.picto,
"expression": stuPicto.attributes.expression ? stuPicto.attributes.expression : picto.expressions[0].text,
"attributes": stuPicto.attributes, "attributes": stuPicto.attributes,
"tags": picto.tags ? picto.tags : [] "tags": picto.tags ? picto.tags : []
}; };
...@@ -542,5 +542,30 @@ module.exports = { ...@@ -542,5 +542,30 @@ module.exports = {
else else
cb(); cb();
}); });
},
/*
* Returns the information about a picto of a student to be sent in HTTP responses
* This method homogenizes the responses in HTTP requests and Websockets
* @param id_stu_pic {ID} stu_picto record id
* @param cb {function(err, result)} callback function
*/
pictoInfo: function(id_stu_pic, cb) {
StuPicto.findOne(id_stu_pic)
.populate('student')
.populate('picto')
.then(sp => {
PictoExp.findOne({id_pic: sp.picto.id, lang: sp.student.lang})
.then(pe => {
if (typeof sp.attributes.expression == 'undefined' || sp.attributes.expression == null)
sp.attributes.expression = pe.text;
sp.student = sp.student.id;
delete sp.picto.expressions;
cb(null,sp);
})
.catch(err => {throw err});
})
.catch(err => {cb(err, null)});
} }
}; };
...@@ -29,12 +29,6 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -29,12 +29,6 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
owner: null, owner: null,
tags: null tags: null
}, },
expression: {
id: null,
lang: null,
text: '',
picto: null
},
attributes: { attributes: {
id_cat: null, id_cat: null,
coord_x: null, coord_x: null,
...@@ -121,7 +115,6 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -121,7 +115,6 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
$scope.getCategoryId(picto); $scope.getCategoryId(picto);
$scope.studentPictos[category] = $scope.studentPictos[category] || generateGrid(); $scope.studentPictos[category] = $scope.studentPictos[category] || generateGrid();
$scope.studentPictos[category][positionX][positionY] = picto; $scope.studentPictos[category][positionX][positionY] = picto;
console.log("picto placed at " + positionX + " " + positionY);
} }
}; };
...@@ -337,7 +330,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -337,7 +330,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
// notify // notify
io.socket.post('/stu/vocabulary', { io.socket.post('/stu/vocabulary', {
action: 'update', action: 'update_category',
attributes: { attributes: {
id_stu: $scope.studentData.id, id_stu: $scope.studentData.id,
stu_picto: studentPicto stu_picto: studentPicto
...@@ -447,10 +440,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec ...@@ -447,10 +440,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
} }
} }
}); });
// Returned data from the modal window
modalInstance.result.then(function (exp) {
studentPicto.expression = exp;
});
}; };
// Add new listener to the event // Add new listener to the event
......
...@@ -13,76 +13,78 @@ dashboardControllers.controller('PictoConfigCtrl', function ($window, $scope, $t ...@@ -13,76 +13,78 @@ dashboardControllers.controller('PictoConfigCtrl', function ($window, $scope, $t
*/ */
$scope.save = function () { $scope.save = function () {
if ($scope.studentPicto.expression != studentPicto.expression) { new Promise(function (resolve, reject) {
// Update picto expressio because user owns it if ($scope.studentPicto.attributes.expression != studentPicto.attributes.expression) {
if (studentPicto.picto.owner == sup.id) { // Update picto expressio because user owns it
if (studentPicto.picto.owner == sup.id) {
$http
.post(config.backend+'/picto/exp',
{ 'picto': studentPicto.picto.id,
'lang': sup.lang,
'text': $scope.studentPicto.attributes.expression
})
.success(function(data, status, headers, config) {
resolve();
})
.error(function(data, status, headers, config) {
reject(data);
});
} else resolve();
} else resolve();
})
.then(function(result) {
return new Promise(function (resolve, reject) {
// Update attributes
$http $http
.post(config.backend+'/picto/exp', .put(config.backend+'/stu/'+ stu.id + '/picto/' + $scope.studentPicto.id,
{ 'picto': studentPicto.picto.id, {
'lang': sup.lang, 'attributes': $scope.studentPicto.attributes
'text': $scope.studentPicto.expression
}) })
.success(function(data, status, headers, config) { .success(function(data, status, headers, config) {
io.socket.post('/stu/vocabulary', { resolve(data);
action: 'update',
attributes: {
id_stu: stu.id,
stu_picto: {
id: data.id,
expression: data.expression,
attributes: $scope.studentPicto.attributes,
picto: { id: data.picto }
}
}
}, function (res) {});
}) })
.error(function(data, status, headers, config) { .error(function(data, status, headers, config) {
console.log("Error from API: " + data.error); reject(data);
}); });
} else { });
$scope.studentPicto.attributes.expression = $scope.studentPicto.expression; })
} .then(function(result) {
} return new Promise(function (resolve, reject) {
if ($scope.update_all_legend) {
// Update attributes $http
$http .put(config.backend+'/stu/'+ stu.id + '/legend/' + $scope.studentPicto.attributes.legend)
.put(config.backend+'/stu/'+ stu.id + '/picto/' + $scope.studentPicto.id, .success(function(data, status, headers, config) {
{ resolve(result);
'attributes': $scope.studentPicto.attributes })
}) .error(function(data, status, headers, config) {
.success(function(data, status, headers, config) { console.log("Error from API: " + data.error);
});
}
else
resolve(result);
});
})
.then(function(result) {
return new Promise(function (resolve, reject) {
io.socket.post('/stu/vocabulary', { io.socket.post('/stu/vocabulary', {
action: 'update', action: 'update',
attributes: { attributes: {
id_stu: stu.id, id_stu: stu.id,
stu_picto: data stu_picto: result
} }
}, },
function(res) {}); function(res) {});
})
.error(function(data, status, headers, config) {
console.log("Error from API: " + data.error);
});
// Update all lengends format studentPicto.attributes = $scope.studentPicto.attributes;
if ($scope.update_all_legend) { $modalInstance.close($scope.studentPicto.attributes.expression);
$http if ($scope.update_all_legend) {
.put(config.backend+'/stu/'+ stu.id + '/legend/' + $scope.studentPicto.attributes.legend) ngToast.success({ content: $translate.instant('reloading_pictos') });
.success(function(data, status, headers, config) { $window.location.reload();
}) }
.error(function(data, status, headers, config) {
console.log("Error from API: " + data.error);
});
}
studentPicto.expression = $scope.studentPicto.expression; })
studentPicto.attributes = $scope.studentPicto.attributes; });
$modalInstance.close($scope.studentPicto.expression);
if ($scope.update_all_legend) {
ngToast.success({ content: $translate.instant('reloading_pictos') });
$window.location.reload();
}
}; };
$scope.close = function () { $scope.close = function () {
......
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
('empty-' + colIndex + '-' + rowIndex) ('empty-' + colIndex + '-' + rowIndex)
}}" }}"
draggable droppable drop="handleDrop" draggable droppable drop="handleDrop"
popover="{{studentPicto != emptyStudentPicto ? studentPicto.expression : ''}}" popover="{{studentPicto != emptyStudentPicto ? studentPicto.attributes.expression : ''}}"
popover-trigger="mouseenter" popover-trigger="mouseenter"
ng-init="colIndex = $index" ng-init="colIndex = $index"
ng-repeat="studentPicto in studentPictoRow track by $index"> ng-repeat="studentPicto in studentPictoRow track by $index">
...@@ -41,12 +41,12 @@ ...@@ -41,12 +41,12 @@
<div <div
class="picto-legend-normal" class="picto-legend-normal"
ng-if="studentPicto.attributes.legend == 'normal'"> ng-if="studentPicto.attributes.legend == 'normal'">
{{ studentPicto.expression }} {{ studentPicto.attributes.expression }}
</div> </div>
<div <div
class="picto-legend-full" class="picto-legend-full"
ng-if="studentPicto.attributes.legend == 'full'"> ng-if="studentPicto.attributes.legend == 'full'">
{{ studentPicto.expression }} {{ studentPicto.attributes.expression }}
</div> </div>
<!-- /.picto-legend --> <!-- /.picto-legend -->
<img <img
...@@ -120,19 +120,19 @@ ...@@ -120,19 +120,19 @@
class="picto pull-left ng-class:{'picto-out': studentData.attributes.size == 'large' && (rowIndex > 3 || colIndex > 7)};" class="picto pull-left ng-class:{'picto-out': studentData.attributes.size == 'large' && (rowIndex > 3 || colIndex > 7)};"
ng-repeat="studentPicto in studentPictoRow track by $index" ng-repeat="studentPicto in studentPictoRow track by $index"
ng-init="colIndex = $index" ng-init="colIndex = $index"
popover="{{ studentPicto != emptyStudentPicto ? studentPicto.expression : ''}}" popover="{{ studentPicto != emptyStudentPicto ? studentPicto.attributes.expression : ''}}"
popover-trigger="mouseenter"> popover-trigger="mouseenter">
<!-- picto-legend --> <!-- picto-legend -->
<div <div
class="picto-legend-normal" class="picto-legend-normal"
ng-if="studentPicto.attributes.legend == 'normal'"> ng-if="studentPicto.attributes.legend == 'normal'">
{{ studentPicto.expression }} {{ studentPicto.attributes.expression }}
</div> </div>
<div <div
class="picto-legend-full" class="picto-legend-full"
ng-if="studentPicto.attributes.legend == 'full'"> ng-if="studentPicto.attributes.legend == 'full'">
{{ studentPicto.expression }} {{ studentPicto.attributes.expression }}
</div> </div>
<!-- /.picto-legend --> <!-- /.picto-legend -->
<img <img
...@@ -225,7 +225,7 @@ ...@@ -225,7 +225,7 @@
class="picto-grid picto-category-grid" class="picto-grid picto-category-grid"
ng-if="selectedCategory !== emptyStudentPicto && !showFreeCategory" ng-if="selectedCategory !== emptyStudentPicto && !showFreeCategory"
ng-style="{ 'background-color': shadeColor(selectedCategory.attributes.color, 0.3) }"> ng-style="{ 'background-color': shadeColor(selectedCategory.attributes.color, 0.3) }">
<h3 class="picto-category-grid__title">{{ selectedCategory.expression }}</h3> <h3 class="picto-category-grid__title">{{ selectedCategory.attributes.expression }}</h3>
<div <div
ng-repeat="studentPictoRow in studentPictos[getCategoryId(selectedCategory)]" ng-repeat="studentPictoRow in studentPictos[getCategoryId(selectedCategory)]"
ng-init="rowIndex = $index" ng-init="rowIndex = $index"
...@@ -239,7 +239,7 @@ ...@@ -239,7 +239,7 @@
('empty-' + colIndex + '-' + rowIndex) ('empty-' + colIndex + '-' + rowIndex)
}}" }}"
draggable droppable drop="handleDrop" draggable droppable drop="handleDrop"
popover="{{ studentPicto != emptyStudentPicto ? studentPicto.expression : '' }}" popover="{{ studentPicto != emptyStudentPicto ? studentPicto.attributes.expression : '' }}"
popover-trigger="mouseenter" popover-trigger="mouseenter"
ng-repeat="studentPicto in studentPictoRow track by $index" ng-repeat="studentPicto in studentPictoRow track by $index"
ng-init="colIndex = $index"> ng-init="colIndex = $index">
...@@ -247,13 +247,13 @@ ...@@ -247,13 +247,13 @@
<div <div
class="picto-legend-normal" class="picto-legend-normal"
ng-if="studentPicto.attributes.legend == 'normal'"> ng-if="studentPicto.attributes.legend == 'normal'">
{{ studentPicto.expression }} {{ studentPicto.attributes.expression }}
</div> </div>
<div <div
class="picto-legend-full" class="picto-legend-full"
ng-if="studentPicto.attributes.legend == 'full'"> ng-if="studentPicto.attributes.legend == 'full'">
{{ studentPicto.expression }} {{ studentPicto.attributes.expression }}
</div> </div>
<!-- /.picto-legend --> <!-- /.picto-legend -->
<img <img
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
<!-- Modificable sólo si es el propietario del picto --> <!-- Modificable sólo si es el propietario del picto -->
<div id="add_label" class="input-group"> <div id="add_label" class="input-group">
<span class="input-group-addon glyphicon glyphicon-comment" aria-hidden="true"></span> <span class="input-group-addon glyphicon glyphicon-comment" aria-hidden="true"></span>
<input type="text" class="form-control" ng-model="studentPicto.expression" /> <input type="text" class="form-control" ng-model="studentPicto.attributes.expression" />
</div> </div>
</div> </div>
......
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