To implement the learning process feature where progress is tracked and displayed, you will indeed need to store this information in a database. Here's a guide on how to design this feature using MySQL:
users
: Stores user information.courses
: Stores course information.sections
: Stores sections information within courses.lessons
: Stores lesson information within sections.user_course_progress
: Tracks the progress of users in each course.CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE courses (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT
);
CREATE TABLE sections (
id INT AUTO_INCREMENT PRIMARY KEY,
course_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
FOREIGN KEY (course_id) REFERENCES courses(id)
);
CREATE TABLE lessons (
id INT AUTO_INCREMENT PRIMARY KEY,
section_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
content TEXT,
FOREIGN KEY (section_id) REFERENCES sections(id)
);
CREATE TABLE user_course_progress (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
course_id INT NOT NULL,
lesson_id INT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT FALSE,
completion_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (course_id) REFERENCES courses(id),
FOREIGN KEY (lesson_id) REFERENCES lessons(id)
);
user_course_progress
table to mark the lesson as completed.INSERT INTO user_course_progress (user_id, course_id, lesson_id, completed)
VALUES (user_id_value, course_id_value, lesson_id_value, TRUE)
ON DUPLICATE KEY UPDATE completed = TRUE, completion_date = CURRENT_TIMESTAMP;
SELECT
COUNT(*) as completed_lessons
FROM
user_course_progress
WHERE
user_id = user_id_value
AND course_id = course_id_value
AND completed = TRUE;
SELECT
COUNT(*) as total_lessons
FROM
lessons
WHERE
section_id IN (SELECT id FROM sections WHERE course_id = course_id_value);
SELECT
l.id as lesson_id,
l.title as lesson_title,
ucp.completed as lesson_completed
FROM
lessons l
LEFT JOIN
user_course_progress ucp
ON l.id = ucp.lesson_id
AND ucp.user_id = user_id_value
WHERE
l.section_id IN (SELECT id FROM sections WHERE course_id = course_id_value);
def get_course_progress(user_id, course_id):
completed_lessons = query_database(f"""
SELECT COUNT(*) as completed_lessons
FROM user_course_progress
WHERE user_id = {user_id}
AND course_id = {course_id}
AND completed = TRUE;
""")
total_lessons = query_database(f"""
SELECT COUNT(*) as total_lessons
FROM lessons
WHERE section_id IN (SELECT id FROM sections WHERE course_id = {course_id});
""")
progress_percentage = (completed_lessons / total_lessons) * 100
return progress_percentage
def display_lesson_status(user_id, course_id):
lessons_status = query_database(f"""
SELECT l.id as lesson_id, l.title as lesson_title, ucp.completed as lesson_completed
FROM lessons l
LEFT JOIN user_course_progress ucp
ON l.id = ucp.lesson_id
AND ucp.user_id = {user_id}
WHERE l.section_id IN (SELECT id FROM sections WHERE course_id = {course_id});
""")
return lessons_status
This design and approach will ensure that you can effectively track and display user progress through each course.
To implement the learning process feature where progress is tracked and displayed, you will indeed need to store this information in a database. Here's a guide on how to design this feature using MySQL:
users
: Stores user information.courses
: Stores course information.sections
: Stores sections information within courses.lessons
: Stores lesson information within sections.user_course_progress
: Tracks the progress of users in each course.CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE courses (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT
);
CREATE TABLE sections (
id INT AUTO_INCREMENT PRIMARY KEY,
course_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
FOREIGN KEY (course_id) REFERENCES courses(id)
);
CREATE TABLE lessons (
id INT AUTO_INCREMENT PRIMARY KEY,
section_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
content TEXT,
FOREIGN KEY (section_id) REFERENCES sections(id)
);
CREATE TABLE user_course_progress (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
course_id INT NOT NULL,
lesson_id INT NOT NULL,
completed BOOLEAN NOT NULL DEFAULT FALSE,
completion_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (course_id) REFERENCES courses(id),
FOREIGN KEY (lesson_id) REFERENCES lessons(id)
);
user_course_progress
table to mark the lesson as completed.INSERT INTO user_course_progress (user_id, course_id, lesson_id, completed)
VALUES (user_id_value, course_id_value, lesson_id_value, TRUE)
ON DUPLICATE KEY UPDATE completed = TRUE, completion_date = CURRENT_TIMESTAMP;
SELECT
COUNT(*) as completed_lessons
FROM
user_course_progress
WHERE
user_id = user_id_value
AND course_id = course_id_value
AND completed = TRUE;
SELECT
COUNT(*) as total_lessons
FROM
lessons
WHERE
section_id IN (SELECT id FROM sections WHERE course_id = course_id_value);
SELECT
l.id as lesson_id,
l.title as lesson_title,
ucp.completed as lesson_completed
FROM
lessons l
LEFT JOIN
user_course_progress ucp
ON l.id = ucp.lesson_id
AND ucp.user_id = user_id_value
WHERE
l.section_id IN (SELECT id FROM sections WHERE course_id = course_id_value);
def get_course_progress(user_id, course_id):
completed_lessons = query_database(f"""
SELECT COUNT(*) as completed_lessons
FROM user_course_progress
WHERE user_id = {user_id}
AND course_id = {course_id}
AND completed = TRUE;
""")
total_lessons = query_database(f"""
SELECT COUNT(*) as total_lessons
FROM lessons
WHERE section_id IN (SELECT id FROM sections WHERE course_id = {course_id});
""")
progress_percentage = (completed_lessons / total_lessons) * 100
return progress_percentage
def display_lesson_status(user_id, course_id):
lessons_status = query_database(f"""
SELECT l.id as lesson_id, l.title as lesson_title, ucp.completed as lesson_completed
FROM lessons l
LEFT JOIN user_course_progress ucp
ON l.id = ucp.lesson_id
AND ucp.user_id = {user_id}
WHERE l.section_id IN (SELECT id FROM sections WHERE course_id = {course_id});
""")
return lessons_status
This design and approach will ensure that you can effectively track and display user progress through each course.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community