/// <summary> /// Gets the occurrences. /// </summary> /// <param name="icalEvent">The ical event.</param> /// <param name="startTime">The start time.</param> /// <param name="endTime">The end time.</param> /// <returns></returns> public static IList <Occurrence> GetOccurrences(DDay.iCal.Event icalEvent, DateTime startTime, DateTime endTime) { lock (ScheduleICalHelper._initLock) { return(icalEvent.GetOccurrences(startTime, endTime)); } }
/// <summary> /// Gets the next Check-in start date for this Schedule. /// </summary> /// <param name="beginDateTime">A <see cref="System.DateTimeOffset"/> representing the base date.</param> /// <returns>A <see cref="System.DateTime"/> containing the next time that Check-in begins for this schedule.</returns> public virtual DateTime? GetNextCheckInStartTime( DateTimeOffset beginDateTime ) { if ( !IsCheckInEnabled ) { return null; } // Get the effective start datetime if there's not a specific effective // start time DateTime fromDate = beginDateTime.DateTime; if ( EffectiveStartDate.HasValue && EffectiveStartDate.Value.CompareTo( fromDate ) > 0 ) { fromDate = EffectiveStartDate.Value; } DateTime? nextStartTime = null; DDay.iCal.Event calEvent = GetCalenderEvent(); if ( calEvent != null ) { var occurrences = calEvent.GetOccurrences( fromDate, fromDate.AddMonths( 1 ) ); if ( occurrences.Count > 0 ) { var nextOccurance = occurrences[0]; nextStartTime = nextOccurance.Period.StartTime.Date.AddMinutes( 0 - CheckInStartOffsetMinutes.Value ); } } // If no start time was found, return null if ( !nextStartTime.HasValue ) { return null; } // If the Effective end date is prior to next start time, return null if ( EffectiveEndDate.HasValue && EffectiveEndDate.Value.CompareTo( nextStartTime.Value ) < 0 ) { return null; } return nextStartTime.Value; }
/// <summary> /// Gets the Friendly Text of the Calendar Event. /// For example, "Every 3 days at 10:30am", "Monday, Wednesday, Friday at 5:00pm", "Saturday at 4:30pm" /// </summary> /// <param name="condensed">if set to <c>true</c> [condensed].</param> /// <returns> /// A <see cref="System.String" /> containing a friendly description of the Schedule. /// </returns> public string ToFriendlyScheduleText(bool condensed) { // init the result to just the schedule name just in case we can't figure out the FriendlyText string result = this.Name; DDay.iCal.Event calendarEvent = this.GetCalenderEvent(); if (calendarEvent != null && calendarEvent.DTStart != null) { string startTimeText = calendarEvent.DTStart.Value.TimeOfDay.ToTimeString(); if (calendarEvent.RecurrenceRules.Any()) { // some type of recurring schedule IRecurrencePattern rrule = calendarEvent.RecurrenceRules[0]; switch (rrule.Frequency) { case FrequencyType.Daily: result = "Daily"; if (rrule.Interval > 1) { result += string.Format(" every {0} days", rrule.Interval); } result += " at " + startTimeText; break; case FrequencyType.Weekly: result = rrule.ByDay.Select(a => a.DayOfWeek.ConvertToString()).ToList().AsDelimited(","); if (string.IsNullOrEmpty(result)) { // no day selected, so it has an incomplete schedule return("No Scheduled Days"); } if (rrule.Interval > 1) { result += string.Format(" every {0} weeks", rrule.Interval); } result += " at " + startTimeText; break; case FrequencyType.Monthly: if (rrule.ByMonthDay.Count > 0) { // Day X of every X Months (we only support one day in the ByMonthDay list) int monthDay = rrule.ByMonthDay[0]; result = string.Format("Day {0} of every ", monthDay); if (rrule.Interval > 1) { result += string.Format("{0} months", rrule.Interval); } else { result += "month"; } result += " at " + startTimeText; } else if (rrule.ByDay.Count > 0) { // The Nth <DayOfWeekName> (we only support one day in the ByDay list) IWeekDay bydate = rrule.ByDay[0]; if (NthNames.ContainsKey(bydate.Offset)) { result = string.Format("The {0} {1} of every month", NthNames[bydate.Offset], bydate.DayOfWeek.ConvertToString()); } else { // unsupported case (just show the name) } result += " at " + startTimeText; } else { // unsupported case (just show the name) } break; default: // some other type of recurring type (probably specific dates). Just return the Name of the schedule break; } } else { // not any type of recurring, might be one-time or from specific dates, etc var dates = calendarEvent.GetOccurrences(DateTime.MinValue, DateTime.MaxValue).Where(a => a.Period != null && a.Period.StartTime != null) .Select(a => a.Period.StartTime.Value) .OrderBy(a => a).ToList(); if (dates.Count() > 1) { if (condensed || dates.Count() > 99) { result = string.Format("Multiple dates between {0} and {1}", dates.First().ToShortDateString(), dates.Last().ToShortDateString()); } else { var listHtml = "<ul class='list-unstyled'>" + Environment.NewLine; foreach (var date in dates) { listHtml += string.Format("<li>{0}</li>", date.ToShortDateTimeString()) + Environment.NewLine; } listHtml += "</ul>"; result = listHtml; } } else if (dates.Count() == 1) { result = "Once at " + calendarEvent.DTStart.Value.ToShortDateTimeString(); } else { return("No Schedule"); } } } else { if (WeeklyDayOfWeek.HasValue) { result = WeeklyDayOfWeek.Value.ConvertToString(); if (WeeklyTimeOfDay.HasValue) { result += " at " + WeeklyTimeOfDay.Value.ToTimeString(); } } else { // no start time. Nothing scheduled return("No Schedule"); } } return(result); }
/// <summary> /// Get scheduled events into calendar /// </summary> /// <returns>Formatted Json events</returns> public JsonResult GetEvents( DateTime start, DateTime end,int id = 0) { IEnumerable<GroupInstance> groupInstances; if (id > 0) { var filteredGroups =db.GroupInstances.Include(e => e.Group) .Include(e => e.Group.Users) .ToList() .Where(x => x.Group.Users.Where(y => y.UserId == id &&(((x.StartDateTime>=start ||x.EndDateTime<=end) &&x.RecurrenceRule==null) ||x.RecurrenceRule!=null)).Any()); groupInstances = filteredGroups; } else { groupInstances = db.GroupInstances.Include(e => e.Group) .Include(e=>e.Group.Users).Where(x=>((x.StartDateTime>=start ||x.EndDateTime<=end)&&x.RecurrenceRule==null)||x.RecurrenceRule!=null).ToList(); } if (groupInstances.Any()) { var recurrence = groupInstances.Where(x => !String.IsNullOrWhiteSpace(x.RecurrenceRule)).FirstOrDefault(); var list = new List<dynamic>(); foreach (var instance in groupInstances) { if (!String.IsNullOrWhiteSpace(instance.RecurrenceRule)) { iCalendarSerializer serializer = new iCalendarSerializer(); iCalendarCollection ical = new iCalendarCollection(); using (TextReader tr = new StringReader(instance.RecurrenceRule)) { ical = (iCalendarCollection)serializer.Deserialize(tr); } Event ev = new Event(); if (ical.Count == 0) { RecurrencePattern rp = new RecurrencePattern(instance.RecurrenceRule); ev.RecurrenceRules.Add(rp); } else { ev = (Event)ical.First().Events.First(); } var ex = ev.ExceptionDates; ev.Start = new iCalDateTime(instance.StartDateTime); var occ = ev.GetOccurrences(start.AddDays(-1), end); if (occ != null) { foreach (var occurence in occ) { var user = instance.Group.Users.FirstOrDefault(y => Roles.IsUserInRole(y.UserName, "Teacher")); var color = "#FFFFFF"; if (user != null) { color = user.HexColor; } list.Add(new { title = instance.Group.Name, start = CreateNewDateTime(occurence.Period.StartTime, instance.StartDateTime).ToString("s"), end = CreateNewDateTime(occurence.Period.EndTime, instance.EndDateTime).ToString("s"), editable = false, GroupInstanceId = instance.GroupInstanceId, ClassroomId = instance.ClassroomId, GroupId = instance.GroupId, Color = color, RecurrenceRule = recurrence.RecurrenceRule }); } } } else { list.Add(new { title = instance.Group.Name, start = instance.StartDateTime.ToString("s"), end = instance.EndDateTime.ToString("s"), editable = false, GroupInstanceId = instance.GroupInstanceId, ClassroomId = instance.ClassroomId, GroupId = instance.GroupId, Color = instance.Group.Users.FirstOrDefault(y => Roles.IsUserInRole(y.UserName, "Teacher")).HexColor }); } } return Json(list, JsonRequestBehavior.AllowGet); } else { return Json(null, JsonRequestBehavior.AllowGet); } }