-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.js
More file actions
378 lines (333 loc) · 10.4 KB
/
dashboard.js
File metadata and controls
378 lines (333 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Dashboard JavaScript for EduMorph platform
// Initialize dashboard
document.addEventListener("DOMContentLoaded", () => {
checkAuthentication()
loadUserData()
loadDashboardData()
initializeDashboard()
})
// Check if user is authenticated
function checkAuthentication() {
const user = getFromStorage("edumorph_user")
if (!user) {
window.location.href = "login.html"
return
}
// Update user name in header
const userNameElement = document.getElementById("user-name")
if (userNameElement) {
userNameElement.textContent = user.firstName
}
}
// Load user data
function loadUserData() {
const user = getFromStorage("edumorph_user")
if (!user) return
// Load user's courses and progress
const users = getFromStorage("edumorph_users") || []
const fullUserData = users.find((u) => u.id === user.id)
if (fullUserData) {
updateDashboardStats(fullUserData)
}
}
// Update dashboard statistics
function updateDashboardStats(userData) {
const enrolledCourses = userData.enrolledCourses || []
const completedCourses = userData.completedCourses || []
// Calculate stats
const totalEnrolled = enrolledCourses.length
const totalCompleted = completedCourses.length
const studyHours = calculateStudyHours(enrolledCourses)
const averageProgress = calculateAverageProgress(enrolledCourses)
// Update DOM elements
updateStatElement("courses-enrolled", totalEnrolled)
updateStatElement("courses-completed", totalCompleted)
updateStatElement("study-hours", studyHours)
updateStatElement("progress-score", `${averageProgress}%`)
}
// Update individual stat element
function updateStatElement(id, value) {
const element = document.getElementById(id)
if (element) {
element.textContent = value
}
}
// Calculate total study hours
function calculateStudyHours(enrolledCourses) {
return enrolledCourses.reduce((total, course) => {
return total + (course.studyTime || 0)
}, 0)
}
// Calculate average progress
function calculateAverageProgress(enrolledCourses) {
if (enrolledCourses.length === 0) return 0
const totalProgress = enrolledCourses.reduce((total, course) => {
return total + (course.progress || 0)
}, 0)
return Math.round(totalProgress / enrolledCourses.length)
}
// Load dashboard data
function loadDashboardData() {
loadCurrentCourses()
loadAIRecommendations()
loadRecentActivity()
}
// Load current courses
function loadCurrentCourses() {
const user = getFromStorage("edumorph_user")
if (!user) return
const users = getFromStorage("edumorph_users") || []
const fullUserData = users.find((u) => u.id === user.id)
const enrolledCourses = fullUserData?.enrolledCourses || []
const coursesContainer = document.getElementById("current-courses")
if (!coursesContainer) return
if (enrolledCourses.length === 0) {
coursesContainer.innerHTML = `
<div class="empty-state">
<i class="fas fa-book-open"></i>
<h3>No courses yet</h3>
<p>Start your learning journey by enrolling in a course</p>
<a href="courses.html" class="btn btn-primary">Browse Courses</a>
</div>
`
return
}
coursesContainer.innerHTML = enrolledCourses
.map(
(course) => `
<div class="course-card">
<div class="course-image">
<i class="fas fa-${getCourseIcon(course.category)}"></i>
</div>
<div class="course-content">
<h3 class="course-title">${course.title}</h3>
<p class="course-description">${course.description}</p>
<div class="course-meta">
<span><i class="fas fa-clock"></i> ${course.duration}</span>
<span><i class="fas fa-signal"></i> ${course.level}</span>
</div>
<div class="course-progress">
<div class="progress-bar">
<div class="progress-fill" style="width: ${course.progress || 0}%"></div>
</div>
<span>${course.progress || 0}% Complete</span>
</div>
<a href="#" class="btn btn-primary" onclick="continueCourse('${course.id}')">Continue Learning</a>
</div>
</div>
`,
)
.join("")
}
// Load AI recommendations
function loadAIRecommendations() {
const recommendationsContainer = document.getElementById("ai-recommendations")
if (!recommendationsContainer) return
const recommendations = generateAIRecommendations()
recommendationsContainer.innerHTML = recommendations
.map(
(rec) => `
<div class="recommendation-card">
<div class="recommendation-icon">
<i class="fas fa-${rec.icon}"></i>
</div>
<h4>${rec.title}</h4>
<p>${rec.description}</p>
<button class="btn btn-outline btn-small" onclick="applyRecommendation('${rec.id}')">
${rec.action}
</button>
</div>
`,
)
.join("")
}
// Load recent activity
function loadRecentActivity() {
const activityContainer = document.getElementById("recent-activity")
if (!activityContainer) return
const activities = getRecentActivities()
if (activities.length === 0) {
activityContainer.innerHTML = `
<div class="empty-state">
<i class="fas fa-history"></i>
<h3>No recent activity</h3>
<p>Your learning activities will appear here</p>
</div>
`
return
}
activityContainer.innerHTML = activities
.map(
(activity) => `
<div class="activity-item">
<div class="activity-icon">
<i class="fas fa-${activity.icon}"></i>
</div>
<div class="activity-content">
<div class="activity-title">${activity.title}</div>
<div class="activity-time">${formatTimeAgo(activity.timestamp)}</div>
</div>
</div>
`,
)
.join("")
}
// Initialize dashboard functionality
function initializeDashboard() {
// Add any interactive elements here
setupProgressAnimations()
}
// Setup progress bar animations
function setupProgressAnimations() {
const progressBars = document.querySelectorAll(".progress-fill")
progressBars.forEach((bar) => {
const width = bar.style.width
bar.style.width = "0%"
setTimeout(() => {
bar.style.width = width
}, 500)
})
}
// Generate AI recommendations
function generateAIRecommendations() {
return [
{
id: "study-schedule",
icon: "calendar-alt",
title: "Optimize Study Schedule",
description: "Based on your learning patterns, we recommend studying for 45 minutes in the morning.",
action: "Create Schedule",
},
{
id: "skill-gap",
icon: "chart-line",
title: "Fill Skill Gap",
description: "Consider taking a course in Data Visualization to complement your current learning.",
action: "View Courses",
},
{
id: "practice-more",
icon: "dumbbell",
title: "Practice Recommendation",
description: "You haven't practiced JavaScript in 3 days. Regular practice improves retention.",
action: "Start Practice",
},
]
}
// Get recent activities
function getRecentActivities() {
const user = getFromStorage("edumorph_user")
if (!user) return []
// Simulate recent activities
return [
{
id: "1",
icon: "play",
title: "Completed lesson: JavaScript Functions",
timestamp: new Date(Date.now() - 2 * 60 * 60 * 1000), // 2 hours ago
},
{
id: "2",
icon: "trophy",
title: "Earned badge: Problem Solver",
timestamp: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000), // 1 day ago
},
{
id: "3",
icon: "book",
title: "Started course: Python Fundamentals",
timestamp: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000), // 2 days ago
},
{
id: "4",
icon: "comment",
title: "Posted in discussion forum",
timestamp: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000), // 3 days ago
},
]
}
// Get course icon based on category
function getCourseIcon(category) {
const icons = {
programming: "code",
design: "palette",
business: "briefcase",
science: "flask",
language: "globe",
math: "calculator",
default: "book",
}
return icons[category] || icons.default
}
// Continue course function
function continueCourse(courseId) {
showNotification("Continuing course...", "info")
// In a real app, this would navigate to the course content
setTimeout(() => {
showNotification("Course content coming soon!", "info")
}, 1000)
}
// Apply recommendation function
function applyRecommendation(recommendationId) {
const actions = {
"study-schedule": () => {
showNotification("Study schedule feature coming soon!", "info")
},
"skill-gap": () => {
window.location.href = "courses.html?category=design"
},
"practice-more": () => {
showNotification("Practice exercises coming soon!", "info")
},
}
const action = actions[recommendationId]
if (action) {
action()
}
}
// Format time ago
function formatTimeAgo(timestamp) {
const now = new Date()
const time = new Date(timestamp)
const diffInSeconds = Math.floor((now - time) / 1000)
if (diffInSeconds < 60) {
return "Just now"
} else if (diffInSeconds < 3600) {
const minutes = Math.floor(diffInSeconds / 60)
return `${minutes} minute${minutes > 1 ? "s" : ""} ago`
} else if (diffInSeconds < 86400) {
const hours = Math.floor(diffInSeconds / 3600)
return `${hours} hour${hours > 1 ? "s" : ""} ago`
} else {
const days = Math.floor(diffInSeconds / 86400)
return `${days} day${days > 1 ? "s" : ""} ago`
}
}
// Logout function
function logout() {
if (window.EduMorph && window.EduMorph.logout) {
window.EduMorph.logout()
} else {
localStorage.removeItem("edumorph_user")
localStorage.removeItem("edumorph_session")
window.location.href = "index.html"
}
}
// Utility functions
function showNotification(message, type) {
if (window.EduMorph && window.EduMorph.showNotification) {
window.EduMorph.showNotification(message, type)
}
}
function getFromStorage(key) {
if (window.EduMorph && window.EduMorph.getFromStorage) {
return window.EduMorph.getFromStorage(key)
}
try {
const data = localStorage.getItem(key)
return data ? JSON.parse(data) : null
} catch (error) {
console.error("Error reading from storage:", error)
return null
}
}