Skip to content

Commit

Permalink
Aftral[premieroctet#113]: added homeworks submitted and missing count
Browse files Browse the repository at this point in the history
  • Loading branch information
SeghirOumo committed Aug 27, 2024
1 parent 2b497a2 commit 11be5a8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 17 deletions.
26 changes: 24 additions & 2 deletions backend/web/server/plugins/aftral-lms/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const mongoose=require('mongoose')
const Progress = require("../../models/Progress")
const { BLOCK_STATUS_CURRENT, BLOCK_STATUS_FINISHED, BLOCK_STATUS_TO_COME, BLOCK_STATUS_UNAVAILABLE, ACHIEVEMENT_RULE_CHECK, ROLE_CONCEPTEUR, ROLE_APPRENANT } = require("./consts");
const { getBlockResources } = require("./resources");
const { idEqual, loadFromDb, getModel } = require("../../utils/database");
const { idEqual, loadFromDb, getModel, getModelAttributes } = require("../../utils/database");
const User = require("../../models/User");

const NAMES_CACHE=new NodeCache()
Expand Down Expand Up @@ -293,6 +293,10 @@ getSession = async (userId, params, data, fields) => {
while (!!currentBlock.parent) {
currentBlock = await mongoose.models.block.findById(currentBlock.parent,{parent:1, type:1})
}
const model = await getModel(currentBlock._id)
if(model != `session`) {
return {}
}
const [result] = await loadFromDb({model: 'block', id:currentBlock._id, fields, user:userId})
return result
}
Expand Down Expand Up @@ -390,12 +394,30 @@ const getBlockHomeworks = async (userId, params, data) => {
return progress.homeworks
}

getBlockHomeworksSubmitted = async (userId, params, data) => {
const progress = await mongoose.models.progress.find({
block:data._id
}).populate('homeworks')
const homeworks = progress.filter(p=> p.homeworks.length>0)
return homeworks.length
}

getBlockHomeworksMissing = async (userId, params, data) => {
const session = await mongoose.models.session.findById(data.session)
const progress = await mongoose.models.progress.find({
block:data._id
}).populate('homeworks')
const homeworks = progress.filter(p=> p.homeworks.length>0)
const result = session.trainees.length - homeworks.length
return result
}

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
getAvailableCodes, getBlockHomeworks, getBlockHomeworksSubmitted, getBlockHomeworksMissing
}


4 changes: 3 additions & 1 deletion backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} = require('./block')
const { getBlockStatus, setParentSession, getAttribute, LINKED_ATTRIBUTES, onBlockAction, LINKED_ATTRIBUTES_CONVERSION, getSession, getAvailableCodes, getBlockHomeworks, getBlockHomeworksSubmitted, getBlockHomeworksMissing} = require('./block')
const { getResourcesProgress } = require('./resources')
const { getResourceAnnotation } = require('./resources')
const { setResourceAnnotation } = require('./resources')
Expand Down Expand Up @@ -101,6 +101,8 @@ BLOCK_MODELS.forEach(model => {
declareComputedField({model, field: 'homeworks', requires: '', getterFn: getBlockHomeworks})
declareVirtualField({model, field: 'homework_limit_str', type: 'String', requires: 'homework_limit_date'})
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})
})

declareEnumField({model: 'homework', field: 'scale', enumValues: SCALE})
Expand Down
9 changes: 9 additions & 0 deletions backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ const BlockSchema = new Schema({
type: Date,
required: false,
},
//computed
homeworks_submitted_count: {
type: Number,
required: false
},
homeworks_missing_count: {
type: Number,
required: false,
},
}, {...schemaOptions, ...BLOCK_DISCRIMINATOR})

BlockSchema.virtual('is_template', DUMMY_REF).get(function() {
Expand Down
40 changes: 26 additions & 14 deletions backend/web/tests/aftral-lms/block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require('../../server/models/Certification')
jest.setTimeout(60000)

describe('User', () => {
let trainer, trainee1, trainee2, homework1, homework2, block, progress1, progress2, sequence, modulee, program, session, productCode, conceptor
let trainer, trainee1, trainee2, homework1, homework2, block, progress1, progress2, sequence, modulee, program, session, productCode, conceptor, id
let limit = new Date('06-06-2022')
beforeAll(async () => {
await mongoose.connect(`mongodb://localhost/aftral-test`, MONGOOSE_OPTIONS)
Expand Down Expand Up @@ -76,21 +76,12 @@ describe('User', () => {
resource: block._id,
trainee: trainee2._id,
})
progress1 = await Progress.create({
user:trainee1._id,
block:block._id,
homeworks:[homework1._id]
})
progress2 = await Progress.create({
user:trainee2._id,
block:block._id,
homeworks:[homework2._id]
})
session = await Session.create({
name: `Test Session`,
creator: trainer._id,
start_date: new Date(`10-10-2024`),
end_date: new Date(`10-10-2025`)
end_date: new Date(`10-10-2025`),
trainees:[trainee1._id, trainee2._id]
})
productCode = await ProductCode.create({code:`Test product code`})
program = await Program.create({
Expand All @@ -111,6 +102,20 @@ describe('User', () => {
await addChildAction({parent: program._id, child: modulee._id}, conceptor)
await addChildAction({parent: modulee._id, child: sequence._id}, conceptor)
await addChildAction({parent: sequence._id, child: block._id}, conceptor)

const [ses] = await loadFromDb({model: `session`, user:conceptor, fields:[`children.children.children.children`]})
id = ses.children[0].children[0].children[0].children[0]._id

progress1 = await Progress.create({
user:trainee1._id,
block:id,
homeworks:[homework1._id]
})
progress2 = await Progress.create({
user:trainee2._id,
block:id,
homeworks:[homework2._id]
})
})

afterAll(async () => {
Expand All @@ -119,16 +124,23 @@ describe('User', () => {
})

it('must return block homeworks attributes', async()=> {
const [result] = await loadFromDb({model:`block`,user:trainee1._id, fields:[`can_upload_homework`,`homeworks`]})
const [result] = await loadFromDb({model:`block`,user:trainee1._id, id, fields:[`can_upload_homework`,`homeworks`]})
expect(result.can_upload_homework).not.toBeTruthy()
expect(result.homeworks.length).toEqual(1)
expect(idEqual(result.homeworks[0]._id,homework1._id)).toBeTruthy()
})

it.only('must return homeworks of a session', async() => {
it('must return homeworks of a session', async() => {
const [data] = await loadFromDb({model:`block`, id:session._id, user:trainer, fields:[
`children.children.children.children.success_note_min`
]})
expect(data.children[0].children[0].children[0].children[0].success_note_min).toEqual(0)
})

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`]})
expect(data.homeworks_missing_count).toEqual(0)
expect(data.homeworks_submitted_count).toEqual(2)
})
})

0 comments on commit 11be5a8

Please sign in to comment.