Skip to content

Commit

Permalink
Aftral: [premieroctet#158] Homeworks
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaauvray committed Sep 24, 2024
1 parent 4885932 commit 3f633a0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
15 changes: 9 additions & 6 deletions backend/web/server/plugins/aftral-lms/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const { getBlockResources } = require("./resources");
const { idEqual, loadFromDb, getModel } = require("../../utils/database");
const User = require("../../models/User");
const SessionConversation = require("../../models/SessionConversation");
const Homework = require("../../models/Homework");
const { createRegExpOR } = require("../../../utils/text");
const Resource = require("../../models/Resource");

const LINKED_ATTRIBUTES_CONVERSION={
name: lodash.identity,
Expand Down Expand Up @@ -334,11 +337,12 @@ const getAvailableCodes = async (userId, params, data) => {
return availableCodes
}

const getBlockHomeworks = async (userId, params, data) => {
const isTrainee=await User.exists({_id: userId, role: ROLE_APPRENANT})
const filter=isTrainee ? {block: data._id} : {block: data._id, user: userId}
const progress=await Progress.findOne(filter).populate('homeworks')
return progress?.homeworks || []
const getBlockHomeworks = async (userId, params, data, displayFields, actualLogged) => {
const isTrainee=await User.exists({_id: actualLogged, role: ROLE_APPRENANT})
const filter=isTrainee ? {resource: data._id, trainee: userId} : {resource: data._id}
const homeworks=await Homework.find(filter)
.populate(['trainee', 'resource'])
return homeworks
}

const getBlockHomeworksSubmitted = async (userId, params, data) => {
Expand Down Expand Up @@ -467,4 +471,3 @@ module.exports={
updateChildrenOrder, cloneTemplate,
}


7 changes: 4 additions & 3 deletions backend/web/server/plugins/aftral-lms/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { generateDocument } = require('../../../utils/fillForm')
const path = require('path')
const { loadFromDb } = require('../../utils/database')
const Resource = require('../../models/Resource')
const Homework = require('../../models/Homework')
const ROOT = path.join(__dirname, `../../../static/assets/aftral_templates`)
const TEMPLATE_NAME = 'template1'

Expand Down Expand Up @@ -74,9 +75,9 @@ const getEvalResources = async (userId, params, data, fields) => {
params: newParams
})

resources = resources.filter(r =>
r.homework_mode == true || (r.note !== undefined && r.note !== null) || (r.scale !== undefined && r.scale !== null)
)
// resources = resources.filter(r =>
// r.homework_mode == true || (r.note !== undefined && r.note !== null) || (r.scale !== undefined && r.scale !== null)
// )

return resources.map(r => new Resource(r))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const mongoose = require('mongoose')
const {schemaOptions} = require('../../../utils/schemas')
const Schema = mongoose.Schema
const {BLOCK_DISCRIMINATOR}=require('../consts')
const { DUMMY_REF } = require('../../../utils/database')
const { BadRequestError } = require('../../../utils/errors')

const ResourceSchema = new Schema({
Expand Down
13 changes: 10 additions & 3 deletions backend/web/server/utils/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ const retainRequiredFields = ({data, fields}) => {
const thisLevelFields = getFirstLevelFields(fields)
const pickedData = lodash.pick(data, thisLevelFields)
const nextLevelFields = getNextLevelFields(fields)
// HACK For AFTRAL to ensure homeworks are properly returned
.filter(f => f!='homeworks')
nextLevelFields.forEach(f => {
pickedData[f] = retainRequiredFields({
data: data[f],
Expand Down Expand Up @@ -686,7 +688,11 @@ const addComputedFields = async (
queryParams,
data,
model,
actualLogged
) => {
if (!actualLogged) {
actualLogged=userId
}
let fields=getFieldsToCompute({model, fields: originalFields})
if (lodash.isEmpty(fields)) {
return data
Expand All @@ -709,6 +715,7 @@ const addComputedFields = async (
queryParams,
child,
attParams.type,
actualLogged,
),
),
)
Expand All @@ -721,7 +728,7 @@ const addComputedFields = async (
return runPromisesWithDelay(
Object.keys(requiredCompFields).map(f => () => {
const displayFields=getRequiredSubFields(originalFields, f)
return requiredCompFields[f](newUserId, queryParams, data, displayFields)
return requiredCompFields[f](newUserId, queryParams, data, displayFields, actualLogged)
.then(res => {
data[f] = res
return data
Expand Down Expand Up @@ -1019,7 +1026,7 @@ const lean = ({model, data}) => {
}

const display = data => {
console.trace("Data", JSON.stringify(data))
console.trace("Data", JSON.stringify(data, null,2))
return data
}

Expand All @@ -1030,7 +1037,7 @@ const ensureUniqueDataFound = (id, data) => {
return data
}

const loadFromDb = ({model, fields, id, user, params={}}) => {
const loadFromDb = ({model, fields, id, user, params={}, retain=true}) => {
// Add filter fields to return them to client
const filters=extractFilters(params)
fields=lodash.uniq([...fields, ...Object.keys(filters)])
Expand Down

0 comments on commit 3f633a0

Please sign in to comment.