Last active
December 26, 2015 09:58
-
-
Save ravenjohn/7132860 to your computer and use it in GitHub Desktop.
Conflict checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(root){ | |
/** | |
* Section Object | |
* | |
* @param object from the database | |
*/ | |
root.Section = function (o) { | |
var _s = this, | |
initMeta = function (a, b, c, d, e) { | |
return { | |
name : a, | |
instructor : b, | |
room : c, | |
raw_days : d, | |
raw_time : e, | |
days : d.replace("Mon", "M") | |
.replace(/Tue(s?)/g, "T") | |
.replace("Wed", "W") | |
.replace(/Thurs|Th/g, "H") //yes, it's Hursday not Thursday | |
.replace("Fri", "F") | |
.replace("Sat", "S"), | |
time : e.toRangeTime(), | |
toString : function () { | |
return [_s.courseCode, this.name, this.raw_days, this.raw_time].join(" "); | |
} | |
}; | |
}; | |
_s.scheds = []; | |
_s.units = o.units; | |
_s.id = o.sectionId; | |
_s.courseCode = o.courseCode; | |
_s.sectionName = o.sectionName; | |
_s.offeringUnit = o.offeringUnit; | |
_s.capacity = o.numberOfStudents; | |
o.daysLecture && _s.scheds.push(initMeta("lecture", o.lectureInstructor, o.roomLecture, o.daysLecture, o.timeLecture)); | |
o.daysLecture2 && _s.scheds.push(initMeta("lecture2", o.lectureInstructor, o.roomLecture2, o.daysLecture2, o.timeLecture2)); | |
o.daysRecitation && _s.scheds.push(initMeta("recitation", o.recitationInstructor, o.roomRecitation, o.daysRecitation, o.timeRecitation)); | |
o.daysLaboratory && _s.scheds.push(initMeta("laboratory", o.laboratoryInstructor, o.roomLaboratory, o.daysLaboratory, o.timeLaboratory)); | |
o.daysRecitation2 && _s.scheds.push(initMeta("recitation2", o.recitationInstructor, o.roomRecitation2, o.daysRecitation2, o.timeRecitation2)); | |
o.daysLaboratory2 && _s.scheds.push(initMeta("laboratory2", o.laboratoryInstructor, o.roomLaboratory2, o.daysLaboratory2, o.timeLaboratory2)); | |
}; | |
/** | |
* To Range Time | |
* converts time(string) to range(int, int) for | |
* easy time comparison. | |
* | |
* @return array containing 2 integers [startTime, endTime] | |
* @example: | |
"7-9".toRangeTime(); // [0, 200] | |
"7-12:30".toRangeTime(); // [0,530] | |
"7-12:30".toRangeTime(); // [1000,1200] | |
* | |
*/ | |
String.prototype.toRangeTime = function () { | |
var s = this.split("-"), | |
t = function (a, m) { | |
var b = +(a = a.split(":"))[0]; | |
a[0] = b + ((b > 7 || (b === 7 && m)) ? -7 : 5); | |
return +(a.length > 1 ? a.join("") : a[0] * 100); | |
}; | |
if (this === "TBA") { | |
return this; | |
} | |
return [t(s[0], 1), t(s[1])]; | |
}; | |
/** | |
* Has Conflict | |
* checks if section1 has a conflict on section2 | |
* | |
* @param s1 (Section Object) | |
* @param s2 (Section Object) | |
* @return false if there's no conflict, else string | |
*/ | |
root.hasConflict = function (s1, s2) { | |
var i, j, k, l, m, n; | |
// e.g. s1 = 8-10, s2 = 9-10 | |
for (i = 0, j = s1.scheds.length; i < j; i += 1){ //loop for schedules of the 1st section | |
for (k = 0, l = s2.scheds.length; k < l; k += 1){ //loop for schedules of the 2nd section | |
for (m = 0, n = s1.scheds[i].days.length; m < n; m += 1){ //loop for the days of the 1st section | |
if(~s2.scheds[k].days.indexOf(s1.scheds[i].days[m]) && // e.g. has both (M)ondays | |
( | |
(s1.scheds[i].time[0] >= s2.scheds[k].time[0] && s1.scheds[i].time[0] < s2.scheds[k].time[1]) || // e.g. 8am between s2's 9-10? | |
(s1.scheds[i].time[1] > s2.scheds[k].time[0] && s1.scheds[i].time[1] <= s2.scheds[k].time[1]) || // e.g. 10am between s2's 9-10? | |
(s1.scheds[i].time[0] <= s2.scheds[k].time[0] && s1.scheds[i].time[1] >= s2.scheds[k].time[1]) // e.g. 8-11am, 9-10am | |
) | |
){ | |
return s1.scheds[i] + " is in conflict with " + s2.scheds[k]; | |
} | |
} | |
} | |
} | |
return !1; | |
}; | |
}(this)); | |
// TESTING | |
// json data from systemone: | |
var data = [{"sectionId":"30780","courseCode":"HORT 232","sectionName":"B-1L","units":"3","numberOfStudents":"10","daysLaboratory":"Tues","daysLecture":"Thurs","daysRecitation":null,"timeLaboratory":"1-4","timeLecture":"8-10","timeRecitation":null,"roomLaboratory":"CSC LIB","roomLecture":"CSC LIB","roomRecitation":null,"laboratoryInstructor":"TBA","lectureInstructor":"MLO CEDO","recitationInstructor":null,"daysLaboratory2":null,"daysLecture2":null,"daysRecitation2":null,"timeLaboratory2":null,"timeLecture2":null,"timeRecitation2":null,"roomLaboratory2":null,"roomLecture2":null,"roomRecitation2":null,"status":"OK","showInstructor":"NONE","offeringUnit":"CSC"},{"sectionId":"32122","courseCode":"SOSC 4(SSP)","sectionName":"S-5R","units":"3","numberOfStudents":"25","daysLaboratory":null,"daysLecture":"MFTueTh","daysRecitation":"Wed","timeLaboratory":null,"timeLecture":"7-8:30","timeRecitation":"10-11:30","roomLaboratory":null,"roomLecture":"CAS A2 MPH2","roomRecitation":"CAS A2 102","laboratoryInstructor":null,"lectureInstructor":"AHT Castro","recitationInstructor":"AHT Castro","daysLaboratory2":null,"daysLecture2":null,"daysRecitation2":null,"timeLaboratory2":null,"timeLecture2":null,"timeRecitation2":null,"roomLaboratory2":null,"roomLecture2":null,"roomRecitation2":null,"status":"OK","showInstructor":"NONE","offeringUnit":"DSS"}], | |
i, | |
j, | |
result, | |
sections = []; | |
for (i = 0, j= data.length; i < j; i += 1) { | |
// create section | |
sections.push(new Section(data[i])); | |
} | |
result = hasConflict(sections[0], sections[1]); | |
// pag nag assertion failed, may mali sa code | |
console.assert(result === "HORT 232 lecture Thurs 8-10 is in conflict with SOSC 4(SSP) lecture MFTueTh 7-8:30"); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment