public ActionResult ViewActivities(UserToActivityModel model) { //inactivate checkins over an hour old foreach (var row in db.Checkin.Where(n => n.Active == true)) { if (DateTime.Now >= row.CheckinTime.AddHours(1)) { row.Active = false; } } //inactivate activities past their expiration times foreach (var row in db.Activity.Where(n => n.Active == true)) { if (DateTime.Now >= row.TimeEnd) { row.Active = false; } } db.SaveChanges(); var UserId = System.Web.HttpContext.Current.User.Identity.GetUserId(); MainPageViewModel main = new MainPageViewModel(); main.Messages = new List <string>(); main.ActivityJoined = model.Id; main.UserId = UserId; // find out if user is checked in foreach (var checkin in db.Checkin.Where(n => n.Active)) { if (checkin.UserId == UserId) { main.CheckedIn = true; break; } else { main.CheckedIn = false; } } // create available activities main.Activities_Invitees = new List <Activity_InviteesViewModel>(); main.UserToActivity = new List <UserToActivityModel>(); foreach (var row in db.UserToActivity.Where(n => n.UserId == UserId)) { main.UserToActivity.Add(row); } ActivitiesViewModel activities = new ActivitiesViewModel(); activities.Activities = new List <ActivityModel>(); foreach (var activity in db.Activity.Where(n => n.Active == true)) { activities.Activities.Add(activity); } try { double userLatitude = (from x in db.Checkin where x.UserId == UserId && x.Active == true select x.Latitude).First(); double userLongitude = (from x in db.Checkin where x.UserId == UserId && x.Active == true select x.Longitude).First(); foreach (var activity in activities.Activities) { //find activites within creator's specified area double activityDistance = DistanceFinder.FindActivitiesDistance(userLatitude, userLongitude, activity.Latitude, activity.Longitude); if (activityDistance <= activity.Area) { Activity_InviteesViewModel activity_Invitees = new Activity_InviteesViewModel(); activity_Invitees.Activity = activity; activity_Invitees.Distance = DistanceFinder.ConvertActivityDistance(activityDistance); activity_Invitees.timeStart = activity_Invitees.Activity.TimeStart.ToShortTimeString(); activity_Invitees.timeEnd = activity_Invitees.Activity.TimeEnd.ToShortTimeString(); //find users that joined that same activity. List <string> invitees = new List <string>(); foreach (var invitee in db.UserToActivity.Include(n => n.User)) { if (invitee.ActivityId == activity.Id && invitee.UserId != null) { //Ratings Joiner joiner = new Joiner(invitee.User.FirstName, invitee.User.Rating, invitee.User.RatingCount, invitee.Message); string listInvitee = joiner.CreateRatingString(); invitees.Add(listInvitee); //Messages if (invitee.Message != null) { string message = joiner.CreateMessageString(); main.Messages.Add(message); } } } activity_Invitees.Invitees = new SelectList(invitees); main.Activities_Invitees.Add(activity_Invitees); ViewData["Joined"] = new SelectList(invitees); foreach (var row in main.UserToActivity) { if (row.ActivityId == activity.Id) { main.ActivityJoined = row.ActivityId; } } } } } catch { } return(View(main)); }