From 08b7eed6b00ee5566ee8b976a58739d046c274c5 Mon Sep 17 00:00:00 2001 From: SeghirOumo Date: Wed, 28 Aug 2024 10:05:34 +0200 Subject: [PATCH] Aftral[#113]: added trainees_count to block --- backend/web/server/plugins/aftral-lms/block.js | 11 ++++++++--- backend/web/server/plugins/aftral-lms/functions.js | 3 ++- .../server/plugins/aftral-lms/schemas/BlockSchema.js | 4 ++++ backend/web/tests/aftral-lms/block.test.js | 3 ++- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/backend/web/server/plugins/aftral-lms/block.js b/backend/web/server/plugins/aftral-lms/block.js index b22afc8ff6..04a33e6fb9 100644 --- a/backend/web/server/plugins/aftral-lms/block.js +++ b/backend/web/server/plugins/aftral-lms/block.js @@ -392,7 +392,7 @@ const getBlockHomeworks = async (userId, params, data) => { return progress.homeworks } -getBlockHomeworksSubmitted = async (userId, params, data) => { +const getBlockHomeworksSubmitted = async (userId, params, data) => { const progress = await mongoose.models.progress.find({ block:data._id }).populate('homeworks') @@ -400,7 +400,7 @@ getBlockHomeworksSubmitted = async (userId, params, data) => { return homeworks.length } -getBlockHomeworksMissing = async (userId, params, data) => { +const getBlockHomeworksMissing = async (userId, params, data) => { const session = await mongoose.models.session.findById(data.session) const progress = await mongoose.models.progress.find({ block:data._id @@ -410,12 +410,17 @@ getBlockHomeworksMissing = async (userId, params, data) => { return result } +const getBlockTraineesCount = async (userId, params, data) => { + const session = await mongoose.models.session.findById(data.session) + return session.trainees ? session.trainees.length : 0 +} + module.exports={ getBlockStatus, getBlockName, getSessionBlocks, setParentSession, cloneTree, getAttribute, LINKED_ATTRIBUTES, onBlockFinished, onBlockCurrent, onBlockAction, getNextResource, getPreviousResource, getParentBlocks,LINKED_ATTRIBUTES_CONVERSION, ChainCache, ATTRIBUTES_CACHE,getSession, getBlockLiked, getBlockDisliked, setBlockLiked, setBlockDisliked, - getAvailableCodes, getBlockHomeworks, getBlockHomeworksSubmitted, getBlockHomeworksMissing + getAvailableCodes, getBlockHomeworks, getBlockHomeworksSubmitted, getBlockHomeworksMissing, getBlockTraineesCount } diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index 3f02d254ea..78fd73dbd9 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -19,7 +19,7 @@ require('../../models/Chapter') //Added chapter, it was removed somehow const { computeStatistics } = require('./statistics') const { searchUsers, searchBlocks } = require('./search') const { getUserHomeworks, getResourceType, getAchievementRules, getBlockSpentTime, getBlockSpentTimeStr, getResourcesCount, getFinishedResourcesCount, getRessourceSession } = require('./resources') -const { getBlockStatus, setParentSession, getAttribute, LINKED_ATTRIBUTES, onBlockAction, LINKED_ATTRIBUTES_CONVERSION, getSession, getAvailableCodes, getBlockHomeworks, getBlockHomeworksSubmitted, getBlockHomeworksMissing} = require('./block') +const { getBlockStatus, setParentSession, getAttribute, LINKED_ATTRIBUTES, onBlockAction, LINKED_ATTRIBUTES_CONVERSION, getSession, getAvailableCodes, getBlockHomeworks, getBlockHomeworksSubmitted, getBlockHomeworksMissing, getBlockTraineesCount} = require('./block') const { getResourcesProgress } = require('./resources') const { getResourceAnnotation } = require('./resources') const { setResourceAnnotation } = require('./resources') @@ -103,6 +103,7 @@ BLOCK_MODELS.forEach(model => { declareVirtualField({model, field: 'can_upload_homework', type: 'Boolean', requires: 'homework_limit_date,homework_mode'}) declareComputedField({model, field: 'homeworks_submitted_count', type: 'Number', requires: 'session', getterFn: getBlockHomeworksSubmitted}) declareComputedField({model, field: 'homeworks_missing_count', type: 'Number', requires: 'session', getterFn: getBlockHomeworksMissing}) + declareComputedField({model, field: 'trainees_count', type: 'Number', requires: 'session', getterFn: getBlockTraineesCount}) }) declareEnumField({model: 'homework', field: 'scale', enumValues: SCALE}) diff --git a/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js b/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js index 833f0622b4..05a3828335 100644 --- a/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js +++ b/backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js @@ -230,6 +230,10 @@ const BlockSchema = new Schema({ type: Number, required: false, }, + trainees_count: { + type: Number, + required: false, + } }, {...schemaOptions, ...BLOCK_DISCRIMINATOR}) BlockSchema.virtual('is_template', DUMMY_REF).get(function() { diff --git a/backend/web/tests/aftral-lms/block.test.js b/backend/web/tests/aftral-lms/block.test.js index 5890591c27..97423716f9 100644 --- a/backend/web/tests/aftral-lms/block.test.js +++ b/backend/web/tests/aftral-lms/block.test.js @@ -139,8 +139,9 @@ describe('User', () => { it('must return homeworks submitted count', async() => { - const [data] = await loadFromDb({model:`block`, user:conceptor, id, fields:[`session`,`homeworks_missing_count`,`homeworks`,`homeworks_submitted_count`]}) + const [data] = await loadFromDb({model:`block`, user:conceptor, id, fields:[`session`,`homeworks_missing_count`,`homeworks`,`homeworks_submitted_count`,`trainees_count`]}) expect(data.homeworks_missing_count).toEqual(0) expect(data.homeworks_submitted_count).toEqual(2) + expect(data.trainees_count).toEqual(2) }) }) \ No newline at end of file