627 lines
15 KiB
JavaScript
627 lines
15 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 QuizzesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const quizzes = await db.quizzes.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
title: data.title
|
|
||
|
|
null
|
|
,
|
|
|
|
instructions: data.instructions
|
|
||
|
|
null
|
|
,
|
|
|
|
status: data.status
|
|
||
|
|
null
|
|
,
|
|
|
|
time_limit_minutes: data.time_limit_minutes
|
|
||
|
|
null
|
|
,
|
|
|
|
attempt_limit: data.attempt_limit
|
|
||
|
|
null
|
|
,
|
|
|
|
pass_percent: data.pass_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
shuffle_questions: data.shuffle_questions
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
show_correct_answers: data.show_correct_answers
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await quizzes.setCourse( data.course || null, {
|
|
transaction,
|
|
});
|
|
|
|
await quizzes.setLesson( data.lesson || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return quizzes;
|
|
}
|
|
|
|
|
|
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 quizzesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
title: item.title
|
|
||
|
|
null
|
|
,
|
|
|
|
instructions: item.instructions
|
|
||
|
|
null
|
|
,
|
|
|
|
status: item.status
|
|
||
|
|
null
|
|
,
|
|
|
|
time_limit_minutes: item.time_limit_minutes
|
|
||
|
|
null
|
|
,
|
|
|
|
attempt_limit: item.attempt_limit
|
|
||
|
|
null
|
|
,
|
|
|
|
pass_percent: item.pass_percent
|
|
||
|
|
null
|
|
,
|
|
|
|
shuffle_questions: item.shuffle_questions
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
show_correct_answers: item.show_correct_answers
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const quizzes = await db.quizzes.bulkCreate(quizzesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return quizzes;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const quizzes = await db.quizzes.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.title !== undefined) updatePayload.title = data.title;
|
|
|
|
|
|
if (data.instructions !== undefined) updatePayload.instructions = data.instructions;
|
|
|
|
|
|
if (data.status !== undefined) updatePayload.status = data.status;
|
|
|
|
|
|
if (data.time_limit_minutes !== undefined) updatePayload.time_limit_minutes = data.time_limit_minutes;
|
|
|
|
|
|
if (data.attempt_limit !== undefined) updatePayload.attempt_limit = data.attempt_limit;
|
|
|
|
|
|
if (data.pass_percent !== undefined) updatePayload.pass_percent = data.pass_percent;
|
|
|
|
|
|
if (data.shuffle_questions !== undefined) updatePayload.shuffle_questions = data.shuffle_questions;
|
|
|
|
|
|
if (data.show_correct_answers !== undefined) updatePayload.show_correct_answers = data.show_correct_answers;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await quizzes.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.course !== undefined) {
|
|
await quizzes.setCourse(
|
|
|
|
data.course,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.lesson !== undefined) {
|
|
await quizzes.setLesson(
|
|
|
|
data.lesson,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return quizzes;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const quizzes = await db.quizzes.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of quizzes) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of quizzes) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return quizzes;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const quizzes = await db.quizzes.findByPk(id, options);
|
|
|
|
await quizzes.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await quizzes.destroy({
|
|
transaction
|
|
});
|
|
|
|
return quizzes;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const quizzes = await db.quizzes.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!quizzes) {
|
|
return quizzes;
|
|
}
|
|
|
|
const output = quizzes.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.quiz_questions_quiz = await quizzes.getQuiz_questions_quiz({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
output.quiz_attempts_quiz = await quizzes.getQuiz_attempts_quiz({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.course = await quizzes.getCourse({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.lesson = await quizzes.getLesson({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
}
|
|
|
|
static async findAll(
|
|
filter,
|
|
options
|
|
) {
|
|
const limit = filter.limit || 0;
|
|
let offset = 0;
|
|
let where = {};
|
|
const currentPage = +filter.page;
|
|
|
|
|
|
|
|
|
|
|
|
offset = currentPage * limit;
|
|
|
|
const orderBy = null;
|
|
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
let include = [
|
|
|
|
{
|
|
model: db.courses,
|
|
as: 'course',
|
|
|
|
where: filter.course ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.course.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.course.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.lessons,
|
|
as: 'lesson',
|
|
|
|
where: filter.lesson ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.lesson.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
title: {
|
|
[Op.or]: filter.lesson.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.title) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'quizzes',
|
|
'title',
|
|
filter.title,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.instructions) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'quizzes',
|
|
'instructions',
|
|
filter.instructions,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.time_limit_minutesRange) {
|
|
const [start, end] = filter.time_limit_minutesRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
time_limit_minutes: {
|
|
...where.time_limit_minutes,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
time_limit_minutes: {
|
|
...where.time_limit_minutes,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.attempt_limitRange) {
|
|
const [start, end] = filter.attempt_limitRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
attempt_limit: {
|
|
...where.attempt_limit,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
attempt_limit: {
|
|
...where.attempt_limit,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.pass_percentRange) {
|
|
const [start, end] = filter.pass_percentRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
pass_percent: {
|
|
...where.pass_percent,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
pass_percent: {
|
|
...where.pass_percent,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.status) {
|
|
where = {
|
|
...where,
|
|
status: filter.status,
|
|
};
|
|
}
|
|
|
|
if (filter.shuffle_questions) {
|
|
where = {
|
|
...where,
|
|
shuffle_questions: filter.shuffle_questions,
|
|
};
|
|
}
|
|
|
|
if (filter.show_correct_answers) {
|
|
where = {
|
|
...where,
|
|
show_correct_answers: filter.show_correct_answers,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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.quizzes.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, ) {
|
|
let where = {};
|
|
|
|
|
|
|
|
if (query) {
|
|
where = {
|
|
[Op.or]: [
|
|
{ ['id']: Utils.uuid(query) },
|
|
Utils.ilike(
|
|
'quizzes',
|
|
'title',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.quizzes.findAll({
|
|
attributes: [ 'id', 'title' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['title', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.title,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|