diff --git a/backend/web/server/plugins/aftral-lms/block.js b/backend/web/server/plugins/aftral-lms/block.js index 470489c7b4..9f8a928d9c 100644 --- a/backend/web/server/plugins/aftral-lms/block.js +++ b/backend/web/server/plugins/aftral-lms/block.js @@ -308,23 +308,28 @@ getSession = async (userId, params, data) => { const getBlockLiked = async (userId, params, data) => { const user = await User.findById(userId, {role:1}) + const template = await getTemplate(data._id) + console.log(template,'****************') if(user.role == ROLE_CONCEPTEUR) { - return data.likes.length > 0 + return template.likes.length > 0 } - return data.likes.some(like => idEqual(like._id, userId)) + return template.likes.some(like => idEqual(like, userId)) } const getBlockDisliked = async (userId, params, data) => { const user = await User.findById(userId, {role:1}) + const template = await getTemplate(data._id) + console.log(template,'----------------') if(user.role == ROLE_CONCEPTEUR) { - return data.dislikes.length > 0 + return template.dislikes.length > 0 } - return data.dislikes.some(dislike => idEqual(dislike._id, userId)) + return template.dislikes.some(dislike => idEqual(dislike, userId)) } -const setBlockLiked = ({ id, attribute, value, user }) => { +const setBlockLiked = async ({ id, attribute, value, user }) => { + const templateId = await getTemplate(id) if(value) { - return mongoose.models['resource'].findByIdAndUpdate(id, + return mongoose.models['block'].findByIdAndUpdate(templateId, { $pull: { dislikes: user._id @@ -336,14 +341,15 @@ const setBlockLiked = ({ id, attribute, value, user }) => { ) } else{ - return mongoose.models['resource'].findByIdAndUpdate(id, + return mongoose.models['block'].findByIdAndUpdate(templateId, {$pull: {likes: user._id}}) } } -const setBlockDisliked = ({ id, attribute, value, user }) => { +const setBlockDisliked = async ({ id, attribute, value, user }) => { + const templateId = await getTemplate(id) if(value) { - return mongoose.models['block'].findByIdAndUpdate(id, + return mongoose.models['block'].findByIdAndUpdate(templateId, { $pull: { likes: user._id @@ -355,11 +361,21 @@ const setBlockDisliked = ({ id, attribute, value, user }) => { ) } else{ - return mongoose.models['block'].findByIdAndUpdate(id, + return mongoose.models['block'].findByIdAndUpdate(templateId, {$pull: {dislikes: user._id}}) } } +const getTemplate = async(id) => { + let currentBlock = await mongoose.models.block.find({_id:id},{origin:1, likes:1, dislikes:1}) + let currentOrigin = currentBlock.origin + while(currentOrigin) { + currentBlock = await mongoose.models.block.find({_id:currentOrigin},{origin:1, likes:1, dislikes:1}) + currentOrigin = currentBlock.origin + } + return currentBlock +} + module.exports={ getBlockStatus, getBlockName, getSessionBlocks, setParentSession, cloneTree, getAttribute, LINKED_ATTRIBUTES, onBlockFinished, onBlockCurrent, onBlockAction, diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index a03e72c3dd..c20dfe8642 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -81,8 +81,8 @@ BLOCK_MODELS.forEach(model => { declareVirtualField({model, field: 'likes_count', instance: 'Number', requires:'likes'}) declareVirtualField({model, field: 'dislikes_count', instance: 'Number', requires:'dislikes'}) - declareComputedField({model, field: 'liked', getterFn: getBlockLiked, setterFn: setBlockLiked, requires:'likes'}) - declareComputedField({model, field: 'disliked', getterFn: getBlockDisliked, setterFn: setBlockDisliked, requires:'dislikes'}) + declareComputedField({model, field: 'liked', getterFn: getBlockLiked, setterFn: setBlockLiked, requires:'likes,origin'}) + declareComputedField({model, field: 'disliked', getterFn: getBlockDisliked, setterFn: setBlockDisliked, requires:'dislikes,origin'}) }) declareEnumField({model: 'homework', field: 'scale', enumValues: SCALE})