734 lines
18 KiB
JavaScript
734 lines
18 KiB
JavaScript
|
|
const db = require('../models');
|
|
const FileDBApi = require('./file');
|
|
const crypto = require('crypto');
|
|
const Utils = require('../utils');
|
|
|
|
|
|
|
|
const Sequelize = db.Sequelize;
|
|
const Op = Sequelize.Op;
|
|
|
|
module.exports = class Attempt_answersDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const attempt_answers = await db.attempt_answers.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
answer_text: data.answer_text
|
|
||
|
|
null
|
|
,
|
|
|
|
answer_true_false: data.answer_true_false
|
|
||
|
|
null
|
|
,
|
|
|
|
points_awarded: data.points_awarded
|
|
||
|
|
null
|
|
,
|
|
|
|
points_max: data.points_max
|
|
||
|
|
null
|
|
,
|
|
|
|
grading_status: data.grading_status
|
|
||
|
|
null
|
|
,
|
|
|
|
teacher_feedback: data.teacher_feedback
|
|
||
|
|
null
|
|
,
|
|
|
|
answered_at: data.answered_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await attempt_answers.setExam_attempt( data.exam_attempt || null, {
|
|
transaction,
|
|
});
|
|
|
|
await attempt_answers.setExam_item( data.exam_item || null, {
|
|
transaction,
|
|
});
|
|
|
|
await attempt_answers.setSchools( data.schools || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
await attempt_answers.setSelected_choices(data.selected_choices || [], {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.attempt_answers.getTableName(),
|
|
belongsToColumn: 'answer_files',
|
|
belongsToId: attempt_answers.id,
|
|
},
|
|
data.answer_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return attempt_answers;
|
|
}
|
|
|
|
|
|
static async bulkImport(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
// Prepare data - wrapping individual data transformations in a map() method
|
|
const attempt_answersData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
answer_text: item.answer_text
|
|
||
|
|
null
|
|
,
|
|
|
|
answer_true_false: item.answer_true_false
|
|
||
|
|
null
|
|
,
|
|
|
|
points_awarded: item.points_awarded
|
|
||
|
|
null
|
|
,
|
|
|
|
points_max: item.points_max
|
|
||
|
|
null
|
|
,
|
|
|
|
grading_status: item.grading_status
|
|
||
|
|
null
|
|
,
|
|
|
|
teacher_feedback: item.teacher_feedback
|
|
||
|
|
null
|
|
,
|
|
|
|
answered_at: item.answered_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const attempt_answers = await db.attempt_answers.bulkCreate(attempt_answersData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < attempt_answers.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.attempt_answers.getTableName(),
|
|
belongsToColumn: 'answer_files',
|
|
belongsToId: attempt_answers[i].id,
|
|
},
|
|
data[i].answer_files,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return attempt_answers;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
const globalAccess = currentUser.app_role?.globalAccess;
|
|
|
|
const attempt_answers = await db.attempt_answers.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.answer_text !== undefined) updatePayload.answer_text = data.answer_text;
|
|
|
|
|
|
if (data.answer_true_false !== undefined) updatePayload.answer_true_false = data.answer_true_false;
|
|
|
|
|
|
if (data.points_awarded !== undefined) updatePayload.points_awarded = data.points_awarded;
|
|
|
|
|
|
if (data.points_max !== undefined) updatePayload.points_max = data.points_max;
|
|
|
|
|
|
if (data.grading_status !== undefined) updatePayload.grading_status = data.grading_status;
|
|
|
|
|
|
if (data.teacher_feedback !== undefined) updatePayload.teacher_feedback = data.teacher_feedback;
|
|
|
|
|
|
if (data.answered_at !== undefined) updatePayload.answered_at = data.answered_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await attempt_answers.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.exam_attempt !== undefined) {
|
|
await attempt_answers.setExam_attempt(
|
|
|
|
data.exam_attempt,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.exam_item !== undefined) {
|
|
await attempt_answers.setExam_item(
|
|
|
|
data.exam_item,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.schools !== undefined) {
|
|
await attempt_answers.setSchools(
|
|
|
|
data.schools,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.selected_choices !== undefined) {
|
|
await attempt_answers.setSelected_choices(data.selected_choices, { transaction });
|
|
}
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.attempt_answers.getTableName(),
|
|
belongsToColumn: 'answer_files',
|
|
belongsToId: attempt_answers.id,
|
|
},
|
|
data.answer_files,
|
|
options,
|
|
);
|
|
|
|
|
|
return attempt_answers;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const attempt_answers = await db.attempt_answers.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of attempt_answers) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of attempt_answers) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return attempt_answers;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const attempt_answers = await db.attempt_answers.findByPk(id, options);
|
|
|
|
await attempt_answers.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await attempt_answers.destroy({
|
|
transaction
|
|
});
|
|
|
|
return attempt_answers;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const attempt_answers = await db.attempt_answers.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!attempt_answers) {
|
|
return attempt_answers;
|
|
}
|
|
|
|
const output = attempt_answers.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.exam_attempt = await attempt_answers.getExam_attempt({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.exam_item = await attempt_answers.getExam_item({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.selected_choices = await attempt_answers.getSelected_choices({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.answer_files = await attempt_answers.getAnswer_files({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.schools = await attempt_answers.getSchools({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
globalAccess, options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
const user = (options && options.currentUser) || null;
|
|
const userSchools = (user && user.schools?.id) || null;
|
|
|
|
|
|
|
|
if (userSchools) {
|
|
if (options?.currentUser?.schoolsId) {
|
|
where.schoolsId = options.currentUser.schoolsId;
|
|
}
|
|
}
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.exam_attempts,
|
|
as: 'exam_attempt',
|
|
|
|
where: filter.exam_attempt ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.exam_attempt.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
integrity_token: {
|
|
[Op.or]: filter.exam_attempt.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.exam_items,
|
|
as: 'exam_item',
|
|
|
|
where: filter.exam_item ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.exam_item.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
order_index: {
|
|
[Op.or]: filter.exam_item.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.schools,
|
|
as: 'schools',
|
|
|
|
},
|
|
|
|
|
|
{
|
|
model: db.question_choices,
|
|
as: 'selected_choices',
|
|
required: false,
|
|
},
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'answer_files',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.answer_text) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'attempt_answers',
|
|
'answer_text',
|
|
filter.answer_text,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.teacher_feedback) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'attempt_answers',
|
|
'teacher_feedback',
|
|
filter.teacher_feedback,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.points_awardedRange) {
|
|
const [start, end] = filter.points_awardedRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
points_awarded: {
|
|
...where.points_awarded,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
points_awarded: {
|
|
...where.points_awarded,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.points_maxRange) {
|
|
const [start, end] = filter.points_maxRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
points_max: {
|
|
...where.points_max,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
points_max: {
|
|
...where.points_max,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.answered_atRange) {
|
|
const [start, end] = filter.answered_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
answered_at: {
|
|
...where.answered_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
answered_at: {
|
|
...where.answered_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.answer_true_false) {
|
|
where = {
|
|
...where,
|
|
answer_true_false: filter.answer_true_false,
|
|
};
|
|
}
|
|
|
|
if (filter.grading_status) {
|
|
where = {
|
|
...where,
|
|
grading_status: filter.grading_status,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.schools) {
|
|
const listItems = filter.schools.split('|').map(item => {
|
|
return Utils.uuid(item)
|
|
});
|
|
|
|
where = {
|
|
...where,
|
|
schoolsId: {[Op.or]: listItems}
|
|
};
|
|
}
|
|
|
|
|
|
|
|
if (filter.selected_choices) {
|
|
const searchTerms = filter.selected_choices.split('|');
|
|
|
|
include = [
|
|
{
|
|
model: db.question_choices,
|
|
as: 'selected_choices_filter',
|
|
required: searchTerms.length > 0,
|
|
where: searchTerms.length > 0 ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: searchTerms.map(term => Utils.uuid(term)) } },
|
|
{
|
|
choice_label: {
|
|
[Op.or]: searchTerms.map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
}
|
|
]
|
|
} : undefined
|
|
},
|
|
...include,
|
|
]
|
|
}
|
|
|
|
|
|
if (filter.createdAtRange) {
|
|
const [start, end] = filter.createdAtRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
['createdAt']: {
|
|
...where.createdAt,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (globalAccess) {
|
|
delete where.schoolsId;
|
|
}
|
|
|
|
|
|
const queryOptions = {
|
|
where,
|
|
include,
|
|
distinct: true,
|
|
order: filter.field && filter.sort
|
|
? [[filter.field, filter.sort]]
|
|
: [['createdAt', 'desc']],
|
|
transaction: options?.transaction,
|
|
logging: console.log
|
|
};
|
|
|
|
if (!options?.countOnly) {
|
|
queryOptions.limit = limit ? Number(limit) : undefined;
|
|
queryOptions.offset = offset ? Number(offset) : undefined;
|
|
}
|
|
|
|
try {
|
|
const { rows, count } = await db.attempt_answers.findAndCountAll(queryOptions);
|
|
|
|
return {
|
|
rows: options?.countOnly ? [] : rows,
|
|
count: count
|
|
};
|
|
} catch (error) {
|
|
console.error('Error executing query:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
static async findAllAutocomplete(query, limit, offset, globalAccess, organizationId,) {
|
|
let where = {};
|
|
|
|
|
|
if (!globalAccess && organizationId) {
|
|
where.organizationId = organizationId;
|
|
}
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'attempt_answers',
|
|
'grading_status',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.attempt_answers.findAll({
|
|
attributes: [ 'id', 'grading_status' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['grading_status', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.grading_status,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|