issue #511 resolved

parent 01353467
Showing with 52 additions and 20 deletions
...@@ -328,24 +328,50 @@ module.exports = { ...@@ -328,24 +328,50 @@ module.exports = {
// Class method for getting the list of pictos associated to a given // Class method for getting the list of pictos associated to a given
// student // student
pictos: function(id_stu, callback) { pictos: function(id_stu, callback) {
Student.findOne(id_stu).exec(function(err, student) {
if (student){
StuPicto.find({student: id_stu}).populate('picto').exec(function(err, stuPictos) {
var l = []; var l = [];
if (err || !stuPictos || stuPictos.length == 0) Student.findOne(id_stu)
return callback(err, l); .then((student) => {
if (!student)
throw new Error("No student found");
var stuPictos = StuPicto.find({student: id_stu})
.populate('picto')
.then((stuPictos) => {
if (!stuPictos || stuPictos.length == 0)
return [];
return stuPictos;
})
.catch((err) => {
throw err;
});
async.eachSeries(stuPictos, function(stuPicto, cb) { return [student, stuPictos];
})
.spread((student, stuPictos) => {
async.eachSeries(stuPictos, function(stuPicto, next_cb) {
// Populate expressions to get it with the picto // Populate expressions to get it with the picto
Picto.findOne(stuPicto.picto.id) Picto.findOne(stuPicto.picto.id)
.populate('expressions', {lang: student.lang}) .populate('expressions', {lang: student.lang})
.exec(function(err, picto) { .then((picto) => {
if (err) throw err;
// check picto has expressions associated in student language
if (picto.expressions.length == 0 || picto.expressions[0].text.length == 0) {
sails.log.debug("No expression for picto " + picto.id);
return;
}
// check picto image is available
var fs = require('fs');
var img_path = sails.config.pictogram.paths.public + stuPicto.picto.uri;
fs.access(img_path, fs.F_OK, function(err) {
if (err) {
sails.log.debug("No image file for " + img_path);
return;
}
// Now we have everythin, so we add the picto to the list
var stuPictoToAdd = { var stuPictoToAdd = {
"id": stuPicto.id, "id": stuPicto.id,
"picto": stuPicto.picto, "picto": stuPicto.picto,
...@@ -354,17 +380,23 @@ module.exports = { ...@@ -354,17 +380,23 @@ module.exports = {
}; };
l.push(stuPictoToAdd); l.push(stuPictoToAdd);
cb(); // next in the series
});
},
function(err) {
callback(err, l); // out loop finished, return result
});
});
}
}); });
})
.catch((err) => {
sails.log.debug("Error found obtaining pictos: " + err);
})
.then(() => next_cb()); // end Picto.findOne
},
function (err) { // loop has end
if (err)
sails.log.debug("Error found obtaining pictos: " + err);
callback(err, l);
}); // end async.eachSeries
})
.catch((err) => {
sails.log.debug("Error found obtaining pictos: " + err);
callback(err, l);
}); // end Student.findOne
}, },
// //
......
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