public ActionResult Save(YearFormViewModel vm) { var proposal = vm.Proposal; proposal = _context.Proposals.SingleOrDefault(m => m.Id == proposal.Id); if (proposal == null || proposal.Submitted) { return(HttpNotFound()); } if (!proposal.IsEditable(User.Identity.GetUserId())) { return(HttpNotFound()); } var year = vm.Year; if (!ModelState.IsValid) { return(View("Form", year)); } if (year.Id == 0) { _context.Years.Add(year); var pr = _context.ProgrammeRationales.SingleOrDefault(m => m.Id == proposal.ProgrammeRationaleId); year.TentativeP = pr.TentativeP; year.TentativePsId = pr.TentativePsId; } else { var yearInDb = _context.Years.SingleOrDefault(m => m.Id == year.Id); yearInDb.TotalEcts = year.TotalEcts; if (year.YearNo != null) { var maxYearNo = _context.Database.SqlQuery <int>("Select Max(YearNo) From dbo.Year Where TentativePsId = " + yearInDb.TentativePsId).First() + 5; if (year.YearNo <= maxYearNo) { // check that no year exists with the same number var takenYearNos = _context.Database.SqlQuery <int>("Select YearNo From dbo.Year Where TentativePsId = " + yearInDb.TentativePsId).ToList(); if (!takenYearNos.Contains((int)year.YearNo)) { yearInDb.YearNo = year.YearNo; } else { return(HttpNotFound()); } } else { return(HttpNotFound()); } } // remove exsting Year_Units where TentativePsId == tentative.Id var toRemove = _context.Year_Unit.Where(m => m.YearId == year.Id).ToList(); foreach (Year_Unit yu in toRemove.ToList()) { var unit = _context.Ref_Unit.SingleOrDefault(m => m.Id == yu.UnitId); var dept = unit.Ref_Department; var stm = dept.GetServStatement(proposal.Id); if (stm != null) { if (stm.Selection == true) { toRemove.Remove(yu); } } } _context.Year_Unit.RemoveRange(toRemove); //string queryYu = "DELETE FROM dbo.Year_Unit WHERE YearId = " + year.Id; //_context.Database.ExecuteSqlCommand(queryYu); } foreach (string name in Request.Form.AllKeys) { try { int unitId = Convert.ToInt32(name); Year_Unit yu = new Year_Unit(); yu.YearId = year.Id; yu.Year = _context.Years.SingleOrDefault(m => m.Id == year.Id); yu.UnitId = unitId; yu.Coe = Convert.ToInt32(Request["coe_" + name]); if (Request["lecturer_" + name] != "null") { yu.Lecturer = Request["lecturer_" + name]; } else { yu.Lecturer = null; } if (Request["credits_" + name] != "") { yu.Ects = Convert.ToInt32(Request["credits_" + name]); } //if (Request["lecturer_" + name] != "") //{ // yu.Lecturer = Request["lecturer_" + name]; //} yu.Period = Convert.ToInt32(Request["period_" + name]); if (proposal.GetGeneral().LevelId == 1) { // if UG, check if compensating was selected if (Request["comp_" + name] == "on") { yu.Compensating = 1; yu.CompensatingReason = null; } else { yu.Compensating = 0; if (Request["reason_" + name] != "") { yu.CompensatingReason = Request["reason_" + name]; } } } else { yu.Compensating = 0; yu.CompensatingReason = "Compensated passes are not applicable for PG courses."; } _context.Year_Unit.Add(yu); _context.SaveChanges(); } catch (Exception e) { } } _context.SaveChanges(); var jump = Request["jump"]; switch (jump) { case "0": { // Save pressed return(RedirectToAction("Index", "TentativePs", new { id = proposal.Id })); } case "1": { // New unit pressed return(RedirectToAction("New", "Unit", new { yearRedirect = year.Id })); } default: { return(RedirectToAction("Index", "TentativePs", new { id = proposal.Id })); } } }
public ActionResult Save(YearFormViewModel vm) { var proposal = vm.Proposal; proposal = _context.Proposals.SingleOrDefault(m => m.Id == proposal.Id); if (proposal == null || proposal.Submitted) { return(HttpNotFound()); } var year = vm.Year; if (!ModelState.IsValid) { return(View("Form", year)); } if (year.Id == 0) { _context.Years.Add(year); var pr = _context.ProgrammeRationales.SingleOrDefault(m => m.Id == proposal.Id); year.TentativeP = pr.TentativeP; year.TentativePsId = pr.TentativePsId; } else { var yearInDb = _context.Years.SingleOrDefault(m => m.Id == year.Id); yearInDb.TotalEcts = year.TotalEcts; // remove exsting Year_Units where TentativePsId == tentative.Id string queryYu = "DELETE FROM dbo.Year_Unit WHERE YearId = " + year.Id; _context.Database.ExecuteSqlCommand(queryYu); } foreach (string name in Request.Form.AllKeys) { try { int unitId = Convert.ToInt32(name); Year_Unit yu = new Year_Unit(); yu.YearId = year.Id; yu.Year = _context.Years.SingleOrDefault(m => m.Id == year.Id); yu.UnitId = unitId; yu.Coe = Convert.ToInt32(Request["coe_" + name]); if (Request["credits_" + name] != "") { yu.Ects = Convert.ToInt32(Request["credits_" + name]); } if (Request["lecturer_" + name] != "") { yu.Lecturer = Request["lecturer_" + name]; } yu.Period = Convert.ToInt32(Request["period_" + name]); if (proposal.GetGeneral().LevelId == 1) { // if UG, check if compensating was selected if (Request["comp_" + name] == "on") { yu.Compensating = 1; } else { yu.Compensating = 0; if (Request["reason_" + name] != "") { yu.CompensatingReason = Request["reason_" + name]; } } } else { yu.Compensating = 0; } _context.Year_Unit.Add(yu); _context.SaveChanges(); } catch (Exception e) { } } _context.SaveChanges(); var jump = Request["jump"]; switch (jump) { case "0": { // Save pressed return(RedirectToAction("Index", "TentativePs", new { id = proposal.Id })); } case "1": { // New unit pressed return(RedirectToAction("New", "Unit", new { yearRedirect = year.Id })); } default: { return(RedirectToAction("Index", "TentativePs", new { id = proposal.Id })); } } }
public ActionResult Save(TentativePsFormViewModel vm) { var tentative = vm.TentativePs; var proposal = _context.Proposals.SingleOrDefault(m => m.Id == vm.Proposal.Id); if (!ModelState.IsValid) { return(View("Form", tentative)); } if (tentative.Id == 0) { _context.TentativePs.Add(tentative); var pr = _context.ProgrammeRationales.SingleOrDefault(m => m.Id == proposal.Id); pr.TentativePsId = tentative.Id; pr.TentativeP = tentative; var addedYears = new List <int>(); foreach (string name in Request.Form.AllKeys) { try { int n = Convert.ToInt32(name); int yearNo = Convert.ToInt32(Request["year_" + name]); if (!addedYears.Contains(yearNo)) { addedYears.Add(yearNo); Year year = new Year(); year.TentativePsId = tentative.Id; year.TentativeP = tentative; year.YearNo = yearNo; year.TotalEcts = 0; _context.Years.Add(year); _context.SaveChanges(); } Year yearInDb = _context.Years.SingleOrDefault(m => m.TentativePsId == tentative.Id && m.YearNo == yearNo); Year_Unit yu = new Year_Unit(); yu.YearId = yearInDb.Id; yu.Year = _context.Years.SingleOrDefault(m => m.Id == yearInDb.Id); yu.UnitId = n; yu.Coe = Convert.ToInt32(Request["coe_" + name]); yu.Ects = Convert.ToInt32(Request["credits_" + name]); yu.Period = Convert.ToInt32(Request["period_" + name]); yu.Lecturer = Request["lecturer_" + name]; _context.Year_Unit.Add(yu); _context.SaveChanges(); //return Content(name + " " + Request["lecturer_" + name] + " " + Request["credits_" + name]); } catch (Exception e) { } } } else { //save all yearno, ects pairs to dictionary SortedDictionary <int, int> pairs = new SortedDictionary <int, int>(); string query = "Select YearNo From dbo.Year Where TentativePsId = " + tentative.Id; var yearNos = _context.Database.SqlQuery <int>(query).ToList(); foreach (int yearNo in yearNos) { string q = "Select TotalEcts From dbo.Year Where YearNo = " + yearNo + " and TentativePsId = " + tentative.Id; int ects = _context.Database.SqlQuery <int>(q).Single(); pairs.Add(yearNo, ects); } // remove exsting Year_Units and Years where TentativePsId == tentative.Id string queryYu = "DELETE FROM dbo.Year_Unit WHERE YearId in (Select Id From dbo.Year Where TentativePsId = " + tentative.Id + "); "; string queryY = "DELETE FROM dbo.Year Where TentativePsId = " + tentative.Id; _context.Database.ExecuteSqlCommand(queryYu); _context.Database.ExecuteSqlCommand(queryY); var addedYears = new List <int>(); foreach (string name in Request.Form.AllKeys) { try { int n = Convert.ToInt32(name); int yearNo = Convert.ToInt32(Request["year_" + name]); if (!addedYears.Contains(yearNo)) { addedYears.Add(yearNo); Year year = new Year(); year.TentativePsId = tentative.Id; year.YearNo = yearNo; if (pairs.ContainsKey(yearNo)) { year.TotalEcts = pairs[yearNo]; } else { year.TotalEcts = 0; } _context.Years.Add(year); _context.SaveChanges(); } Year yearInDb = _context.Years.SingleOrDefault(m => m.TentativePsId == tentative.Id && m.YearNo == yearNo); Year_Unit yu = new Year_Unit(); yu.YearId = yearInDb.Id; yu.Year = _context.Years.SingleOrDefault(m => m.Id == yearInDb.Id); yu.UnitId = n; yu.Coe = Convert.ToInt32(Request["coe_" + name]); yu.Ects = Convert.ToInt32(Request["credits_" + name]); yu.Period = Convert.ToInt32(Request["period_" + name]); yu.Lecturer = Request["lecturer_" + name]; _context.Year_Unit.Add(yu); _context.SaveChanges(); } catch (Exception e) { } } } _context.SaveChanges(); var jump = Request["jump"]; switch (jump) { case "0": { // Save pressed -> return form return(RedirectToAction("Edit", "TentativePs", new { id = proposal.Id })); } case "-1": { // Previous pressed -> return form return(RedirectToAction("Edit", "ProgrammeStudy", new { id = proposal.Id })); } case "1": { // Next pressed -> return next page return(RedirectToAction("YearTotals", "TentativePs", new { id = proposal.Id })); } case "A": { // A pressed -> go to Section A return(RedirectToAction("Edit", "General", new { id = proposal.Id })); } case "B": { // B pressed -> go to Section B return(RedirectToAction("Edit", "Rationale", new { id = proposal.Id })); } case "C": { // C pressed -> go to Section C return(RedirectToAction("Jump", "ExternalReview", new { id = proposal.Id })); } default: { return(RedirectToAction("Index", "Proposal")); } } }