Blanks Exercises were removing the blanks on the editor and text but not on solutions, added submit and preview to writing and reading
This commit is contained in:
@@ -1,59 +1,9 @@
|
||||
import { FillBlanksExercise, LevelPart, ListeningPart, MatchSentencesExercise, MultipleChoiceExercise, ReadingPart, TrueFalseExercise, WriteBlanksExercise } from "@/interfaces/exam";
|
||||
import { Exercise, FillBlanksExercise, LevelPart, ListeningPart, MatchSentencesExercise, MultipleChoiceExercise, ReadingPart, TrueFalseExercise, WriteBlanksExercise } from "@/interfaces/exam";
|
||||
import { ModuleState } from "../types";
|
||||
import ReorderResult from "./types";
|
||||
|
||||
const reorderFillBlanks = (exercise: FillBlanksExercise, startId: number): ReorderResult<FillBlanksExercise> => {
|
||||
const newSolutions = exercise.solutions
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
.map((solution, index) => ({
|
||||
...solution,
|
||||
id: (startId + index).toString()
|
||||
}));
|
||||
|
||||
const idMapping = exercise.solutions
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
.reduce((acc, solution, index) => {
|
||||
acc[solution.id] = (startId + index).toString();
|
||||
return acc;
|
||||
}, {} as Record<string, string>);
|
||||
|
||||
let newText = exercise.text;
|
||||
Object.entries(idMapping).forEach(([oldId, newId]) => {
|
||||
const regex = new RegExp(`\\{\\{${oldId}\\}\\}`, 'g');
|
||||
newText = newText.replace(regex, `{{${newId}}}`);
|
||||
});
|
||||
|
||||
|
||||
const newWords = exercise.words.map(word => {
|
||||
if (typeof word === 'string') {
|
||||
return word;
|
||||
} else if ('letter' in word && 'word' in word) {
|
||||
return word;
|
||||
} else if ('options' in word) {
|
||||
return word;
|
||||
}
|
||||
return word;
|
||||
});
|
||||
|
||||
const newUserSolutions = exercise.userSolutions?.map(solution => ({
|
||||
...solution,
|
||||
id: idMapping[solution.id] || solution.id
|
||||
}));
|
||||
|
||||
return {
|
||||
exercise: {
|
||||
...exercise,
|
||||
solutions: newSolutions,
|
||||
text: newText,
|
||||
words: newWords,
|
||||
userSolutions: newUserSolutions
|
||||
},
|
||||
lastId: startId + newSolutions.length - 1
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
const reorderWriteBlanks = (exercise: WriteBlanksExercise, startId: number): ReorderResult<WriteBlanksExercise> => {
|
||||
console.log();
|
||||
const newSolutions = exercise.solutions
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
.map((solution, index) => ({
|
||||
@@ -61,21 +11,78 @@ const reorderWriteBlanks = (exercise: WriteBlanksExercise, startId: number): Reo
|
||||
id: (startId + index).toString()
|
||||
}));
|
||||
|
||||
const idMapping = exercise.solutions
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
.reduce((acc, solution, index) => {
|
||||
acc[solution.id] = (startId + index).toString();
|
||||
return acc;
|
||||
}, {} as Record<string, string>);
|
||||
|
||||
let newText = exercise.text;
|
||||
Object.entries(idMapping).forEach(([oldId, newId]) => {
|
||||
const regex = new RegExp(`\\{\\{${oldId}\\}\\}`, 'g');
|
||||
newText = newText.replace(regex, `{{${newId}}}`);
|
||||
});
|
||||
|
||||
|
||||
const newWords = exercise.words.map(word => {
|
||||
if (typeof word === 'string') {
|
||||
return word;
|
||||
} else if ('letter' in word && 'word' in word) {
|
||||
return word;
|
||||
} else if ('options' in word) {
|
||||
return word;
|
||||
}
|
||||
return word;
|
||||
});
|
||||
|
||||
const newUserSolutions = exercise.userSolutions?.map(solution => ({
|
||||
...solution,
|
||||
id: idMapping[solution.id] || solution.id
|
||||
}));
|
||||
|
||||
return {
|
||||
exercise: {
|
||||
...exercise,
|
||||
solutions: newSolutions,
|
||||
text: newSolutions.reduce((text, solution, index) => {
|
||||
return text.replace(
|
||||
new RegExp(`\\{\\{${solution.id}\\}\\}`),
|
||||
`{{${startId + index}}}`
|
||||
);
|
||||
}, exercise.text)
|
||||
text: newText,
|
||||
words: newWords,
|
||||
userSolutions: newUserSolutions
|
||||
},
|
||||
lastId: startId + newSolutions.length
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
const reorderWriteBlanks = (exercise: WriteBlanksExercise, startId: number): ReorderResult<WriteBlanksExercise> => {
|
||||
const oldIds = exercise.solutions.map(s => s.id);
|
||||
const newIds = oldIds.map((_, index) => (startId + index).toString());
|
||||
|
||||
const newSolutions = exercise.solutions.map((solution, index) => ({
|
||||
id: newIds[index],
|
||||
solution: [...solution.solution]
|
||||
}));
|
||||
|
||||
let newText = exercise.text;
|
||||
oldIds.forEach((oldId, index) => {
|
||||
newText = newText.replace(
|
||||
`{{${oldId}}}`,
|
||||
`{{${newIds[index]}}}`
|
||||
);
|
||||
});
|
||||
|
||||
const result = {
|
||||
exercise: {
|
||||
...exercise,
|
||||
solutions: newSolutions,
|
||||
text: newText
|
||||
},
|
||||
lastId: startId + newSolutions.length
|
||||
};
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const reorderTrueFalse = (exercise: TrueFalseExercise, startId: number): ReorderResult<TrueFalseExercise> => {
|
||||
const newQuestions = exercise.questions
|
||||
.sort((a, b) => parseInt(a.id) - parseInt(b.id))
|
||||
@@ -129,60 +136,79 @@ const reorderMultipleChoice = (exercise: MultipleChoiceExercise, startId: number
|
||||
};
|
||||
|
||||
|
||||
const reorderSection = (exercises: Exercise[], startId: number): { exercises: Exercise[], lastId: number } => {
|
||||
let currentId = startId;
|
||||
const reorderedExercises = exercises.map(exercise => {
|
||||
let result;
|
||||
|
||||
switch (exercise.type) {
|
||||
case 'fillBlanks':
|
||||
console.log("Reordering FillBlanks");
|
||||
result = reorderFillBlanks(exercise, currentId);
|
||||
currentId = result.lastId;
|
||||
return result.exercise;
|
||||
|
||||
const reorderExercises = (moduleState: ModuleState) => {
|
||||
let currentId = 1;
|
||||
case 'writeBlanks':
|
||||
result = reorderWriteBlanks(exercise, currentId);
|
||||
currentId = result.lastId;
|
||||
return result.exercise;
|
||||
|
||||
const reorderedSections = moduleState.sections.map(section => {
|
||||
const currentSection = section.state as ReadingPart | ListeningPart |LevelPart;
|
||||
const reorderedExercises = currentSection.exercises.map(exercise => {
|
||||
let result;
|
||||
switch (exercise.type) {
|
||||
case 'fillBlanks':
|
||||
result = reorderFillBlanks(exercise, currentId);
|
||||
currentId = result.lastId;
|
||||
return result.exercise;
|
||||
case 'writeBlanks':
|
||||
result = reorderWriteBlanks(exercise, currentId);
|
||||
currentId = result.lastId;
|
||||
return result.exercise;
|
||||
case 'trueFalse':
|
||||
result = reorderTrueFalse(exercise, currentId);
|
||||
currentId = result.lastId;
|
||||
return result.exercise;
|
||||
case 'matchSentences':
|
||||
result = reorderMatchSentences(exercise, currentId);
|
||||
currentId = result.lastId;
|
||||
return result.exercise;
|
||||
case 'multipleChoice':
|
||||
result = reorderMultipleChoice(exercise, currentId);
|
||||
currentId = result.lastId
|
||||
return result.exercise;
|
||||
default:
|
||||
return exercise;
|
||||
}
|
||||
});
|
||||
return {
|
||||
...section,
|
||||
state: {
|
||||
...currentSection,
|
||||
exercises: reorderedExercises
|
||||
}
|
||||
};
|
||||
case 'trueFalse':
|
||||
result = reorderTrueFalse(exercise, currentId);
|
||||
currentId = result.lastId;
|
||||
return result.exercise;
|
||||
|
||||
case 'matchSentences':
|
||||
result = reorderMatchSentences(exercise, currentId);
|
||||
currentId = result.lastId;
|
||||
return result.exercise;
|
||||
|
||||
case 'multipleChoice':
|
||||
result = reorderMultipleChoice(exercise, currentId);
|
||||
currentId = result.lastId;
|
||||
return result.exercise;
|
||||
|
||||
default:
|
||||
return exercise;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
...moduleState,
|
||||
sections: reorderedSections
|
||||
exercises: reorderedExercises,
|
||||
lastId: currentId
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
const reorderModule = (moduleState: ModuleState) => {
|
||||
let currentId = 1;
|
||||
const reorderedSections = moduleState.sections.map(section => {
|
||||
const currentSection = section.state as ReadingPart | ListeningPart | LevelPart;
|
||||
console.log(currentSection.exercises);
|
||||
const result = reorderSection(currentSection.exercises, currentId);
|
||||
currentId = result.lastId;
|
||||
console.log(result);
|
||||
return {
|
||||
...section,
|
||||
state: {
|
||||
...currentSection,
|
||||
exercises: result.exercises
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
...moduleState,
|
||||
sections: reorderedSections
|
||||
};
|
||||
};
|
||||
|
||||
export {
|
||||
reorderFillBlanks,
|
||||
reorderWriteBlanks,
|
||||
reorderTrueFalse,
|
||||
reorderMatchSentences,
|
||||
reorderExercises,
|
||||
reorderSection,
|
||||
reorderModule,
|
||||
reorderMultipleChoice,
|
||||
};
|
||||
Reference in New Issue
Block a user