-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
634 lines (534 loc) · 21 KB
/
Copy pathserver.js
File metadata and controls
634 lines (534 loc) · 21 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const app = express();
const PORT = process.env.PORT || 3000;
const { ObjectId } = require('mongodb');
require('dotenv').config();
let dbConnectionStr = process.env.DB_STRING;
let classesCollection;
let student1Collection;
let db;
let selectedStudent = '10289';
app.set('view engine', 'ejs');
//DATA BASE CONNECTION
MongoClient.connect(dbConnectionStr, { useUnifiedTopology: true })
.then(client => {
console.log(`Connected to Database`);
const database = client.db('CourseTimeline');
classesCollection = database.collection('classes');
student1Collection = database.collection('student1');
majorsCollection = database.collection('majors');
minorsCollection = database.collection('minors');
concentrationsCollection = database.collection('concentration');
db = database;
})
.catch(error => {
console.error('Error connecting to the database:', error);
});
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Define a function to fetch the currentCourses for the selected student
async function fetchCurrentCourses() {
try {
if (!selectedStudent) {
return [];
}
const student = await student1Collection.findOne({ studentId: selectedStudent });
if (student && student.currentCourses) {
const currentCourseIds = student.currentCourses;
// Perform a lookup to retrieve class/course names based on _id values
const currentCourses = await classesCollection
.find({ _id: { $in: currentCourseIds } })
.toArray();
return currentCourses.map(course => course.courseNumber + ": " + course.courseTitle);
} else {
return [];
}
} catch (error) {
throw error;
}
}
// Define the function to calculate progress
async function calculateProgress(studentId) {
try {
if (db) {
const student = await student1Collection.findOne({ studentId });
if (student) {
const major = student.major;
const coursesTaken = student.coursesTaken;
const courseData = await classesCollection.find({ _id: { $in: coursesTaken } }).toArray();
const totalCreditsFromCourses = courseData.reduce((total, course) => total + course.creditHours, 0);
const majorsCollection = db.collection('majors');
const majorData = await majorsCollection.findOne({ name: major });
if (majorData) {
const creditsNeededForCompletion = majorData.creditsNeededForCompletion;
const progress = ((totalCreditsFromCourses / creditsNeededForCompletion) * 100).toFixed(2);
return progress;
}
}
}
return null;
} catch (error) {
console.error(`Error: ${error}`);
throw error;
}
}
async function getUserFirstName(studentId) {
try {
const student = await student1Collection.findOne({ studentId });
// Check if the student exists and has a first name
if (student && student.name) {
return student.name;
} else {
return null; // Return null if the student or their first name is not found
}
} catch (error) {
// Handle errors here
console.error('Error retrieving student name:', error.message);
return null;
}
}
// HOME PAGE
app.get('/', async (req, res) => {
try {
// Fetch the progress and currentCourses for the selected student
const progress = await calculateProgress(selectedStudent);
const currentCourses = await fetchCurrentCourses();
const firstName = await getUserFirstName(selectedStudent);
if (progress !== null) {
res.render('index', { progress, currentCourses,firstName });
} else {
res.status(404).send('Student not found or data not available');
}
} catch (error) {
console.error(`Error: ${error}`);
res.status(500).send('Internal Server Error');
}
});
// PLAN AHEAD PAGE
app.get('/planAhead', async (req, res) => {
try {
if (!selectedStudent) {
res.status(400).send('No student selected');
return;
}
const student = await student1Collection.aggregate([
{
$match: { studentId: selectedStudent }
},
{
$lookup: {
from: 'classes',
localField: 'coursesTaken',
foreignField: '_id',
as: 'coursesTaken'
}
},
{
$lookup: {
from: 'classes',
localField: 'currentCourses',
foreignField: '_id',
as: 'currentCourses'
}
}
]).toArray();
if (student.length === 0) {
res.status(404).send('Student not found');
return;
}
const allClasses = await classesCollection.aggregate([
{
$match: { coreCourse: true }
}
]).toArray();
const studentCoursesTaken = student[0].coursesTaken;
const studentCurrentCourses = student[0].currentCourses;
const totalCreditsTaken = studentCoursesTaken.reduce((total, course) => total + course.creditHours, 0);
const totalCreditsCurrent = studentCurrentCourses.reduce((total, course) => total + course.creditHours, 0);
const coursesNotTaken = allClasses.filter(course => {
return !studentCoursesTaken.some(c => c._id.toString() === course._id.toString()) && !studentCurrentCourses.some(c => c._id.toString() === course._id.toString());
});
const studentWithCourses = {
name: student[0].name,
creditsCompleted: totalCreditsTaken + totalCreditsCurrent,
coursesTaken: studentCoursesTaken,
currentCourses: studentCurrentCourses,
coursesNotTaken: coursesNotTaken
};
res.render('planAhead', { student: studentWithCourses });
} catch (error) {
console.error('Error:', error);
res.status(500).send('Internal Server Error');
}
});
async function fetchAllCourses() {
try {
const allClasses = await classesCollection.find({}, { courseNumber: 1, creditHours: 1 }).toArray();
return allClasses;
} catch (error) {
throw error;
}
}
let currentCourses = [];
// PLANNER PAGE
app.get('/planner', async (req, res) => {
try {
// Define coursesNotTaken as an empty array
let coursesNotTaken = [];
let activeCourses = [];
// Populate coursesNotTaken based on the conditions in your GET route
const student = await student1Collection.findOne({ studentId: selectedStudent });
if (student) {
const allClasses = await fetchAllCourses();
coursesNotTaken = allClasses.filter(course => {
if (!student.coursesTaken.includes(course._id) && !student.currentCourses.includes(course._id)) {
// Check if the student has all prerequisites for this course
const prerequisitesMet = course.prerequisites.every(prereq => {
const met = student.currentCourses.includes(prereq) || student.coursesTaken.includes(prereq);
return met;
});
return prerequisitesMet && course.coreCourse;
}
return false;
});
}
if (student) {
let currentCourses = student.currentCourses;
const allClasses = await fetchAllCourses();
// Find active courses that are in currentCourses
activeCourses = allClasses.filter(course => currentCourses.includes(course._id));
const recommendations = [];
activeCourses.forEach(activeCourse => {
if (activeCourse.nextCourses && Array.isArray(activeCourse.nextCourses)) {
const activeCourseRecommendations = allClasses.filter(course =>
course._id !== activeCourse._id && // Exclude the active course
activeCourse.nextCourses.includes(course._id) &&
!currentCourses.includes(course._id) && // Check if the course is not in currentCourses
!student.coursesTaken.includes(course._id) // Check if the course is not in coursesTaken
);
if (activeCourseRecommendations.length > 0) {
recommendations.push({
course: activeCourse,
recommendedCourses: activeCourseRecommendations,
});
}
}
});
currentCourses = [];
// Render the 'planner' template and pass the data
res.render('planner', {
coursesNotTaken,
currentCourses,
activeCourses,
recommendations,
});
} else {
// Handle the case where the student is not found
res.status(404).send('Student not found');
}
} catch (error) {
console.error('Error:', error);
res.status(500).send('Internal Server Error');
}
});
app.post('/planner', async (req, res) => {
try {
// Define coursesNotTaken as an empty array
let coursesNotTaken = [];
//let activeCourses = [];
let recommendations = [];
const selectedCourseIds = req.body.selectedCourses;
if (!selectedCourseIds || (!Array.isArray(selectedCourseIds) && !Array.isArray([selectedCourseIds]))) {
res.status(400).send('Invalid course selections');
return;
}
// Fetch all courses, including the creditHours field
const allClasses = await fetchAllCourses();
// Calculate selectedCourses
const selectedCourses = allClasses.filter(course => selectedCourseIds.includes(course._id));
// Retrieve the current courses from the hidden input field
currentCourses = JSON.parse(req.body.currentCourses);
// Pass 'coursesNotTaken' and 'currentCourses' when rendering the template
res.render('planner', {
coursesNotTaken: coursesNotTaken,
selectedCourses: selectedCourses,
currentCourses: currentCourses.concat(selectedCourses),
recommendations: recommendations,
});
} catch (error) {
console.error('Error:', error);
res.status(500).send('Internal Server Error');
}
});
// DISCOVER PAGE
async function getPrerequisiteCourseNumbers(prerequisiteIds) {
if (!prerequisiteIds || prerequisiteIds.length === 0) {
return [];
}
const prerequisiteCourses = await classesCollection.find({ _id: { $in: prerequisiteIds } }).toArray();
return prerequisiteCourses.map(course => course.courseNumber);
}
app.get('/api/search', async (req, res) => {
const query = req.query.q || '';
let results = [];
if (query) {
results = await classesCollection.find({ "courseNumber": new RegExp(query, 'i') }).toArray();
} else {
results = await classesCollection.find().toArray();
}
// Fetch prerequisites for each course
for (let course of results) {
if (course.prerequisites && course.prerequisites.length > 0) {
console.log("Before processing: ", course.prerequisites);
const prerequisitesCourses = await classesCollection.find({ _id: { $in: course.prerequisites.map(id => id) } }).toArray();
course.prerequisites = prerequisitesCourses.map(course => course.courseNumber);
console.log("After processing: ", course.prerequisites);
}
}
res.json(results);
});
app.get('/discover', async (req, res) => {
let results = await classesCollection.find().toArray();
// For each course in results
for (let course of results) {
if (course.prerequisites && course.prerequisites.length > 0) {
// Fetch the course numbers for each prerequisite
const prerequisiteCourses = await getPrerequisiteCourseNumbers(course.prerequisites);
course.prerequisites = prerequisiteCourses;
}
}
res.render('discover', { results });
});
// WHAT IF PAGE
app.get('/whatIf', async (req, res) => {
try {
// Initialize the variables to empty objects
const selectedMajorInfo = {};
const selectedMinorInfo = {};
const selectedConcentrationInfo = {};
// Render the 'whatIf' template and pass the initialized variables
res.render('whatIf', {
selectedMajorInfo,
selectedMinorInfo,
selectedConcentrationInfo
});
} catch (error) {
console.error('Error:', error);
res.status(500).send('Internal Server Error');
}
});
app.post('/whatIf', async (req, res) => {
try {
// Retrieve the selected major, minor, and concentration from the form
const selectedMajor = req.body.major;
const selectedMinor = req.body.minor;
const selectedConcentration = req.body.concentration;
// Initialize the variables to empty objects
const selectedMajorInfo = {};
const selectedMinorInfo = {};
const selectedConcentrationInfo = {};
////////////////////////////////////////////// MAJOR ///////////////////////////////////////////
const majorInfo = await majorsCollection.findOne({ name: selectedMajor });
if (majorInfo) {
selectedMajorInfo.name = majorInfo.name;
selectedMajorInfo.creditsNeededForCompletion = majorInfo.creditsNeededForCompletion;
selectedMajorInfo.description = majorInfo.description;
}
////////////////////////////////////////////// MINOR ///////////////////////////////////////////
const minorInfo = await minorsCollection.findOne({ name: selectedMinor });
const student = await student1Collection.findOne({ studentId: selectedStudent });
const completedCourseIds = student.coursesTaken;
let remainingMinorCourses = [];
let remainingMinorElectives = [];
if (minorInfo) {
selectedMinorInfo.name = minorInfo.name;
selectedMinorInfo.totalCredits = minorInfo.totalCredits;
selectedMinorInfo.description = minorInfo.description;
const requiredCourseIds = minorInfo.requiredCourses;
const requiredCourses = await Promise.all(
requiredCourseIds.map(async (courseId) => {
const course = await classesCollection.findOne({ _id: courseId });
return course ? { _id: course._id.toString(), number: course.courseNumber, title: course.courseTitle } : null;
})
);
selectedMinorInfo.requiredCourses = requiredCourses.filter(Boolean);
const electiveIds = minorInfo.electives;
const electives = await Promise.all(
electiveIds.map(async (electiveId) => {
const elective = await classesCollection.findOne({ _id: electiveId });
return elective ? { _id: elective._id.toString(), number: elective.courseNumber, title: elective.courseTitle } : null;
})
);
selectedMinorInfo.electives = electives.filter(Boolean);
const completedRequiredCourses = selectedMinorInfo.requiredCourses.filter(course =>
completedCourseIds.includes(course._id)
);
const completedElectives = selectedMinorInfo.electives.filter(course =>
completedCourseIds.includes(course._id)
);
selectedMinorInfo.completedCourses = {
requiredCourses: completedRequiredCourses,
electives: completedElectives
};
remainingMinorCourses = selectedMinorInfo.requiredCourses.filter(course =>
!completedCourseIds.includes(course._id)
);
selectedMinorInfo.remainingCourses = remainingMinorCourses;
// console.log("-----remaining main-----",remainingMinorCourses);
remainingMinorElectives = selectedMinorInfo.electives.filter(elective =>
!completedCourseIds.includes(elective._id)
);
selectedMinorInfo.remainingElectives = remainingMinorElectives;
// console.log("-----remaining electives-----",remainingMinorElectives);
}
////////////////////////////////////////////// CONCENTRATION ///////////////////////////////////////////
const concentrationInfo = await concentrationsCollection.findOne({ name: selectedConcentration });
if (concentrationInfo) {
selectedConcentrationInfo.name = concentrationInfo.name;
selectedConcentrationInfo.description = concentrationInfo.description;
selectedConcentrationInfo.totalCredits = concentrationInfo.totalCredits;
const requiredCourseId = concentrationInfo.requiredCourse;
const requiredCourse = await classesCollection.findOne({ _id: requiredCourseId });
const electiveIds = concentrationInfo.electives;
const electives = await Promise.all(
electiveIds.map(async (electiveId) => {
const elective = await classesCollection.findOne({ _id: electiveId });
return elective ? { _id: elective._id.toString(), number: elective.courseNumber, title: elective.courseTitle } : null;
})
);
selectedConcentrationInfo.requiredCourse = requiredCourse ? { _id: requiredCourse._id.toString(), number: requiredCourse.courseNumber, title: requiredCourse.courseTitle } : null;
selectedConcentrationInfo.electives = electives.filter(Boolean);
remainingConcentrationElectives = selectedConcentrationInfo.electives.filter(elective =>
!completedCourseIds.includes(elective._id)
);
selectedConcentrationInfo.remainingElectives = remainingConcentrationElectives;
let completedRequiredCourses = [];
if (completedCourseIds.includes(selectedConcentrationInfo.requiredCourse._id)) {
completedRequiredCourses.push(selectedConcentrationInfo.requiredCourse);
}
const completedElectives = electives.filter(course =>
completedCourseIds.includes(course._id)
);
selectedConcentrationInfo.completedCourses = {
requiredCourses: completedRequiredCourses,
electives: completedElectives
};
if (completedCourseIds.includes(selectedConcentrationInfo.requiredCourse._id)) {
selectedConcentrationInfo.remainingCourse = false;
}
else {
selectedConcentrationInfo.remainingCourse = true;
}
}
// Render the 'whatIf' template and pass the fetched information
res.render('whatIf', {
selectedMajorInfo,
selectedMinorInfo,
selectedConcentrationInfo
});
} catch (error) {
console.error('Error:', error);
res.status(500).send('Internal Server Error');
}
});
//PLAN OF STUDY PAGE
async function fetchCurrentAndCompletedCourses(selectedStudent) {
try {
// Check if a student is selected
if (!selectedStudent) {
return [];
}
// Fetch the student's data based on their studentId
const student = await student1Collection.findOne({ studentId: selectedStudent });
if (student) {
const currentCourseIds = student.currentCourses || [];
const completedCourseIds = student.coursesTaken || [];
// Fetch current and completed courses
const currentCourses = await classesCollection
.find({ _id: { $in: currentCourseIds.concat(completedCourseIds) } })
.toArray();
return currentCourses.map(course => course.courseNumber);
} else {
return [];
}
} catch (error) {
throw error;
}
}
function recommendCourseProgression(allCourses, completedCourses) {
const courseProgression = [];
const semesters = ['Fall 2023', 'Spring 2024', 'Fall 2024', 'Spring 2025', 'Fall 2025', 'Spring 2026', 'Fall 2026', 'Spring 2027'];
let currentSemester = 0; // Index to keep track of the current semester
while (currentSemester < semesters.length) {
const semester = semesters[currentSemester];
let availableCourses = allCourses.filter(course => {
// Filter out completed courses
if (completedCourses.includes(course._id)) {
return false;
}
if (course.coreCourse === false) {
return false;
}
// Filter out courses with unmet prerequisites
if (course.prerequisites) {
if (course.prerequisites.some(prereq => !completedCourses.includes(prereq))) {
return false;
}
}
// Prioritize courses that are in the nextCourses of completed courses
if (course.nextCourses) {
if (course.nextCourses.some(nextCourse => completedCourses.includes(nextCourse))) {
return true;
}
}
// Include other courses
return true;
});
// Handle alternatives
const selectedCourses = [];
const alternativeSet = new Set();
availableCourses = availableCourses.filter(course => {
const alternativeId = course.alternativeTo;
if (alternativeId) {
// Skip alternative course if the primary course or another alternative has been selected
if (completedCourses.includes(course._id) || alternativeSet.has(alternativeId)) {
return false;
}
// Mark the alternative as selected
alternativeSet.add(alternativeId);
}
// Include other courses
return true;
});
let totalCreditHours = 0;
for (const course of availableCourses) {
if (totalCreditHours + course.creditHours <= 12) {
// Add the course to this semester
totalCreditHours += course.creditHours;
selectedCourses.push(course);
completedCourses.push(course._id);
}
}
// Add the semester and its courses to courseProgression
courseProgression.push({ semester, courses: selectedCourses });
// Move on to the next semester
currentSemester++;
}
return courseProgression;
}
app.get('/planOfStudy', async (req, res) => {
try {
const allCourses = await fetchAllCourses();
const currentAndCompletedCourses = await fetchCurrentAndCompletedCourses(selectedStudent);
const courseProgression = recommendCourseProgression(allCourses, currentAndCompletedCourses);
res.render('planOfStudy', { courseProgression });
} catch (error) {
console.error('Error fetching courses:', error);
res.status(500).send('Internal Server Error');
}
});
app.listen(process.env.PORT || PORT, () => {
console.log(`Server running on port ${PORT}`);
});