diff --git a/controllers/learningControllers/stdExercise.js b/controllers/learningControllers/stdExercise.js index 7a12d27..6d21467 100644 --- a/controllers/learningControllers/stdExercise.js +++ b/controllers/learningControllers/stdExercise.js @@ -124,50 +124,6 @@ export const getStudentAnswersByStdLearningId = async (req, res) => { }, ], }, - { - model: models.StdExercise, - as: "stdExercises", - include: [ - { - model: models.Exercise, - as: "stdExerciseExercises", - attributes: [ - "ID_ADMIN_EXERCISE", - "TITLE", - "QUESTION", - "QUESTION_TYPE", - "SCORE_WEIGHT", - "IMAGE", - "AUDIO", - "VIDEO", - ], - 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", - ], - }, ], attributes: ["ID_STUDENT_LEARNING", "ID_LEVEL", "SCORE", "IS_PASS"], }); @@ -177,47 +133,97 @@ export const getStudentAnswersByStdLearningId = async (req, res) => { } const stdLearningData = stdLearning.toJSON(); + const { ID_LEVEL } = stdLearningData; + + const exercises = await models.Exercise.findAll({ + where: { ID_LEVEL }, + 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_ADMIN_EXERCISE", + "TITLE", + "QUESTION", + "QUESTION_TYPE", + "SCORE_WEIGHT", + "IMAGE", + "AUDIO", + "VIDEO", + ], + order: [ + [ + models.Sequelize.literal( + "CAST(SUBSTRING_INDEX(TITLE, ' ', -1) AS UNSIGNED)" + ), + "ASC", + ], + ], + }); + + const stdExercises = await models.StdExercise.findAll({ + where: { ID_STUDENT_LEARNING: stdLearning.ID_STUDENT_LEARNING }, + include: [ + { + model: models.Exercise, + as: "stdExerciseExercises", + attributes: ["ID_ADMIN_EXERCISE"], + }, + ], + attributes: [ + "ID_STUDENT_EXERCISE", + "ID_ADMIN_EXERCISE", + "ANSWER_STUDENT", + "IS_CORRECT", + "RESULT_SCORE_STUDENT", + ], + }); + + const mappedExercises = exercises.map((exercise) => { + const stdExercise = stdExercises.find( + (stdEx) => stdEx.ID_ADMIN_EXERCISE === exercise.ID_ADMIN_EXERCISE + ); + + return { + ID_STUDENT_EXERCISE: stdExercise?.ID_STUDENT_EXERCISE || null, + ID_ADMIN_EXERCISE: exercise.ID_ADMIN_EXERCISE, + TITLE: exercise.TITLE, + QUESTION: exercise.QUESTION, + QUESTION_TYPE: exercise.QUESTION_TYPE, + SCORE_WEIGHT: exercise.SCORE_WEIGHT, + IMAGE: exercise.IMAGE, + VIDEO: exercise.VIDEO, + AUDIO: exercise.AUDIO, + ANSWER_STUDENT: stdExercise?.ANSWER_STUDENT || null, + IS_CORRECT: stdExercise?.IS_CORRECT || null, + RESULT_SCORE_STUDENT: stdExercise?.RESULT_SCORE_STUDENT || null, + ...(exercise.QUESTION_TYPE === "MCQ" && { + multipleChoices: exercise.multipleChoices, + }), + ...(exercise.QUESTION_TYPE === "MPQ" && { + matchingPairs: exercise.matchingPairs, + }), + }; + }); const { NAME_SECTION } = stdLearningData.level.levelTopic.topicSection; const { NAME_TOPIC } = stdLearningData.level.levelTopic; const { NAME_LEVEL } = stdLearningData.level; - const sortedExercises = stdLearningData.stdExercises.sort((a, b) => { - const titleA = a.stdExerciseExercises.TITLE.toUpperCase(); - const titleB = b.stdExerciseExercises.TITLE.toUpperCase(); - - return titleA.localeCompare(titleB); - }); - - 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, - IMAGE: exerciseDetails.IMAGE, - VIDEO: exerciseDetails.VIDEO, - AUDIO: exerciseDetails.AUDIO, - 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; - }); - return response( 200, {