Exemple #1
0
        /// <summary>
        /// Gets the active registration instance.
        /// </summary>
        /// <param name="registrationInstanceId">The registration instance identifier.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <returns></returns>
        private RegistrationInstance GetActiveRegistrationInstance(int registrationInstanceId, out string errorMessage)
        {
            errorMessage = string.Empty;

            var now = RockDateTime.Now;
            var registrationInstanceService = new RegistrationInstanceService(Context as RockContext);
            var registrationInstance        = registrationInstanceService.Get(registrationInstanceId);
            var registrationTemplate        = registrationInstance?.RegistrationTemplate;

            // Ensure that the registration entities are active
            if (registrationInstance is null || registrationTemplate is null)
            {
                errorMessage = "We could not find the item you are looking for.";
                return(null);
            }
Exemple #2
0
        /// <summary>
        /// Get a list of all inherited Attributes that should be applied to this entity.
        /// </summary>
        /// <returns>A list of all inherited AttributeCache objects.</returns>
        public override List <AttributeCache> GetInheritedAttributes(Rock.Data.RockContext rockContext)
        {
            var entityTypeCache = EntityTypeCache.Read(TypeId);

            // Get the registration
            var registration = this.Registration;

            if (registration == null && this.RegistrationId > 0)
            {
                registration = new RegistrationService(rockContext)
                               .Queryable().AsNoTracking()
                               .FirstOrDefault(r => r.Id == this.RegistrationId);
            }
            if (entityTypeCache == null || registration == null)
            {
                return(null);
            }

            // Get the instance
            var registrationInstance = registration.RegistrationInstance;

            if (registrationInstance == null && registration.RegistrationInstanceId > 0)
            {
                registrationInstance = new RegistrationInstanceService(rockContext)
                                       .Queryable().AsNoTracking()
                                       .FirstOrDefault(r => r.Id == registration.RegistrationInstanceId);
            }
            if (registrationInstance == null)
            {
                return(null);
            }

            // Get all attributes there were defined for instance's template.
            var attributes = new List <Rock.Web.Cache.AttributeCache>();

            foreach (var entityAttributes in AttributeCache.GetByEntity(entityTypeCache.Id)
                     .Where(e =>
                            e.EntityTypeQualifierColumn == "RegistrationTemplateId" &&
                            e.EntityTypeQualifierValue.AsInteger() == registrationInstance.RegistrationTemplateId))
            {
                foreach (int attributeId in entityAttributes.AttributeIds)
                {
                    attributes.Add(Rock.Web.Cache.AttributeCache.Read(attributeId));
                }
            }

            return(attributes);
        }
        /// <summary>
        /// Gets the discounts available for registration template used by the provided registration instance.
        /// </summary>
        /// <param name="registrationInstanceId">The registration instance identifier.</param>
        /// <returns></returns>
        public IQueryable <RegistrationTemplateDiscount> GetDiscountsForRegistrationInstance(int?registrationInstanceId)
        {
            if (registrationInstanceId == null || registrationInstanceId == 0)
            {
                return(Queryable());
            }

            var registrationInstanceService = new RegistrationInstanceService(( RockContext )this.Context);
            int registrationTemplateId      = registrationInstanceService
                                              .Queryable()
                                              .Where(r => r.Id == registrationInstanceId)
                                              .Select(r => r.RegistrationTemplateId)
                                              .FirstOrDefault();

            return(Queryable().Where(d => d.RegistrationTemplateId == registrationTemplateId));
        }
            /// <summary>
            /// Called before the save operation is executed.
            /// </summary>
            protected override void PreSave()
            {
                if (State == EntityContextState.Deleted)
                {
                    // manually clear any registrations using this workflow type
                    var templates = new RegistrationTemplateService(RockContext).Queryable().Where(r =>
                                                                                                   r.RegistrationWorkflowTypeId.HasValue &&
                                                                                                   r.RegistrationWorkflowTypeId.Value == this.Entity.Id);

                    foreach (var template in templates)
                    {
                        template.RegistrationWorkflowTypeId = null;
                    }

                    var instances = new RegistrationInstanceService(RockContext).Queryable().Where(r =>
                                                                                                   r.RegistrationWorkflowTypeId.HasValue &&
                                                                                                   r.RegistrationWorkflowTypeId.Value == this.Entity.Id);

                    foreach (var instance in instances)
                    {
                        instance.RegistrationWorkflowTypeId = null;
                    }

                    /*
                     *  7/6/2020 - JH
                     *  The deletion of the Workflow object graph is accomplished by a combination of SQL cascade deletes and
                     *  PreSaveChanges() implementations.
                     *
                     *  When a WorkflowType is deleted, any child Workflows will be cascade-deleted. Most children of the deleted
                     *  Workflows will also be cascade-deleted, with the exception of any ConnectionRequestWorkflow records. Therefore,
                     *  we need to manually delete those here in order to prevent foreign key violations within the database.
                     *
                     *  Reason: GitHub Issue #4068
                     *  https://github.com/SparkDevNetwork/Rock/issues/4068#issuecomment-648908315
                     */
                    var connectionRequestWorkflows = new ConnectionRequestWorkflowService(RockContext)
                                                     .Queryable()
                                                     .Where(c => c.Workflow.WorkflowTypeId == this.Entity.Id);

                    if (connectionRequestWorkflows.Any())
                    {
                        DbContext.BulkDelete(connectionRequestWorkflows);
                    }
                }

                base.PreSave();
            }
        /// <summary>
        /// Loads the registration Instances.
        /// </summary>
        private void LoadRegistrationInstances()
        {
            ddlRegistrationInstance.SelectedValue = null;
            ddlRegistrationInstance.Items.Clear();
            if (RegistrationTemplateId.HasValue)
            {
                var registrationInstanceId = RegistrationInstanceId;
                ddlRegistrationInstance.Items.Add(new ListItem());

                var registrationInstanceService = new Rock.Model.RegistrationInstanceService(new RockContext());
                var registrationInstances       = registrationInstanceService.Queryable().Where(r => r.RegistrationTemplateId == RegistrationTemplateId.Value && r.IsActive).OrderBy(a => a.Name).ToList();

                foreach (var r in registrationInstances)
                {
                    var item = new ListItem(r.Name, r.Id.ToString().ToUpper());
                    item.Selected = r.Id == registrationInstanceId;
                    ddlRegistrationInstance.Items.Add(item);
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Gets the generic context about the registration.
        /// </summary>
        /// <param name="registrationInstanceId">The registration instance identifier.</param>
        /// <param name="errorMessage">The error result.</param>
        /// <returns></returns>
        public RegistrationContext GetRegistrationContext(int registrationInstanceId, out string errorMessage)
        {
            var rockContext = Context as RockContext;

            errorMessage = string.Empty;

            // Load the instance and template
            var registrationInstance = GetActiveRegistrationInstance(registrationInstanceId, out errorMessage);
            var registrationTemplate = registrationInstance?.RegistrationTemplate;

            if (registrationInstance == null || registrationTemplate == null)
            {
                // In this case, errorMessage will already contain the reason for it thanks to GetActiveRegistrationInstance
                return(null);
            }

            // Validate that there are enough spots left for this registration
            var registrationInstanceService = new RegistrationInstanceService(rockContext);
            var context = new RegistrationContext
            {
                RegistrationSettings = new RegistrationSettings(registrationTemplate, registrationInstance),
                Registration         = null,
                Discount             = null,
                SpotsRemaining       = null
            };

            var spotsRemaining = registrationInstanceService.GetSpotsAvailable(context);

            context.SpotsRemaining = spotsRemaining;

            var feeItemCountRemaining = registrationInstanceService.GetFeeItemCountRemaining(context);

            context.FeeItemsCountRemaining = feeItemCountRemaining;

            return(context);
        }
Exemple #7
0
        /// <summary>
        /// Gets the active registration instance.
        /// </summary>
        /// <param name="registrationInstanceId">The registration instance identifier.</param>
        /// <returns></returns>
        private RegistrationInstance GetActiveRegistrationInstance(int registrationInstanceId)
        {
            var now = RockDateTime.Now;
            var registrationInstanceService = new RegistrationInstanceService(Context as RockContext);
            var registrationInstance        = registrationInstanceService.Get(registrationInstanceId);
            var registrationTemplate        = registrationInstance?.RegistrationTemplate;

            // Ensure that the registration entities are active
            if (registrationInstance is null || registrationTemplate is null || !registrationTemplate.IsActive || !registrationInstance.IsActive)
            {
                return(null);
            }

            // Make sure the registration is open
            var isBeforeRegistrationOpens = registrationInstance.StartDateTime.HasValue && registrationInstance.StartDateTime > now;
            var isAfterRegistrationCloses = registrationInstance.EndDateTime.HasValue && registrationInstance.EndDateTime < now;

            if (isBeforeRegistrationOpens || isAfterRegistrationCloses)
            {
                return(null);
            }

            return(registrationInstance);
        }
        /// <summary>
        /// Gets the group placement registrants.
        /// </summary>
        /// <param name="options">The options.</param>
        /// <param name="currentPerson">The current person.</param>
        /// <returns></returns>
        public List <GroupPlacementRegistrant> GetGroupPlacementRegistrants(GetGroupPlacementRegistrantsParameters options, Person currentPerson)
        {
            var rockContext = this.Context as RockContext;

            var registrationRegistrantService = new RegistrationRegistrantService(rockContext);
            var registrationRegistrantQuery   = registrationRegistrantService.Queryable();

            registrationRegistrantQuery = registrationRegistrantQuery
                                          .Where(a => a.Registration.RegistrationInstance.RegistrationTemplateId == options.RegistrationTemplateId);

            if (options.RegistrationInstanceId.HasValue)
            {
                registrationRegistrantQuery = registrationRegistrantQuery.Where(a => a.Registration.RegistrationInstanceId == options.RegistrationInstanceId.Value);
            }
            else if (options.RegistrationTemplateInstanceIds?.Any() == true)
            {
                registrationRegistrantQuery = registrationRegistrantQuery.Where(a => options.RegistrationTemplateInstanceIds.Contains(a.Registration.RegistrationInstanceId));
            }

            if (options.RegistrantPersonDataViewFilterId.HasValue)
            {
                var           dataFilter    = new DataViewFilterService(rockContext).Get(options.RegistrantPersonDataViewFilterId.Value);
                List <string> errorMessages = new List <string>();

                var personService   = new PersonService(rockContext);
                var paramExpression = personService.ParameterExpression;

                var personWhereExpression = dataFilter?.GetExpression(typeof(Person), personService, paramExpression, errorMessages);
                if (personWhereExpression != null)
                {
                    var personIdQry = personService.Queryable().Where(paramExpression, personWhereExpression, null).Select(x => x.Id);
                    registrationRegistrantQuery = registrationRegistrantQuery.Where(a => personIdQry.Contains(a.PersonAlias.PersonId));
                }
            }

            if (options.RegistrantId.HasValue)
            {
                registrationRegistrantQuery = registrationRegistrantQuery.Where(a => a.Id == options.RegistrantId.Value);
            }

            Block registrationInstanceGroupPlacementBlock = new BlockService(rockContext).Get(options.BlockId);

            if (registrationInstanceGroupPlacementBlock != null && currentPerson != null)
            {
                const string RegistrantAttributeFilter_RegistrationInstanceId = "RegistrantAttributeFilter_RegistrationInstanceId_{0}";
                const string RegistrantAttributeFilter_RegistrationTemplateId = "RegistrantAttributeFilter_RegistrationTemplateId_{0}";
                string       userPreferenceKey;
                if (options.RegistrationInstanceId.HasValue)
                {
                    userPreferenceKey = PersonService.GetBlockUserPreferenceKeyPrefix(options.BlockId) + string.Format(RegistrantAttributeFilter_RegistrationInstanceId, options.RegistrationInstanceId);
                }
                else
                {
                    userPreferenceKey = PersonService.GetBlockUserPreferenceKeyPrefix(options.BlockId) + string.Format(RegistrantAttributeFilter_RegistrationTemplateId, options.RegistrationTemplateId);
                }

                var        attributeFilters          = PersonService.GetUserPreference(currentPerson, userPreferenceKey).FromJsonOrNull <Dictionary <int, string> >() ?? new Dictionary <int, string>();
                var        parameterExpression       = registrationRegistrantService.ParameterExpression;
                Expression registrantWhereExpression = null;
                foreach (var attributeFilter in attributeFilters)
                {
                    var attribute             = AttributeCache.Get(attributeFilter.Key);
                    var attributeFilterValues = attributeFilter.Value.FromJsonOrNull <List <string> >();
                    var entityField           = EntityHelper.GetEntityFieldForAttribute(attribute);
                    if (entityField != null && attributeFilterValues != null)
                    {
                        var attributeWhereExpression = ExpressionHelper.GetAttributeExpression(registrationRegistrantService, parameterExpression, entityField, attributeFilterValues);
                        if (registrantWhereExpression == null)
                        {
                            registrantWhereExpression = attributeWhereExpression;
                        }
                        else
                        {
                            registrantWhereExpression = Expression.AndAlso(registrantWhereExpression, attributeWhereExpression);
                        }
                    }
                }

                if (registrantWhereExpression != null)
                {
                    registrationRegistrantQuery = registrationRegistrantQuery.Where(parameterExpression, registrantWhereExpression);
                }
            }

            var registrationTemplatePlacement = new RegistrationTemplatePlacementService(rockContext).Get(options.RegistrationTemplatePlacementId);

            if (options.FilterFeeId.HasValue)
            {
                registrationRegistrantQuery = registrationRegistrantQuery.Where(a => a.Fees.Any(f => f.RegistrationTemplateFeeId == options.FilterFeeId.Value));
            }

            if (options.FilterFeeOptionIds?.Any() == true)
            {
                registrationRegistrantQuery = registrationRegistrantQuery.Where(a => a.Fees.Any(f => f.RegistrationTemplateFeeItemId.HasValue && options.FilterFeeOptionIds.Contains(f.RegistrationTemplateFeeItemId.Value)));
            }

            // don't include registrants that are on the waiting list
            registrationRegistrantQuery = registrationRegistrantQuery.Where(a => a.OnWaitList == false);

            registrationRegistrantQuery = registrationRegistrantQuery.OrderBy(a => a.PersonAlias.Person.LastName).ThenBy(a => a.PersonAlias.Person.NickName);

            var registrationTemplatePlacementService = new RegistrationTemplatePlacementService(rockContext);
            var registrationInstanceService          = new RegistrationInstanceService(rockContext);

            // get a queryable of PersonIds for the registration template shared groups so we can determine if the registrant has been placed
            var registrationTemplatePlacementGroupsPersonIdQuery = registrationTemplatePlacementService.GetRegistrationTemplatePlacementPlacementGroups(registrationTemplatePlacement).SelectMany(a => a.Members).Select(a => a.PersonId);

            // and also get a queryable of PersonIds for the registration instance placement groups so we can determine if the registrant has been placed
            IQueryable <InstancePlacementGroupPersonId> allInstancesPlacementGroupInfoQuery = null;

            if (!options.RegistrationInstanceId.HasValue && (options.RegistrationTemplateInstanceIds == null || !options.RegistrationTemplateInstanceIds.Any()))
            {
                // if neither RegistrationInstanceId or RegistrationTemplateInstanceIds was specified, use all of the RegistrationTemplates instances
                options.RegistrationTemplateInstanceIds = new RegistrationTemplateService(rockContext).GetSelect(options.RegistrationTemplateId, s => s.Instances.Select(i => i.Id)).ToArray();
            }

            if (options.RegistrationInstanceId.HasValue)
            {
                allInstancesPlacementGroupInfoQuery =
                    registrationInstanceService.GetRegistrationInstancePlacementGroups(registrationInstanceService.Get(options.RegistrationInstanceId.Value))
                    .Where(a => a.GroupTypeId == registrationTemplatePlacement.GroupTypeId)
                    .SelectMany(a => a.Members).Select(a => a.PersonId)
                    .Select(s => new InstancePlacementGroupPersonId
                {
                    PersonId = s,
                    RegistrationInstanceId = options.RegistrationInstanceId.Value
                });
            }
            else if (options.RegistrationTemplateInstanceIds?.Any() == true)
            {
                foreach (var registrationInstanceId in options.RegistrationTemplateInstanceIds)
                {
                    var instancePlacementGroupInfoQuery = registrationInstanceService.GetRegistrationInstancePlacementGroups(registrationInstanceService.Get(registrationInstanceId))
                                                          .Where(a => a.GroupTypeId == registrationTemplatePlacement.GroupTypeId)
                                                          .SelectMany(a => a.Members).Select(a => a.PersonId)
                                                          .Select(s => new InstancePlacementGroupPersonId
                    {
                        PersonId = s,
                        RegistrationInstanceId = registrationInstanceId
                    });

                    if (allInstancesPlacementGroupInfoQuery == null)
                    {
                        allInstancesPlacementGroupInfoQuery = instancePlacementGroupInfoQuery;
                    }
                    else
                    {
                        allInstancesPlacementGroupInfoQuery = allInstancesPlacementGroupInfoQuery.Union(instancePlacementGroupInfoQuery);
                    }
                }
            }

            if (allInstancesPlacementGroupInfoQuery == null)
            {
                throw new ArgumentNullException("Registration Instance(s) must be specified");
            }

            // select in a way to avoid lazy loading
            var registrationRegistrantPlacementQuery = registrationRegistrantQuery.Select(r => new
            {
                Registrant = r,
                r.PersonAlias.Person,
                r.Registration.RegistrationInstance,

                // marked as AlreadyPlacedInGroup if the Registrant is a member of any of the registrant template placement group or the registration instance placement groups
                AlreadyPlacedInGroup =
                    registrationTemplatePlacementGroupsPersonIdQuery.Contains(r.PersonAlias.PersonId) ||
                    allInstancesPlacementGroupInfoQuery.Any(x => x.RegistrationInstanceId == r.Registration.RegistrationInstanceId && x.PersonId == r.PersonAlias.PersonId)
            });

            var registrationRegistrantPlacementList = registrationRegistrantPlacementQuery.AsNoTracking().ToList();

            var groupPlacementRegistrantList = registrationRegistrantPlacementList
                                               .Select(x => new GroupPlacementRegistrant(x.Registrant, x.Person, x.AlreadyPlacedInGroup, x.RegistrationInstance, options))
                                               .ToList();

            return(groupPlacementRegistrantList.ToList());
        }