Skip to content

Commit 7ee3829

Browse files
Add and delete year added
1 parent 201ab01 commit 7ee3829

File tree

5 files changed

+223
-2
lines changed

5 files changed

+223
-2
lines changed

app/assets/stylesheets/project4.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,17 @@ table div, .dataTables_scrollHead, .dataTables_scrollHeadInner, .dataTables_scro
203203
#trash{
204204
font-size: 64px;
205205
}
206+
207+
.inlineForm {
208+
display: flex;
209+
flex-flow: row wrap;
210+
align-items: center;
211+
}
212+
213+
#numYears {
214+
width: 20px;
215+
}
216+
217+
#yearToDelete {
218+
width: 40px;
219+
}

app/javascript/packs/application.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,34 @@ require ('datatables.net-bs4')
2020
//
2121
const images = require.context('../../assets/images', true)
2222
const imagePath = (name) => images(name, true)
23+
24+
$(document).ready(function() {
25+
$("#addYear").on("click", function() {
26+
alert("Got into add year function");
27+
if (currPlan.years.length >= 12) {
28+
alert("You can only have a maximum of 12 years in a plan!");
29+
return;
30+
}
31+
32+
// let queryString = window.location.search;
33+
// let urlParams = new URLSearchParams(queryString);
34+
// let numYears = urlParams.get("numYears");
35+
36+
let numYears = document.getElementById("numYears");
37+
38+
if (numYears > 12 || currPlan.years.length + numYears >= 12) {
39+
alert("This will put you over the maximum of 12 years per plan! Try again.");
40+
return;
41+
}
42+
43+
let lastTermHeaderInPlan = $(".termHeader").get(-1).split(" ");
44+
let lastYearInPlan = int (lastTermHeaderInPlan[1]);
45+
46+
for (let i = 0; i < numYears; i++) {
47+
currPlan.years.push(new Year(lastYearInPlan + i));
48+
}
49+
50+
currPlan.generateHTML();
51+
return;
52+
});
53+
});

app/javascript/packs/myjs.js

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ window.getPlan = function(){
8282
$("#hrsCurrent").html("Current Hours: " + currPlan.hrsCurrent);
8383
$("#hrsFuture").html("Remaining Hours: " + currPlan.hrsFuture);
8484
$("#hrsTotal").html("Total Hours Planned: " + currPlan.hrsTotal);
85+
checkMissingReqs();
8586

8687
// load catalog table
8788
let courses = [];
@@ -159,6 +160,156 @@ function checkMissingReqs() {
159160
}
160161
}
161162

163+
window.addYears = function() {
164+
alert("Got into add year function");
165+
if (currPlan.years.length >= 12) {
166+
alert("You can only have a maximum of 12 years in a plan!");
167+
return;
168+
}
169+
170+
// let queryString = window.location.search;
171+
// let urlParams = new URLSearchParams(queryString);
172+
// let numYears = urlParams.get("numYears");
173+
174+
let numYears = parseInt($("#numYears").val());
175+
176+
if (numYears > 12 || currPlan.years.length + numYears >= 12) {
177+
alert("This will put you over the maximum of 12 years per plan! Try again.");
178+
return;
179+
}
180+
181+
let lastTermHeaderInPlan = $(".termHeader").get(-1);
182+
let termAndYear = lastTermHeaderInPlan.textContent.split(" ");
183+
let lastYearInPlan = parseInt(termAndYear[1]);
184+
185+
for (let i = 1; i < numYears + 1; i++) {
186+
currPlan.years.push(new Year(lastYearInPlan + i));
187+
}
188+
189+
currPlan.generateHTML();
190+
191+
// $.post("/plan", {
192+
// plan: plan.plan_name,
193+
// user: plan.user.id,
194+
// designator: draggedCourse.designator,
195+
// term: event.target.children[0].children[0].innerText.split(" ")[0],
196+
// year: parseInt(event.target.children[0].children[0].innerText.split(" ")[1]),
197+
// });
198+
return;
199+
}
200+
201+
window.deleteYear = function() {
202+
let yearToDelete = parseInt($("#yearToDelete").val());
203+
let isValidYear = validateYear(yearToDelete);
204+
if (!isValidYear) {
205+
alert("That's not valid year! Try again.");
206+
return;
207+
}
208+
209+
deleteCoursesAndYear(yearToDelete);
210+
211+
$("#hrsCompleted").html("Hours Completed: " + currPlan.hrsCompleted);
212+
$("#hrsCurrent").html("Current Hours: " + currPlan.hrsCurrent);
213+
$("#hrsFuture").html("Remaining Hours: " + currPlan.hrsFuture);
214+
$("#hrsTotal").html("Total Hours Planned: " + currPlan.hrsTotal);
215+
currPlan.generateHTML();
216+
217+
}
218+
219+
function validateYear(year) {
220+
let isValid = false;
221+
for (let i = 0; i < currPlan.years.length; i++) {
222+
if (year == parseInt(currPlan.years[i].name)) {
223+
isValid = true;
224+
break;
225+
}
226+
}
227+
return isValid;
228+
}
229+
230+
function deleteCoursesAndYear(year) {
231+
let planYear = null;
232+
for (let i = 0; i < currPlan.years.length; i++) {
233+
if (year == parseInt(currPlan.years[i].name)) {
234+
planYear = currPlan.years[i];
235+
break;
236+
}
237+
}
238+
let fallCourses = planYear.fall;
239+
let springCourses = planYear.spring;
240+
let summerCourses = planYear.summer;
241+
242+
let coursesToRemove = fallCourses.concat(springCourses).concat(summerCourses);
243+
244+
let hoursRemoved = 0;
245+
let hoursCompletedRemoved = 0;
246+
let hoursRemainingRemoved = 0;
247+
let currentHoursRemoved = 0;
248+
249+
let currYear = currPlan.currYear;
250+
let currTerm = currPlan.currTerm;
251+
252+
for (x in coursesToRemove) {
253+
let course = coursesToRemove[x];
254+
hoursRemoved += course.hours;
255+
if (currYear > course.year) {
256+
hoursCompletedRemoved += course.hours;
257+
} else if (currYear < course.year) {
258+
hoursRemainingRemoved += course.hours;
259+
} else if (currTerm == "Fall") {
260+
if (course.term == "Fall") {
261+
currentHoursRemoved += course.hours;
262+
} else {
263+
hoursRemainingRemoved += course.hours;
264+
}
265+
} else if (currTerm == "Spring") {
266+
if (course.term == "Fall") {
267+
hoursCompletedRemoved += course.hours;
268+
} else if (course.term == "Spring") {
269+
currentHoursRemoved += course.hours;
270+
} else {
271+
hoursRemainingRemoved += course.hours;
272+
}
273+
} else if (currTerm == "Summer") {
274+
if (course.term == "Summer") {
275+
currentHoursRemoved += course.hours;
276+
} else {
277+
hoursCompletedRemoved += course.hours;
278+
}
279+
}
280+
}
281+
282+
currPlan.hrsCompleted -= hoursCompletedRemoved;
283+
currPlan.hrsCurrent -= currentHoursRemoved;
284+
currPlan.hrsFuture -= hoursRemainingRemoved;
285+
currPlan.hrsTotal -= hoursRemoved;
286+
287+
288+
for (j in currPlan.courses) {
289+
for (k in coursesToRemove) {
290+
if (currPlan.courses[j].designator == coursesToRemove[k].id) {
291+
delete currPlan.courses[j];
292+
break;
293+
}
294+
}
295+
}
296+
297+
for (l in currPlan.years) {
298+
if (parseInt(currPlan.years[l].name) == year) {
299+
currPlan.years.splice(l, 1);
300+
}
301+
}
302+
303+
for (designator in coursesToRemove) {
304+
$.get("/plan_courses", {
305+
designator: designator,
306+
user: plan.user.id,
307+
plan: plan.plan_name
308+
});
309+
}
310+
311+
}
312+
162313
function courseInPlan(designator){
163314
let c = currPlan.courses[designator];
164315
return c !== undefined;
@@ -366,6 +517,18 @@ class Course {
366517
}
367518
}
368519

520+
var yearControlHtml = "<div id='yearControlContainer'>" +
521+
"<div class='inlineForm'>" +
522+
"<input type='submit' id='addYear' value='Add Year(s)' onclick='addYears()'>" +
523+
"<label for='numYears' id='numYearsLabel'>Number of years to add: </label>" +
524+
"<input type='text' id='numYears' name='numYears' value='1'>" +
525+
"</div>" +
526+
"<div class='inlineForm'>" +
527+
"<input type='submit' id='deleteYear' onclick='return deleteYear();' value='Delete Year'>" +
528+
"<label for='numYears' id='yearToDeleteLabel'>Year to delete: </label>" +
529+
"<input type='text' id='yearToDelete' name='yearToDelete'>" +
530+
"</div></div>";
531+
369532
class Plan {
370533
constructor(student, name, major, currYr, currTerm, courses, cat_yr){
371534
this.name = name;
@@ -482,6 +645,7 @@ class Plan {
482645
}
483646
urHTML += "</ul></div></div>";
484647
}
648+
urHTML += yearControlHtml;
485649
urHTML += "</div>";
486650
var upperRight = $("#upperRight").html(urHTML);
487651
}
@@ -530,4 +694,4 @@ class Year {
530694
}
531695
}
532696
}
533-
}
697+
}

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<%= csrf_meta_tags %>
88
<%= csp_meta_tag %>
99

10-
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"/>
10+
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css"/>
1111
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
1212
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
1313
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

app/views/plans/show.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@
3939
<header class="panelHeader">Course Schedule</header>
4040
<div class="container">
4141
</div>
42+
<div id="yearControlContainer">
43+
<form class="inlineForm" action="">
44+
<input type="submit" id="addYear" onclick="addYear()" value="Add Year(s)">
45+
<label for="numYears" id="numYearsLabel">Number of years to add: </label>
46+
<input type="text" id="numYears" name="numYears" value="1">
47+
</form>
48+
<form class="inlineForm" action="">
49+
<input type="submit" id="deleteYear" onclick="deleteYear()" value="Delete Year">
50+
<label for="numYears" id="yearToDeleteLabel">Year to delete: </label>
51+
<input type="text" id="yearToDelete" name="yearToDelete">
52+
</form>
53+
</div>
4254
</div>
4355
</div>
4456

0 commit comments

Comments
 (0)