Пример #1
0
        /// <summary>
        /// Loads the prerequisite list.
        /// </summary>
        private void SyncPrerequisiteList()
        {
            var streakTypeCache      = GetStreakTypeCache();
            var achievementTypeCache = GetAchievementTypeCache();
            var isNew = IsAddMode();

            var eligiblePrerequisites = isNew ?
                                        StreakTypeAchievementTypeService.GetEligiblePrerequisiteAchievementTypeCaches(streakTypeCache) :
                                        StreakTypeAchievementTypeService.GetEligiblePrerequisiteAchievementTypeCaches(achievementTypeCache);

            cblPrerequsities.DataSource = eligiblePrerequisites;
            cblPrerequsities.DataBind();
            cblPrerequsities.Visible = eligiblePrerequisites.Any();

            if (!isNew)
            {
                cblPrerequsities.SetValues(achievementTypeCache.Prerequisites.Select(statp => statp.PrerequisiteStreakTypeAchievementTypeId));
            }
        }
Пример #2
0
        /// <summary>
        /// Save the current record.
        /// </summary>
        /// <returns></returns>
        private void SaveRecord()
        {
            var rockContext            = GetRockContext();
            var achievementType        = GetAchievementType();
            var achievementTypeService = GetAchievementTypeService();
            var isNew = achievementType == null;

            if (isNew)
            {
                achievementType = new StreakTypeAchievementType();
                achievementTypeService.Add(achievementType);

                var streakTypeCache = GetStreakTypeCache();
                achievementType.StreakTypeId            = streakTypeCache != null ? streakTypeCache.Id : (stpStreakType.SelectedValueAsInt() ?? 0);
                achievementType.AchievementEntityTypeId = cpAchievementComponent.SelectedEntityTypeId ?? 0;
            }

            achievementType.Name                             = tbName.Text;
            achievementType.Description                      = tbDescription.Text;
            achievementType.IsActive                         = cbActive.Checked;
            achievementType.AchievementIconCssClass          = tbIconCssClass.Text;
            achievementType.MaxAccomplishmentsAllowed        = cbAllowOverachievement.Checked ? 1 : nbMaxAccomplishments.IntegerValue;
            achievementType.AllowOverAchievement             = cbAllowOverachievement.Checked;
            achievementType.AchievementStartWorkflowTypeId   = wtpStartWorkflowType.SelectedValueAsInt();
            achievementType.AchievementSuccessWorkflowTypeId = wtpSuccessWorkflowType.SelectedValueAsInt();
            achievementType.AchievementFailureWorkflowTypeId = wtpFailureWorkflowType.SelectedValueAsInt();
            achievementType.BadgeLavaTemplate                = ceBadgeLava.Text;
            achievementType.ResultsLavaTemplate              = ceResultsLava.Text;
            achievementType.CategoryId                       = cpCategory.SelectedValueAsInt();

            // Both step type and status are required together or neither can be set
            var stepTypeId   = spstStepType.StepTypeId;
            var stepStatusId = sspStepStatus.SelectedValueAsInt();

            if (cbAddStep.Checked && stepTypeId.HasValue && stepStatusId.HasValue)
            {
                achievementType.AchievementStepTypeId   = stepTypeId;
                achievementType.AchievementStepStatusId = stepStatusId;
            }
            else
            {
                achievementType.AchievementStepTypeId   = null;
                achievementType.AchievementStepStatusId = null;
            }

            // Upsert Prerequisites
            var prerequisiteService = new StreakTypeAchievementTypePrerequisiteService(rockContext);
            var selectedPrerequisiteAchievementTypeIds = cblPrerequsities.SelectedValuesAsInt;

            // Remove existing prerequisites that are not selected
            var removePrerequisiteAchievementTypes = achievementType.Prerequisites
                                                     .Where(statp => !selectedPrerequisiteAchievementTypeIds.Contains(statp.PrerequisiteStreakTypeAchievementTypeId)).ToList();

            foreach (var prerequisite in removePrerequisiteAchievementTypes)
            {
                achievementType.Prerequisites.Remove(prerequisite);
                prerequisiteService.Delete(prerequisite);
            }

            // Add selected achievement types prerequisites that are not existing
            var addPrerequisiteAchievementTypeIds = selectedPrerequisiteAchievementTypeIds
                                                    .Where(statId => !achievementType.Prerequisites.Any(statp => statp.PrerequisiteStreakTypeAchievementTypeId == statId));

            foreach (var prerequisiteAchievementTypeId in addPrerequisiteAchievementTypeIds)
            {
                achievementType.Prerequisites.Add(new StreakTypeAchievementTypePrerequisite
                {
                    StreakTypeAchievementTypeId             = achievementType.Id,
                    PrerequisiteStreakTypeAchievementTypeId = prerequisiteAchievementTypeId
                });
            }

            // Validate Prerequisites.
            // This is necessary because other Achievement Types may have been modified after this record edit was started.
            if (!isNew)
            {
                var achievementTypeCache     = GetAchievementTypeCache();
                var eligibleAchievementTypes = StreakTypeAchievementTypeService.GetEligiblePrerequisiteAchievementTypeCaches(achievementTypeCache);

                foreach (var prerequisite in achievementType.Prerequisites)
                {
                    if (!eligibleAchievementTypes.Any(stat => stat.Id == prerequisite.PrerequisiteStreakTypeAchievementTypeId))
                    {
                        cvCustomValidator.IsValid      = false;
                        cvCustomValidator.ErrorMessage = string.Format(
                            "This achievement type cannot have prerequisite \"{0}\" because it would create a circular dependency.",
                            prerequisite.PrerequisiteStreakTypeAchievementType.Name);
                        return;
                    }
                }
            }

            if (!achievementType.IsValid)
            {
                // Controls will render the error messages
                return;
            }

            try
            {
                rockContext.SaveChanges();

                if (!achievementType.IsAuthorized(Authorization.VIEW, CurrentPerson))
                {
                    achievementType.AllowPerson(Authorization.VIEW, CurrentPerson, rockContext);
                }

                if (!achievementType.IsAuthorized(Authorization.EDIT, CurrentPerson))
                {
                    achievementType.AllowPerson(Authorization.EDIT, CurrentPerson, rockContext);
                }

                if (!achievementType.IsAuthorized(Authorization.ADMINISTRATE, CurrentPerson))
                {
                    achievementType.AllowPerson(Authorization.ADMINISTRATE, CurrentPerson, rockContext);
                }
            }
            catch (Exception ex)
            {
                ShowBlockException(nbEditModeMessage, ex);
                return;
            }

            achievementType.LoadAttributes(rockContext);
            avcComponentAttributes.GetEditValues(achievementType);
            achievementType.SaveAttributeValues(rockContext);

            // If the save was successful, reload the page using the new record Id.
            NavigateToPage(RockPage.Guid, new Dictionary <string, string> {
                { PageParameterKey.StreakTypeAchievementTypeId, achievementType.Id.ToString() }
            });
        }