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,
|
model: models.Exercise,
|
||||||
as: "stdExerciseExercises",
|
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: [
|
attributes: [
|
||||||
"ID_STUDENT_EXERCISE",
|
"ID_STUDENT_EXERCISE",
|
||||||
"ANSWER_STUDENT",
|
"ANSWER_STUDENT",
|
||||||
"IS_CORRECT",
|
"IS_CORRECT",
|
||||||
|
"RESULT_SCORE_STUDENT",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
@ -126,12 +151,12 @@ export const getStudentAnswersByStdLearningId = async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!stdLearning) {
|
if (!stdLearning) {
|
||||||
return res.status(404).json({
|
return response(404, null, "Student learning data not found", res);
|
||||||
message: "Student learning data not found",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 titleA = a.stdExerciseExercises.TITLE.toUpperCase();
|
||||||
const titleB = b.stdExerciseExercises.TITLE.toUpperCase();
|
const titleB = b.stdExerciseExercises.TITLE.toUpperCase();
|
||||||
|
|
||||||
|
|
@ -140,23 +165,40 @@ export const getStudentAnswersByStdLearningId = async (req, res) => {
|
||||||
return 0;
|
return 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
const mappedExercises = sortedExercises.map((exercise) => ({
|
const mappedExercises = sortedExercises.map((exercise) => {
|
||||||
...exercise.toJSON(),
|
const exerciseData = { ...exercise };
|
||||||
exerciseDetails: exercise.stdExerciseExercises,
|
const exerciseDetails = exercise.stdExerciseExercises;
|
||||||
stdExerciseExercises: undefined,
|
const questionType = exerciseDetails.QUESTION_TYPE;
|
||||||
}));
|
|
||||||
|
|
||||||
return res.status(200).json({
|
// Create the formatted exercise object
|
||||||
message: "Student learning exercises retrieved successfully",
|
const formattedExercise = {
|
||||||
payload: {
|
ID_STUDENT_EXERCISE: exerciseData.ID_STUDENT_EXERCISE,
|
||||||
...stdLearning.toJSON(),
|
ID_ADMIN_EXERCISE: exerciseDetails.ID_ADMIN_EXERCISE,
|
||||||
stdExercises: mappedExercises,
|
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) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return res.status(500).json({
|
return response(500, null, "Error retrieving student learning exercises", res);
|
||||||
message: "Error retrieving student learning exercises",
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue
Block a user