public ActionResult AddOther(RatingViewModel post)
        {
            CoreRating ratingToAdd;
            if (post.Rating.OtherCoreRatings != null)
            {
                int addindex = post.Rating.OtherCoreRatings.Count - 1;
                ratingToAdd = post.Rating.OtherCoreRatings[addindex];
            }
            else ratingToAdd = null;

            if (ratingToAdd != null && ratingToAdd.RatingID == 0 && ValidateCoreRating(ratingToAdd))
            {
                CompleteRating rating = new CompleteRating()
                {
                    OtherCoreRatings = new List<CoreRating>()
                };
                rating.OtherCoreRatings.Add(ratingToAdd);
                db.saveTaskRating(rating, post.VersID);
            }

            RatingViewModel model = db.getOtherRatings(post.VersID);
            model.OtherVisible = true;
            return PartialView("Other", model);
        }
        private CompleteRating getTaskRating(int versID)
        {
            int seminarID = db.TaskVersions.Find(versID).Task.SeminarID;
            List<CoreRating> taskRatings = new List<CoreRating>();
            IEnumerable<CoreRating> existingTaskRatings = db.CoreRatings.Include(r => r.TaskRating).Include(r => r.CoreTopic).
                Where(r => r.TaskRating.VersID == versID && r.CoreTopic.SeminarID == seminarID);

            foreach (CoreTopic topic in db.CoreTopics.Where(c => c.SeminarID == seminarID).OrderBy(c => c.CoreTopicNum))
            {
                CoreRating existingTopicRating = existingTaskRatings.FirstOrDefault(c => c.CoreTopicID == topic.CoreTopicID);
                if (existingTopicRating == null)
                    taskRatings.Add(new CoreRating { RatingID = -1, CoreTopic = topic });
                else
                    taskRatings.Add(existingTopicRating);
            }

            CompleteRating taskRating = new CompleteRating()
            {
                TaskCoreRatings = taskRatings,
                ImpactRating = db.ImpactTypeRatings.FirstOrDefault(r => r.TaskRating.VersID == versID)
            };

            return taskRating;
        }
        public void saveTaskRating(CompleteRating taskRating, int VersID)
        {
            if (taskRating.TaskCoreRatings != null)
            {
                IEnumerable<CoreRating> validTaskRatings = taskRating.TaskCoreRatings.
                Where(r => r.Cscore != null || r.Pscore != null || r.Sscore != null || r.RatingID > 0);
                if (validTaskRatings != null)
                {
                    foreach (CoreRating rating in validTaskRatings)
                    {
                        this._saveTaskRating(rating, VersID, taskRating.FacultyID);
                    }
                }
            }

            if (taskRating.OtherCoreRatings != null)
            {
                foreach (CoreRating rating in taskRating.OtherCoreRatings)
                {
                    this._saveTaskRating(rating, VersID, taskRating.FacultyID);
                }
            }

            if (taskRating.ImpactRating != null)
            {
                saveImpactRating(taskRating, VersID);
            }
        }
        private CompleteRating getOtherRating(int versID)
        {
            int seminarID = db.TaskVersions.Find(versID).Task.SeminarID;
            List<CoreRating> otherRatings = db.CoreRatings.Include(r => r.TaskRating).Include(r => r.CoreTopic).
                Where(r => r.TaskRating.VersID == versID && r.CoreTopic.SeminarID != seminarID)
                .OrderBy(r => r.CoreTopic.CoreTopicNum).ToList();

            CompleteRating otherRating = new CompleteRating()
            {
                OtherCoreRatings = otherRatings,
                ImpactRating = db.ImpactTypeRatings.FirstOrDefault(r => r.TaskRating.VersID == versID)
            };

            return otherRating;
        }
        public void saveImpactRating(CompleteRating impactRating, int VersID)
        {
            ImpactTypeRating saveRating = impactRating.ImpactRating;
            if (impactRating.ImpactRating.RatingID > 0)
            {
                ImpactTypeRating impact = db.ImpactTypeRatings.Find(impactRating.ImpactRating.RatingID);
                impact.Sscore = impactRating.ImpactRating.Sscore;
                impact.Pscore = impactRating.ImpactRating.Pscore;
                impact.Lscore = impactRating.ImpactRating.Lscore;
            }
            else if(saveRating.Sscore != null || saveRating.Pscore != null || saveRating.Lscore != null)
            {
                TaskRating rating = new TaskRating()
                {
                    FacultyID = impactRating.FacultyID == 0 ? 2: impactRating.FacultyID,
                    ReviewDate = DateTime.Now,
                    VersID = VersID,
                };

                ImpactTypeRating impact = new ImpactTypeRating()
                {
                    Sscore = impactRating.ImpactRating.Sscore,
                    Pscore = impactRating.ImpactRating.Pscore,
                    Lscore = impactRating.ImpactRating.Lscore,
                    TaskRating = rating
                };
                db.TaskRatings.Add(rating);
                db.ImpactTypeRatings.Add(impact);
            }
            db.SaveChanges();
        }