Commit 17009258 authored by janithgamage1.ed's avatar janithgamage1.ed

fix; update

Desc : update project
parent 603a830f
import UserProgress from "../models/userProgress.model.js";
export const getUserSubscribedCurriculums = async (req, res) => {
const userId = req.params.userId;
try {
const userProgress = await UserProgress.find({ userId })
.populate('curriculumProgress.curriculumId')
.populate('curriculumProgress.tutorialProgress.tutorialId')
res.status(200).json(userProgress);
} catch (error) {
res.status(500).json({ message: "Error fetching user progress", error });
}
};
export const subscribeToCurriculum = async (req, res) => {
const { userId, curriculumId } = req.body;
// Logic to subscribe to a curriculum for a user
export const subscribeCurriculum = async (req, res) => {
const { userId, curriculum } = req.body;
try {
let userProgress = await UserProgress.findOne({ userId });
if (!userProgress) {
// If there is no user progress record for this user, create a new one
userProgress = new UserProgress({
userId,
curriculumProgress: [],
totalMarks: 0
curriculums: [curriculum], // Add the curriculum to the curriculums array
});
} else {
// If there is an existing user progress record, check if the curriculum already exists
const existingCurriculumIndex = userProgress.curriculums.findIndex(c => c.curriculumCode === curriculum.curriculumCode);
if (existingCurriculumIndex !== -1) {
// If the curriculum exists, update it
userProgress.curriculums[existingCurriculumIndex] = curriculum;
} else {
// If the curriculum doesn't exist, add it to the array
userProgress.curriculums.push(curriculum);
}
}
const curriculumProgressIndex = userProgress.curriculumProgress.findIndex(
progress => progress.curriculumId.toString() === curriculumId
);
// Update the totalCurriculumsMarks based on curriculum marks
const totalCurriculumsMarks = userProgress.curriculums.reduce((total, curriculum) => total + curriculum.curriculumMark, 0);
userProgress.totalCurriculumsMarks = totalCurriculumsMarks;
if (curriculumProgressIndex === -1) {
userProgress.curriculumProgress.push({
curriculumId,
tutorialProgress: []
});
// Save the user progress record
await userProgress.save();
res.status(200).json({ message: 'Curriculum subscription successful' });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error' });
}
};
// Get user's curriculum subscriptions and progress
export const getUserProgress = async (req, res) => {
const userId = req.params.userId;
try {
const userProgress = await UserProgress.findOne({ userId });
if (!userProgress) {
return res.status(404).json({ message: 'User progress not found' });
}
await userProgress.save();
res.status(201).json({ message: "User subscribed to curriculum", userProgress });
res.status(200).json({ userProgress });
} catch (error) {
res.status(500).json({ message: "Error subscribing to curriculum", error });
console.error(error);
res.status(500).json({ error: 'Internal server error' });
}
};
export const updateUserProgress = async (req, res) => {
const { userId, curriculumId, tutorialId, taskId, taskMarks } = req.body;
// Update task item progress and calculate overall progress
export const updateTaskItemProgress = async (req, res) => {
const { userId, curriculumCode, tutorialCode, taskItemTitle, taskItemMarkUser, taskItemSpentTime } = req.body;
try {
const userProgress = await UserProgress.findOne({ userId });
if (!userProgress) {
return res.status(404).json({ message: "User progress not found" });
return res.status(404).json({ message: 'User progress not found' });
}
const curriculumProgressIndex = userProgress.curriculumProgress.findIndex(
progress => progress.curriculumId.toString() === curriculumId
);
if (curriculumProgressIndex !== -1) {
const curriculumProgress = userProgress.curriculumProgress[curriculumProgressIndex];
const tutorialProgressIndex = curriculumProgress.tutorialProgress.findIndex(
tutorialProgress => tutorialProgress.tutorialId.toString() === tutorialId
);
if (tutorialProgressIndex !== -1) {
const tutorialProgress = curriculumProgress.tutorialProgress[tutorialProgressIndex];
const taskProgressIndex = tutorialProgress.taskProgress.findIndex(
taskProgress => taskProgress.taskId.toString() === taskId
);
if (taskProgressIndex !== -1) {
const taskProgress = tutorialProgress.taskProgress[taskProgressIndex];
taskProgress.completed = true;
taskProgress.marks = taskMarks;
// Update tutorial completion status
const tutorialTotalMarks = tutorialProgress.tutorialId.totalTutorialMarks; // You'll need to populate this field
if (tutorialTotalMarks > 0) {
tutorialProgress.completed = (tutorialProgress.marks / tutorialTotalMarks) > 0.85;
}
// Update curriculum completion status
const curriculumTotalMarks = curriculumProgress.curriculumId.totalCurriculumMarks; // You'll need to populate this field
if (curriculumTotalMarks > 0) {
curriculumProgress.completed = (curriculumProgress.marks / curriculumTotalMarks) > 0.85;
}
// Update user total marks
userProgress.totalMarks = userProgress.curriculumProgress.reduce(
(total, curriculumProgress) => total + curriculumProgress.marks,
0
);
}
}
// Find the curriculum, tutorial, and task item to update
const curriculumIndex = userProgress.curriculums.findIndex(c => c.curriculumCode === curriculumCode);
if (curriculumIndex === -1) {
return res.status(404).json({ message: 'Curriculum not found' });
}
const tutorialIndex = userProgress.curriculums[curriculumIndex].tutorials.findIndex(t => t.tutorialCode === tutorialCode);
if (tutorialIndex === -1) {
return res.status(404).json({ message: 'Tutorial not found' });
}
const taskItemIndex = userProgress.curriculums[curriculumIndex].tutorials[tutorialIndex].taskItems.findIndex(item => item.title === taskItemTitle);
if (taskItemIndex === -1) {
return res.status(404).json({ message: 'Task item not found' });
}
// Update task item progress
userProgress.curriculums[curriculumIndex].tutorials[tutorialIndex].taskItems[taskItemIndex].taskItemMarkUser = taskItemMarkUser;
userProgress.curriculums[curriculumIndex].tutorials[tutorialIndex].taskItems[taskItemIndex].taskItemSpentTime = taskItemSpentTime;
// Calculate total task marks and spent time for the tutorial
const tutorial = userProgress.curriculums[curriculumIndex].tutorials[tutorialIndex];
tutorial.tutorialMarkUser = tutorial.taskItems.reduce((total, item) => total + item.taskItemMarkUser, 0);
tutorial.tutorialSpentTime = tutorial.taskItems.reduce((total, item) => total + item.taskItemSpentTime, 0);
// Calculate total tutorial marks and spent time for the curriculum
const curriculum = userProgress.curriculums[curriculumIndex];
curriculum.curriculumMarkUser = curriculum.tutorials.reduce((total, t) => total + t.tutorialMarkUser, 0);
curriculum.curriculumSpentTime = curriculum.tutorials.reduce((total, t) => total + t.tutorialSpentTime, 0);
// Calculate total curriculum marks and spent time for the user
userProgress.totalCurriculumsMarks = userProgress.curriculums.reduce((total, c) => total + c.curriculumMarkUser, 0);
userProgress.totalCurriculumSpentTime = userProgress.curriculums.reduce((total, c) => total + c.curriculumSpentTime, 0);
// Save the updated user progress
await userProgress.save();
res.status(200).json({ message: "User progress updated", userProgress });
res.status(200).json({ message: 'Task item progress updated successfully' });
} catch (error) {
res.status(500).json({ message: "Error updating user progress", error });
console.error(error);
res.status(500).json({ error: 'Internal server error' });
}
};
\ No newline at end of file
};
import mongoose from "mongoose";
const commonFields = {
createdBy: String,
updatedBy: String,
createdAt: {
type: Date,
default: new Date(),
const taskItemSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
updatedAt: {
type: Date,
default: new Date(),
description: {
type: String,
required: true,
},
};
const taskProgressSchema = new mongoose.Schema({
taskId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Task'
howToDo: [String],
referenceImage: String,
referenceVideo: String,
taskItemMark: {
type: Number,
default: 0
},
completed: {
type: Boolean,
default: false
taskItemMarkUser: {
type: Number,
default: 0
},
taskMarkUser: {
taskItemSpentTime: {
type: Number,
default: 0
}
});
},
}, { _id: false });
const tutorialProgressSchema = new mongoose.Schema({
tutorialId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Tutorial'
const tutorialTypeUserProgressSchema = new mongoose.Schema({
tutorialCode: String,
tutorialTitle: String,
tutorialDescription: String,
tutorialImage: String,
tutorialMark: {
type: Number,
default: 0
},
taskProgress: [taskProgressSchema],
completed: {
type: Boolean,
default: false
},
tutorialMarkUser: {
type: Number,
default: 0
}
});
const curriculumProgressSchema = new mongoose.Schema({
curriculumId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Curriculum'
},
tutorialProgress: [tutorialProgressSchema],
completed: {
type: Boolean,
default: false
tutorialSpentTime: {
type: Number,
default: 0
},
taskItems: [taskItemSchema],
}, { _id: false });
const curriculumTypeUserProgressSchema = new mongoose.Schema({
curriculumCode: String,
curriculumLevel: Number,
curriculumTitle: String,
curriculumDescription: String,
curriculumImage: String,
curriculumMark: {
type: Number,
default: 0
},
curriculumMarkUser: {
type: Number,
default: 0
}
});
},
curriculumSpentTime: {
type: Number,
default: 0
},
tutorials: [tutorialTypeUserProgressSchema],
}, { _id: false });
const userProgressSchema = new mongoose.Schema({
userId: {
......@@ -66,16 +73,29 @@ const userProgressSchema = new mongoose.Schema({
required: true,
ref: 'User'
},
curriculumProgress: [curriculumProgressSchema],
curriculums: [curriculumTypeUserProgressSchema],
totalCurriculumsMarks: {
type: Number,
default: 0
},
totalCurriculumsMarksUser: {
totalCurriculumSpentTime: {
type: Number,
default: 0
},
...commonFields
status: {
type: Number,
default: 1,
},
createdBy: String,
createdAt: {
type: Date,
default: new Date(),
},
updatedBy: String,
updatedAt: {
type: Date,
default: new Date(),
},
});
const UserProgress = mongoose.model("UserProgress", userProgressSchema);
......
import express from "express";
import { getUserSubscribedCurriculums, subscribeToCurriculum, updateUserProgress } from "../controllers/userProgress.controller.js";
import { getUserProgress, subscribeCurriculum, updateTaskItemProgress } from "../controllers/userProgress.controller.js";
const router = express.Router();
router.get('/:userId', getUserSubscribedCurriculums);
router.post('/', subscribeToCurriculum);
router.put('/:userId', updateUserProgress);
router.post('/subscribe-curriculum', subscribeCurriculum);
router.get('/:userId', getUserProgress);
router.put('/update-task-item-progress', updateTaskItemProgress);
export default router;
......@@ -136,6 +136,12 @@ const application: NavItemType = {
title: <FormattedMessage id="learning-curriculums-subscribed" />,
type: 'item',
url: '/learning-management/curriculums-subscribed',
},
{
id: 'learning-curriculums-subscribed-tutorial',
title: <FormattedMessage id="learning-curriculums-subscribed-tutorial" />,
type: 'item',
url: '/learning-management/curriculums-subscribed-tutorial',
},
{
id: 'learning-lead-board',
......
// material-ui
// project import
// types
// assets
// ==============================|| Tutorial ||============================== //
const Tutorial = () => {
return (
<>
</>
)
}
export default Tutorial;
......@@ -27,7 +27,7 @@ const Dashboard = Loadable(lazy(() => import('pages/home/dashboard')));
const MemberManagementList = Loadable(lazy(() => import('pages/member-management/list/list')));
// render - user management page
const UserManagementList = Loadable(lazy(() => import('pages/user-management/list/list')));
const UserManagementList = Loadable(lazy(() => import('pages/user-management/list/list')));
// render - ssl translate process page
const SSLTranslateProcess = Loadable(lazy(() => import('pages/ssl-translate/process/process')));
......@@ -36,6 +36,7 @@ const SSLTranslateProcess = Loadable(lazy(() => import('pages/ssl-translate/proc
const LearningDashboard = Loadable(lazy(() => import('pages/learning-management/dashboard')));
const LearningCurriculums = Loadable(lazy(() => import('pages/learning-management/learning-curriculums/list/list')));
const LearningCurriculumsSubscribed = Loadable(lazy(() => import('pages/learning-management/learning-curriculums-subscribed/list/list')));
const LearningCurriculumsSubscribedTutorial = Loadable(lazy(() => import('pages/learning-management/learning-curriculums-subscribed/tutorial/tutorial')));
const LearningLeadBoard = Loadable(lazy(() => import('pages/learning-management/learning-lead-board/list/list')));
const LearningFeedBack = Loadable(lazy(() => import('pages/learning-management/learning-feedback/list/list')));
......@@ -134,6 +135,10 @@ const MainRoutes = {
path: 'curriculums-subscribed',
element: <LearningCurriculumsSubscribed />
},
{
path: 'curriculums-subscribed-tutorial',
element: <LearningCurriculumsSubscribedTutorial />
},
{
path: 'lead-board',
element: <LearningLeadBoard />
......
export interface userProgressType {
userId: string
curriculums?: curriculumTypeUserProgress[]
totalCurriculumsMarks : number
totalCurriculumSpentTime : number
status: number
createdBy: string
createdAt: Date
updatedBy?: string
updatedAt?: Date
}
export interface curriculumTypeUserProgress {
_id?: string
curriculumCode: string
curriculumLevel: number
curriculumTitle: string
curriculumDescription: string
curriculumImage: string
curriculumMark: number
curriculumMarkUser: number
curriculumSpentTime: number
tutorials: tutorialTypeUserProgress[],
}
export interface tutorialTypeUserProgress {
_id?: string
tutorialCode?: string
tutorialTitle?: string
tutorialDescription?: string
tutorialImage?: string
tutorialMark: number
tutorialMarkUser: number
tutorialSpentTime: number
taskItems: taskItemTypeUserProgress[]
}
export interface taskItemTypeUserProgress {
_id?: string
title: string,
description: string,
howToDo: string[],
referenceImage: string,
referenceVideo: string,
taskItemMark: number
taskItemMarkUser: number
taskItemSpentTime: number
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment