private List <GroupType> GetTopGroupTypes(RockContext rockContext) { var groupTypes = new List <GroupType>(); // populate the GroupType DropDownList only with GroupTypes with GroupTypePurpose of Check-in Template // or with group types that allow multiple locations/schedules and support named locations int groupTypePurposeCheckInTemplateId = DefinedValueCache.Get(new Guid(Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE)).Id; GroupTypeService groupTypeService = new GroupTypeService(rockContext); // First find all the group types that have a purpose of 'Check-in Template' var checkInGroupTypeIds = groupTypeService.Queryable() .Where(t => t.GroupTypePurposeValueId.HasValue && t.GroupTypePurposeValueId.Value == groupTypePurposeCheckInTemplateId) .Select(t => t.Id) .ToList(); // Now find all their descendants (so we can exclude them in a sec) var descendentGroupTypeIds = new List <int>(); foreach (int id in checkInGroupTypeIds) { descendentGroupTypeIds.AddRange(groupTypeService.GetCheckinAreaDescendants(id).Select(a => a.Id).ToList()); } // Now query again for all the types that have a purpose of 'Check-in Template' or support check-in outside of being a descendant of the template var groupTypeList = groupTypeService.Queryable() .Where(a => checkInGroupTypeIds.Contains(a.Id) || ( !descendentGroupTypeIds.Contains(a.Id) && a.AllowMultipleLocations && a.EnableLocationSchedules.HasValue && a.EnableLocationSchedules.Value && a.LocationTypes.Any() )) .OrderBy(a => a.Order) .ThenBy(a => a.Name) .ToList(); foreach (var groupType in groupTypeList) { // Make sure the group type supports named locations (we can't query on this in the above qry) if (groupType.GroupTypePurposeValueId == groupTypePurposeCheckInTemplateId || (groupType.LocationSelectionMode & GroupLocationPickerMode.Named) == GroupLocationPickerMode.Named) { groupTypes.Add(groupType); } } return(groupTypes); }
/// <summary> /// Binds the grid. /// </summary> protected void BindGrid() { AddScheduleColumns(); var rockContext = new RockContext(); var groupLocationService = new GroupLocationService(rockContext); var groupTypeService = new GroupTypeService(rockContext); var groupService = new GroupService(rockContext); var groupLocationQry = groupLocationService.Queryable(); var templateGroupPaths = new Dictionary <int, List <CheckinAreaPath> >(); var currentAndDescendantGroupTypeIds = new List <int>(); foreach (var groupType in groupTypeService.Queryable().Where(a => this.LocalDeviceConfig.CurrentGroupTypeIds.Contains(a.Id))) { foreach (var parentGroupType in groupType.ParentGroupTypes) { if (!templateGroupPaths.ContainsKey(parentGroupType.Id)) { templateGroupPaths.Add(parentGroupType.Id, groupTypeService.GetCheckinAreaDescendantsPath(parentGroupType.Id).ToList()); } } currentAndDescendantGroupTypeIds.Add(groupType.Id); currentAndDescendantGroupTypeIds.AddRange(groupTypeService.GetCheckinAreaDescendants(groupType.Id).Select(a => a.Id).ToList()); } var groupPaths = new List <CheckinAreaPath>(); foreach (var path in templateGroupPaths) { groupPaths.AddRange(path.Value); } groupLocationQry = groupLocationQry.Where(a => currentAndDescendantGroupTypeIds.Contains(a.Group.GroupTypeId)); groupLocationQry = groupLocationQry.OrderBy(a => a.Group.Name).ThenBy(a => a.Location.Name); List <int> currentDeviceLocationIdList = this.GetGroupTypesLocations(rockContext).Select(a => a.Id).Distinct().ToList(); var qryList = groupLocationQry .Where(a => currentDeviceLocationIdList.Contains(a.LocationId)) .Select(a => new { GroupLocationId = a.Id, a.Location, GroupId = a.GroupId, GroupName = a.Group.Name, ScheduleIdList = a.Schedules.Select(s => s.Id), GroupTypeId = a.Group.GroupTypeId }).ToList(); var locationService = new LocationService(rockContext); // put stuff in a datatable so we can dynamically have columns for each Schedule DataTable dataTable = new DataTable(); dataTable.Columns.Add("GroupLocationId"); dataTable.Columns.Add("GroupId"); dataTable.Columns.Add("GroupName"); dataTable.Columns.Add("GroupPath"); dataTable.Columns.Add("LocationName"); dataTable.Columns.Add("LocationPath"); foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>()) { dataTable.Columns.Add(field.DataField, typeof(bool)); } var locationPaths = new Dictionary <int, string>(); foreach (var row in qryList) { DataRow dataRow = dataTable.NewRow(); dataRow["GroupLocationId"] = row.GroupLocationId; dataRow["GroupName"] = groupService.GroupAncestorPathName(row.GroupId); dataRow["GroupPath"] = groupPaths.Where(gt => gt.GroupTypeId == row.GroupTypeId).Select(gt => gt.Path).FirstOrDefault(); dataRow["LocationName"] = row.Location.Name; if (row.Location.ParentLocationId.HasValue) { int locationId = row.Location.ParentLocationId.Value; if (!locationPaths.ContainsKey(locationId)) { var locationNames = new List <string>(); var parentLocation = locationService.Get(locationId); while (parentLocation != null) { locationNames.Add(parentLocation.Name); parentLocation = parentLocation.ParentLocation; } if (locationNames.Any()) { locationNames.Reverse(); locationPaths.Add(locationId, locationNames.AsDelimited(" > ")); } else { locationPaths.Add(locationId, string.Empty); } } dataRow["LocationPath"] = locationPaths[locationId]; } foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>()) { int scheduleId = int.Parse(field.DataField.Replace("scheduleField_", string.Empty)); dataRow[field.DataField] = row.ScheduleIdList.Any(a => a == scheduleId); } dataTable.Rows.Add(dataRow); } gGroupLocationSchedule.EntityTypeId = EntityTypeCache.Get <GroupLocation>().Id; gGroupLocationSchedule.DataSource = dataTable; gGroupLocationSchedule.DataBind(); }
/// <summary> /// Gets the expression. /// </summary> /// <param name="entityType">Type of the entity.</param> /// <param name="serviceInstance">The service instance.</param> /// <param name="parameterExpression">The parameter expression.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression(Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection) { string[] options = selection.Split('|'); if (options.Length < 4) { return(null); } Guid groupTypeGuid = options[0].AsGuid(); ComparisonType comparisonType = options[1].ConvertToEnum <ComparisonType>(ComparisonType.GreaterThanOrEqualTo); int? attended = options[2].AsIntegerOrNull(); string slidingDelimitedValues; if (options[3].AsIntegerOrNull().HasValue) { //// selection was from when it just simply a LastXWeeks instead of Sliding Date Range // Last X Weeks was treated as "LastXWeeks * 7" days, so we have to convert it to a SlidingDateRange of Days to keep consistent behavior int lastXWeeks = options[3].AsIntegerOrNull() ?? 1; var fakeSlidingDateRangePicker = new SlidingDateRangePicker(); fakeSlidingDateRangePicker.SlidingDateRangeMode = SlidingDateRangePicker.SlidingDateRangeType.Last; fakeSlidingDateRangePicker.TimeUnit = SlidingDateRangePicker.TimeUnitType.Day; fakeSlidingDateRangePicker.NumberOfTimeUnits = lastXWeeks * 7; slidingDelimitedValues = fakeSlidingDateRangePicker.DelimitedValues; } else { slidingDelimitedValues = options[3].Replace(',', '|'); } var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues(slidingDelimitedValues); bool includeChildGroupTypes = options.Length >= 5 ? options[4].AsBooleanOrNull() ?? false : false; var groupTypeService = new GroupTypeService(new RockContext()); var groupType = groupTypeService.Get(groupTypeGuid); List <int> groupTypeIds = new List <int>(); if (groupType != null) { groupTypeIds.Add(groupType.Id); if (includeChildGroupTypes) { var childGroupTypes = groupTypeService.GetCheckinAreaDescendants(groupType.Id); if (childGroupTypes.Any()) { groupTypeIds.AddRange(childGroupTypes.Select(a => a.Id)); // get rid of any duplicates groupTypeIds = groupTypeIds.Distinct().ToList(); } } } var rockContext = serviceInstance.Context as RockContext; var attendanceQry = new AttendanceService(rockContext).Queryable().Where(a => a.DidAttend.HasValue && a.DidAttend.Value); if (dateRange.Start.HasValue) { var startDate = dateRange.Start.Value; attendanceQry = attendanceQry.Where(a => a.Occurrence.OccurrenceDate >= startDate); } if (dateRange.End.HasValue) { var endDate = dateRange.End.Value; attendanceQry = attendanceQry.Where(a => a.Occurrence.OccurrenceDate < endDate); } if (groupTypeIds.Count == 1) { int groupTypeId = groupTypeIds[0]; attendanceQry = attendanceQry.Where(a => a.Occurrence.Group.GroupTypeId == groupTypeId); } else if (groupTypeIds.Count > 1) { attendanceQry = attendanceQry.Where(a => groupTypeIds.Contains(a.Occurrence.Group.GroupTypeId)); } else { // no group type selected, so return nothing return(Expression.Constant(false)); } var qry = new PersonService(rockContext).Queryable() .Where(p => attendanceQry.Where(xx => xx.PersonAlias.PersonId == p.Id).Count() == attended); BinaryExpression compareEqualExpression = FilterExpressionExtractor.Extract <Rock.Model.Person>(qry, parameterExpression, "p") as BinaryExpression; BinaryExpression result = FilterExpressionExtractor.AlterComparisonType(comparisonType, compareEqualExpression, null); return(result); }
/// <summary> /// Binds the grid. /// </summary> protected void BindGrid() { AddScheduleColumns(); var rockContext = new RockContext(); var groupLocationService = new GroupLocationService(rockContext); var groupTypeService = new GroupTypeService(rockContext); var groupService = new GroupService(rockContext); var groupPaths = new List <CheckinAreaPath>(); var groupLocationQry = groupLocationService.Queryable().Where(gl => gl.Group.IsActive && !gl.Group.IsArchived); int groupTypeId; // if this page has a PageParam for groupTypeId use that to limit which groupTypeId to see. Otherwise, use the groupTypeId specified in the filter if (_groupTypeId.HasValue) { groupTypeId = _groupTypeId.Value; } else { groupTypeId = ddlGroupType.SelectedValueAsInt() ?? Rock.Constants.All.Id; } if (groupTypeId != Rock.Constants.All.Id) { var descendantGroupTypeIds = groupTypeService.GetCheckinAreaDescendants(groupTypeId).Select(a => a.Id); // filter to groups that either are of the GroupType or are of a GroupType that has the selected GroupType as a parent (ancestor) groupLocationQry = groupLocationQry.Where(a => a.Group.GroupType.Id == groupTypeId || descendantGroupTypeIds.Contains(a.Group.GroupTypeId)); groupPaths = groupTypeService.GetCheckinAreaDescendantsPath(groupTypeId).ToList(); } else { List <int> descendantGroupTypeIds = new List <int>(); foreach (GroupType groupType in GetTopGroupTypes(rockContext)) { descendantGroupTypeIds.Add(groupType.Id); groupPaths.AddRange(groupTypeService.GetCheckinAreaDescendantsPath(groupType.Id).ToList()); foreach (var childGroupType in groupTypeService.GetChildGroupTypes(groupType.Id)) { descendantGroupTypeIds.Add(childGroupType.Id); descendantGroupTypeIds.AddRange(groupTypeService.GetCheckinAreaDescendants(childGroupType.Id).Select(a => a.Id).ToList()); } } groupLocationQry = groupLocationQry.Where(a => descendantGroupTypeIds.Contains(a.Group.GroupTypeId)); } if (gGroupLocationSchedule.SortProperty != null) { groupLocationQry = groupLocationQry.Sort(gGroupLocationSchedule.SortProperty); } else { groupLocationQry = groupLocationQry.OrderBy(a => a.Group.Name).ThenBy(a => a.Location.Name); } var qryList = groupLocationQry .Where(a => a.Location != null) .Select(a => new { GroupLocationId = a.Id, a.Location, GroupId = a.GroupId, GroupName = a.Group.Name, ScheduleIdList = a.Schedules.Select(s => s.Id), GroupTypeId = a.Group.GroupTypeId }).ToList(); var locationService = new LocationService(rockContext); int parentLocationId = pkrParentLocation.SelectedValueAsInt() ?? Rock.Constants.All.Id; if (parentLocationId != Rock.Constants.All.Id) { var currentAndDescendantLocationIds = new List <int>(); currentAndDescendantLocationIds.Add(parentLocationId); currentAndDescendantLocationIds.AddRange(locationService.GetAllDescendents(parentLocationId).Select(a => a.Id)); qryList = qryList.Where(a => currentAndDescendantLocationIds.Contains(a.Location.Id)).ToList(); } // put stuff in a DataTable so we can dynamically have columns for each Schedule DataTable dataTable = new DataTable(); dataTable.Columns.Add("GroupLocationId"); dataTable.Columns.Add("GroupId"); dataTable.Columns.Add("GroupName"); dataTable.Columns.Add("GroupPath"); dataTable.Columns.Add("LocationName"); dataTable.Columns.Add("LocationPath"); foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>()) { dataTable.Columns.Add(field.DataField, typeof(bool)); } var locationPaths = new Dictionary <int, string>(); foreach (var row in qryList) { DataRow dataRow = dataTable.NewRow(); dataRow["GroupLocationId"] = row.GroupLocationId; dataRow["GroupName"] = groupService.GroupAncestorPathName(row.GroupId); dataRow["GroupPath"] = groupPaths.Where(gt => gt.GroupTypeId == row.GroupTypeId).Select(gt => gt.Path).FirstOrDefault(); dataRow["LocationName"] = row.Location.Name; if (row.Location.ParentLocationId.HasValue) { int locationId = row.Location.ParentLocationId.Value; if (!locationPaths.ContainsKey(locationId)) { var locationNames = new List <string>(); var parentLocation = locationService.Get(locationId); while (parentLocation != null) { locationNames.Add(parentLocation.Name); parentLocation = parentLocation.ParentLocation; } if (locationNames.Any()) { locationNames.Reverse(); locationPaths.Add(locationId, locationNames.AsDelimited(" > ")); } else { locationPaths.Add(locationId, string.Empty); } } dataRow["LocationPath"] = locationPaths[locationId]; } foreach (var field in gGroupLocationSchedule.Columns.OfType <CheckBoxEditableField>()) { int scheduleId = int.Parse(field.DataField.Replace("scheduleField_", string.Empty)); dataRow[field.DataField] = row.ScheduleIdList.Any(a => a == scheduleId); } dataTable.Rows.Add(dataRow); } gGroupLocationSchedule.EntityTypeId = EntityTypeCache.Get <GroupLocation>().Id; gGroupLocationSchedule.DataSource = dataTable; gGroupLocationSchedule.DataBind(); }