Exemple #1
0
            private void UpdateStepProgramCompletion(RockContext rockContext)
            {
                if (!Entity.CompletedDateTime.HasValue)
                {
                    return;
                }

                var stepTypeService = new StepTypeService(rockContext);
                var stepType        = Entity.StepType ?? stepTypeService.Get(Entity.StepTypeId);

                if (stepType == null)
                {
                    return;
                }

                var programStepTypeIds = stepTypeService
                                         .Queryable()
                                         .Where(a => a.StepProgramId == stepType.StepProgramId && a.IsActive)
                                         .Select(a => a.Id)
                                         .ToList();

                var steps = new StepService(rockContext)
                            .Queryable()
                            .AsNoTracking()
                            .Where(a => a.PersonAliasId == Entity.PersonAliasId && !a.StepProgramCompletionId.HasValue && a.CompletedDateTime.HasValue)
                            .OrderBy(a => a.CompletedDateTime)
                            .ToList();

                while (steps.Any() && programStepTypeIds.All(a => steps.Any(b => b.StepTypeId == a)))
                {
                    var stepSet = new List <Step>();
                    foreach (var programStepTypeId in programStepTypeIds)
                    {
                        var step = steps.Where(a => a.StepTypeId == programStepTypeId).FirstOrDefault();
                        if (step == null)
                        {
                            continue;
                        }

                        stepSet.Add(step);
                        steps.RemoveAll(a => a.Id == step.Id);
                    }

                    StepService.UpdateStepProgramCompletion(stepSet, Entity.PersonAliasId, stepType.StepProgramId, rockContext);
                }
            }
Exemple #2
0
        /// <summary>
        /// Determines whether this instance can add the specified item.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <returns>
        ///   <c>true</c> if this instance can add the specified item; otherwise, <c>false</c>.
        /// </returns>
        public bool CanAdd(Step item, out string errorMessage)
        {
            errorMessage = string.Empty;

            if (item == null)
            {
                errorMessage = "A null step cannot be added";
                return(false);
            }

            if (!item.IsValid)
            {
                errorMessage = item.ValidationResults.FirstOrDefault()?.ErrorMessage;
                return(false);
            }

            var stepTypeService = new StepTypeService(Context as RockContext);
            var stepType        = stepTypeService.Queryable("StepTypePrerequisites.PrerequisiteStepType").AsNoTracking().FirstOrDefault(st => st.Id == item.StepTypeId);

            if (stepType == null)
            {
                errorMessage = "The step type identifier is invalid and the step type could not be found";
                return(false);
            }

            // If the step type doesn't allow multiple then the person cannot have two records of the same step type
            if (!CanAddBecauseMeetsAllowMultipleRule(item.PersonAliasId, stepType))
            {
                errorMessage = "A person cannot be added multiple times to this step type";
                return(false);
            }

            // Make sure the person has completed all of the prerequisites (has a step record of that type that is a completed status) for the
            // step type before allowing a new step record
            if (!CanAddBecausePrereqsAreMet(item.PersonAliasId, stepType))
            {
                errorMessage = "All of the prerequisite steps have not yet been completed";
                return(false);
            }

            return(true);
        }