Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LMS Hackathon Submission #77

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 64 additions & 30 deletions dashboard.html
Original file line number Diff line number Diff line change
@@ -1,33 +1,67 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
<link rel="stylesheet" href="styles.css"> <!-- Include your CSS file -->
</head>
<body>
<header>
<h1>Welcome to Your Dashboard</h1>
<nav>
<ul>
<li><a href="/leaderboard">Leaderboard</a></li>
<li><a href="/course-content">Course Content</a></li>
<!-- Add more navigation links as needed -->
</ul>
</nav>
</header>
<main>
<section class="welcome-message">
<h2>Hello, <span id="user-fullname"></span>!</h2>
<p>Welcome to your personalized dashboard.</p>
</section>
</main>
<footer>
<p>&copy; 2024 Your LMS</p>
</footer>

<!-- Include your client-side JavaScript file -->
<script src="script.js"></script>
</body>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard</title>
<link rel="stylesheet" href="styles.css">
<!-- Include your CSS file -->
</head>
<body>
<header>
<h1>Welcome to Your Dashboard</h1>
<nav>
<ul>
<li>
<a href="/leaderboard">Leaderboard</a>
</li>
<li>
<a href="/course-content">Course Content</a>
</li>
<main>
<section id="course-selection">
<h2>Select Your Courses</h2>
<form id="course-selection-form">
<label>
<input type="checkbox" name="course" value="Course 1">
Course 1
</label>
<br>
<label>
<input type="checkbox" name="course" value="Course 2">
Course 2
</label>
<br>
<label>
<input type="checkbox" name="course" value="Course 3">
Course 3
</label>
<br>
<button type="submit">Save Selection</button>
</form>
</section>
</main>
</ul>
</nav>
</header>
<main>
<section class="welcome-message">
<h2>
Hello,
<span id="user-fullname"></span>
!
</h2>
<p>Welcome to your personalized dashboard.</p>
<section id="selected-courses">
<h2>Your Selected Courses</h2>
<ul id="selected-courses-list"></ul>
</section>
</section>
</main>
<footer>
<p>&copy; 2024 Your LMS</p>
</footer>
<script src="script.js"></script>
<script src="server.js"></script>
</body>
</html>
56 changes: 54 additions & 2 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ document.addEventListener('DOMContentLoaded', () => {
const registerForm = document.getElementById('register-form');
const loginForm = document.getElementById('login-form');
const logoutForm = document.getElementById('logout-form');
const courseSelectionForm = document.getElementById('course-selection-form');


registerForm.addEventListener('submit', async (e) => {
e.preventDefault();
Expand Down Expand Up @@ -74,7 +76,7 @@ document.addEventListener('DOMContentLoaded', () => {
fetchCourseContent();
}

// Check if the current page is the course content page
// Check if the current page is the course content page
if (window.location.pathname === '/leader-board') {
// Fetch course content from server
fetchLeaderboardData();
Expand All @@ -85,6 +87,30 @@ document.addEventListener('DOMContentLoaded', () => {
//fetch Logged in user's full name
fetchFullName();
}

courseSelectionForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(courseSelectionForm);
const selectedCourses = Array.from(formData.getAll('course'));

try {
const response = await fetch('/save-courses', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ courses: selectedCourses })
});

if (response.ok) {
alert('Courses saved successfully');
} else {
alert('Failed to save courses');
}
} catch (error) {
console.error('Error:', error);
}
});
});

function fetchCourseContent() {
Expand Down Expand Up @@ -204,4 +230,30 @@ function displayFullName(fullName) {
const fullNameElement = document.getElementById('user-fullname');
// Set the inner HTML of the element to the user's full name
fullNameElement.textContent = fullName;
}
}



document.addEventListener('DOMContentLoaded', async () => {
try {
const response = await fetch('/user-courses');
if (response.ok) {
const data = await response.json();
displaySelectedCourses(data.courses);
} else {
throw new Error('Failed to fetch user courses');
}
} catch (error) {
console.error('Error:', error);
}
});

function displaySelectedCourses(courses) {
const selectedCoursesList = document.getElementById('selected-courses-list');
selectedCoursesList.innerHTML = '';
courses.forEach(course => {
const listItem = document.createElement('li');
listItem.textContent = course;
selectedCoursesList.appendChild(listItem);
});
}
52 changes: 37 additions & 15 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ app.get('/', (req, res) => {
});



// Define a User representation for clarity
const User = {
tableName: 'users',
createUser: function(newUser, callback) {
tableName: 'users',
createUser: function (newUser, callback) {
connection.query('INSERT INTO ' + this.tableName + ' SET ?', newUser, callback);
},
getUserByEmail: function(email, callback) {
},
getUserByEmail: function (email, callback) {
connection.query('SELECT * FROM ' + this.tableName + ' WHERE email = ?', email, callback);
},
getUserByUsername: function(username, callback) {
getUserByUsername: function (username, callback) {
connection.query('SELECT * FROM ' + this.tableName + ' WHERE username = ?', username, callback);
}
};
Expand Down Expand Up @@ -102,12 +102,12 @@ app.post('/register', [
// Insert user into MySQL
User.createUser(newUser, (error, results, fields) => {
if (error) {
console.error('Error inserting user: ' + error.message);
return res.status(500).json({ error: error.message });
console.error('Error inserting user: ' + error.message);
return res.status(500).json({ error: error.message });
}
console.log('Inserted a new user with id ' + results.insertId);
res.status(201).json(newUser);
});
});
});

// Login route
Expand Down Expand Up @@ -153,13 +153,35 @@ app.get('/course/:id', (req, res) => {
const courseId = req.params.id;
const sql = 'SELECT * FROM courses WHERE id = ?';
db.query(sql, [courseId], (err, result) => {
if (err) {
throw err;
}
// Send course content as JSON response
res.json(result);
if (err) {
throw err;
}
// Send course content as JSON response
res.json(result);
});
});
});

app.post('/save-courses', (req, res) => {
const userId = req.session.user.id;
const selectedCourses = req.body.courses;

// Save selected courses to the database (implementation depends on your database schema)
// Example: Insert selected courses into a table named 'user_courses'

res.sendStatus(200); // Send success response
});

app.get('/user-courses', (req, res) => {
const userId = req.session.user.id;

// Retrieve selected courses for the logged-in user from the database (implementation depends on your database schema)
// Example: Query the 'user_courses' table for courses associated with the user ID

const userCourses = ['Course 1', 'Course 3']; // Dummy data (replace with actual data)

res.json({ courses: userCourses }); // Send selected courses as JSON response
});


// Start server
const PORT = process.env.PORT || 3000;
Expand Down