2024-09-13 13:03:35 +00:00
|
|
|
import response from "../../response.js";
|
|
|
|
|
import models from "../../models/index.js";
|
|
|
|
|
|
|
|
|
|
export const getStdExercises = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const stdExercise = await models.StdExercise.findAll();
|
|
|
|
|
response(200, stdExercise, "Success", res);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
response(500, null, "Error retrieving student exercise data!", res);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getStdExerciseById = async (req, res) => {
|
|
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
const stdExercise = await models.StdExercise.findByPk(id);
|
|
|
|
|
|
|
|
|
|
if (!stdExercise) {
|
|
|
|
|
return response(404, null, "Student exercise data not found", res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response(200, stdExercise, "Success", res);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
res.status(500).json({ message: "Internal Server Error" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const stdAnswerExercise = async (req, res, next) => {
|
|
|
|
|
try {
|
2024-09-30 01:06:15 +00:00
|
|
|
const { answers } = req.body;
|
2024-09-13 13:03:35 +00:00
|
|
|
|
2024-09-30 01:06:15 +00:00
|
|
|
if (!Array.isArray(answers) || answers.length === 0) {
|
|
|
|
|
return response(400, null, "Answers array is required", res);
|
2024-09-13 13:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-30 01:06:15 +00:00
|
|
|
for (const answer of answers) {
|
|
|
|
|
const { ID_STUDENT_LEARNING, ID_ADMIN_EXERCISE, ANSWER_STUDENT } = answer;
|
2024-09-13 13:03:35 +00:00
|
|
|
|
2024-09-30 01:06:15 +00:00
|
|
|
if (!ID_STUDENT_LEARNING) {
|
|
|
|
|
return response(400, null, "Id student learning is required", res);
|
|
|
|
|
}
|
2024-09-13 13:03:35 +00:00
|
|
|
|
2024-09-30 01:06:15 +00:00
|
|
|
const existingStdLearning = await models.StdLearning.findByPk(
|
|
|
|
|
ID_STUDENT_LEARNING
|
|
|
|
|
);
|
|
|
|
|
if (!existingStdLearning) {
|
|
|
|
|
return response(404, null, "Id student learning not found", res);
|
|
|
|
|
}
|
2024-09-13 13:03:35 +00:00
|
|
|
|
2024-09-30 01:06:15 +00:00
|
|
|
if (!ID_ADMIN_EXERCISE) {
|
|
|
|
|
return response(400, null, "Id exercise is required", res);
|
|
|
|
|
}
|
2024-09-13 13:03:35 +00:00
|
|
|
|
2024-09-30 01:06:15 +00:00
|
|
|
const exercise = await models.Exercise.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
ID_ADMIN_EXERCISE: ID_ADMIN_EXERCISE,
|
|
|
|
|
ID_LEVEL: existingStdLearning.ID_LEVEL,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!exercise) {
|
|
|
|
|
return response(404, null, "Exercise not found in this level", res);
|
|
|
|
|
}
|
2024-09-13 13:03:35 +00:00
|
|
|
|
2024-09-30 01:06:15 +00:00
|
|
|
if (!ANSWER_STUDENT) {
|
|
|
|
|
return response(400, null, "Answer is required", res);
|
|
|
|
|
}
|
2024-09-13 13:03:35 +00:00
|
|
|
|
2024-09-30 01:06:15 +00:00
|
|
|
const existingStdExercise = await models.StdExercise.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
ID_STUDENT_LEARNING: ID_STUDENT_LEARNING,
|
|
|
|
|
ID_ADMIN_EXERCISE: ID_ADMIN_EXERCISE,
|
|
|
|
|
},
|
2024-09-13 13:03:35 +00:00
|
|
|
});
|
2024-09-30 01:06:15 +00:00
|
|
|
|
|
|
|
|
if (existingStdExercise) {
|
|
|
|
|
existingStdExercise.ANSWER_STUDENT = ANSWER_STUDENT;
|
|
|
|
|
await existingStdExercise.save();
|
|
|
|
|
} else {
|
|
|
|
|
await models.StdExercise.create({
|
|
|
|
|
ID_STUDENT_LEARNING,
|
|
|
|
|
ID_ADMIN_EXERCISE,
|
|
|
|
|
ANSWER_STUDENT,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-09-13 13:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-30 01:06:15 +00:00
|
|
|
req.params.id = answers[0].ID_STUDENT_LEARNING;
|
2024-09-13 13:03:35 +00:00
|
|
|
next();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return response(
|
|
|
|
|
500,
|
|
|
|
|
null,
|
|
|
|
|
"Error creating or updating student exercise data!",
|
|
|
|
|
res
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-09-30 03:20:31 +00:00
|
|
|
|
|
|
|
|
export const getStudentAnswersByStdLearningId = async (req, res) => {
|
2024-10-01 08:03:44 +00:00
|
|
|
try {
|
|
|
|
|
const { id } = req.params;
|
|
|
|
|
|
|
|
|
|
const stdLearning = await models.StdLearning.findByPk(id, {
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: models.StdExercise,
|
|
|
|
|
as: "stdExercises",
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: models.Exercise,
|
|
|
|
|
as: "stdExerciseExercises",
|
2024-10-24 05:35:32 +00:00
|
|
|
attributes: [
|
|
|
|
|
"ID_ADMIN_EXERCISE",
|
|
|
|
|
"TITLE",
|
|
|
|
|
"QUESTION",
|
|
|
|
|
"QUESTION_TYPE",
|
|
|
|
|
"SCORE_WEIGHT",
|
2024-10-31 02:39:27 +00:00
|
|
|
"IMAGE",
|
|
|
|
|
"AUDIO",
|
|
|
|
|
"VIDEO",
|
2024-10-24 05:35:32 +00:00
|
|
|
],
|
|
|
|
|
include: [
|
|
|
|
|
{
|
|
|
|
|
model: models.MultipleChoices,
|
|
|
|
|
as: "multipleChoices",
|
|
|
|
|
attributes: [
|
|
|
|
|
"OPTION_A",
|
|
|
|
|
"OPTION_B",
|
|
|
|
|
"OPTION_C",
|
|
|
|
|
"OPTION_D",
|
|
|
|
|
"OPTION_E",
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
model: models.MatchingPairs,
|
|
|
|
|
as: "matchingPairs",
|
|
|
|
|
attributes: ["LEFT_PAIR", "RIGHT_PAIR"],
|
|
|
|
|
},
|
|
|
|
|
],
|
2024-10-01 08:03:44 +00:00
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
attributes: [
|
|
|
|
|
"ID_STUDENT_EXERCISE",
|
|
|
|
|
"ANSWER_STUDENT",
|
|
|
|
|
"IS_CORRECT",
|
2024-10-24 05:35:32 +00:00
|
|
|
"RESULT_SCORE_STUDENT",
|
2024-10-01 08:03:44 +00:00
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
attributes: ["ID_STUDENT_LEARNING", "ID_LEVEL", "SCORE", "IS_PASS"],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!stdLearning) {
|
2024-10-24 05:35:32 +00:00
|
|
|
return response(404, null, "Student learning data not found", res);
|
2024-10-01 08:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-24 05:35:32 +00:00
|
|
|
const stdLearningData = stdLearning.toJSON();
|
|
|
|
|
|
|
|
|
|
const sortedExercises = stdLearningData.stdExercises.sort((a, b) => {
|
2024-10-01 08:03:44 +00:00
|
|
|
const titleA = a.stdExerciseExercises.TITLE.toUpperCase();
|
|
|
|
|
const titleB = b.stdExerciseExercises.TITLE.toUpperCase();
|
|
|
|
|
|
|
|
|
|
if (titleA < titleB) return -1;
|
|
|
|
|
if (titleA > titleB) return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-24 05:35:32 +00:00
|
|
|
const mappedExercises = sortedExercises.map((exercise) => {
|
|
|
|
|
const exerciseData = { ...exercise };
|
|
|
|
|
const exerciseDetails = exercise.stdExerciseExercises;
|
|
|
|
|
const questionType = exerciseDetails.QUESTION_TYPE;
|
|
|
|
|
|
|
|
|
|
const formattedExercise = {
|
|
|
|
|
ID_STUDENT_EXERCISE: exerciseData.ID_STUDENT_EXERCISE,
|
|
|
|
|
ID_ADMIN_EXERCISE: exerciseDetails.ID_ADMIN_EXERCISE,
|
|
|
|
|
TITLE: exerciseDetails.TITLE,
|
|
|
|
|
QUESTION: exerciseDetails.QUESTION,
|
|
|
|
|
QUESTION_TYPE: exerciseDetails.QUESTION_TYPE,
|
|
|
|
|
SCORE_WEIGHT: exerciseDetails.SCORE_WEIGHT,
|
2024-10-31 02:39:27 +00:00
|
|
|
IMAGE: exerciseDetails.IMAGE,
|
|
|
|
|
VIDEO: exerciseDetails.VIDEO,
|
|
|
|
|
AUDIO: exerciseDetails.AUDIO,
|
2024-10-24 05:35:32 +00:00
|
|
|
ANSWER_STUDENT: exercise.ANSWER_STUDENT,
|
|
|
|
|
IS_CORRECT: exercise.IS_CORRECT,
|
|
|
|
|
RESULT_SCORE_STUDENT: exercise.RESULT_SCORE_STUDENT,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (questionType === "MCQ") {
|
|
|
|
|
formattedExercise.multipleChoices = exerciseDetails.multipleChoices;
|
|
|
|
|
} else if (questionType === "MPQ") {
|
|
|
|
|
formattedExercise.matchingPairs = exerciseDetails.matchingPairs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return formattedExercise;
|
2024-10-01 08:03:44 +00:00
|
|
|
});
|
2024-10-24 05:35:32 +00:00
|
|
|
|
2024-10-28 02:36:05 +00:00
|
|
|
return response(
|
|
|
|
|
200,
|
|
|
|
|
{
|
|
|
|
|
...stdLearningData,
|
|
|
|
|
stdExercises: mappedExercises,
|
|
|
|
|
},
|
|
|
|
|
"Student learning exercises retrieved successfully",
|
|
|
|
|
res
|
|
|
|
|
);
|
2024-10-01 08:03:44 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
2024-10-28 02:36:05 +00:00
|
|
|
return response(
|
|
|
|
|
500,
|
|
|
|
|
null,
|
|
|
|
|
"Error retrieving student learning exercises",
|
|
|
|
|
res
|
|
|
|
|
);
|
2024-10-01 08:03:44 +00:00
|
|
|
}
|
2024-10-28 02:36:05 +00:00
|
|
|
};
|