Exemplo n.º 1
0
        public ActionResult AddAchievement(AddAchievementViewModel model)
        {
            //Add the Logged In User(Creator) ID to the Model
            model.CreatorID = WebSecurity.CurrentUserId;

            //Create a new Unit of Work
            UnitOfWork work = new UnitOfWork();

            #region Modify Model based on achievement type

            //Only scans get caretakers| Only thresholds have a threshold number and parent
            //Only user submissions have content types | Only system achievements have system trigger types
            //Thresholds can't be repeatable | Only repeatable achievements have a delay, which must be at least 1

            JPPConstants.AchievementTypes achievementType = (JPPConstants.AchievementTypes)model.Type;
            model.IsRepeatable = !achievementType.Equals(JPPConstants.AchievementTypes.Scan) ? false : model.IsRepeatable;
            model.SelectedCaretakersList = achievementType.Equals(JPPConstants.AchievementTypes.Scan) ? model.SelectedCaretakersList : null;
            model.Threshold = achievementType.Equals(JPPConstants.AchievementTypes.Threshold) ? model.Threshold : null;
            model.ParentID = achievementType.Equals(JPPConstants.AchievementTypes.Threshold) ? model.ParentID : null;
            model.ContentType = achievementType.Equals(JPPConstants.AchievementTypes.UserSubmission) ? model.ContentType : null;
            model.SystemTriggerType = achievementType.Equals(JPPConstants.AchievementTypes.System) ? model.SystemTriggerType : null;
            model.RepeatDelayDays = model.RepeatDelayDays >= 1 ? model.RepeatDelayDays : 1;
            model.RepeatDelayDays = model.IsRepeatable ? model.RepeatDelayDays : null;

            #endregion

            if (model.Type == (int)JPPConstants.AchievementTypes.System && work.AchievementRepository.SystemAchievementExists((int)model.SystemTriggerType))
                ModelState.AddModelError(String.Empty, "There is already a system achievement of that type");
            if (model.Icon == null && model.UploadedIcon == null)
                ModelState.AddModelError(String.Empty, "An icon must be selected for this achievement");
            if (model.Title != null)
                if (work.AchievementRepository.AchievementTitleExists(model.Title))
                    ModelState.AddModelError("", "An achievement with that title already exists");
            //Check to make sure the model is valid and the image uploaded is an actual image
            if (ModelState.IsValid)
            {
                try
                {
                    if(model.UploadedIcon != null)
                    {
                        String filePath = Utilities.JPPDirectory.CreateFilePath(JPPDirectory.ImageTypes.NewIconUpload);
                        model.Icon = filePath.Replace("~/Content/Images/Icons/", "");
                        model.Icon = model.Icon.Replace(".png", "");
                        JPPImage.Save(Server, filePath, model.UploadedIcon.InputStream, 400, 400, true);
                    }
                    //Make Sure the Directories Exist
                    Utilities.JPPDirectory.CheckAndCreateAchievementAndQuestDirectory(Server);

                    //Create the file path and save the image
                    model.IconFilePath = Utilities.JPPDirectory.CreateFilePath(JPPDirectory.ImageTypes.AchievementIcon);
                    if (JPPImage.SaveAchievementIcons(model.IconFilePath, model.Icon, model.PointsCreate, model.PointsExplore, model.PointsLearn, model.PointsSocialize))
                    {
                        //Add the Achievement to the Database
                        work.AchievementRepository.AdminAddAchievement(model);

                        //Return to the Admin index page
                        TempData["Message"] = "Achievement: " + model.Title + " successfully created and is in draft mode awaiting approval.";
                        return RedirectToAction("EditAchievementList");
                    }
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Achievement Creation Failed: " + e.Message);
                }
            }

            //ModelState was not valid, refresh the ViewModel
            AddAchievementViewModel refreshModel = AddAchievementViewModel.Populate();
            model.PotentialCaretakersList = refreshModel.PotentialCaretakersList;
            model.ParentAchievements = refreshModel.ParentAchievements;
            model.IconList = refreshModel.IconList;

            //Return the user to the AddAchievement view with the current model
            return View(model);
        }
        public void AdminAddAchievement(AddAchievementViewModel model)
        {
            //Create the new achievement template from the model
            achievement_template newAchievement = new achievement_template()
            {
                title = model.Title,
                description = model.Description,
                icon = model.IconFilePath,
                icon_file_name = model.Icon,
                type = model.Type,
                featured = false,
                hidden = model.Hidden,
                is_repeatable = model.IsRepeatable,
                state = (int)JPPConstants.AchievementQuestStates.Draft,
                parent_id = model.ParentID,
                threshold = model.Threshold,
                creator_id = model.CreatorID,
                created_date = DateTime.Now,
                posted_date = null,
                retire_date = null,
                modified_date = null,
                last_modified_by_id = null,
                content_type = model.ContentType,
                system_trigger_type = model.SystemTriggerType,
                repeat_delay_days = model.RepeatDelayDays,
                points_create = model.PointsCreate,
                points_explore = model.PointsExplore,
                points_learn = model.PointsLearn,
                points_socialize = model.PointsSocialize,
                keywords = ""

            };

            //Create all the requirements for the achievement to be added to the database
            List<achievement_requirement> requirementsList = new List<achievement_requirement>();
            if (!String.IsNullOrWhiteSpace(model.Requirement1))
                requirementsList.Add(new achievement_requirement { achievement_id = newAchievement.id, description = model.Requirement1 });
            if (!String.IsNullOrWhiteSpace(model.Requirement2))
                requirementsList.Add(new achievement_requirement { achievement_id = newAchievement.id, description = model.Requirement2 });
            if (!String.IsNullOrWhiteSpace(model.Requirement3))
                requirementsList.Add(new achievement_requirement { achievement_id = newAchievement.id, description = model.Requirement3 });
            if (!String.IsNullOrWhiteSpace(model.Requirement4))
                requirementsList.Add(new achievement_requirement { achievement_id = newAchievement.id, description = model.Requirement4 });
            if (!String.IsNullOrWhiteSpace(model.Requirement5))
                requirementsList.Add(new achievement_requirement { achievement_id = newAchievement.id, description = model.Requirement5 });
            if (!String.IsNullOrWhiteSpace(model.Requirement6))
                requirementsList.Add(new achievement_requirement { achievement_id = newAchievement.id, description = model.Requirement6 });

            //Create all the caretakers for the achievement to be added to the database
            List<achievement_caretaker> caretakersList = new List<achievement_caretaker>();
            if (model.SelectedCaretakersList != null)
            {
                for (int j = 0; j < model.SelectedCaretakersList.Count; j++)
                {
                    caretakersList.Add(new achievement_caretaker() { achievement_id = newAchievement.id, caretaker_id = model.SelectedCaretakersList[j] });
                }
            }

            AddAchievementToDatabase(newAchievement);
            AddRequirementsToDatabase(requirementsList);
            AddCaretakersToDatabase(caretakersList);

            Save();
        }