|
1 | 1 | $(function(){
|
2 |
| - $('#testDiv').html("hi mom"); |
3 |
| - $('#example').DataTable(); |
4 |
| - $('#calendarField').datepicker( { dateFormat: 'yy-mm-dd' } ); |
| 2 | + console.log("got here"); |
| 3 | + $.get("plans/1.json", function(data){ |
| 4 | + console.log(data); |
| 5 | + }); |
5 | 6 | });
|
| 7 | + |
| 8 | +class Course { |
| 9 | + constructor(desig, year, term){ |
| 10 | + this.term = term; |
| 11 | + this.year = year; |
| 12 | + this.id = desig; |
| 13 | + this.name = catalog.courses[desig].name; |
| 14 | + this.hours = catalog.courses[desig].credits; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +class Plan { |
| 19 | + constructor(student, name, major, currYr, currTerm, courses, cat_yr){ |
| 20 | + this.name = name; |
| 21 | + this.catalogYear = cat_yr; |
| 22 | + this.major = major; |
| 23 | + this.studentName = student; |
| 24 | + this.currTerm = currTerm; |
| 25 | + this.currYear = currYr; |
| 26 | + if (currTerm === "Fall"){ |
| 27 | + currYear++; |
| 28 | + } |
| 29 | + this.courses = courses; |
| 30 | + this.years = []; |
| 31 | + this.hrsCompleted = 0; |
| 32 | + this.hrsCurrent = 0; |
| 33 | + this.hrsPlanned = 0; |
| 34 | + } |
| 35 | + |
| 36 | + sortCourses(){ |
| 37 | + for(let c in this.courses){ |
| 38 | + let yrNum = this.courses[c].year; |
| 39 | + if (this.courses[c].term === "Fall"){ |
| 40 | + yrNum++; |
| 41 | + } |
| 42 | + let course = new Course(c, yrNum, this.courses[c].term); |
| 43 | + let year = this.years.find(x => x.name === yrNum); |
| 44 | + if(typeof year === 'undefined'){ |
| 45 | + year = new Year(yrNum); |
| 46 | + this.years.push(year); |
| 47 | + } |
| 48 | + year.addToYear(course); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + generateHTML(){ |
| 53 | + this.years.sort((a, b) => (a.name > b.name) ? 1 : -1); |
| 54 | + let urHTML = "<header class='panelHeader'>Course Schedule</header><div class='container'>"; |
| 55 | + var beforeCurrent = true; |
| 56 | + for (let i = 0; i < this.years.length; i++){ |
| 57 | + let year = this.years[i]; |
| 58 | + urHTML += "<div class='year'>"; |
| 59 | + urHTML += "<div class='term"; |
| 60 | + if (beforeCurrent){ |
| 61 | + if (year.name === this.currYear && this.currTerm === "Fall"){ |
| 62 | + this.hrsCurrent += year.fallHrs; |
| 63 | + urHTML += " current"; |
| 64 | + beforeCurrent = false; |
| 65 | + } |
| 66 | + } |
| 67 | + else{ |
| 68 | + urHTML += " notStarted"; |
| 69 | + } |
| 70 | + urHTML += "'>"; |
| 71 | + urHTML += "<header><span class='termHeader'>Fall " + (year.name - 1) + "</span><span class='termHours'>Hours: " + year.fallHrs + "</span></header>"; |
| 72 | + urHTML += "<ul class='courses'>"; |
| 73 | + for (let j = 0; j < year.fall.length; j++){ |
| 74 | + let course = year.fall[j]; |
| 75 | + this.hrsPlanned += course.hours; |
| 76 | + if (beforeCurrent){ |
| 77 | + this.hrsCompleted += course.hours; |
| 78 | + } |
| 79 | + urHTML += "<li>" + course.id + " " + course.name + "</li>"; |
| 80 | + } |
| 81 | + urHTML += "</ul></div>"; |
| 82 | + |
| 83 | + urHTML += "<div class='term"; |
| 84 | + if (beforeCurrent){ |
| 85 | + if (year.name === this.currYear && this.currTerm === "Spring"){ |
| 86 | + this.hrsCurrent += year.springHrs; |
| 87 | + urHTML += " current"; |
| 88 | + beforeCurrent = false; |
| 89 | + } |
| 90 | + } |
| 91 | + else{ |
| 92 | + urHTML += " notStarted"; |
| 93 | + } |
| 94 | + urHTML += "'>"; |
| 95 | + urHTML += "<header><span class='termHeader'>Spring " + year.name + "</span><span class='termHours'>Hours: " + year.springHrs + "</span></header>"; |
| 96 | + urHTML += "<ul class='courses'>"; |
| 97 | + for (let j = 0; j < year.spring.length; j++){ |
| 98 | + let course = year.spring[j]; |
| 99 | + this.hrsPlanned += course.hours; |
| 100 | + if (beforeCurrent){ |
| 101 | + this.hrsCompleted += course.hours; |
| 102 | + } |
| 103 | + urHTML += "<li>" + course.id + " " + course.name + "</li>"; |
| 104 | + } |
| 105 | + urHTML += "</ul></div>"; |
| 106 | + |
| 107 | + urHTML += "<div class='term"; |
| 108 | + if (beforeCurrent){ |
| 109 | + if (year.name === this.currYear && this.currTerm === "Summer"){ |
| 110 | + this.hrsCurrent += year.summerHrs; |
| 111 | + urHTML += " current"; |
| 112 | + beforeCurrent = false; |
| 113 | + } |
| 114 | + } |
| 115 | + else{ |
| 116 | + urHTML += " notStarted"; |
| 117 | + } |
| 118 | + urHTML += "'>"; |
| 119 | + urHTML += "<header><span class='termHeader'>Summer " + year.name + "</span><span class='termHours'>Hours: " + year.summerHrs + "</span></header><ul class='courses'>"; |
| 120 | + for (let j = 0; j < year.summer.length; j++){ |
| 121 | + let course = year.summer[j]; |
| 122 | + this.hrsPlanned += course.hours; |
| 123 | + if (beforeCurrent){ |
| 124 | + this.hrsCompleted += course.hours; |
| 125 | + } |
| 126 | + urHTML += "<li>" + course.id + " " + course.name + "</li>"; |
| 127 | + } |
| 128 | + urHTML += "</ul></div></div>"; |
| 129 | + } |
| 130 | + urHTML += "</div>"; |
| 131 | + var upperRight = $("#upperRight").html(urHTML); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +class Year { |
| 136 | + constructor(name) { |
| 137 | + this.name = name; |
| 138 | + this.fall = []; |
| 139 | + this.fallHrs = 0; |
| 140 | + this.spring = []; |
| 141 | + this.springHrs = 0; |
| 142 | + this.summer = []; |
| 143 | + this.summerHrs = 0; |
| 144 | + |
| 145 | + this.maxCourses = 8; |
| 146 | + this.maxHrs = 30; |
| 147 | + } |
| 148 | + |
| 149 | + addToYear(course){ |
| 150 | + if (course.term === "Fall"){ |
| 151 | + if (this.fallHrs + course.hours > this.maxHrs || this.fall.length > this.maxCourses){ |
| 152 | + alert("You ain't got time for that, fool!"); |
| 153 | + } |
| 154 | + else{ |
| 155 | + this.fall.push(course); |
| 156 | + this.fallHrs += course.hours; |
| 157 | + } |
| 158 | + } |
| 159 | + else if (course.term === "Spring"){ |
| 160 | + if (this.springHrs + course.hours > this.maxHrs || this.spring.length > this.maxCourses){ |
| 161 | + alert("You ain't got time for that, fool!"); |
| 162 | + } |
| 163 | + else{ |
| 164 | + this.spring.push(course); |
| 165 | + this.springHrs += course.hours; |
| 166 | + } |
| 167 | + } |
| 168 | + else { |
| 169 | + if (this.summerHrs + course.hours > this.maxHrs || this.summer.length > this.maxCourses){ |
| 170 | + alert("You ain't got time for that, fool!"); |
| 171 | + } |
| 172 | + else { |
| 173 | + this.summer.push(course); |
| 174 | + this.summerHrs += course.hours; |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
| 180 | +var currPlan = false; |
| 181 | +var catalog = false; |
| 182 | +var planRequest = false; |
| 183 | +var accordionRequest = false; |
| 184 | +var selectedMajor = "Comp. Sci."; |
| 185 | +var selectedCatalogYear = 2017; |
| 186 | +var catalogLoaded = false; |
| 187 | + |
| 188 | +function createRequest(){ |
| 189 | + let request = false; |
| 190 | + if(window.XMLHttpRequest && !(window.ActiveXObject)){ |
| 191 | + try{ |
| 192 | + request = new XMLHttpRequest(); |
| 193 | + } |
| 194 | + catch(e){ |
| 195 | + request = false; |
| 196 | + } |
| 197 | + } |
| 198 | + else if(window.ActiveXObject){ |
| 199 | + try{ |
| 200 | + request = new ActiveXObject("Msxml2.XMLHTTP"); |
| 201 | + } |
| 202 | + catch(e){ |
| 203 | + try{ |
| 204 | + request = new ActiveXObject("Microsoft.XMLHTTP"); |
| 205 | + } |
| 206 | + catch(e){ |
| 207 | + request = false; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + return request; |
| 212 | +} |
| 213 | + |
| 214 | +function sendRequest(Method, URL, callback) { |
| 215 | + request = createRequest(); |
| 216 | + if (request){ |
| 217 | + request.open(Method, URL, true); |
| 218 | + request.onreadystatechange = callback; |
| 219 | + request.send(null); |
| 220 | + } |
| 221 | + return request; |
| 222 | +} |
| 223 | + |
| 224 | +function planCallback(){ |
| 225 | + if (planRequest.readyState == 4){ |
| 226 | + var jsonResponse = JSON.parse(planRequest.responseText); |
| 227 | + var plans = jsonResponse.plan; |
| 228 | + catalog = jsonResponse.catalog; |
| 229 | + var planObject = false; |
| 230 | + $(".dropdown-content").empty(); |
| 231 | + for (let i in plans){ |
| 232 | + if (plans[i].major === selectedMajor && plans[i].catalog_year === selectedCatalogYear){ |
| 233 | + planObject = plans[i]; |
| 234 | + } |
| 235 | + else{ |
| 236 | + // Put other plan options in dropdown menu on nav bar |
| 237 | + $(".dropdown-content").append("<a onclick='changePlan(this.text)'>" + plans[i].major + ", " + plans[i].catalog_year + "</a>"); |
| 238 | + } |
| 239 | + } |
| 240 | + if (planObject === false){ |
| 241 | + console.log("Error: did not find selected plan"); |
| 242 | + } |
| 243 | + |
| 244 | + currPlan = new Plan(planObject.student, planObject.plan_name, planObject.major, planObject.currYear, planObject.currTerm, planObject.courses, planObject.catalog_year); |
| 245 | + currPlan.sortCourses(); |
| 246 | + currPlan.generateHTML(); |
| 247 | + $("#username").html(planObject.student); |
| 248 | + $("#major").html(planObject.major); |
| 249 | + $("#catYear").html(planObject.catalog_year); |
| 250 | + |
| 251 | + $("#hrsCompleted").html("Hours Completed: " + currPlan.hrsCompleted); |
| 252 | + $("#hrsCurrent").html("Current Hours: " + currPlan.hrsCurrent); |
| 253 | + $("#hrsPlanned").html("Total Hours Planned: " + currPlan.hrsPlanned); |
| 254 | + |
| 255 | + let courses = []; |
| 256 | + for(let i in catalog.courses) { |
| 257 | + courses.push(catalog.courses[i]); |
| 258 | + } |
| 259 | + |
| 260 | + if (!catalogLoaded) { |
| 261 | + $("#catalogTable").DataTable( { |
| 262 | + "dom": '<"top"if>t', |
| 263 | + "data": courses, |
| 264 | + "columns": [ |
| 265 | + { "data": "id" }, |
| 266 | + { "data": "name" }, |
| 267 | + { "data": "description" }, |
| 268 | + { "data": "credits"} |
| 269 | + ], |
| 270 | + "scrollY": "95px", |
| 271 | + "paging": false, |
| 272 | + "scrollCollapse": false |
| 273 | + }); |
| 274 | + $('.dataTables_scrollHeadInner').css('padding', '0'); |
| 275 | + catalogLoaded = true; |
| 276 | + } |
| 277 | + |
| 278 | + // Send accordion request here because we don't want to create the accordion until the catalog has been created |
| 279 | + accordionRequest = sendRequest("GET", 'http://judah.cedarville.edu/~jacobs/TermProject/php/getRequirements.php', accordionCallback); |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +function accordionCallback(){ |
| 284 | + if (accordionRequest.readyState == 4){ |
| 285 | + var accordionObjects = JSON.parse(accordionRequest.responseText); |
| 286 | + |
| 287 | + var categories = false; |
| 288 | + for (let i in accordionObjects){ |
| 289 | + if (accordionObjects[i].major === selectedMajor && accordionObjects[i].catalog_year === selectedCatalogYear){ |
| 290 | + categories = accordionObjects[i].categories; |
| 291 | + } |
| 292 | + } |
| 293 | + if (categories === false){ |
| 294 | + console.log("Error: did not find selected requirements"); |
| 295 | + } |
| 296 | + |
| 297 | + $('#accordion').empty(); |
| 298 | + for (let item in categories){ |
| 299 | + let courses = categories[item].courses; |
| 300 | + let itemHtml = ""; |
| 301 | + for (let c in courses){ |
| 302 | + itemHtml += '<li>' + courses[c] + ': ' + catalog.courses[courses[c]].name + '</li>'; |
| 303 | + } |
| 304 | + $('#accordion').append('<h3><a href="#">' + item + '</a></h3><div>' + itemHtml + '</div>').accordion('refresh'); |
| 305 | + } |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | + |
| 310 | +// changes plan, triggered on selection of new plan in dropdown |
| 311 | +function changePlan(selected){ |
| 312 | + console.log(selected); |
| 313 | + selected = selected.split(", "); |
| 314 | + console.log(selected); |
| 315 | + selectedMajor = selected[0]; |
| 316 | + selectedCatalogYear = parseInt(selected[1]); |
| 317 | + |
| 318 | + // send request to load new plan |
| 319 | + planRequest = sendRequest("GET", 'http://judah.cedarville.edu/~jacobs/TermProject/php/getCombined.php', planCallback); |
| 320 | + |
| 321 | +} |
0 commit comments