Untitled
unknown
plain_text
a month ago
4.3 kB
2
Indexable
Never
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; // Get the list of students enrolled in the course const students = await userCollection.find({ "courses.courseId": courseId, }).toArray(); const studentCount = students.length; // Task type breakdown with task data, completion percentage, and overall percentage const taskTypeDetails = chapters.reduce((acc, chapter) => { chapter.tasks.forEach((task) => { if (!task || !task.participants) return; // Skip null tasks or tasks without participants // Calculate completion percentage for the individual task const participantsCount = task.participants.length; 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 ); // Calculate individual student course completion percentages const studentsWithCompletion = students.map(student => { let studentTaskCount = 0; chapters.forEach(chapter => { chapter.tasks.forEach(task => { // Ensure the task and its participants are valid before checking if (task && task.participants) { const participantIds = task.participants.map(p => p.participantId); // Extract participant IDs if (participantIds.includes(student._id.toString())) { studentTaskCount += 1; } } }); }); const studentCompletionPercentage = (studentTaskCount / totalTaskCount) * 100; return { studentId: student._id, studentName: student.name, studentCompletionPercentage: Math.round(studentCompletionPercentage), }; }); // Construct the response const courseWithDetails = { numberOfChapters, numberOfWeeks, totalTaskCount, studentCount, taskTypeDetails, // Include detailed task information with overall completion percentages studentsWithCompletion, // Include individual student completion percentages // 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" }); } };
Leave a Comment