Esempio n. 1
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)
        {
            /*
             * 2019-12-09 - ED
             * This activity will create a new assessment for each assessment type selected. If an assessment already exists for that type it is left in a pending status but a new assessment is still created.
             * Per Jon, code in Rock should account for the possibility of multiple assessments for a type that are in a pending state and only select the latest one.
             */

            rockContext = rockContext ?? new RockContext();

            errorMessages = new List <string>();

            var assessmentTypesGuidString = GetAttributeValue(action, AttributeKey.AssessmentTypesKey, true);
            var assessmentTypeGuids       = assessmentTypesGuidString.IsNullOrWhiteSpace() ? null : assessmentTypesGuidString.Split(new char[] { ',' });

            var  personAliasGuid      = GetAttributeValue(action, AttributeKey.Person, true).AsGuidOrNull();
            Guid?requestedByAliasGuid = GetAttributeValue(action, AttributeKey.RequestedBy, true).AsGuidOrNull();
            var  dueDate = GetAttributeValue(action, AttributeKey.DueDate, true).AsDateTime();

            // Validate attribute data
            if (!assessmentTypeGuids.Any())
            {
                errorMessages.Add("No Assessments selected.");
                return(false);
            }

            if (personAliasGuid == null)
            {
                errorMessages.Add("Invalid Person Attribute or Value.");
                return(false);
            }

            var personAlias = new PersonAliasService(rockContext).Get(personAliasGuid.Value);

            if (personAlias == null)
            {
                errorMessages.Add("Invalid Person Attribute or Value.");
                return(false);
            }

            var requestedByAlias = new PersonAliasService(rockContext).Get(requestedByAliasGuid.Value);

            foreach (string assessmentTypeGuid in assessmentTypeGuids)
            {
                var assessmentTypeService = new AssessmentTypeService(rockContext);
                int?assessmentTypeId      = assessmentTypeService.GetId(assessmentTypeGuid.AsGuid());

                if (assessmentTypeId == null)
                {
                    // This really shouldn't be able to happen, but let's not risk an NRE.
                    errorMessages.Add($"Invalid Assessment Type: {assessmentTypeGuid}");
                    continue;
                }

                // Create a new assessment
                var assessment = new Assessment
                {
                    PersonAliasId          = personAlias.Id,
                    AssessmentTypeId       = assessmentTypeId.Value,
                    RequesterPersonAliasId = requestedByAlias.Id,
                    RequestedDateTime      = RockDateTime.Now,
                    RequestedDueDate       = dueDate,
                    Status = AssessmentRequestStatus.Pending
                };

                var assessmentService = new AssessmentService(rockContext);
                assessmentService.Add(assessment);
                rockContext.SaveChanges();
            }

            return(true);
        }
Esempio n. 2
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)
        {
            rockContext = rockContext ?? new RockContext();

            errorMessages = new List <string>();

            var assessmentTypesGuidString = GetAttributeValue(action, AttributeKey.AssessmentTypesKey, true);
            var assessmentTypeGuids       = assessmentTypesGuidString.IsNullOrWhiteSpace() ? null : assessmentTypesGuidString.Split(new char[] { ',' });

            var  personAliasGuid      = GetAttributeValue(action, AttributeKey.Person, true).AsGuidOrNull();
            Guid?requestedByAliasGuid = GetAttributeValue(action, AttributeKey.RequestedBy, true).AsGuidOrNull();
            var  dueDate = GetAttributeValue(action, AttributeKey.DueDate, true).AsDateTime();

            // Validate attribute data
            if (!assessmentTypeGuids.Any())
            {
                errorMessages.Add("No Assessments selected.");
                return(false);
            }

            if (personAliasGuid == null)
            {
                errorMessages.Add("Invalid Person Attribute or Value.");
                return(false);
            }

            var personAlias = new PersonAliasService(rockContext).Get(personAliasGuid.Value);

            if (personAlias == null)
            {
                errorMessages.Add("Invalid Person Attribute or Value.");
                return(false);
            }

            var requestedByAlias = new PersonAliasService(rockContext).Get(requestedByAliasGuid.Value);

            foreach (string assessmentTypeGuid in assessmentTypeGuids)
            {
                // Check for an existing record
                var assessmentTypeService = new AssessmentTypeService(rockContext);
                int?assessmentTypeId      = assessmentTypeService.GetId(assessmentTypeGuid.AsGuid());

                var assessmentService  = new AssessmentService(rockContext);
                var existingAssessment = assessmentService
                                         .Queryable()
                                         .Where(a => a.PersonAliasId == personAlias.Id)
                                         .Where(a => a.AssessmentTypeId == assessmentTypeId)
                                         .Where(a => a.Status == AssessmentRequestStatus.Pending)
                                         .FirstOrDefault();

                // If a pending record for this person/type is found mark it complete.
                if (existingAssessment != null)
                {
                    existingAssessment.Status = AssessmentRequestStatus.Complete;
                }

                // Create a new assessment
                var assessment = new Assessment
                {
                    PersonAliasId          = personAlias.Id,
                    AssessmentTypeId       = assessmentTypeId.Value,
                    RequesterPersonAliasId = requestedByAlias.Id,
                    RequestedDateTime      = RockDateTime.Now,
                    RequestedDueDate       = dueDate,
                    Status = AssessmentRequestStatus.Pending
                };

                assessmentService.Add(assessment);
                rockContext.SaveChanges();
            }

            return(true);
        }