/// <summary>
        /// Imports the user content from V2
        /// </summary>
        /// <param name="userStoriesTable">User Story data</param>
        /// <param name="userContentTable">User Content data</param>
        /// <param name="delimiter">Delimiter between data</param>
        /// <param name="work">The DB access</param>
        private void ImportAchievementUserStuff(
			HttpPostedFileBase userStoriesTable,
			HttpPostedFileBase userContentTable,
			String delimiter,
			UnitOfWork work)
        {
            // Grab the data
            List<Dictionary<String, String>> storyData = GetDataFromFile(userStoriesTable, delimiter);
            if (storyData == null)
            {
                ModelState.AddModelError("", "Error with User Story table.  Check Debug Output");
                return;
            }

            List<Dictionary<String, String>> contentData = GetDataFromFile(userContentTable, delimiter);
            if (contentData == null)
            {
                ModelState.AddModelError("", "Error with User Content table.  Check Debug Output");
                return;
            }

            // Go through stories
            try
            {
                work.EntityContext.Configuration.AutoDetectChangesEnabled = false;

                foreach (Dictionary<String, String> row in storyData)
                {
                    int oldID = int.Parse(row["userstoryID"]);
                    achievement_user_story story = new achievement_user_story()
                    {
                        date_submitted = DateTime.Parse(row["date_submitted"]),
                        image = row["uc_url"],
                        text = row["uc_text"]
                    };
                    ImportedEarnable impStory = new ImportedEarnable()
                    {
                        OldID = oldID,
                        UniqueData = row["date_submitted"]
                    };

                    work.EntityContext.achievement_user_story.Add(story);
                    _userStoryMap.Add(impStory.OldID, impStory);
                }
            }
            finally { work.EntityContext.Configuration.AutoDetectChangesEnabled = true; }

            // Save, then get new ids
            work.SaveChanges();
            foreach (ImportedEarnable impStory in _userStoryMap.Values)
            {
                DateTime submitTime = DateTime.Parse(impStory.UniqueData);
                impStory.NewID = (from s in work.EntityContext.achievement_user_story where s.date_submitted == submitTime select s.id).FirstOrDefault();
            }

            // Go through content
            try
            {
                work.EntityContext.Configuration.AutoDetectChangesEnabled = false;
                foreach (Dictionary<String, String> row in contentData)
                {
                    // Get the user
                    ImportedUser approver = GetImportedUserByOldID(row["approverID"]);
                    if (approver == null)
                        continue;

                    // Make the data entry
                    int oldID = int.Parse(row["usercontentID"]);
                    achievement_user_content content = new achievement_user_content()
                    {
                        submitted_date = DateTime.Parse(row["date_submitted"]),
                        text = row["uc_text"],
                        approved_by_id = approver.NewID,
                        approved_date = DateTime.Parse(row["date_handled"])
                    };

                    // content type
                    // 1 - image
                    // 2 - url
                    // 3 - text
                    int contentType = int.Parse(row["typeID"]);
                    switch (contentType)
                    {
                        case 1: // image
                            content.content_type = (int)JPPConstants.UserSubmissionTypes.Image;
                            content.image = row["uc_url"];
                            break;

                        case 2: // url
                            content.content_type = (int)JPPConstants.UserSubmissionTypes.URL;
                            content.url = row["uc_url"];
                            break;

                        case 3: // text
                            content.content_type = (int)JPPConstants.UserSubmissionTypes.Text;
                            break;
                    }

                    // Make the map item
                    ImportedEarnable impContent = new ImportedEarnable()
                    {
                        OldID = oldID,
                        UniqueData = row["date_submitted"]
                    };

                    work.EntityContext.achievement_user_content.Add(content);
                    _userContentMap.Add(impContent.OldID, impContent);
                }
            }
            finally { work.EntityContext.Configuration.AutoDetectChangesEnabled = true; }

            // Save, then get new ids
            work.SaveChanges();
            foreach (ImportedEarnable impStory in _userContentMap.Values)
            {
                DateTime submitTime = DateTime.Parse(impStory.UniqueData);
                impStory.NewID = (from s in work.EntityContext.achievement_user_content where s.submitted_date == submitTime select s.id).FirstOrDefault();
            }
        }
        /// <summary>
        /// Assigns an achievement with user content associated with it.
        /// TODO: CHECK THE LOGIC TO MAKE SURE IT ALL WORKS THE WAY IT SHOULD
        /// </summary>
        private void AssignContentSubmissionAchievement(int approvedByID, achievement_user_content_pending pendingContent)
        {
            //Assign the achievement
            var test = AssignAchievement(pendingContent.submitted_by_id, pendingContent.achievement_id, approvedByID);
            //Get the newly assigned achievement
            achievement_instance newInstance = _dbContext.achievement_instance.SingleOrDefault(ai => ai.user_id == pendingContent.submitted_by_id && ai.achievement_id == pendingContent.achievement_id);
            //Create the user content to be added
            achievement_user_content newUserContent = new achievement_user_content()
            {
                approved_by_id = approvedByID,
                approved_date = DateTime.Now,
                content_type = pendingContent.content_type,
                image = pendingContent.content_type == (int)JPPConstants.UserSubmissionTypes.Image ? pendingContent.image : null,
                submitted_date = pendingContent.submitted_date,
                text = pendingContent.text,
                url = pendingContent.content_type == (int)JPPConstants.UserSubmissionTypes.URL ? pendingContent.url : null
            };

            achievement_user_story newuserStory = new achievement_user_story()
            {
                image = pendingContent.content_type == (int)JPPConstants.UserSubmissionTypes.Image ? pendingContent.image : null,
                text = pendingContent.text,
                date_submitted = DateTime.Now
            };

            //Add the new user content to the database
            _dbContext.achievement_user_content.Add(newUserContent);
            _dbContext.achievement_user_story.Add(newuserStory);
            //append the instance to point to the new user content
            newInstance.has_user_content = true;
            newInstance.user_content_id = newUserContent.id;
            newInstance.has_user_story = true;
            newInstance.user_story_id = newuserStory.id;
            //Remove the content from the pending list
            _dbContext.achievement_user_content_pending.Remove(pendingContent);
            //Save changes
            Save();
        }
        /// <summary>
        /// Imports the achievement instances from V2
        /// </summary>
        /// <param name="achieveTemplateTable">File w/ achievement instance data</param>
        /// <param name="achievePointTemplateTable">Achievement point instance data (to be rolled into achievement)</param>
        /// <param name="delimiter">Delimiter between data</param>
        /// <param name="work">The DB access</param>
        private void ImportAchievementInstances(
			HttpPostedFileBase achieveInstanceTable,
			HttpPostedFileBase achievePointInstanceTable,
			String delimiter,
			UnitOfWork work)
        {
            // Grab the data
            List<Dictionary<String, String>> achieveData = GetDataFromFile(achieveInstanceTable, delimiter);
            if (achieveData == null)
            {
                ModelState.AddModelError("", "Error with Achievement Instance table.  Check Debug Output");
                return;
            }

            List<Dictionary<String, String>> pointData = GetDataFromFile(achievePointInstanceTable, delimiter);
            if (pointData == null)
            {
                ModelState.AddModelError("", "Error with Achievement Point Instance table.  Check Debug Output");
                return;
            }

            // Put points into a dictionary
            Dictionary<int, int[]> pointLookUp = new Dictionary<int, int[]>();
            foreach (Dictionary<String, String> row in pointData)
            {
                int oldID = int.Parse(row["achievement_instanceID"]);
                if (!pointLookUp.ContainsKey(oldID))
                {
                    pointLookUp.Add(oldID, new int[5]);
                }
                pointLookUp[oldID][int.Parse(row["categoryID"])] = int.Parse(row["points"]);
            }

            // Go through each data row
            try
            {
                work.EntityContext.Configuration.AutoDetectChangesEnabled = false;

                foreach (Dictionary<String, String> row in achieveData)
                {
                    // Get the user and achievement
                    ImportedEarnable achieve = GetImportedEarnableByOldID(_achievementMap, row["achievementID"]);
                    ImportedUser user = GetImportedUserByOldID(row["userID"]);
                    ImportedUser assigner = GetImportedUserByOldID(row["assignedbyID"]);
                    if (achieve == null || achieve.NewID == 0 ||
                        user == null || user.NewID == 0)
                        continue;

                    // How many?
                    int count = int.Parse(row["achievementcount"]);
                    if (count == 0)
                        count = 1;

                    // Make multiple if it's repeatable!
                    for (int i = 0; i < count; i++)
                    {
                        achievement_instance instance = new achievement_instance()
                        {
                            achieved_date = DateTime.Parse(row["date_achieved"]),
                            achievement_id = achieve.NewID,
                            assigned_by_id = assigner == null ? user.NewID : assigner.NewID,
                            card_given = Boolean.Parse(row["has_cardbeengiven"]),
                            card_given_date = String.IsNullOrWhiteSpace(row["givenDate"]) ? (DateTime?)null : DateTime.Parse(row["givenDate"]),
                            comments_disabled = false,
                            has_user_content = Boolean.Parse(row["has_usercontent"]),
                            has_user_story = Boolean.Parse(row["has_userstory"]),
                            user_id = user.NewID,
                            globally_assigned = false
                        };

                        if (i == 0)
                        {
                            // Look up points
                            int[] points;
                            if (pointLookUp.TryGetValue(int.Parse(row["achievement_instanceID"]), out points))
                            {
                                instance.points_create = points[1]; // Create = 1
                                instance.points_explore = points[4]; // Explore = 4
                                instance.points_learn = points[2]; // Learn = 2
                                instance.points_socialize = points[3]; // Socialize = 3
                            }

                            // Get user content/story stuff
                            ImportedEarnable userContent = GetImportedEarnableByOldID(_userContentMap, row["usercontentID"]);
                            ImportedEarnable userStory = GetImportedEarnableByOldID(_userStoryMap, row["userstoryID"]);
                            if (instance.has_user_content)
                            {
                                if (userContent == null || userContent.NewID == 0)
                                    continue;	// If content is REQUIRED, and not found, skip this - User will need to re-get achievement
                                else
                                    instance.user_content_id = userContent.NewID;
                            }

                            // Check for legit user story
                            if (instance.has_user_story && userStory != null && userStory.NewID > 0)
                            {
                                instance.user_story_id = userStory.NewID;
                            }

                            if (!instance.has_user_story && instance.has_user_content)
                            {
                                var content = work.EntityContext.achievement_user_content.Local.Single(c => c.id == userContent.NewID);
                                achievement_user_story newStory = new achievement_user_story()
                                {
                                    date_submitted = content.submitted_date,
                                    image = content.image,
                                    text = content.text
                                };
                                work.EntityContext.achievement_user_story.Add(newStory);
                                instance.has_user_story = true;
                                instance.user_story = newStory;
                            }
                        }

                        // Add to the DB
                        work.EntityContext.achievement_instance.Add(instance);
                    }

                }
            }
            finally { work.EntityContext.Configuration.AutoDetectChangesEnabled = true; }

            work.SaveChanges();

            // Update content paths
            try
            {
                work.EntityContext.Configuration.AutoDetectChangesEnabled = false;
                foreach (achievement_instance instance in work.EntityContext.achievement_instance)
                {
                    if (instance.user_content != null)
                    {
                        instance.user_content.image = UpdateContentPath(instance.user_id, instance.user_content.image);
                    }

                    if (instance.user_story != null)
                    {
                        instance.user_story.image = UpdateContentPath(instance.user_id, instance.user_story.image);
                    }
                }
            }
            finally { work.EntityContext.Configuration.AutoDetectChangesEnabled = true; }
            work.SaveChanges();
        }
        public Boolean UserAddAchievementStoryText(int instanceID, String text)
        {
            //Get the achievement instance, if it doesn't exist, don't continue
            achievement_instance instance = _dbContext.achievement_instance.Find(instanceID);
            if (instance == null)
                return false;

            //Set up the user story
            achievement_user_story userStory = null;
            //If the instance has a story already, get it and set userStory equal to it
            if (instance.has_user_story)
                userStory = _dbContext.achievement_user_story.Find(instance.user_story_id);
            //Make sure the userStory isn't null and then set the text equal to the new text
            if (userStory != null)
                userStory.text = text;
            else
            {
                //userStory was null create one and add it to the database
                userStory = new achievement_user_story()
                {
                    date_submitted = DateTime.Now,
                    text = text
                };

                _dbContext.achievement_user_story.Add(userStory);
                //Update the instance to include the user story
                instance.has_user_story = true;
                instance.user_story_id = userStory.id;
            }
            LoggerModel logUserStoryText = new LoggerModel()
            {
                Action = Logger.UserStoryLogType.AddStoryText.ToString(),
                UserID = instance.user_id,
                IPAddress = HttpContext.Current.Request.UserHostAddress,
                TimeStamp = DateTime.Now,
                ID1 = userStory.id,
                IDType1 = Logger.LogIDType.UserStory.ToString(),
                Value1 = text
            };
            Logger.LogSingleEntry(logUserStoryText, _dbContext);

            Save();
            return true;
        }