Untitled
unknown
plain_text
a year ago
3.2 kB
7
Indexable
module.exports.getCourseDetailsById = async (req, res, next) => {
const courseId = req.params.courseId;
try {
// Find the course by ID
const course = await courseCollection.findOne({ _id: new ObjectId(courseId) });
if (!course) {
return res.status(404).json({ message: "Course not found" });
}
// Find chapters for the course
const chapters = await chapterCollection.find({ courseId: courseId }).toArray();
// Count the number of chapters
const numberOfChapters = chapters.length;
// Extract unique weekIds and count the number of weeks
const weekIds = new Set(chapters.map((chapter) => chapter.weekId));
const numberOfWeeks = weekIds.size;
// Count the number of students enrolled in the course
const studentCount = await userCollection.countDocuments({
"courses.courseId": courseId,
});
// Task type breakdown with task data, completion percentage, and overall percentage
const taskTypeDetails = chapters.reduce((acc, chapter) => {
chapter.tasks.forEach((task) => {
if (!task) return; // Skip null tasks
// Calculate completion percentage for the individual task
const participantsCount = task.participants ? task.participants.length : 0;
const completionPercentage = (participantsCount / studentCount) * 100;
// Initialize the task type in the accumulator if not already present
if (!acc[task.taskType]) {
acc[task.taskType] = {
tasks: [],
totalCompletionParticipants: 0,
taskCount: 0
};
}
// Add task details along with completion percentage
acc[task.taskType].tasks.push({
taskId: task.taskId,
taskName: task.taskName,
participantsCount,
completionPercentage,
...task // Include all other task details
});
// Accumulate the completion percentage and task count
acc[task.taskType].totalCompletionParticipants += completionPercentage;
acc[task.taskType].taskCount += 1;
});
return acc;
}, {});
// Calculate the overall completion percentage for each task type
Object.keys(taskTypeDetails).forEach(taskType => {
const taskTypeInfo = taskTypeDetails[taskType];
taskTypeInfo.overallCompletionPercentage = (taskTypeInfo.totalCompletionParticipants / taskTypeInfo.taskCount).toFixed(2);
});
// Calculate the total task count
const totalTaskCount = Object.values(taskTypeDetails).reduce(
(sum, { taskCount }) => sum + taskCount,
0
);
// Construct the response
const courseWithDetails = {
numberOfChapters,
numberOfWeeks,
totalTaskCount,
studentCount,
taskTypeDetails, // Include detailed task information with overall completion percentages
chapters, // Include chapters if you still need the full details
};
res.status(200).json(courseWithDetails);
} catch (error) {
console.error("Error retrieving course details:", error);
res.status(500).json({ message: "Internal Server Error" });
}
};Editor is loading...
Leave a Comment