Пример #1
0
        public ActionResult EditAchievement(int id, EditAchievementViewModel model)
        {
            //Add the Logged In User(Creator) ID to the Model
            model.EditorID = WebSecurity.CurrentUserId;
            ViewBag.ModelStateBeforeCode = ModelState.IsValid;
            //Create a new Unit of Work
            UnitOfWork work = new UnitOfWork();

            JPPConstants.AchievementQuestStates currentAchievementState = (JPPConstants.AchievementQuestStates)work.AchievementRepository.GetAchievementState(id);
            JPPConstants.AchievementQuestStates modelAchievementState = (JPPConstants.AchievementQuestStates)model.State;

            if (!currentAchievementState.Equals(JPPConstants.AchievementQuestStates.Draft) && modelAchievementState.Equals(JPPConstants.AchievementQuestStates.Draft))
                ModelState.AddModelError(String.Empty, "This Achievement was already moved out of draft mode, it cannot be moved back");

            #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
            //Only scans are 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) && id != work.AchievementRepository.GetSystemAchievementID((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, id))
                    ModelState.AddModelError("", "An achievement with that title already exists");
            //Check to make sure the model is valid
            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);
                    }

                    Utilities.JPPDirectory.CheckAndCreateAchievementAndQuestDirectory(Server);
                    achievement_template template = work.EntityContext.achievement_template.Find(id);
                    model.IconFilePath = template == null ?
                        Utilities.JPPDirectory.CreateFilePath(JPPDirectory.ImageTypes.AchievementIcon) :
                        template.icon;
                    if (model.IconFilePath.Contains(".jpg"))
                        model.IconFilePath = model.IconFilePath.Replace(".jpg", "");
                    if (!model.IconFilePath.Contains(".png"))
                        model.IconFilePath += ".png";
                   if(JPPImage.SaveAchievementIcons(model.IconFilePath, model.Icon, model.PointsCreate, model.PointsExplore, model.PointsLearn, model.PointsSocialize))
                    {
                        //Add the Achievement to the Database
                        work.AchievementRepository.AdminEditAchievement(id, model);

                        //Return to the Admin index page
                        TempData["Message"] = "The changes to " + model.Title + " were successfully saved.";
                        return RedirectToAction("EditAchievementList");
                    }
                }
                catch(Exception e)
                {
                    ModelState.AddModelError("", "Achievement Editing 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 EditAchievement view with the current model
            return View(model);
        }
        public void AdminEditAchievement(int id, EditAchievementViewModel model)
        {
            achievement_template currentAchievement = _dbContext.achievement_template.Find(id);
            List<LoggerModel> logChanges = new List<LoggerModel>();
            //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 = currentAchievement.id, description = model.Requirement1 });
            if (!String.IsNullOrWhiteSpace(model.Requirement2))
                requirementsList.Add(new achievement_requirement { achievement_id = currentAchievement.id, description = model.Requirement2 });
            if (!String.IsNullOrWhiteSpace(model.Requirement3))
                requirementsList.Add(new achievement_requirement { achievement_id = currentAchievement.id, description = model.Requirement3 });
            if (!String.IsNullOrWhiteSpace(model.Requirement4))
                requirementsList.Add(new achievement_requirement { achievement_id = currentAchievement.id, description = model.Requirement4 });
            if (!String.IsNullOrWhiteSpace(model.Requirement5))
                requirementsList.Add(new achievement_requirement { achievement_id = currentAchievement.id, description = model.Requirement5 });
            if (!String.IsNullOrWhiteSpace(model.Requirement6))
                requirementsList.Add(new achievement_requirement { achievement_id = currentAchievement.id, description = model.Requirement6 });

            //Get the old list of requirements and remove them from the database to prevent duplicate entries
            List<achievement_requirement> oldRequirements = _dbContext.achievement_requirement.Where(ar => ar.achievement_id == currentAchievement.id).ToList();
            foreach (achievement_requirement requirement in oldRequirements)
                _dbContext.achievement_requirement.Remove(requirement);

            //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 = currentAchievement.id, caretaker_id = model.SelectedCaretakersList[j] });

            //Get the old list of caretakers and remove them from the database to prevent duplicate entries
            List<achievement_caretaker> oldCaretakers = _dbContext.achievement_caretaker.Where(ac => ac.achievement_id == currentAchievement.id).ToList();
            foreach (achievement_caretaker caretaker in oldCaretakers)
                _dbContext.achievement_caretaker.Remove(caretaker);

            //Compare the current achievement values in the DB to the model, if they are different, set the current achievement values equal to the model values
            #region//ContentType
            if (model.ContentType != currentAchievement.content_type)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.ContentType.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.content_type.ToString(),
                    Value2 = model.ContentType.ToString()
                });
                currentAchievement.content_type = model.ContentType;
            }
            #endregion

            #region//Description
            if (!String.IsNullOrWhiteSpace(model.Description) && !String.Equals(currentAchievement.description, model.Description))
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.Description.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.description,
                    Value2 = model.Description
                });
                currentAchievement.description = model.Description;
            }
            #endregion

            #region//Hidden
            if (currentAchievement.hidden != model.Hidden)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.Hidden.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.hidden.ToString(),
                    Value2 = model.Hidden.ToString()
                });
                currentAchievement.hidden = model.Hidden;
            }
            #endregion

            #region//Base Icon
            if (!String.IsNullOrWhiteSpace(model.Icon) && !String.Equals(currentAchievement.icon_file_name, model.Icon))
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.Icon.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.icon_file_name.ToString(),
                    Value2 = model.Icon.ToString()
                });
                currentAchievement.icon = model.IconFilePath;
                currentAchievement.icon_file_name = model.Icon;
            }
            #endregion

            #region//Icon File Path
            if (!String.IsNullOrWhiteSpace(model.IconFilePath) && !String.Equals(currentAchievement.icon, model.IconFilePath))
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.Icon.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.icon.ToString(),
                    Value2 = model.IconFilePath.ToString()
                });
                currentAchievement.icon = model.IconFilePath;
                currentAchievement.icon_file_name = model.Icon;
            }
            #endregion

            #region//IsRepeatable
            if (currentAchievement.is_repeatable != model.IsRepeatable)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.IsRepeatable.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.is_repeatable.ToString(),
                    Value2 = model.IsRepeatable.ToString()
                });
                currentAchievement.is_repeatable = model.IsRepeatable;
            }
            #endregion

            #region//Last Modified By (userID and DateTime)
            currentAchievement.last_modified_by_id = model.EditorID;
            currentAchievement.modified_date = DateTime.Now;
            #endregion

            #region//Parent Achievement ID
            if (currentAchievement.parent_id != model.ParentID)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.ParentID.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.parent_id.ToString(),
                    Value2 = model.ParentID.ToString()
                });
                currentAchievement.parent_id = model.ParentID;
            }
            #endregion

            #region//Points Create
            if (currentAchievement.points_create != model.PointsCreate)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.PointsCreate.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.points_create.ToString(),
                    Value2 = model.PointsCreate.ToString()
                });
                currentAchievement.points_create = model.PointsCreate;
            }
            #endregion

            #region//Points Explore
            if (currentAchievement.points_explore != model.PointsExplore)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.PointsExplore.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.points_explore.ToString(),
                    Value2 = model.PointsExplore.ToString()
                });
                currentAchievement.points_explore = model.PointsExplore;
            }
            #endregion

            #region//Points Learn
            if (currentAchievement.points_learn != model.PointsLearn)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.PointsLearn.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.points_learn.ToString(),
                    Value2 = model.PointsLearn.ToString()
                });
                currentAchievement.points_learn = model.PointsLearn;
            }
            #endregion

            #region//Points Socialize
            if (currentAchievement.points_socialize != model.PointsSocialize)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.PointsSocialize.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.points_socialize.ToString(),
                    Value2 = model.PointsSocialize.ToString()
                });
                currentAchievement.points_socialize = model.PointsSocialize;
            }
            #endregion

            #region//Posted Date
            if (currentAchievement.state != model.State && model.State == (int)JPPConstants.AchievementQuestStates.Active && currentAchievement.posted_date == null)
                currentAchievement.posted_date = DateTime.Now;
            #endregion

            #region//Repeat Delay Days
            if (currentAchievement.repeat_delay_days != model.RepeatDelayDays)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.RepeatDelayDays.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.repeat_delay_days.ToString(),
                    Value2 = model.RepeatDelayDays.ToString()
                });
                currentAchievement.repeat_delay_days = model.RepeatDelayDays;
            }
            #endregion

            #region//Retire Date
            if (currentAchievement.state != model.State && model.State == (int)JPPConstants.AchievementQuestStates.Retired && currentAchievement.retire_date == null)
                currentAchievement.retire_date = DateTime.Now;
            if (currentAchievement.state != model.State && currentAchievement.state == (int)JPPConstants.AchievementQuestStates.Retired)
                currentAchievement.retire_date = null;
            #endregion

            #region//Achievement State
            if (currentAchievement.state != model.State)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.State.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.state.ToString(),
                    Value2 = model.State.ToString()
                });
                currentAchievement.state = model.State;
            }
            #endregion

            #region//Featured
            if (currentAchievement.state != (int)JPPConstants.AchievementQuestStates.Active)
                currentAchievement.featured = false;
            #endregion

            #region//System Trigger Type
            if (currentAchievement.system_trigger_type != model.SystemTriggerType)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.SystemTriggerType.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.system_trigger_type.ToString(),
                    Value2 = model.SystemTriggerType.ToString()
                });
                currentAchievement.system_trigger_type = model.SystemTriggerType;
            }
            #endregion

            #region//Threshold
            if (currentAchievement.threshold != model.Threshold)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.Threshold.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.threshold.ToString(),
                    Value2 = model.Threshold.ToString()
                });
                currentAchievement.threshold = model.Threshold;
            }
            #endregion

            #region//Title
            if (!String.IsNullOrWhiteSpace(model.Title) && !String.Equals(currentAchievement.title, model.Title))
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.Title.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.title.ToString(),
                    Value2 = model.Title.ToString()
                });
                currentAchievement.title = model.Title;
            }
            #endregion

            #region//Type
            if (currentAchievement.type != model.Type)
            {
                logChanges.Add(new LoggerModel()
                {
                    Action = "Edit Achievement: " + Logger.EditAchievementLogType.Type.ToString(),
                    UserID = model.EditorID,
                    IPAddress = HttpContext.Current.Request.UserHostAddress,
                    TimeStamp = DateTime.Now,
                    IDType1 = Logger.LogIDType.AchievementTemplate.ToString(),
                    ID1 = id,
                    Value1 = currentAchievement.type.ToString(),
                    Value2 = model.Type.ToString()
                });
                currentAchievement.type = model.Type;
            }
            #endregion

            if (logChanges.Count > 0)
                Logger.LogMultipleEntries(logChanges, _dbContext);

            AddRequirementsToDatabase(requirementsList);
            AddCaretakersToDatabase(caretakersList);

            Save();
        }