diff --git a/backend/web/server/plugins/aftral-lms/actions.js b/backend/web/server/plugins/aftral-lms/actions.js index f1a6c16a57..2fdd03ecfa 100644 --- a/backend/web/server/plugins/aftral-lms/actions.js +++ b/backend/web/server/plugins/aftral-lms/actions.js @@ -2,7 +2,7 @@ const mongoose=require('mongoose') const Block = require('../../models/Block') const { ForbiddenError, NotFoundError, BadRequestError } = require('../../utils/errors') const {addAction, setAllowActionFn}=require('../../utils/studio/actions') -const { BLOCK_TYPE, ROLE_CONCEPTEUR, ROLE_FORMATEUR, ROLES, BLOCK_STATUS_FINISHED, BLOCK_STATUS_CURRENT, BLOCK_STATUS_TO_COME, BLOCK_STATUS_UNAVAILABLE, ROLE_ADMINISTRATEUR } = require('./consts') +const { BLOCK_TYPE, ROLE_CONCEPTEUR, ROLE_FORMATEUR, ROLES, BLOCK_STATUS_FINISHED, BLOCK_STATUS_CURRENT, BLOCK_STATUS_TO_COME, BLOCK_STATUS_UNAVAILABLE, ROLE_ADMINISTRATEUR, RESOURCE_TYPE_SCORM } = require('./consts') const { cloneTree, onBlockFinished, getNextResource, getPreviousResource, getParentBlocks, getSession } = require('./block') const { lockSession } = require('./functions') const Progress = require('../../models/Progress') @@ -145,6 +145,13 @@ const isActionAllowed = async ({ action, dataId, user }) => { } const actionFn={'play': canPlay, 'resume': canResume, 'replay': canReplay}[action] if (actionFn) { + // const block = await Block.findById(dataId, {resource_type: 1, max_attempts: 1}) + //if(block.max_attempts && block.resource_type == RESOURCE_TYPE_SCORM) { + // const progress = await Progress.findOne({block: dataId, user}, {attempts_count: 1}) + // if (progress.attempts_count >= block.max_attempts) { + // throw new ForbiddenError(`Vous avez atteint le nombre limite de tentatives`) + // } + //} return actionFn({action, dataId, user}) } if (action=='next') { diff --git a/backend/web/server/plugins/aftral-lms/functions.js b/backend/web/server/plugins/aftral-lms/functions.js index c6c116b6d6..3e1eaffe66 100644 --- a/backend/web/server/plugins/aftral-lms/functions.js +++ b/backend/web/server/plugins/aftral-lms/functions.js @@ -4,7 +4,7 @@ const { declareVirtualField, setPreCreateData, setPreprocessGet, setMaxPopulateDepth, setFilterDataUser, declareComputedField, declareEnumField, idEqual, getModel, declareFieldDependencies, setPostPutData, setPreDeleteData, setPrePutData, loadFromDb, setPostCreateData, } = require('../../utils/database') -const { RESOURCE_TYPE, PROGRAM_STATUS, ROLES, MAX_POPULATE_DEPTH, BLOCK_STATUS, ROLE_CONCEPTEUR, ROLE_FORMATEUR,ROLE_APPRENANT, FEED_TYPE_GENERAL, FEED_TYPE_SESSION, FEED_TYPE_GROUP, FEED_TYPE, ACHIEVEMENT_RULE, SCALE, RESOURCE_TYPE_LINK, DEFAULT_ACHIEVEMENT_RULE, BLOCK_STATUS_TO_COME, BLOCK_STATUS_CURRENT, TICKET_STATUS, TICKET_TAG, PERMISSIONS, ROLE_HELPDESK } = require('./consts') +const { RESOURCE_TYPE, PROGRAM_STATUS, ROLES, MAX_POPULATE_DEPTH, BLOCK_STATUS, ROLE_CONCEPTEUR, ROLE_FORMATEUR,ROLE_APPRENANT, FEED_TYPE_GENERAL, FEED_TYPE_SESSION, FEED_TYPE_GROUP, FEED_TYPE, ACHIEVEMENT_RULE, SCALE, RESOURCE_TYPE_LINK, DEFAULT_ACHIEVEMENT_RULE, BLOCK_STATUS_TO_COME, BLOCK_STATUS_CURRENT, TICKET_STATUS, TICKET_TAG, PERMISSIONS, ROLE_HELPDESK, RESOURCE_TYPE_SCORM } = require('./consts') const mongoose = require('mongoose') require('../../models/Resource') const Session = require('../../models/Session') @@ -230,6 +230,9 @@ const preCreate = async ({model, params, user}) => { params.trainee=user } if (model=='resource') { + if (params.max_attempts && params.resource_type != RESOURCE_TYPE_SCORM) { + throw new Error(`Vous ne pouvez pas mettre de nombre de tentatives maximum sur une ressource autre qu'un SCORM`) + } if (!params.url && !params.plain_url) { throw new Error(`Vous devez télécharger un fichier ou saisir une URL`) } @@ -296,7 +299,7 @@ const preCreate = async ({model, params, user}) => { 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`) + throw new ForbiddenError(`Vous ne pouvez plus importer de devoir`) } } return Promise.resolve({model, params}) @@ -489,7 +492,10 @@ 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, $inc: { attempts_count: 1 }}, + {block, user, consult: true, consult_partial: true, join_partial: true, download: true, + //if scorm, increment attempts count + ...block.resource_type == RESOURCE_TYPE_SCORM ? {$inc: { attempts_count: 1 }} : {} + }, {upsert: true}, ) await onBlockAction(user, block) diff --git a/backend/web/tests/aftral-lms/block.test.js b/backend/web/tests/aftral-lms/block.test.js index e40b632037..6ef02d1e59 100644 --- a/backend/web/tests/aftral-lms/block.test.js +++ b/backend/web/tests/aftral-lms/block.test.js @@ -347,7 +347,6 @@ describe('User', () => { 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() @@ -356,11 +355,7 @@ describe('User', () => { 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`)}) + await Block.findByIdAndUpdate(res._id, {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() })