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

Update YacsService.js #627

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions src/web/src/services/YacsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ export const getCourses = (semester, search = null, filter = true) =>
});
return filter ? courses.filter((c) => c.sections.length !== 0) : courses;
});
/**
* Input is the full list of courses and a list of filters,
* of size 5, where (except 0) forbiddenTimes[i] is a list
* of filtered times on day i, each filtered time being a list of time
* strings
*
* Returns a list of all courses that pass the time filter
* @returns {Course[]}
*/
export const filterCoursesByTime = (courses, forbiddenTimes) => {
if(forbiddenTimes.length === 0 || courses.length === 0) {
return courses;
}

return courses.filter((c) => {
let ov = overlaps(c, forbiddenTimes);
return (c.sections.length === 0 || (ov === false));
});
}
/**
* Returns a list of all departments
* @returns {Promise<Department>}
Expand Down Expand Up @@ -148,3 +167,29 @@ export const removeStudentCourse = (course_info) =>

export const getStudentCourses = () =>
client.get("/user/course").then((res) => res.data);

const overlaps = (course, filter) => {
for(const section of course.sections) {
let is_valid = true;

for(const session of section.sessions) {
for(const time of filter[session.day_of_week]) {
if(session.time_start <= time[1] && session.time_end >= time[0]) {
is_valid = false;
break;
}
}
if(is_valid === false) {
break;
}
}

//Found a valid section, therefore this course does not completely overlap
if(is_valid === true) {
return false;
}
}

//A course overlaps if there are no valid sections
return true;
};