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

parent 33d65db5
......@@ -786,12 +786,6 @@ module.exports = {
* source: 1 @TODO Other sources
* owner: supervisorId or null
* },
* expression: {
* id: expressionId,
* lang: 'es-es',
* text: 'Picto Expression',
* picto: pictoId
* },
* attributes: { @see StuPicto.getValidAttributes() }
* },
* ...
......@@ -852,7 +846,6 @@ module.exports = {
* student: studentId (speficied as url parameter)
* picto: { @see Picto model}
* attributes: { @see StuPicto.getValidAttributes() }
* expression: { @see Picto model }
* }
*/
add_picto: function (req, res) {
......@@ -892,12 +885,9 @@ module.exports = {
if (!stuPicto)
throw new Error("stu_picto not created");
sails.log.debug("->>" + JSON.stringify(picto));
return res.ok({
id: stuPicto.id,
student: params.id_stu,
attributes: stuPicto.attributes,
picto: picto,
expression: picto.expressions[0]
Student.pictoInfo(stuPicto.id, function(err, resp) {
if (err) throw err;
return res.ok(resp);
});
})
.catch(err => res.serverError("Error adding picto: " + err));
......@@ -944,14 +934,11 @@ module.exports = {
if (!updated)
throw new Error ("error on update");
// return res.json(updated[0]);
return res.ok({
id: updated[0].id, // id of stu_picto
attributes: updated[0].attributes, // picto attributes for student
picto: {
id: updated[0].picto // picto information
}
Student.pictoInfo(updated[0].id, function(err, resp) {
if (err) throw err;
return res.ok(resp);
});
})
.catch(err => {
return res.serverError('Unable to update picto for student: ' + err);
......@@ -1007,7 +994,10 @@ module.exports = {
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});
});
......@@ -1148,8 +1138,6 @@ module.exports = {
var roomName = 'studentRoom' + attributes.id_stu;
sails.log.debug("Inside vocabulary");
if (req.isSocket) {
sails.log.debug("Inside vocabulary - isSocket");
......
......@@ -130,6 +130,22 @@ module.exports = function eventsHook(sails) {
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 = {
color: null,
expression: null,
legend: 'none',
delete_strip_after_delivery: true
};
if (typeof attributes === 'object') {
......@@ -129,10 +128,7 @@ module.exports = {
}
if (!((/^(none|normal|full)$/).test(validAttributes.legend))) {
delete validAttributes.legend;
}
if (typeof validAttributes.delete_strip_after_delivery !== 'boolean') {
delete validAttributes.delete_strip_after_delivery;
}
}
if (typeof validAttributes.highlight !== 'boolean') {
delete validAttributes.highlight;
}
......
......@@ -378,10 +378,10 @@ module.exports = {
picto.imageFileExists(function(found) {
if (found) {
// 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 = {
"id": stuPicto.id,
"picto": stuPicto.picto,
"expression": stuPicto.attributes.expression ? stuPicto.attributes.expression : picto.expressions[0].text,
"attributes": stuPicto.attributes,
"tags": picto.tags ? picto.tags : []
};
......@@ -542,5 +542,30 @@ module.exports = {
else
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
owner: null,
tags: null
},
expression: {
id: null,
lang: null,
text: '',
picto: null
},
attributes: {
id_cat: null,
coord_x: null,
......@@ -121,7 +115,6 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
$scope.getCategoryId(picto);
$scope.studentPictos[category] = $scope.studentPictos[category] || generateGrid();
$scope.studentPictos[category][positionX][positionY] = picto;
console.log("picto placed at " + positionX + " " + positionY);
}
};
......@@ -337,7 +330,7 @@ dashboardControllers.controller('StudentCollectionsCtrl', function StudentCollec
// notify
io.socket.post('/stu/vocabulary', {
action: 'update',
action: 'update_category',
attributes: {
id_stu: $scope.studentData.id,
stu_picto: studentPicto
......@@ -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
......
......@@ -13,76 +13,78 @@ dashboardControllers.controller('PictoConfigCtrl', function ($window, $scope, $t
*/
$scope.save = function () {
if ($scope.studentPicto.expression != studentPicto.expression) {
// Update picto expressio because user owns it
if (studentPicto.picto.owner == sup.id) {
new Promise(function (resolve, reject) {
if ($scope.studentPicto.attributes.expression != studentPicto.attributes.expression) {
// 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
.post(config.backend+'/picto/exp',
{ 'picto': studentPicto.picto.id,
'lang': sup.lang,
'text': $scope.studentPicto.expression
.put(config.backend+'/stu/'+ stu.id + '/picto/' + $scope.studentPicto.id,
{
'attributes': $scope.studentPicto.attributes
})
.success(function(data, status, headers, config) {
io.socket.post('/stu/vocabulary', {
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) {});
resolve(data);
})
.error(function(data, status, headers, config) {
console.log("Error from API: " + data.error);
reject(data);
});
} else {
$scope.studentPicto.attributes.expression = $scope.studentPicto.expression;
}
}
// Update attributes
$http
.put(config.backend+'/stu/'+ stu.id + '/picto/' + $scope.studentPicto.id,
{
'attributes': $scope.studentPicto.attributes
})
.success(function(data, status, headers, config) {
});
})
.then(function(result) {
return new Promise(function (resolve, reject) {
if ($scope.update_all_legend) {
$http
.put(config.backend+'/stu/'+ stu.id + '/legend/' + $scope.studentPicto.attributes.legend)
.success(function(data, status, headers, config) {
resolve(result);
})
.error(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', {
action: 'update',
attributes: {
id_stu: stu.id,
stu_picto: data
stu_picto: result
}
},
function(res) {});
})
.error(function(data, status, headers, config) {
console.log("Error from API: " + data.error);
});
// Update all lengends format
if ($scope.update_all_legend) {
$http
.put(config.backend+'/stu/'+ stu.id + '/legend/' + $scope.studentPicto.attributes.legend)
.success(function(data, status, headers, config) {
})
.error(function(data, status, headers, config) {
console.log("Error from API: " + data.error);
});
}
studentPicto.attributes = $scope.studentPicto.attributes;
$modalInstance.close($scope.studentPicto.attributes.expression);
if ($scope.update_all_legend) {
ngToast.success({ content: $translate.instant('reloading_pictos') });
$window.location.reload();
}
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 () {
......
......@@ -33,7 +33,7 @@
('empty-' + colIndex + '-' + rowIndex)
}}"
draggable droppable drop="handleDrop"
popover="{{studentPicto != emptyStudentPicto ? studentPicto.expression : ''}}"
popover="{{studentPicto != emptyStudentPicto ? studentPicto.attributes.expression : ''}}"
popover-trigger="mouseenter"
ng-init="colIndex = $index"
ng-repeat="studentPicto in studentPictoRow track by $index">
......@@ -41,12 +41,12 @@
<div
class="picto-legend-normal"
ng-if="studentPicto.attributes.legend == 'normal'">
{{ studentPicto.expression }}
{{ studentPicto.attributes.expression }}
</div>
<div
class="picto-legend-full"
ng-if="studentPicto.attributes.legend == 'full'">
{{ studentPicto.expression }}
{{ studentPicto.attributes.expression }}
</div>
<!-- /.picto-legend -->
<img
......@@ -120,19 +120,19 @@
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-init="colIndex = $index"
popover="{{ studentPicto != emptyStudentPicto ? studentPicto.expression : ''}}"
popover="{{ studentPicto != emptyStudentPicto ? studentPicto.attributes.expression : ''}}"
popover-trigger="mouseenter">
<!-- picto-legend -->
<div
class="picto-legend-normal"
ng-if="studentPicto.attributes.legend == 'normal'">
{{ studentPicto.expression }}
{{ studentPicto.attributes.expression }}
</div>
<div
class="picto-legend-full"
ng-if="studentPicto.attributes.legend == 'full'">
{{ studentPicto.expression }}
{{ studentPicto.attributes.expression }}
</div>
<!-- /.picto-legend -->
<img
......@@ -225,7 +225,7 @@
class="picto-grid picto-category-grid"
ng-if="selectedCategory !== emptyStudentPicto && !showFreeCategory"
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
ng-repeat="studentPictoRow in studentPictos[getCategoryId(selectedCategory)]"
ng-init="rowIndex = $index"
......@@ -239,7 +239,7 @@
('empty-' + colIndex + '-' + rowIndex)
}}"
draggable droppable drop="handleDrop"
popover="{{ studentPicto != emptyStudentPicto ? studentPicto.expression : '' }}"
popover="{{ studentPicto != emptyStudentPicto ? studentPicto.attributes.expression : '' }}"
popover-trigger="mouseenter"
ng-repeat="studentPicto in studentPictoRow track by $index"
ng-init="colIndex = $index">
......@@ -247,13 +247,13 @@
<div
class="picto-legend-normal"
ng-if="studentPicto.attributes.legend == 'normal'">
{{ studentPicto.expression }}
{{ studentPicto.attributes.expression }}
</div>
<div
class="picto-legend-full"
ng-if="studentPicto.attributes.legend == 'full'">
{{ studentPicto.expression }}
{{ studentPicto.attributes.expression }}
</div>
<!-- /.picto-legend -->
<img
......
......@@ -11,7 +11,7 @@
<!-- Modificable sólo si es el propietario del picto -->
<div id="add_label" class="input-group">
<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>
......
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