Skip to content

Commit

Permalink
Aftral[premieroctet#75]: stop user from uploading homeworks if not al…
Browse files Browse the repository at this point in the history
…l conditions are met
  • Loading branch information
SeghirOumo committed Sep 5, 2024
1 parent b9e9d1e commit e09da5f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 19 deletions.
13 changes: 10 additions & 3 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const Comment = require('../../models/Comment')
const { parseAsync } = require('@babel/core')
const Progress = require('../../models/Progress')
const { BadRequestError, ForbiddenError } = require('../../utils/errors')
const { getTraineeCurrentResources } = require('./user')
const { getTraineeCurrentResources, getUserCanUploadHomework } = require('./user')
const { isMine } = require('./message')
const { DURATION_UNIT } = require('./consts')
const { isLiked } = require('./post')
Expand Down Expand Up @@ -97,7 +97,7 @@ BLOCK_MODELS.forEach(model => {
declareComputedField({model, field: 'available_codes', requires: 'codes,type', getterFn: getAvailableCodes})
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: 'can_upload_homework', type: 'Boolean', requires: 'homework_limit_date,homework_mode,max_attempts', getterFn: getUserCanUploadHomework})
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})
Expand Down Expand Up @@ -292,6 +292,13 @@ const preCreate = async ({model, params, user}) => {
return {data: conv}
}
}

if (model == `homework` ){
const [block] = await loadFromDb({model: `resource`, id: params.parent, user, fields:[`can_upload_homework`]})
if(!block.can_upload_homework) {
throw new ForbiddenError(`Vous avez atteint la limite de tentatives`)
}
}
return Promise.resolve({model, params})
}

Expand Down Expand Up @@ -482,7 +489,7 @@ const preprocessGet = async ({model, fields, id, user, params}) => {
if (block._locked && user.role==ROLE_APPRENANT) {
await Progress.findOneAndUpdate(
{block, user},
{block, user, consult: true, consult_partial: true, join_partial: true, download: true},
{block, user, consult: true, consult_partial: true, join_partial: true, download: true, $inc: { attempts_count: 1 }},
{upsert: true},
)
await onBlockAction(user, block)
Expand Down
20 changes: 5 additions & 15 deletions backend/web/server/plugins/aftral-lms/schemas/BlockSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ const BlockSchema = new Schema({
}],
required: true,
default: []
},
can_upload_homework: {
type: Boolean,
required: true,
default: false,
}
}, {...schemaOptions, ...BLOCK_DISCRIMINATOR})

Expand Down Expand Up @@ -324,21 +329,6 @@ BlockSchema.virtual('homework_limit_str', DUMMY_REF).get(function() {
: ``
})

BlockSchema.virtual('can_upload_homework', DUMMY_REF).get(function() {
if (!this.homework_mode) {
return true
}
if (!this.homework_limit_date) {
return true
}
const limitDate = moment(this.homework_limit_date)
return limitDate.isAfter(moment())
})

BlockSchema.methods.getDependants = function () {
return mongoose.models.block.find({origin:this._id})
}

// Validate Succes achievemnt
BlockSchema.pre('validate', async function(next) {
// #36 Can't create two templates with same type and same name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ const ProgressSchema = new Schema({
note: {
type: Number,
},
attempts_count: {
type: Number,
required: true,
default: 0,
}
}, {...schemaOptions, ...BLOCK_DISCRIMINATOR})

module.exports = ProgressSchema
21 changes: 20 additions & 1 deletion backend/web/server/plugins/aftral-lms/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Progress = require('../../models/Progress')
const Resource = require('../../models/Resource')
const { loadFromDb } = require('../../utils/database')
const { BLOCK_STATUS_CURRENT } = require('./consts')
const moment = require('moment')

const getTraineeCurrentResources = async (userId, params, data, fields) => {
// Find curent blocks for this user
Expand All @@ -18,6 +19,24 @@ const getUserPermissions = async (userId, params, data) => {
return data.permission_groups.flatMap(group => group.permissions)
}

const getUserCanUploadHomework = async (userId, params, data) => {
if (data.homework_mode == false) {
return false
}
if (data.max_attempts) {
const progress = await Progress.findOne({ block: data._id, user: userId })
if (progress && progress.attempts_count >= data.max_attempts) {
return false
}
}
if (data.homework_limit_date) {
if (moment().isAfter(moment(data.homework_limit_date))) {
return false
}
}
return true
}

module.exports = {
getTraineeCurrentResources,getTraineeCurrentResources, getUserPermissions
getTraineeCurrentResources,getTraineeCurrentResources, getUserPermissions, getUserCanUploadHomework
}
37 changes: 37 additions & 0 deletions backend/web/tests/aftral-lms/block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,41 @@ describe('User', () => {
prog = await Progress.findOne({user:trainee2._id})
expect(prog.achievement_status == BLOCK_STATUS_FINISHED).toBeTruthy()
})

it.only('must return can_upload_homework', async()=> {
let res = await Block.create({
name: `Res458`,
type: `resource`,
resource_type:RESOURCE_TYPE_PDF,
url: `test`,
achievement_rule:ACHIEVEMENT_RULE_CONSULT,
creator: trainer._id,
homework_mode: false,
success_note_min:0,
success_note_max: 20,
})

let progress = await Progress.create({
user:trainee1._id,
block:res._id,
homeworks:[homework1._id],
achievement_status: BLOCK_STATUS_FINISHED,
note: 10,
attempts_count: 10,
})
let result = await loadFromDb({model:`block`,user:trainee1._id, id:res._id, fields:[`can_upload_homework`]})
expect(result[0].can_upload_homework).not.toBeTruthy()

await Block.findByIdAndUpdate(res._id, {homework_mode: true})
result = await loadFromDb({model:`block`,user:trainee1._id, id:res._id, fields:[`can_upload_homework`]})
expect(result[0].can_upload_homework).toBeTruthy()

await Block.findByIdAndUpdate(res._id, {max_attempts: 10})
result = await loadFromDb({model:`block`,user:trainee1._id, id:res._id, fields:[`can_upload_homework`]})
expect(result[0].can_upload_homework).not.toBeTruthy()

await Block.findByIdAndUpdate(res._id, {max_attempts: 20, homework_limit_date: new Date(`02-02-2022`)})
result = await loadFromDb({model:`block`,user:trainee1._id, id:res._id, fields:[`can_upload_homework`]})
expect(result[0].can_upload_homework).not.toBeTruthy()
})
})

0 comments on commit e09da5f

Please sign in to comment.