Skip to content

Commit

Permalink
+ (Group) Fixed group scheduler block that was showing inactive locat…
Browse files Browse the repository at this point in the history
…ions and schedules. Fixes #4104
  • Loading branch information
Ben Wiley committed Mar 20, 2020
1 parent e23a376 commit 35a6d86
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
32 changes: 28 additions & 4 deletions Rock/Model/AttendanceService.Partial.cs
Expand Up @@ -1044,11 +1044,27 @@ public IEnumerable<SchedulerResource> GetSchedulerResources( SchedulerResourcePa
} )
.ToDictionary( k => k.PersonId, v => v.LastScheduledDate );

/*
* BJW 03-20-2020
*
* I added the where clauses in the following query that limit to active schedules or locations. The intent was to
* prevent people from being rescheduled that are scheduled for a location or schedule where that location or
* schedule has since become inactive.
*
* Consider this scenario:
* - Ted is scheduled for AV on Sunday at 12
* - The Sunday schedule for 12 is marked inactive
* - Alisha comes to the scheduler block to put Ted in another schedule but she cannot because:
* - Ted is "already scheduled" and doesn't show in the available people panel
* - Alisha cannot remove Ted's now invalid assignement because she can no longer see the inactive schedule
*/
var scheduledAttendanceGroupIdsLookup = attendanceService.Queryable()
.Where( a => ( a.RequestedToAttend == true || a.ScheduledToAttend == true )
&& a.Occurrence.ScheduleId == schedulerResourceParameters.AttendanceOccurrenceScheduleId
&& scheduleOccurrenceDateList.Contains( a.Occurrence.OccurrenceDate )
&& a.Occurrence.GroupId.HasValue )
&& a.Occurrence.GroupId.HasValue
&& ( a.Occurrence.Schedule == null || a.Occurrence.Schedule.IsActive )
&& ( a.Occurrence.Location == null || a.Occurrence.Location.IsActive ) )
.GroupBy( a => a.PersonAlias.PersonId )
.Select( a => new
{
Expand Down Expand Up @@ -1102,7 +1118,10 @@ public IEnumerable<SchedulerResource> GetSchedulerResources( SchedulerResourcePa
/// <returns></returns>
public IEnumerable<SchedulerResourceAttend> GetAttendingSchedulerResources( int attendanceOccurrenceId )
{
var conflictingScheduledAttendancesQuery = this.Queryable();
// Don't count scheduled attendances for inactive locations or inactive schedules as conflicting
var conflictingScheduledAttendancesQuery = this.Queryable().Where( a =>
( a.Occurrence.Schedule == null || a.Occurrence.Schedule.IsActive ) &&
( a.Occurrence.Location == null || a.Occurrence.Location.IsActive ) );

var attendanceOccurrenceInfo = new AttendanceOccurrenceService( this.Context as RockContext ).GetSelect( attendanceOccurrenceId, s => new
{
Expand Down Expand Up @@ -1208,7 +1227,7 @@ public void SchedulePersonsAutomatically( int groupId, DateTime sundayDate, Pers
var endDate = sundayDate.Date;

var groupLocationQry = new GroupLocationService( rockContext ).Queryable().Where( a => a.GroupId == groupId );
var scheduleList = groupLocationQry.SelectMany( a => a.Schedules ).Distinct().AsNoTracking().ToList();
var scheduleList = groupLocationQry.SelectMany( a => a.Schedules ).Where( s => s.IsActive ).Distinct().AsNoTracking().ToList();
var scheduleOccurrenceDateList = scheduleList
.Select( s => new
{
Expand Down Expand Up @@ -1245,7 +1264,12 @@ public void SchedulePersonsAutomaticallyForAttendanceOccurrences( List<int> atte
var rockContext = this.Context as RockContext;
var groupLocationQry = new GroupLocationService( rockContext ).Queryable();
var attendanceOccurrencesQry = new AttendanceOccurrenceService( rockContext ).GetByIds( attendanceOccurrenceIdList )
.Where( a => a.GroupId.HasValue && a.LocationId.HasValue && a.ScheduleId.HasValue );
.Where( ao =>
ao.GroupId.HasValue &&
ao.LocationId.HasValue &&
ao.ScheduleId.HasValue &&
ao.Location.IsActive &&
ao.Schedule.IsActive );

// sort the occurrences by the Date, then by the associated GroupLocation.Order then by Location.name
var sortedAttendanceOccurrenceList = attendanceOccurrencesQry
Expand Down
13 changes: 11 additions & 2 deletions RockWeb/Blocks/GroupScheduling/GroupScheduler.ascx.cs
Expand Up @@ -213,7 +213,13 @@ private void UpdateScheduleList()
{
var groupLocations = group.GroupLocations.ToList();

var groupSchedules = groupLocations.SelectMany( a => a.Schedules ).DistinctBy( a => a.Guid ).ToList();
var groupSchedules = groupLocations
.Where( gl => gl.Location.IsActive )
.SelectMany( gl => gl.Schedules )
.Where( s => s.IsActive )
.DistinctBy( a => a.Guid )
.ToList();

if ( !groupSchedules.Any() )
{
nbGroupWarning.Text = "Group does not have any locations or schedules";
Expand Down Expand Up @@ -454,7 +460,10 @@ private void UpdateGroupLocationList()

var rockContext = new RockContext();
var groupLocationsQuery = new GroupLocationService( rockContext ).Queryable()
.Where( a => a.GroupId == group.Id && a.Schedules.Any( s => s.Id == scheduleId ) )
.Where( gl =>
gl.GroupId == group.Id &&
gl.Schedules.Any( s => s.Id == scheduleId ) &&
gl.Location.IsActive )
.OrderBy( a => new { a.Order, a.Location.Name } )
.AsNoTracking();

Expand Down

0 comments on commit 35a6d86

Please sign in to comment.