-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourses.js
More file actions
424 lines (372 loc) · 11.6 KB
/
courses.js
File metadata and controls
424 lines (372 loc) · 11.6 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Courses JavaScript for EduMorph platform
// Sample course data
const coursesData = [
{
id: "js-basics",
title: "JavaScript Fundamentals",
description: "Learn the basics of JavaScript programming language",
category: "programming",
level: "beginner",
duration: "8 hours",
price: "Free",
rating: 4.8,
students: 1250,
instructor: "John Smith",
image: "code",
},
{
id: "python-intro",
title: "Python for Beginners",
description: "Start your programming journey with Python",
category: "programming",
level: "beginner",
duration: "12 hours",
price: "$49",
rating: 4.9,
students: 2100,
instructor: "Sarah Johnson",
image: "python",
},
{
id: "web-design",
title: "Modern Web Design",
description: "Create beautiful and responsive websites",
category: "design",
level: "intermediate",
duration: "15 hours",
price: "$79",
rating: 4.7,
students: 890,
instructor: "Mike Chen",
image: "palette",
},
{
id: "data-science",
title: "Data Science Essentials",
description: "Introduction to data analysis and visualization",
category: "science",
level: "intermediate",
duration: "20 hours",
price: "$99",
rating: 4.6,
students: 750,
instructor: "Dr. Emily Davis",
image: "chart-bar",
},
{
id: "business-strategy",
title: "Business Strategy Fundamentals",
description: "Learn strategic thinking and business planning",
category: "business",
level: "beginner",
duration: "10 hours",
price: "$59",
rating: 4.5,
students: 650,
instructor: "Robert Wilson",
image: "briefcase",
},
{
id: "spanish-basics",
title: "Spanish for Beginners",
description: "Start speaking Spanish with confidence",
category: "language",
level: "beginner",
duration: "25 hours",
price: "$69",
rating: 4.8,
students: 1100,
instructor: "Maria Rodriguez",
image: "globe",
},
]
let filteredCourses = [...coursesData]
let currentPage = 1
const coursesPerPage = 6
// Initialize courses page
document.addEventListener("DOMContentLoaded", () => {
checkAuthentication()
initializeFilters()
loadCourses()
setupEventListeners()
})
// Check authentication
function checkAuthentication() {
const user = getFromStorage("edumorph_user")
if (!user) {
// Redirect to login for protected features
const enrollButtons = document.querySelectorAll(".enroll-btn")
enrollButtons.forEach((btn) => {
btn.addEventListener("click", (e) => {
e.preventDefault()
showNotification("Please log in to enroll in courses", "info")
setTimeout(() => {
window.location.href = "login.html"
}, 2000)
})
})
}
}
// Initialize filters
function initializeFilters() {
const categoryFilter = document.getElementById("category-filter")
const levelFilter = document.getElementById("level-filter")
const durationFilter = document.getElementById("duration-filter")
const searchInput = document.getElementById("course-search")
if (categoryFilter) {
categoryFilter.addEventListener("change", applyFilters)
}
if (levelFilter) {
levelFilter.addEventListener("change", applyFilters)
}
if (durationFilter) {
durationFilter.addEventListener("change", applyFilters)
}
if (searchInput) {
searchInput.addEventListener("input", debounce(applyFilters, 300))
}
}
// Setup event listeners
function setupEventListeners() {
const loadMoreBtn = document.getElementById("load-more-btn")
if (loadMoreBtn) {
loadMoreBtn.addEventListener("click", loadMoreCourses)
}
}
// Apply filters
function applyFilters() {
const categoryFilter = document.getElementById("category-filter")
const levelFilter = document.getElementById("level-filter")
const durationFilter = document.getElementById("duration-filter")
const searchInput = document.getElementById("course-search")
const category = categoryFilter?.value || ""
const level = levelFilter?.value || ""
const duration = durationFilter?.value || ""
const search = searchInput?.value.toLowerCase() || ""
filteredCourses = coursesData.filter((course) => {
const matchesCategory = !category || course.category === category
const matchesLevel = !level || course.level === level
const matchesDuration = !duration || matchesDurationFilter(course.duration, duration)
const matchesSearch =
!search ||
course.title.toLowerCase().includes(search) ||
course.description.toLowerCase().includes(search) ||
course.instructor.toLowerCase().includes(search)
return matchesCategory && matchesLevel && matchesDuration && matchesSearch
})
currentPage = 1
loadCourses()
}
// Check if course matches duration filter
function matchesDurationFilter(courseDuration, filter) {
const hours = Number.parseInt(courseDuration)
switch (filter) {
case "short":
return hours < 5
case "medium":
return hours >= 5 && hours <= 20
case "long":
return hours > 20
default:
return true
}
}
// Load courses
function loadCourses() {
const courseGrid = document.getElementById("course-grid")
if (!courseGrid) return
const startIndex = 0
const endIndex = currentPage * coursesPerPage
const coursesToShow = filteredCourses.slice(startIndex, endIndex)
if (coursesToShow.length === 0) {
courseGrid.innerHTML = `
<div class="empty-state">
<i class="fas fa-search"></i>
<h3>No courses found</h3>
<p>Try adjusting your search criteria</p>
<button class="btn btn-outline" onclick="clearFilters()">Clear Filters</button>
</div>
`
updateLoadMoreButton(false)
return
}
courseGrid.innerHTML = coursesToShow.map((course) => createCourseCard(course)).join("")
// Update load more button
const hasMore = endIndex < filteredCourses.length
updateLoadMoreButton(hasMore)
}
// Create course card HTML
function createCourseCard(course) {
const user = getFromStorage("edumorph_user")
const isEnrolled = user && isUserEnrolled(user.id, course.id)
return `
<div class="course-card">
<div class="course-image">
<i class="fas fa-${course.image}"></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>
<span><i class="fas fa-star"></i> ${course.rating}</span>
</div>
<div class="course-footer">
<div class="course-price">${course.price}</div>
<button class="btn ${isEnrolled ? "btn-outline" : "btn-primary"}"
onclick="${isEnrolled ? `continueCourse('${course.id}')` : `enrollInCourse('${course.id}')`}">
${isEnrolled ? "Continue" : "Enroll Now"}
</button>
</div>
<div class="course-instructor">
<i class="fas fa-user"></i>
${course.instructor} • ${course.students} students
</div>
</div>
</div>
`
}
// Check if user is enrolled in course
function isUserEnrolled(userId, courseId) {
const users = getFromStorage("edumorph_users") || []
const user = users.find((u) => u.id === userId)
if (!user || !user.enrolledCourses) return false
return user.enrolledCourses.some((course) => course.id === courseId)
}
// Enroll in course
function enrollInCourse(courseId) {
const user = getFromStorage("edumorph_user")
if (!user) {
showNotification("Please log in to enroll in courses", "info")
setTimeout(() => {
window.location.href = "login.html"
}, 2000)
return
}
const course = coursesData.find((c) => c.id === courseId)
if (!course) {
showNotification("Course not found", "error")
return
}
// Update user data
const users = getFromStorage("edumorph_users") || []
const userIndex = users.findIndex((u) => u.id === user.id)
if (userIndex === -1) {
showNotification("User not found", "error")
return
}
// Check if already enrolled
if (!users[userIndex].enrolledCourses) {
users[userIndex].enrolledCourses = []
}
const alreadyEnrolled = users[userIndex].enrolledCourses.some((c) => c.id === courseId)
if (alreadyEnrolled) {
showNotification("You are already enrolled in this course", "info")
return
}
// Add course to user's enrolled courses
const enrolledCourse = {
...course,
enrolledAt: new Date().toISOString(),
progress: 0,
studyTime: 0,
lastAccessed: new Date().toISOString(),
}
users[userIndex].enrolledCourses.push(enrolledCourse)
// Save updated data
saveToStorage("edumorph_users", users)
showNotification(`Successfully enrolled in ${course.title}!`, "success")
// Reload courses to update UI
setTimeout(() => {
loadCourses()
}, 1000)
}
// Continue course
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)
}
// Load more courses
function loadMoreCourses() {
currentPage++
loadCourses()
}
// Update load more button
function updateLoadMoreButton(hasMore) {
const loadMoreBtn = document.getElementById("load-more-btn")
if (!loadMoreBtn) return
if (hasMore) {
loadMoreBtn.style.display = "block"
loadMoreBtn.textContent = "Load More Courses"
} else {
loadMoreBtn.style.display = "none"
}
}
// Clear all filters
function clearFilters() {
const categoryFilter = document.getElementById("category-filter")
const levelFilter = document.getElementById("level-filter")
const durationFilter = document.getElementById("duration-filter")
const searchInput = document.getElementById("course-search")
if (categoryFilter) categoryFilter.value = ""
if (levelFilter) levelFilter.value = ""
if (durationFilter) durationFilter.value = ""
if (searchInput) searchInput.value = ""
applyFilters()
}
// Debounce function for search
function debounce(func, wait) {
let timeout
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout)
func(...args)
}
clearTimeout(timeout)
timeout = setTimeout(later, wait)
}
}
// 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
}
}
function saveToStorage(key, data) {
if (window.EduMorph && window.EduMorph.saveToStorage) {
return window.EduMorph.saveToStorage(key, data)
}
try {
localStorage.setItem(key, JSON.stringify(data))
return true
} catch (error) {
console.error("Error saving to storage:", error)
return false
}
}