refactor: get student answer function
This commit is contained in:
parent
a3b34c3136
commit
cbd3977858
|
|
@ -112,13 +112,38 @@ export const getStudentAnswersByStdLearningId = async (req, res) => {
|
|||
{
|
||||
model: models.Exercise,
|
||||
as: "stdExerciseExercises",
|
||||
attributes: ["ID_ADMIN_EXERCISE", "TITLE", "QUESTION", "QUESTION_TYPE"],
|
||||
attributes: [
|
||||
"ID_ADMIN_EXERCISE",
|
||||
"TITLE",
|
||||
"QUESTION",
|
||||
"QUESTION_TYPE",
|
||||
"SCORE_WEIGHT",
|
||||
],
|
||||
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"],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
attributes: [
|
||||
"ID_STUDENT_EXERCISE",
|
||||
"ANSWER_STUDENT",
|
||||
"IS_CORRECT",
|
||||
"RESULT_SCORE_STUDENT",
|
||||
],
|
||||
},
|
||||
],
|
||||
|
|
@ -126,12 +151,12 @@ export const getStudentAnswersByStdLearningId = async (req, res) => {
|
|||
});
|
||||
|
||||
if (!stdLearning) {
|
||||
return res.status(404).json({
|
||||
message: "Student learning data not found",
|
||||
});
|
||||
return response(404, null, "Student learning data not found", res);
|
||||
}
|
||||
|
||||
const sortedExercises = stdLearning.stdExercises.sort((a, b) => {
|
||||
const stdLearningData = stdLearning.toJSON();
|
||||
|
||||
const sortedExercises = stdLearningData.stdExercises.sort((a, b) => {
|
||||
const titleA = a.stdExerciseExercises.TITLE.toUpperCase();
|
||||
const titleB = b.stdExerciseExercises.TITLE.toUpperCase();
|
||||
|
||||
|
|
@ -140,23 +165,40 @@ export const getStudentAnswersByStdLearningId = async (req, res) => {
|
|||
return 0;
|
||||
});
|
||||
|
||||
const mappedExercises = sortedExercises.map((exercise) => ({
|
||||
...exercise.toJSON(),
|
||||
exerciseDetails: exercise.stdExerciseExercises,
|
||||
stdExerciseExercises: undefined,
|
||||
}));
|
||||
const mappedExercises = sortedExercises.map((exercise) => {
|
||||
const exerciseData = { ...exercise };
|
||||
const exerciseDetails = exercise.stdExerciseExercises;
|
||||
const questionType = exerciseDetails.QUESTION_TYPE;
|
||||
|
||||
return res.status(200).json({
|
||||
message: "Student learning exercises retrieved successfully",
|
||||
payload: {
|
||||
...stdLearning.toJSON(),
|
||||
stdExercises: mappedExercises,
|
||||
},
|
||||
// Create the formatted exercise object
|
||||
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,
|
||||
ANSWER_STUDENT: exercise.ANSWER_STUDENT,
|
||||
IS_CORRECT: exercise.IS_CORRECT,
|
||||
RESULT_SCORE_STUDENT: exercise.RESULT_SCORE_STUDENT,
|
||||
};
|
||||
|
||||
// Include appropriate details based on question type
|
||||
if (questionType === "MCQ") {
|
||||
formattedExercise.multipleChoices = exerciseDetails.multipleChoices;
|
||||
} else if (questionType === "MPQ") {
|
||||
formattedExercise.matchingPairs = exerciseDetails.matchingPairs;
|
||||
}
|
||||
|
||||
return formattedExercise;
|
||||
});
|
||||
|
||||
return response(200, {
|
||||
...stdLearningData,
|
||||
stdExercises: mappedExercises,
|
||||
}, "Student learning exercises retrieved successfully", res);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return res.status(500).json({
|
||||
message: "Error retrieving student learning exercises",
|
||||
});
|
||||
return response(500, null, "Error retrieving student learning exercises", res);
|
||||
}
|
||||
};
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user