public void EditLessonPlanobj(lessonPlan LessonPlan) { var lessonPlanToEdit = db.AspNetLessonPlans.FirstOrDefault(s => s.Date == LessonPlan.Date && s.SubjectID == LessonPlan.subjectID); AspNetLessonPlan aspNetLessonPlan = db.AspNetLessonPlans.FirstOrDefault(x => x.Id == lessonPlanToEdit.Id); aspNetLessonPlan.LessonPlanNo = LessonPlan.lessonPlanNo; aspNetLessonPlan.SubjectID = LessonPlan.subjectID; aspNetLessonPlan.Date = LessonPlan.Date; aspNetLessonPlan.Duration = LessonPlan.Duration; //db.AspNetLessonPlans.Add(aspNetLessonPlan); db.SaveChanges(); int LessonPlanID = db.AspNetLessonPlans.Max(x => x.Id); foreach (var topic in LessonPlan.Topics) { AspNetLessonPlan_Topic lessonPlanTopicExist = db.AspNetLessonPlan_Topic.FirstOrDefault(x => x.TopicID == topic && x.LessonPlanID == LessonPlanID); if (lessonPlanTopicExist == null) { AspNetLessonPlan_Topic lessonPlanTopic = new AspNetLessonPlan_Topic(); lessonPlanTopic.TopicID = topic; lessonPlanTopic.LessonPlanID = LessonPlanID; db.AspNetLessonPlan_Topic.Add(lessonPlanTopic); db.SaveChanges(); } } foreach (var breakdown in LessonPlan.BreakDown) { AspNetLessonPlanBreakdown BreakdownCheck = db.AspNetLessonPlanBreakdowns.FirstOrDefault(x => x.BreakDownHeadingID == breakdown.HeadingID && x.LessonPlanID == LessonPlanID); if (BreakdownCheck == null) { AspNetLessonPlanBreakdown aspNetLessonBreakdown = new AspNetLessonPlanBreakdown(); aspNetLessonBreakdown.LessonPlanID = LessonPlanID; aspNetLessonBreakdown.Minutes = breakdown.Minutes; aspNetLessonBreakdown.Resources = breakdown.Resources; aspNetLessonBreakdown.BreakDownHeadingID = breakdown.HeadingID; aspNetLessonBreakdown.Description = WebUtility.HtmlEncode(breakdown.Description).ToString(); db.AspNetLessonPlanBreakdowns.Add(aspNetLessonBreakdown); db.SaveChanges(); } else { BreakdownCheck.Minutes = breakdown.Minutes; BreakdownCheck.BreakDownHeadingID = breakdown.HeadingID; BreakdownCheck.Description = breakdown.Description; db.SaveChanges(); } } }
public void LessonPlanfromFile(AspNetLessonPlan aspNetLessonPlan) { HttpPostedFileBase file = Request.Files["LessonPlan"]; if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName)) { string fileName = file.FileName; string fileContentType = file.ContentType; byte[] fileBytes = new byte[file.ContentLength]; var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength)); } using (var package = new ExcelPackage(file.InputStream)) { var currentSheet = package.Workbook.Worksheets; var workSheet = currentSheet.First(); var noOfCol = workSheet.Dimension.End.Column; var noOfRow = workSheet.Dimension.End.Row; List <int> TopicsID = new List <int>(); int LessonPlanID = -1; for (int rowIterator = 1; rowIterator <= noOfRow; rowIterator++) { string Heading = workSheet.Cells[rowIterator, 1].Value.ToString(); if (Heading == "Lesson No") { aspNetLessonPlan.LessonPlanNo = Convert.ToInt32(workSheet.Cells[rowIterator, 2].Value); } else if (Heading == "Chapter") { } else if (Heading == "Topic") { while (true) { rowIterator++; string check = workSheet.Cells[rowIterator, 1].Value.ToString(); if (check != "Duration") { string topicName = workSheet.Cells[rowIterator, 1].Value.ToString(); int topicID = db.AspNetTopics.Where(x => x.TopicName == topicName).Select(x => x.Id).FirstOrDefault(); TopicsID.Add(topicID); } else { rowIterator--; break; } } } else if (Heading == "Duration") { aspNetLessonPlan.Duration = Convert.ToInt32(workSheet.Cells[rowIterator, 2].Value); db.AspNetLessonPlans.Add(aspNetLessonPlan); db.SaveChanges(); LessonPlanID = db.AspNetLessonPlans.Max(x => x.Id); foreach (var topic in TopicsID) { AspNetLessonPlan_Topic lessonPlanTopic = new AspNetLessonPlan_Topic(); lessonPlanTopic.TopicID = topic; lessonPlanTopic.LessonPlanID = LessonPlanID; db.AspNetLessonPlan_Topic.Add(lessonPlanTopic); db.SaveChanges(); } } else if (Heading == "Breakdown") { AspNetLessonPlanBreakdown aspNetLessonBreakdown = new AspNetLessonPlanBreakdown(); while (true) { rowIterator++; string check = workSheet.Cells[rowIterator, 1].Value.ToString(); if (check != "End") { if (check == "Heading") { aspNetLessonBreakdown.LessonPlanID = LessonPlanID; string headingName = workSheet.Cells[rowIterator, 2].Value.ToString(); aspNetLessonBreakdown.BreakDownHeadingID = db.AspNetLessonPlanBreakdownHeadings.Where(x => x.BreakDownHeadingName == headingName).Select(x => x.Id).FirstOrDefault(); } else if (check == "Description") { aspNetLessonBreakdown.Description = workSheet.Cells[rowIterator, 2].Value.ToString(); } else if (check == "Time") { aspNetLessonBreakdown.Minutes = Convert.ToInt32(workSheet.Cells[rowIterator, 2].Value.ToString()); } else if (check == "Resources") { aspNetLessonBreakdown.Resources = workSheet.Cells[rowIterator, 2].Value.ToString(); db.AspNetLessonPlanBreakdowns.Add(aspNetLessonBreakdown); db.SaveChanges(); } } else { break; } } } else if (Heading == "End") { break; } } } }
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public JsonResult AddLessonPlan(lessonPlan LessonPlan) { var TransactionObj = db.Database.BeginTransaction(); try { AspNetLessonPlan aspNetLessonPlan = new AspNetLessonPlan(); aspNetLessonPlan.LessonPlanNo = LessonPlan.lessonPlanNo; aspNetLessonPlan.SubjectID = LessonPlan.subjectID; aspNetLessonPlan.Date = LessonPlan.Date; aspNetLessonPlan.Duration = LessonPlan.Duration; db.AspNetLessonPlans.Add(aspNetLessonPlan); db.SaveChanges(); int LessonPlanID = db.AspNetLessonPlans.Max(x => x.Id); foreach (var topic in LessonPlan.Topics) { AspNetLessonPlan_Topic lessonPlanTopic = new AspNetLessonPlan_Topic(); lessonPlanTopic.TopicID = topic; lessonPlanTopic.LessonPlanID = LessonPlanID; db.AspNetLessonPlan_Topic.Add(lessonPlanTopic); db.SaveChanges(); } if (LessonPlan.BreakDown != null) { foreach (var breakdown in LessonPlan.BreakDown) { AspNetLessonPlanBreakdown aspNetLessonBreakdown = new AspNetLessonPlanBreakdown(); aspNetLessonBreakdown.LessonPlanID = LessonPlanID; aspNetLessonBreakdown.Minutes = breakdown.Minutes; aspNetLessonBreakdown.Resources = breakdown.Resources; aspNetLessonBreakdown.BreakDownHeadingID = breakdown.HeadingID; aspNetLessonBreakdown.Description = WebUtility.HtmlEncode(breakdown.Description).ToString(); db.AspNetLessonPlanBreakdowns.Add(aspNetLessonBreakdown); db.SaveChanges(); } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// var UserNameLog = User.Identity.Name; AspNetUser a = db.AspNetUsers.First(x => x.UserName == UserNameLog); string UserIDLog = a.Id; var logMessage = "New LessonPlan Added, SubjectID: " + LessonPlan.subjectID + ", Date: " + LessonPlan.Date; var LogControllerObj = new AspNetLogsController(); LogControllerObj.CreateLogSave(logMessage, UserIDLog); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// db.SaveChanges(); TransactionObj.Commit(); } catch (Exception) { TransactionObj.Dispose(); } return(Json("data", JsonRequestBehavior.AllowGet)); }
public void EditLessonPlan1234(EditLessonPlanType LessonPlan) { var TransactionObj = db.Database.BeginTransaction(); try { var lessonID = LessonPlan.LessonPlanID; AspNetLessonPlan aspNetLessonPlan = db.AspNetLessonPlans.FirstOrDefault(x => x.Id == lessonID); //aspNetLessonPlan.LessonPlanNo = LessonPlan.lessonPlanNo; aspNetLessonPlan.SubjectID = LessonPlan.subjectID; aspNetLessonPlan.Date = LessonPlan.Date; aspNetLessonPlan.Duration = LessonPlan.Duration; //db.AspNetLessonPlans.Add(aspNetLessonPlan); db.SaveChanges(); int LessonPlanID = aspNetLessonPlan.Id; var Lesson_TopicList = (from lesson_topic in db.AspNetLessonPlan_Topic where lesson_topic.LessonPlanID == LessonPlanID select lesson_topic).ToList(); int count = 1; do { var lessonObj = db.AspNetLessonPlan_Topic.FirstOrDefault(x => x.LessonPlanID == LessonPlanID); if (lessonObj == null) { count = 0; continue; } db.AspNetLessonPlan_Topic.Attach(lessonObj); db.AspNetLessonPlan_Topic.Remove(lessonObj); db.SaveChanges(); }while (count == 1); foreach (var topic in LessonPlan.Topics) { AspNetLessonPlan_Topic lessonPlanTopic = new AspNetLessonPlan_Topic(); lessonPlanTopic.TopicID = topic; lessonPlanTopic.LessonPlanID = LessonPlanID; db.AspNetLessonPlan_Topic.Add(lessonPlanTopic); db.SaveChanges(); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// var UserNameLog = User.Identity.Name; AspNetUser a = db.AspNetUsers.First(x => x.UserName == UserNameLog); string UserIDLog = a.Id; var logMessage = "LessonPlan Edited, LessonPlanID: " + lessonID + ", Date: " + LessonPlan.Date; var LogControllerObj = new AspNetLogsController(); LogControllerObj.CreateLogSave(logMessage, UserIDLog); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// db.SaveChanges(); TransactionObj.Commit(); } catch (Exception) { TransactionObj.Dispose(); } }