Exemple #1
0
        public void Update()
        {
            UpdateTime = DateTime.UtcNow;

            var svc       = new TimetableService();
            var courseUri = new Uri("GetCoursesByStudent?id=" + Id, UriKind.Relative);
            var groupUri  = new Uri("GetGroupsByStudent?id=" + Id, UriKind.Relative);
            var responses = svc.ExecuteBatch(new DataServiceRequest <TTCourse>(courseUri),
                                             new DataServiceRequest <TTGroup>(groupUri));

            Courses = new HashSet <Course>();
            Groups  = new Dictionary <Course, Group>();

            foreach (QueryOperationResponse response in responses)
            {
                if (response.Query.ElementType == typeof(TTCourse))
                {
                    foreach (TTCourse ttc in response)
                    {
                        AddCourse(ttc);
                    }
                }
                else if (response.Query.ElementType == typeof(TTGroup))
                {
                    foreach (TTGroup ttg in response)
                    {
                        var course = Courses.FirstOrDefault(c => c.CatalogNumber == ttg.CatalogNumber);
                        if (course != null)
                        {
                            Groups[course] = new Group(ttg.ID, ttg.Identifier, course);
                        }
                    }
                }
            }
        }
Exemple #2
0
        protected override IEnumerable <Event> DownloadEvents(Week week)
        {
            var events = new List <Event>();

            var svc          = new TimetableService();
            var academicYear = week.Number >= 36 ? week.Year : week.Year - 1;
            var firstWeek    = new Week(academicYear, 36);
            var academicWeek = (int)Math.Round((week.StartTime - firstWeek.StartTime).TotalDays / 7);
            var activityUri  = new Uri("GetActivitiesByStudent?id=" + Student.Id +
                                       "&week=" + academicWeek + "&acyear=" + academicYear,
                                       UriKind.Relative);

            var activityResponse      = svc.Execute <TTActivity>(activityUri);
            var locationStaffRequests = new List <DataServiceRequest>();

            if (activityResponse != null)
            {
                foreach (TTActivity tta in activityResponse)
                {
                    var startTime = week.StartTime.AddDays(Math.Log(tta.Day) / Math.Log(2)).AddHours(tta.StartTime);
                    var duration  = TimeSpan.FromHours(tta.Duration);
                    var course    = Student.Courses.FirstOrDefault(c => tta.Name.Contains(c.CatalogNumber));
                    // If we don't know about this course, the student information needs to be updated
                    if (course == null)
                    {
                        Student.Update();
                        course = Student.Courses.FirstOrDefault(c => tta.Name.Contains(c.CatalogNumber));
                    }
                    var groups = new List <Group>();
                    if (course != null)
                    {
                        Group group;
                        if (Student.Groups.TryGetValue(course, out group) && tta.Groups.Contains(group.Identifier))
                        {
                            groups.Add(group);
                        }
                    }

                    if (course == null)
                    {
                        course = new Course(0, tta.Name, tta.Description, academicYear);
                    }

                    events.Add(new Event(tta.ID, startTime, duration, course, groups, tta.ActivityType,
                                         tta.Description, new HashSet <Location>(), new List <string>()));

                    locationStaffRequests.Add(new DataServiceRequest <TTLocation>(
                                                  new Uri("GetLocationsByActivity?id=" + tta.ID, UriKind.Relative)));

                    locationStaffRequests.Add(new DataServiceRequest <TTStaff>(
                                                  new Uri("GetStaffByActivity?id=" + tta.ID, UriKind.Relative)));
                }

                if (locationStaffRequests.Count > 0)
                {
                    var locationResponses = svc.ExecuteBatch(locationStaffRequests.ToArray());

                    foreach (QueryOperationResponse response in locationResponses)
                    {
                        int activityId = int.Parse(response.Query.RequestUri.ToString().Split('=').Last());
                        var ev         = events.Find(e => e.Id == activityId);
                        if (response.Query.ElementType == typeof(TTLocation))
                        {
                            foreach (TTLocation ttl in response)
                            {
                                ev.Locations.Add(new Location(ttl.ID, ttl.Name, ttl.InfoURL));
                            }
                        }
                        else if (response.Query.ElementType == typeof(TTStaff))
                        {
                            foreach (TTStaff tts in response)
                            {
                                ev.Staff.Add(tts.Name);
                            }
                        }
                    }
                }
            }

            return(events);
        }