Esempio n. 1
0
        /// <summary>
        /// Synchronize all student signups to the Google Calendar.
        /// </summary>
        /// <param name="WeekendId"></param>
        /// <param name="log"></param>
        public static void SyncronizeStudentSignups(int WeekendId, StreamWriter log = null)
        {
            if (log == null)
            {
                log           = new StreamWriter(Console.OpenStandardOutput());
                log.AutoFlush = true;
                log.WriteLine("Hello Console!");
            }

            using (WebhostEntities db = new WebhostEntities())
            {
                using (GoogleCalendarCall gcal = new GoogleCalendarCall())
                {
                    Weekend weekend = db.Weekends.Where(w => w.id == WeekendId).Single();
                    log.WriteLine("Syncing Student Signups for {0} Weekend {1}", weekend.DutyTeam.Name, weekend.StartDate.ToShortDateString());
                    String activitiesCalendarId = db.GoogleCalendars.Where(c => c.CalendarName.Equals("Weekend Activities")).Single().CalendarId;
                    foreach (WeekendActivity activity in weekend.WeekendActivities.ToList())
                    {
                        log.WriteLine("Updating Student Signups for {0}, {1} {2}", activity.Name, activity.DateAndTime.ToLongDateString(), activity.DateAndTime.ToShortTimeString());
                        List <String> students = gcal.GetEventParticipants(activitiesCalendarId, activity.GoogleCalendarEventId).Where(un => un.Contains("_")).ToList();

                        foreach (StudentSignup signup in activity.StudentSignups.ToList())
                        {
                            if (students.Contains(signup.Student.UserName))
                            {
                                log.WriteLine("{0} {1} is already on the calendar.", signup.Student.FirstName, signup.Student.LastName);
                                students.Remove(signup.Student.UserName);
                            }
                            String status = signup.IsBanned?"declined":"accepted";
                            log.WriteLine("Status:  {0}", status);
                            gcal.AddEventParticipant(activitiesCalendarId, activity.GoogleCalendarEventId, signup.Student.UserName, status, false, signup.IsBanned ? "Not Allowed" : "");
                        }

                        foreach (String toRemove in students)
                        {
                            gcal.RemoveParticipant(activitiesCalendarId, activity.GoogleCalendarEventId, toRemove);
                            log.WriteLine("Removed {0} from Event.", toRemove);
                        }
                    }
                }
            }
        }
        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();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Publish all the Weekend Items to the Weekend Duty and Weekend Activities calendars.
        /// </summary>
        /// <param name="WeekendId"></param>
        /// <param name="log">Pass your favorite Log object here in the Web context, or leave it blank to use the Console Standard output.</param>
        public static void PublishWeekendScheduleToGoogleCalendars(int WeekendId, StreamWriter log = null)
        {
            if (log == null)
            {
                log           = new StreamWriter(Console.OpenStandardOutput());
                log.AutoFlush = true;
                log.WriteLine("Hello Console!");
            }
            using (WebhostEntities db = new WebhostEntities())
            {
                using (GoogleCalendarCall gcal = new GoogleCalendarCall())
                {
                    Weekend weekend = db.Weekends.Where(w => w.id == WeekendId).Single();
                    log.WriteLine("Publishing {0} Weekend {1}", weekend.DutyTeam.Name, weekend.StartDate.ToShortDateString());
                    String dutyCalendarId       = db.GoogleCalendars.Where(c => c.CalendarName.Equals("Weekend Duty")).Single().CalendarId;
                    String activitiesCalendarId = db.GoogleCalendars.Where(c => c.CalendarName.Equals("Weekend Activities")).Single().CalendarId;

                    foreach (WeekendDuty duty in weekend.WeekendDuties.ToList())
                    {
                        if (duty.GoogleCalendarEventId.Equals(""))
                        {
                            log.WriteLine("Creating New Event for {0}, {1} {2}", duty.Name, duty.DateAndTime.ToLongDateString(), duty.DateAndTime.ToShortTimeString());
                            duty.GoogleCalendarEventId = gcal.PostEventToCalendar(dutyCalendarId, duty.Name, duty.DateAndTime, TimeSpan.FromMinutes(duty.Duration), duty.Description);
                            log.WriteLine("Got EventId:  {0}", duty.GoogleCalendarEventId);
                        }
                        else
                        {
                            log.WriteLine("Updating EventId: {0}", duty.GoogleCalendarEventId);
                            try
                            {
                                gcal.UpdateEvent(dutyCalendarId, duty.GoogleCalendarEventId, duty.Name, duty.DateAndTime, TimeSpan.FromMinutes(duty.Duration), duty.Description);
                            }
                            catch (GoogleAPICall.GoogleAPIException gae)
                            {
                                log.WriteLine(gae.Message);
                                log.WriteLine(gae.InnerException.Message);
                                log.WriteLine("Creating New Event for {0}, {1} {2}", duty.Name, duty.DateAndTime.ToLongDateString(), duty.DateAndTime.ToShortTimeString());
                                duty.GoogleCalendarEventId = gcal.PostEventToCalendar(dutyCalendarId, duty.Name, duty.DateAndTime, TimeSpan.FromMinutes(duty.Duration), duty.Description);
                                log.WriteLine("Got EventId:  {0}", duty.GoogleCalendarEventId);
                            }
                        }

                        List <String> participants = gcal.GetEventParticipants(dutyCalendarId, duty.GoogleCalendarEventId);
                        foreach (Faculty adult in duty.DutyTeamMembers.ToList())
                        {
                            if (participants.Contains(adult.UserName))
                            {
                                log.WriteLine("{0} {1} is already assigned to this Event.", adult.FirstName, adult.LastName);
                                participants.Remove(adult.UserName);
                            }
                            else
                            {
                                gcal.AddEventParticipant(dutyCalendarId, duty.GoogleCalendarEventId, adult.UserName);
                                log.WriteLine("Added {0} {1} to this event.", adult.FirstName, adult.LastName);
                            }
                        }

                        foreach (String toRemove in participants)
                        {
                            gcal.RemoveParticipant(dutyCalendarId, duty.GoogleCalendarEventId, toRemove);
                            log.WriteLine("Removed no longer assigned adult {0}", toRemove);
                        }
                    }

                    foreach (WeekendActivity activity in weekend.WeekendActivities.ToList())
                    {
                        if (activity.GoogleCalendarEventId.Equals(""))
                        {
                            log.WriteLine("Creating New Event for {0}, {1} {2}", activity.Name, activity.DateAndTime.ToLongDateString(), activity.DateAndTime.ToShortTimeString());
                            activity.GoogleCalendarEventId = gcal.PostEventToCalendar(activitiesCalendarId, activity.Name, activity.DateAndTime, TimeSpan.FromMinutes(activity.Duration), activity.Description);
                            log.WriteLine("Got EventId:  {0}", activity.GoogleCalendarEventId);
                        }
                        else
                        {
                            log.WriteLine("Updating EventId: {0}", activity.GoogleCalendarEventId);
                            try
                            {
                                gcal.UpdateEvent(activitiesCalendarId, activity.GoogleCalendarEventId, activity.Name, activity.DateAndTime, TimeSpan.FromMinutes(activity.Duration), activity.Description);
                            }
                            catch (GoogleAPICall.GoogleAPIException gae)
                            {
                                log.WriteLine(gae.Message);
                                log.WriteLine(gae.InnerException.Message);
                                log.WriteLine("Creating New Event for {0}, {1} {2}", activity.Name, activity.DateAndTime.ToLongDateString(), activity.DateAndTime.ToShortTimeString());
                                activity.GoogleCalendarEventId = gcal.PostEventToCalendar(activitiesCalendarId, activity.Name, activity.DateAndTime, TimeSpan.FromMinutes(activity.Duration), activity.Description);
                                log.WriteLine("Got EventId:  {0}", activity.GoogleCalendarEventId);
                            }
                        }

                        List <String> participants = gcal.GetEventParticipants(activitiesCalendarId, activity.GoogleCalendarEventId).Where(un => !un.Contains("_")).ToList();
                        foreach (Faculty adult in activity.Adults.ToList())
                        {
                            if (participants.Contains(adult.UserName))
                            {
                                log.WriteLine("{0} {1} is already assigned to this Event.", adult.FirstName, adult.LastName);
                                participants.Remove(adult.UserName);
                            }
                            else
                            {
                                gcal.AddEventParticipant(activitiesCalendarId, activity.GoogleCalendarEventId, adult.UserName);
                                log.WriteLine("Added {0} {1} to this event.", adult.FirstName, adult.LastName);
                            }
                        }

                        foreach (String toRemove in participants)
                        {
                            gcal.RemoveParticipant(activitiesCalendarId, activity.GoogleCalendarEventId, toRemove);
                            log.WriteLine("Removed no longer assigned adult {0}", toRemove);
                        }
                    }
                }

                db.SaveChanges();
            }
        }