private void setPreviousWeekliesToFalse() { TutorMasterDBEntities4 db = new TutorMasterDBEntities4(); //connect to the database List <Commitment> cmtList = (from stucmt in db.StudentCommitments //get the commit list of the signed in student where stucmt.ID == id join cmt in db.Commitments on stucmt.CmtID equals cmt.CmtID select cmt).ToList(); List <DateTime> searchList = new List <DateTime>(); //initialize search list SortsAndSearches.QuickSort(ref cmtList, cmtList.Count()); searchList = getStartTimes(); //get the startTimes from the listview for (int i = 0; i < cmtList.Count(); i++) //for each commitment in the commit list { if (DateTimeMethods.weeklyAndFound(cmtList[i], searchList)) //if the commitment is in the search list and weekly { DateTime startSemes = new DateTime(2017, 1, 1, 0, 0, 0); DateTime weekBack = Convert.ToDateTime(cmtList[i].StartTime).AddDays(-7); //go a week back in time while (DateTime.Compare(startSemes, weekBack) <= 0) //perform a binary search here { bool found = false; int first = 0; int last = cmtList.Count() - 1; while (first <= last && !found) { int midpoint = (first + last) / 2; if (DateTimeMethods.sameTime(cmtList[midpoint], weekBack)) //if you find the weekBack date time { if (cmtList[midpoint].Open == true) //if the commitment is open { cmtList[midpoint].Weekly = false; //set its weekly to false db.SaveChanges(); //save the changes to the database } found = true; } else { if (DateTimeMethods.weekBackEarlier(weekBack, cmtList[midpoint])) //if weekback is earlier, search first half of list { last = midpoint - 1; } else //else, search the second half of the list { first = midpoint + 1; } } } weekBack = weekBack.AddDays(-7); //go a week back in time } } } }
private void deleteAvail(bool week) { TutorMasterDBEntities4 db = new TutorMasterDBEntities4(); //connect to the database List <Commitment> cmtList = (from stucmt in db.StudentCommitments //get the student's commitments where stucmt.ID == id join cmt in db.Commitments on stucmt.CmtID equals cmt.CmtID select cmt).ToList(); SortsAndSearches.QuickSort(ref cmtList, cmtList.Count()); //sort the list by DateTime List <DateTime> searchList = new List <DateTime>(); searchList = getStartTimes(); //get the starttimes from the listview if (week) { for (int i = 0; i < cmtList.Count(); i++) { if (SortsAndSearches.BinarySearch(searchList, Convert.ToDateTime(cmtList[i].StartTime))) { if (cmtList[i].Weekly == true) { //ask the user if they want to delete the weekly commitment through the end of the semester DateTime endSemes = new DateTime(2017, 5, 1, 0, 0, 0); //get end of semester DateTime weekForward = Convert.ToDateTime(cmtList[i].StartTime).AddDays(7); //go a week forward while (DateTimeMethods.endOfSemesIsLater(endSemes, weekForward)) //if the end of the semester is later than our commitment start Time { //run a binary search bool found = false; int first = 0; int last = cmtList.Count() - 1; while (first <= last && !found) { int midpoint = (first + last) / 2; if (DateTimeMethods.sameTime(cmtList[midpoint], weekForward)) //if commitment time and weekforward time are the same { if (cmtList[midpoint].Open == true) //and if the midpoint commitment is open { db.Commitments.DeleteObject(cmtList[midpoint]); //delete the commitment from the database cmtList.Remove(cmtList[midpoint]); //remove it from the commit list as well db.SaveChanges(); } found = true; //say we found what we were looking for break; //break out of the search } else { if (DateTimeMethods.forwardEarlierThanStart(weekForward, cmtList[midpoint])) { last = midpoint - 1; } else { first = midpoint + 1; } } } weekForward = weekForward.AddDays(7); } } searchList.Remove(Convert.ToDateTime(cmtList[i].StartTime)); db.Commitments.DeleteObject(cmtList[i]); i--; db.SaveChanges(); } } MessageBox.Show("The checked 15 minute time blocks have been removed from your availability until the end of the semster."); } else { for (int i = 0; i < cmtList.Count(); i++) { if (SortsAndSearches.BinarySearch(searchList, Convert.ToDateTime(cmtList[i].StartTime))) { searchList.Remove(Convert.ToDateTime(cmtList[i].StartTime)); db.Commitments.DeleteObject(cmtList[i]); i--; db.SaveChanges(); } } MessageBox.Show("Only the checked 15 minute time blocks have been removed from your availability."); } }