/// <summary>
        /// Adds the step.
        /// </summary>
        /// <param name="stepTypeId">The step type identifier.</param>
        /// <param name="stepStatusId">The step status identifier.</param>
        /// <param name="personAliasId">The person alias identifier.</param>
        private void AddStep(int stepTypeId, int stepStatusId, int personAliasId)
        {
            var rockContext        = new RockContext();
            var stepService        = new StepService(rockContext);
            var stepProgramService = new StepProgramService(rockContext);

            // Get the step program with step types and statuses to better calculate the dates for the new step
            var stepProgram = stepProgramService.Queryable("StepTypes, StepStatuses").FirstOrDefault(sp =>
                                                                                                     sp.StepTypes.Any(st => st.Id == stepTypeId) &&
                                                                                                     sp.StepStatuses.Any(ss => ss.Id == stepStatusId));

            var stepType   = stepProgram?.StepTypes.FirstOrDefault(st => st.Id == stepTypeId);
            var stepStatus = stepProgram?.StepStatuses.FirstOrDefault(ss => ss.Id == stepStatusId);

            if (stepType == null)
            {
                ExceptionLogService.LogException($"Error adding step related to an achievement. The step type {stepTypeId} did not resolve.");
                return;
            }

            if (stepStatus == null)
            {
                ExceptionLogService.LogException($"Error adding step related to an achievement. The step status {stepStatusId} did not resolve.");
                return;
            }

            // Add the new step
            var step = new Step
            {
                StepTypeId        = stepTypeId,
                StepStatusId      = stepStatusId,
                CompletedDateTime = stepStatus.IsCompleteStatus ? EndDate : null,
                StartDateTime     = StartDate,
                EndDateTime       = stepType.HasEndDate ? EndDate : null,
                PersonAliasId     = personAliasId
            };

            // If the person cannot be added to the step type, then don't add anything since some step types only allow one step
            // or require pre-requisites
            if (stepService.CanAdd(step, out _))
            {
                stepService.Add(step);
            }

            rockContext.SaveChanges();
        }
Beispiel #2
0
        /// <summary>
        /// Adds the step.
        /// </summary>
        /// <param name="stepTypeId">The step type identifier.</param>
        /// <param name="stepStatusId">The step status identifier.</param>
        /// <param name="personAliasId">The person alias identifier.</param>
        private static void AddStep(int stepTypeId, int stepStatusId, int personAliasId)
        {
            var rockContext = new RockContext();
            var stepService = new StepService(rockContext);

            var step = new Step
            {
                StepTypeId        = stepTypeId,
                StepStatusId      = stepStatusId,
                CompletedDateTime = RockDateTime.Today,
                PersonAliasId     = personAliasId
            };

            // If the person cannot be added to the step type, then don't add anything since some step types only allow one step
            // or require pre-requisites
            if (stepService.CanAdd(step, out _))
            {
                stepService.Add(step);
            }

            rockContext.SaveChanges();
        }
Beispiel #3
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var rockContext        = GetRockContext();
            var service            = new StepService(rockContext);
            var step               = GetStep();
            var stepType           = GetStepType();
            var person             = GetPerson();
            var isPersonSelectable = IsPersonSelectable();

            // If the person is allowed to be selected and the person is missing, query for it
            if (isPersonSelectable && ppPerson.PersonId.HasValue && person == null)
            {
                var personService = new PersonService(rockContext);
                person = personService.Get(ppPerson.PersonId.Value);
            }

            // Person is the only required field for the step
            if (person == null)
            {
                ShowError("The person is required to save a step record.");
            }

            // If the step is null, then the aim is to create a new step
            var isAdd = step == null;

            if (isAdd)
            {
                step = new Step
                {
                    StepTypeId    = stepType.Id,
                    PersonAliasId = person.PrimaryAliasId.Value
                };
            }

            // Update the step properties. Person cannot be changed (only set when the step is added)
            step.StartDateTime = rdpStartDate.SelectedDate;
            step.EndDateTime   = stepType.HasEndDate ? rdpEndDate.SelectedDate : null;
            step.StepStatusId  = rsspStatus.SelectedValueAsId();

            // Update the completed date time, which is based on the start, end, and status
            if (!step.StepStatusId.HasValue)
            {
                step.CompletedDateTime = null;
            }
            else
            {
                var stepStatusService = new StepStatusService(rockContext);
                var stepStatus        = stepStatusService.Get(step.StepStatusId.Value);

                if (stepStatus == null || !stepStatus.IsCompleteStatus)
                {
                    step.CompletedDateTime = null;
                }
                else
                {
                    step.CompletedDateTime = step.EndDateTime ?? step.StartDateTime;
                }
            }

            if (!step.IsValid)
            {
                ShowError(step.ValidationResults.Select(vr => vr.ErrorMessage).ToList().AsDelimited("<br />"));
                return;
            }

            if (isAdd)
            {
                var errorMessage = string.Empty;
                var canAdd       = service.CanAdd(step, out errorMessage);

                if (!errorMessage.IsNullOrWhiteSpace())
                {
                    ShowError(errorMessage);
                    return;
                }

                if (!canAdd)
                {
                    ShowError("The step cannot be added for an unspecified reason");
                    return;
                }

                service.Add(step);
            }

            // Save the step record
            rockContext.SaveChanges();

            // Save the step attributes from the attribute controls
            step.LoadAttributes(rockContext);
            avcAttributes.GetEditValues(step);
            step.SaveAttributeValues(rockContext);

            GoToSuccessPage(step.Id);
        }
Beispiel #4
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();
            var mergeFields = GetMergeFields(action);

            // Validate the person exists
            var personGuid = GetAttributeValue(action, AttributeKey.Person, true).AsGuidOrNull();

            if (!personGuid.HasValue)
            {
                errorMessages.Add("The person guid is required but was missing");
                return(LogMessagesForExit(action, errorMessages));
            }

            var personService = new PersonService(rockContext);
            var person        = personService.Queryable("Aliases").AsNoTracking()
                                .FirstOrDefault(p => p.Guid == personGuid.Value || p.Aliases.Any(pa => pa.Guid == personGuid.Value));

            if (person == null)
            {
                errorMessages.Add($"The person with the guid '{personGuid.Value}' was not found");
                return(LogMessagesForExit(action, errorMessages));
            }

            if (!person.PrimaryAliasId.HasValue)
            {
                errorMessages.Add($"{person.FullName} does not have a primary alias identifier");
                return(LogMessagesForExit(action, errorMessages));
            }

            // Validate the step type exists. Could be a step type id or a guid
            var stepType = GetStepType(rockContext, action, out var errorMessage);

            if (!errorMessage.IsNullOrWhiteSpace())
            {
                errorMessages.Add(errorMessage);
                return(LogMessagesForExit(action, errorMessages));
            }

            if (stepType == null)
            {
                errorMessages.Add("The step type could not be found");
                return(LogMessagesForExit(action, errorMessages));
            }

            // Validate the step status exists and is in the same program as the step type
            var stepStatus = GetStepStatus(stepType, action, out errorMessage);

            if (!errorMessage.IsNullOrWhiteSpace())
            {
                errorMessages.Add(errorMessage);
                return(LogMessagesForExit(action, errorMessages));
            }

            if (stepStatus == null)
            {
                errorMessages.Add("The step status could not be found");
                return(LogMessagesForExit(action, errorMessages));
            }

            // Get the start and end dates
            var startDate = GetLavaAttributeValue(action, AttributeKey.StartDate).AsDateTime() ?? RockDateTime.Now;
            var endDate   = GetLavaAttributeValue(action, AttributeKey.EndDate).AsDateTime();

            var campusAttributeValue = GetLavaAttributeValue(action, AttributeKey.Campus);
            var campusId             = campusAttributeValue.AsIntegerOrNull();
            var campusGuid           = campusAttributeValue.AsGuidOrNull();

            if (campusGuid != null)
            {
                var campus = CampusCache.Get(campusGuid.Value);
                if (campus != null)
                {
                    campusId = campus.Id;
                }
            }

            // The completed date is today or the end date if the status is a completed status
            var completedDate = stepStatus.IsCompleteStatus ? (endDate ?? RockDateTime.Now) : ( DateTime? )null;

            // Create the step object
            var step = new Step
            {
                StepTypeId        = stepType.Id,
                PersonAliasId     = person.PrimaryAliasId.Value,
                StartDateTime     = startDate,
                EndDateTime       = endDate,
                CompletedDateTime = completedDate,
                StepStatusId      = stepStatus.Id,
                CampusId          = campusId
            };

            // Validate the step
            if (!step.IsValid)
            {
                errorMessages.AddRange(step.ValidationResults.Select(a => a.ErrorMessage));
                return(LogMessagesForExit(action, errorMessages));
            }

            // Check if the step can be created because of Allow Multiple rules on the step type and also prerequisite requirements
            var stepService = new StepService(rockContext);
            var canAdd      = stepService.CanAdd(step, out errorMessage);

            if (!errorMessage.IsNullOrWhiteSpace())
            {
                errorMessages.Add(errorMessage);
            }
            else if (!canAdd)
            {
                errorMessages.Add("Cannot add the step for an unspecified reason");
            }
            else
            {
                try
                {
                    stepService.Add(step);
                    rockContext.SaveChanges();

                    SetCreatedItemAttribute(action, AttributeKey.StepAttribute, step, rockContext);
                }
                catch (Exception exception)
                {
                    errorMessages.Add($"Exception thrown: {exception.Message}");
                    ExceptionLogService.LogException(exception);
                }
            }

            return(LogMessagesForExit(action, errorMessages));
        }