public WeekendTableItem(WeekendActivity activity) { DateAndTime = activity.DateAndTime; Adults = activity.Adults.ToList(); Duration = activity.Duration; Name = activity.Name; Notes = activity.Description; }
protected void DeleteActivityBtn_Click(object sender, EventArgs e) { using (WebhostEntities db = new WebhostEntities()) { int id = -1; try { id = Convert.ToInt32(DeleteActivityDDL.SelectedValue.Substring(1)); } catch { State.log.WriteLine("Invalid Activity '{0}' Cannot Delete.", DeleteActivityDDL.SelectedValue); LogError("Invalid Activity '{0}' Cannot Delete.", DeleteActivityDDL.SelectedValue); return; } if (DeleteActivityDDL.SelectedValue[0] == 'A') { WeekendActivity activity = db.WeekendActivities.Find(id); activity.IsDeleted = true; LogInformation("Deleted Weekend Activity {0}", activity.Name); if (!activity.GoogleCalendarEventId.Equals("")) { using (GoogleCalendarCall call = new GoogleCalendarCall()) { String message = call.DeleteEvent(db.GoogleCalendars.Where(cal => cal.CalendarName.Equals("Weekend Activities")).Single().CalendarId, activity.GoogleCalendarEventId); State.log.WriteLine("Deleted un-wanted calendar event with response: {0}", message); activity.GoogleCalendarEventId = ""; } } } else { WeekendDuty duty = db.WeekendDuties.Find(id); duty.IsDeleted = true; if (!duty.GoogleCalendarEventId.Equals("")) { using (GoogleCalendarCall call = new GoogleCalendarCall()) { String message = call.DeleteEvent(db.GoogleCalendars.Where(cal => cal.CalendarName.Equals("Weekend Duty")).Single().CalendarId, duty.GoogleCalendarEventId); State.log.WriteLine("Deleted un-wanted calendar event with response: {0}", message); duty.GoogleCalendarEventId = ""; } } LogInformation("Deleted Weekend Duty {0}", duty.Name); } db.SaveChanges(); } LoadWeekend(); }
protected bool ConflictsWith(int otherSignupId) { using (WebhostEntities db = new WebhostEntities()) { WeekendActivity thisActivity = db.WeekendActivities.Where(act => act.id == ActivityId).Single(); WeekendActivity other = db.WeekendActivities.Where(act => act.id == otherSignupId).Single(); DateRange thisRange = new DateRange(thisActivity.DateAndTime, thisActivity.Duration == 0 ? thisActivity.DateAndTime.AddHours(2) : thisActivity.DateAndTime.AddMinutes(thisActivity.Duration)); DateRange otherRange = new DateRange(other.DateAndTime, other.Duration == 0 ? other.DateAndTime.AddHours(2) : other.DateAndTime.AddMinutes(other.Duration)); return(thisRange.Intersects(otherRange)); } }
protected void SubmitBtn_Click(object sender, EventArgs e) { using (WebhostEntities db = new WebhostEntities()) { WeekendActivity activity = db.WeekendActivities.Where(act => act.id == ActivityId).Single(); String students = ""; foreach (TableRow row in AttendanceTabel.Rows) { if (!(row is ActivityAttendanceTableRow)) { continue; } ActivityAttendanceTableRow arow = ((ActivityAttendanceTableRow)row); WebhostMySQLConnection.StudentSignup signup = activity.StudentSignups.Where(s => s.StudentId == arow.StudentId).Single(); signup.Attended = arow.IsAttending; students += arow.Cells[0].Text + Environment.NewLine; } List <int> studentIds = AdditionalStudentsSelector.GroupIds; foreach (int id in studentIds) { if (activity.StudentSignups.Where(s => s.StudentId == id).Count() > 0) { WebhostMySQLConnection.StudentSignup signup = activity.StudentSignups.Where(s => s.StudentId == id).Single(); signup.Attended = true; } else { WebhostMySQLConnection.StudentSignup newSignup = new WebhostMySQLConnection.StudentSignup() { StudentId = id, ActivityId = activity.id, IsBanned = false, Attended = true, IsRescended = false, TimeStamp = DateTime.Now }; Student student = db.Students.Find(id); students += String.Format("{0} {1}{2}", student.FirstName, student.LastName, Environment.NewLine); db.StudentSignups.Add(newSignup); } } LogInformation("Submitted Student Trip {2} Attendance:{1}{0}", students, Environment.NewLine, activity.Name); db.SaveChanges(); } }
public IHttpActionResult PutSignupRequest(int activity_id, [FromUri] bool isRescend = false) { if (!DateRange.WeekendSignupsAreOpen) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, new MethodAccessException("Weekend Signups will be available at 11:30 on Friday.")))); } using (WebhostEntities db = new WebhostAPI.WebhostEntities()) { WebhostUserPrinciple principal = (WebhostUserPrinciple)ActionContext.RequestContext.Principal; int id = ((WebhostIdentity)principal.Identity).User.Id; WeekendActivity activity = db.WeekendActivities.Find(activity_id); if (activity == null) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new InvalidOperationException("Invalid Activity Id.")))); } StudentSignup signup = db.StudentSignups.Find(activity_id, id); if (signup == null) // New Signup! { signup = new WebhostAPI.StudentSignup() { ActivityId = activity_id, Attended = false, IsBanned = false, IsRescended = isRescend, StudentId = id, TimeStamp = DateTime.Now }; db.StudentSignups.Add(signup); db.SaveChanges(); return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Created, new StudentSignupInfo(activity_id, id), "text/json"))); } signup.IsRescended = isRescend; signup.TimeStamp = DateTime.Now; db.SaveChanges(); return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new StudentSignupInfo(activity_id, id), "text/json"))); } }
public IHttpActionResult PutOverrideSignup(int activity_id, int student_id, [FromBody] StudentSignupInfo info) { using (WebhostEntities db = new WebhostAPI.WebhostEntities()) { WeekendActivity activity = db.WeekendActivities.Find(activity_id); if (activity == null) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new InvalidOperationException("Invalid Activity Id.")))); } Student student = db.Students.Find(student_id); if (student == null) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new InvalidOperationException("Invalid Student Id.")))); } StudentSignup signup = db.StudentSignups.Find(activity_id, student_id); if (signup == null) // Create a new one. { signup = new WebhostAPI.StudentSignup() { ActivityId = activity_id, StudentId = student_id, IsBanned = info.IsBanned, IsRescended = info.IsRescended, Attended = false, TimeStamp = DateTime.Now }; db.StudentSignups.Add(signup); db.SaveChanges(); return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Created, new StudentSignupInfo(activity_id, student_id), "text/json"))); } signup.IsBanned = info.IsBanned; signup.IsRescended = info.IsRescended; signup.TimeStamp = DateTime.Now; db.SaveChanges(); return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new StudentSignupInfo(activity_id, student_id), "text/json"))); } }
protected void DeleteBtn_Click(object sender, EventArgs e) { using (WebhostEntities db = new WebhostEntities()) { WeekendActivity activity = db.WeekendActivities.Where(act => act.id == ActivityId).Single(); activity.IsDeleted = true; /*if(!activity.GoogleCalendarEventId.Equals("")) * { * using(GoogleCalendarCall call = new GoogleCalendarCall()) * { * String message = call.DeleteEvent(db.GoogleCalendars.Where(cal => cal.CalendarName.Equals("Weekend Activities")).Single().CalendarId, activity.GoogleCalendarEventId); * State.log.WriteLine("Deleted un-wanted calendar event with response: {0}", message); * activity.GoogleCalendarEventId = ""; * } * }*/ db.SaveChanges(); SuccessLabel.Text = "Delete Successful"; SuccessPanel.Visible = true; } }
protected void reload() { using (WebhostEntities db = new WebhostEntities()) { if (db.WeekendActivities.Where(act => act.id == ActivityId).Count() > 0) { WeekendActivity activity = db.WeekendActivities.Where(act => act.id == ActivityId).Single(); ActivityNameLabel.Text = String.Format("{0} [{1}, {2}{3}]", activity.Name, activity.DateAndTime.ToLongDateString(), activity.DateAndTime.ToShortTimeString(), activity.Duration == 0 ? "" : String.Format(" ~ {0}", activity.DateAndTime.AddMinutes(activity.Duration).ToShortTimeString())); CurrentSignups.Text = ""; int count = 0; foreach (WebhostMySQLConnection.StudentSignup signup in activity.StudentSignups.Where(s => !s.IsBanned && !s.IsRescended).OrderBy(s => s.TimeStamp)) { CurrentSignups.Text += String.Format("[{0}] {1} {2}{3}", ++count, signup.Student.FirstName, signup.Student.FirstName, Environment.NewLine); if (count == activity.MaxSignups) { CurrentSignups.Text += "************************************************************" + Environment.NewLine; } if (signup.StudentId == ((BasePage)Page).user.ID) { Signup.Text = "Remove me from this List."; } } // Check for conflicting Signup int studentId = ((BasePage)Page).user.ID; Student student = db.Students.Where(stu => stu.ID == studentId).Single(); foreach (WebhostMySQLConnection.StudentSignup signup in student.StudentSignups.Where(sig => sig.WeekendActivity.WeekendIndex == activity.WeekendIndex && sig.ActivityId != ActivityId)) { if (this.ConflictsWith(signup.WeekendActivity.id)) { Signup.Text = String.Format("Conflict: You are already signed up for {0}", signup.WeekendActivity.Name); } } } } }
public IHttpActionResult GetWeekendActivity(int id, [FromUri] bool listStudents) { using (WebhostEntities db = new WebhostAPI.WebhostEntities()) { WeekendActivity activity = db.WeekendActivities.Find(id); if (activity == null) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentOutOfRangeException(nameof(id))))); } WeekendActivityInfo info = null; try { info = new WeekendActivityInfo(id); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e))); } return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, info, "text/json"))); } }
/// <summary> /// Get information about a given weekend activity. /// </summary> /// <param name="id"></param> /// <param name="listStudents"></param> public WeekendActivityInfo(int id, bool listStudents = true) { using (WebhostEntities db = new WebhostAPI.WebhostEntities()) { WeekendActivity activity = db.WeekendActivities.Find(id); if (activity == null) { throw new ArgumentException("Invalid Activity Id."); } if (activity.IsDeleted) { throw new ArgumentException("That Activity has been marked deleted."); } Id = id; Name = activity.Name; DayAndTime = activity.DateAndTime; IsOffCampus = activity.IsOffCampus; MaxSignups = activity.MaxSignups; Description = activity.Description; Categories = new List <string>(); foreach (WeekendActivityCategory category in activity.WeekendActivityCategories.ToList()) { Categories.Add(category.CategoryName); } if (listStudents) { Students = new List <RequestHandlers.StudentSignupInfo>(); foreach (StudentSignup signup in activity.StudentSignups.ToList()) { Students.Add(new RequestHandlers.StudentSignupInfo(signup.ActivityId, signup.StudentId)); } } } }
protected void LoadTemplate_Click(object sender, EventArgs e) { using (WebhostEntities db = new WebhostEntities()) { if (!LoadWeekendFromDate()) { // Initialize a new weekend! int id = db.Weekends.Count() > 0 ? db.Weekends.OrderBy(w => w.id).ToList().Last().id + 1 : 0; Weekend weekend = new Weekend() { id = id, DutyTeamIndex = DutyTeamID, StartDate = WeekendRange.Start, EndDate = WeekendRange.End, Notes = "" }; // Get the Weekend to Copy! int otherId = Convert.ToInt32(TemplateDDL.SelectedValue); LogInformation("Copying Weekend.id {0} to the current weekend.", otherId); Weekend other = db.Weekends.Where(w => w.id == otherId).Single(); //Copy Activities int actId = db.WeekendActivities.OrderBy(act => act.id).ToList().Last().id; int dutyId = db.WeekendDuties.Count() > 0 ? db.WeekendDuties.OrderBy(act => act.id).ToList().Last().id : 0; foreach (WeekendActivity activity in other.WeekendActivities.Where(act => !act.IsDeleted)) { // Adjust date and time. TimeSpan diff = activity.DateAndTime - activity.Weekend.StartDate; WeekendActivity newActivity = new WeekendActivity() { id = ++actId, Name = activity.Name, isMandatory = activity.isMandatory, IsDeleted = false, IsOffCampus = activity.IsOffCampus, MaxSignups = activity.MaxSignups, DateAndTime = weekend.StartDate + diff, showStudents = activity.showStudents, WeekendIndex = id, isSignup = activity.isSignup, Duration = activity.Duration, Description = activity.Description, GoogleCalendarEventId = "" }; db.WeekendActivities.Add(newActivity); } foreach (WeekendDuty duty in other.WeekendDuties.Where(duty => !duty.IsDeleted)) { // Adjust date and time. TimeSpan diff = duty.DateAndTime - duty.Weekend.StartDate; WeekendDuty newDuty = new WeekendDuty() { id = ++dutyId, Name = duty.Name, DateAndTime = weekend.StartDate + diff, WeekendId = id, Duration = duty.Duration, Description = duty.Description, GoogleCalendarEventId = "", IsDeleted = false }; /*if(duty.DutyTeamMembers.Count > 5) * { * foreach(Faculty member in weekend.DutyTeam.Members.ToList()) * { * newDuty.DutyTeamMembers.Add(member); * } * }*/ db.WeekendDuties.Add(newDuty); } db.Weekends.Add(weekend); db.SaveChanges(); WeekendID = id; } LoadWeekend(); } }
public bool Save() { using (WebhostEntities db = new WebhostEntities()) { State.log.WriteLine("Save Weekend Activity Called."); int id = ActivityId; Weekend weekend = db.Weekends.Where(w => w.id == WeekendId).Single(); WeekendActivity activity = new WeekendActivity(); bool update = true; if (id == -1) { State.log.WriteLine("Creating a New Activity."); update = false; id = db.WeekendActivities.Count() > 0 ? db.WeekendActivities.OrderBy(act => act.id).ToList().Last().id + 1 : 0; activity.id = id; activity.isMandatory = false; } else { State.log.WriteLine("Updating existing activity."); activity = db.WeekendActivities.Where(act => act.id == id).Single(); } activity.Name = ActivityName.Text; activity.showStudents = isSignupCB.Checked; activity.isSignup = !activity.isMandatory; activity.IsOffCampus = isSignupCB.Checked && !MaxSignupDL.SelectedValue.Equals("0"); activity.Description = DescriptionInput.Text.Equals("Description") ? "" : DescriptionInput.Text; activity.Adults.Clear(); foreach (int aid in FacultyGroupSelector1.GroupIds) { Faculty fac = db.Faculties.Where(f => f.ID == aid).Single(); activity.Adults.Add(fac); } activity.WeekendIndex = WeekendId; DateTime date = weekend.StartDate.Date.AddDays(DaySelect.SelectedIndex); if (!AllDayCB.Checked) { try { date = date.AddHours(StartTimeSelector.Hour); date = date.AddMinutes(StartTimeSelector.Minute); } catch (WebhostException we) { State.log.WriteLine(we.Message); Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert_" + UniqueID, String.Format("alert('{0}');", we.Message), true); return(false); } } activity.DateAndTime = date; if (!AllDayCB.Checked && DurationCB.Checked) { try { int hours = DurationSelector.Hour - StartTimeSelector.Hour; int minutes = DurationSelector.Minute - StartTimeSelector.Minute; if (minutes < 0) { hours--; minutes = 60 + minutes; } activity.Duration = 60 * hours + minutes; } catch (WebhostException we) { State.log.WriteLine(we.Message); Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert_" + UniqueID, String.Format("alert('{0}');", we.Message), true); return(false); } } else { activity.Duration = 0; } if (!update) { activity.GoogleCalendarEventId = ""; db.WeekendActivities.Add(activity); State.log.WriteLine("Added New Activty to Database."); } db.SaveChanges(); State.log.WriteLine("Saved Changes to Database."); return(true); } }
protected void Signup_Click(object sender, EventArgs e) { using (WebhostEntities db = new WebhostEntities()) { WeekendActivity activity = db.WeekendActivities.Where(act => act.id == ActivityId).Single(); int studentId = ((BasePage)Page).user.ID; String calendarId = db.GoogleCalendars.Where(c => c.CalendarName.Equals("Weekend Activities")).Single().CalendarId; Student student = db.Students.Where(s => s.ID == studentId).Single(); LogInformation("Processing {0} click for {1}.", Signup.Text, activity.Name); /* * // Check Campused * if(activity.Weekend.CampusedStudents.Contains(student)) * { * State.log.WriteLine("Signup was blocked. Campused!"); * MailControler.MailToUser("Signup was blocked.", String.Format("You are not allowed to sign up for {0} because you have been campused this weekend.", activity.Name), ((BasePage)Page).user); * return; * } * * // Check Detention * DateRange activityTimes = activity.DateAndTime.Hour == 0 ? new DateRange(activity.DateAndTime, activity.DateAndTime.AddDays(1)) : * activity.Duration == 0 ? new DateRange(activity.DateAndTime, activity.DateAndTime.AddHours(3)) : * new DateRange(activity.DateAndTime, activity.DateAndTime.AddMinutes(activity.Duration)); * * if(activityTimes.Intersects(DateRange.Detention) && activity.Weekend.DetentionList.Contains(student)) * { * State.log.WriteLine("Signup was blocked. Detention!"); * MailControler.MailToUser("Signup was blocked.", String.Format("You are not allowed to sign up for {0} because you are in Detention this weekend.", activity.Name), ((BasePage)Page).user); * return; * } */ if (Signup.Text.Contains("Remove")) { State.log.WriteLine("Removing {0} {1} from {2}", student.FirstName, student.LastName, activity.Name); LogInformation("Removing {0} {1} from {2}", student.FirstName, student.LastName, activity.Name); try { WebhostMySQLConnection.StudentSignup signup = student.StudentSignups.Where(sig => sig.ActivityId == ActivityId).Single(); signup.IsRescended = true; db.SaveChanges(); } catch (Exception ex) { State.log.WriteLine("Failed to remove...\r\n{0}", ex.Message); LogError("Failed to remove {0} {1} from {2}\r\n{3}", student.FirstName, student.LastName, activity.Name, ex.Message); MailControler.MailToWebmaster("Webhost Error: Removing Signup", String.Format( "Could not remove {0} {1} from signup {2}\r\n{3}", student.FirstName, student.LastName, activity.Name, ex.Message )); return; } if (!activity.GoogleCalendarEventId.Equals("")) { using (GoogleCalendarCall gcal = new GoogleCalendarCall()) { gcal.RemoveParticipant(calendarId, activity.GoogleCalendarEventId, student.UserName); State.log.WriteLine("Removed {0} {1} from Calendar Event."); } } } else { State.log.WriteLine("Atempting to sign up {0} {1} for {2}", student.FirstName, student.LastName, activity.Name); if (student.StudentSignups.Where(sig => sig.ActivityId == ActivityId).Count() > 0) { WebhostMySQLConnection.StudentSignup signup = student.StudentSignups.Where(sig => sig.ActivityId == ActivityId).Single(); if (signup.IsBanned) { State.log.WriteLine("Signup was blocked."); LogWarning("{0} {1} was blocked from signing up for {2} because they have been banned.", student.FirstName, student.LastName, activity.Name); MailControler.MailToUser("Signup was blocked.", String.Format("You are not allowed to sign up for {0}", activity.Name), ((BasePage)Page).user); return; } signup.IsRescended = false; signup.TimeStamp = DateTime.Now; Signup.Text = "Sign me up!"; State.log.WriteLine("Re-signed up!"); LogInformation("{0} {1} has resigned up for {2}.", student.FirstName, student.LastName, activity.Name); } else { WebhostMySQLConnection.StudentSignup newSig = new WebhostMySQLConnection.StudentSignup() { StudentId = studentId, ActivityId = ActivityId, IsBanned = false, IsRescended = false, TimeStamp = DateTime.Now }; db.StudentSignups.Add(newSig); State.log.WriteLine("New Signup created."); LogInformation("{0} {1} has signed up for {2}.", student.FirstName, student.LastName, activity.Name); } if (!activity.GoogleCalendarEventId.Equals("")) { using (GoogleCalendarCall call = new GoogleCalendarCall()) { call.AddEventParticipant(calendarId, activity.GoogleCalendarEventId, student.UserName); State.log.WriteLine("Updated calendar Event to include {0}", student.UserName); } } db.SaveChanges(); } State.log.WriteLine("Signup Changes Saved to Database."); reload(); } }