public static int AddUser(Domain.User dto)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                DateTime CurrentTime = DateTime.Now;

                User User = new User
                {
                    Name = dto.Name,
                    EmailAddress = dto.EmailAddress,
                    Password = dto.Password,
                    Title = dto.Title,
                    Organization = dto.Organization,
                    RoleID = dto.RoleID,
                    TimeZoneID = dto.TimeZoneID,
                    DateCreated = CurrentTime,
                    DateModified = CurrentTime
                };

                db.Users.InsertOnSubmit(User);
                db.SubmitChanges();
                dto.ID = User.UserID;

                UpdateUserLastLogin(dto.ID, CurrentTime);
                UserLoginHistory.AddUserLoginHistory(new Domain.UserLoginHistory(dto.ID, CurrentTime));
                return dto.ID;
            }
        }
 public static List<Domain.Evaluation> GetEvaluations(int userID)
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         return db.Evaluations.Where(i => i.UserID == userID).Select(i => i.GetDomainObject(false, false)).ToList();
     }
 }
 public static List<Domain.Response> GetResponses(int evaluationID)
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         return db.Responses.Where(i => i.EvaluationID == evaluationID && i.DimensionID > (int)DimensionEnum.ScopeEvaluation).Select(i => i.GetDomainObject()).ToList();
     }
 }
        public static int AddEvaluation(Domain.Evaluation dto)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                Evaluation Evaluation = new Evaluation
                {
                    Name = dto.Name,
                    Description = dto.Description,
                    StatusID = dto.StatusID,
                    UserID = dto.UserID,
                    RoleID = dto.RoleID,
                    DateCreated = DateTime.Now,
                    DateModified = DateTime.Now
                };

                // Update evaluation levels

                // Update responses

                db.Evaluations.InsertOnSubmit(Evaluation);
                db.SubmitChanges();
                dto.ID = Evaluation.EvaluationID;

                return dto.ID;
            }
        }
 public static List<Domain.Dimension> GetDimensions()
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         return db.Dimensions.Select(i => i.GetDomainObject(false)).ToList();
     }
 }
 public static List<Domain.Question> GetQuestions()
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         return db.Questions.Select(i => i.GetDomainObject(true)).ToList();
     }
 }
        public static void AddEvaluationLevel(Domain.Evaluation dto)
        {
            DeleteEvaluationLevels(dto.ID);

            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                foreach (Domain.Level level in dto.CurrentLevels)
                {
                    if (level.LevelNumber != 0)
                    {
                        EvaluationLevel eLevel = new EvaluationLevel
                        {
                            EvaluationID = dto.ID,
                            LevelID = level.ID,
                            DimensionID = level.DimensionID,
                            DateCreated = DateTime.Now,
                            DateModified = DateTime.Now
                        };

                        db.EvaluationLevels.InsertOnSubmit(eLevel);
                        db.SubmitChanges();
                    }
                }
            }
        }
 public static List<Domain.UserLoginHistory> GetLoginHistory(int userID)
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         return db.UserLoginHistories.Where(i => i.UserID == userID).Select(i => i.GetDomainObject()).ToList();
     }
 }
 public static List<Domain.User> GetUsers()
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         return db.Users.Select(i => i.GetDomainObject(false, false)).ToList();
     }
 }
        public static void AddResponses(List<Domain.Response> responses, int evaluationID, int dimensionID)
        {
            // Clear existing values first
            //DeleteResponses(evaluationID, dimensionID);

            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                foreach (Domain.Response dto in responses)
                {
                    Response response = new Response
                    {
                        QuestionID = dto.QuestionID,
                        EvaluationID = evaluationID,
                        DimensionID = dimensionID,
                        Name = "",
                        SelectedValue = (int)dto.SelectedValue,
                        DateCreated = DateTime.Now,
                        DateModified = DateTime.Now
                    };

                    db.Responses.InsertOnSubmit(response);
                    db.SubmitChanges();
                }
            }

            Evaluation.UpdateEvaluationLastModified(evaluationID, DateTime.Now);
        }
 public static Domain.Dimension GetDimension(int dimensionID, bool getChildrenObject)
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         Domain.Dimension dimension = db.Dimensions.Where(i => i.DimensionID == dimensionID).Select(i => i.GetDomainObject(getChildrenObject)).FirstOrDefault();
         return dimension;
     }
 }
 public static List<Domain.Level> GetLevels()
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         List<Domain.Level> levels = db.Levels.Where(i => i.DimensionID > (int)DimensionEnum.Implementation).OrderBy(i => i.DimensionID).Select(i => i.GetDomainObject(0, false, true)).ToList();
         return levels;
     }
 }
 public static List<Domain.Level> GetLevels(int dimensionID, bool getChildrenObject, bool getParentObject)
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         List<Domain.Level> levels = db.Levels.OrderBy(i => i.LevelNumber).Where(i => i.DimensionID == dimensionID).Select(i => i.GetDomainObject(dimensionID, getChildrenObject, getParentObject)).ToList();
         return levels;
     }
 }
 public static List<Domain.EvaluationResult> GetEvaluationResults()
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         List<Domain.EvaluationResult> EvaluationResults = db.EvaluationResults.Select(q => q.GetDomainObject()).ToList();
         return EvaluationResults;
     }
 }
 public static List<Domain.TimeZone> GetTimeZones()
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         List<Domain.TimeZone> timeZones = db.TimeZones.Select(q => q.GetDomainObject()).ToList();
         return timeZones;
     }
 }
 public static List<Domain.User> GetUsers(bool getChildrenObject, bool getParentObject)
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         List<Domain.User> Users = db.Users.OrderBy(i => i.Name).Select(i => i.GetDomainObject(getChildrenObject, getParentObject)).ToList();
         return Users;
     }
 }
 public static List<Domain.Role> GetRoles(bool getChildrenObject)
 {
     using (EvaluationDBDataContext db = new EvaluationDBDataContext())
     {
         List<Domain.Role> roles = db.Roles.Select(q => q.GetDomainObject(getChildrenObject)).ToList();
         return roles;
     }
 }
        public static Domain.Evaluation GetEvaluation(int evaluationID)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                var q = (from Evaluations in db.Evaluations
                        where Evaluations.EvaluationID == evaluationID
                        select Evaluations).FirstOrDefault();

                return q.GetDomainObject(true, true);
            }
        }
        public static Domain.User GetUser(int userID)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                var q = (from Users in db.Users
                        where Users.UserID == userID
                        select Users).FirstOrDefault();

                return q.GetDomainObject(true, true);
            }
        }
        public static List<Domain.Question> GetLevelQuestions(int levelID, int congestionID)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                var q = (from questions in db.Questions
                        join levelQuestions in db.LevelQuestions on questions.QuestionID equals levelQuestions.QuestionID
                        where levelQuestions.LevelID == levelID && levelQuestions.CongestionID == congestionID
                        select questions).Distinct();

                return q.Select(i => i.GetDomainObject()).ToList();
            }
        }
        public static void DeleteResponses(int evaluationID, int dimensionID)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                var rowsToDelete = from q in db.Responses
                                where q.EvaluationID == evaluationID && q.DimensionID == dimensionID
                                select q;

                db.Responses.DeleteAllOnSubmit(rowsToDelete);
                db.SubmitChanges();
            }
        }
        public static Domain.Level GetQuestionLevel(int questionID)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                var q = (from levels in db.Levels
                        join levelQuestions in db.LevelQuestions on levels.LevelID equals levelQuestions.LevelID
                        where levelQuestions.QuestionID == questionID
                        select levels).FirstOrDefault();

                return q.GetDomainObject(0, false, true);
            }
        }
        public static List<Domain.Level> GetLevels(int evaluationID)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                var q = (from eLevels in db.EvaluationLevels
                        join levels in db.Levels on eLevels.LevelID equals levels.LevelID
                        where eLevels.EvaluationID == evaluationID
                        select levels);

                return q.Select(i => i.GetDomainObject(0, false, false)).ToList().OrderBy(i => i.Name).ToList();
            }
        }
        public static void DeleteEvaluationLevels(int evaluationID)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                var rowsToDelete = from q in db.EvaluationLevels
                                where q.EvaluationID == evaluationID
                                select q;

                db.EvaluationLevels.DeleteAllOnSubmit(rowsToDelete);
                db.SubmitChanges();
            }
        }
        public static int GetUserIDByEmail(string emailAddress)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                User foundUser = db.Users.Where(i => i.EmailAddress == emailAddress).SingleOrDefault();

                if (foundUser != null)
                {
                    return foundUser.UserID;
                }
                return 0;
            }
        }
        public static bool IsExistingUser(string emailAddress)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                User foundUser = db.Users.Where(i => i.EmailAddress == emailAddress).SingleOrDefault();

                if (foundUser != null)
                {
                    return true;
                }
                return false;
            }
        }
        public static void UpdateEvaluationResult(int EvaluationResultID, string resultText)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                EvaluationResult EvaluationResult = db.EvaluationResults.Where(i => i.EvaluationResultID == EvaluationResultID).SingleOrDefault();

                if (EvaluationResult != null)
                {
                    EvaluationResult.Description = resultText;
                    EvaluationResult.DateModified = DateTime.Now;
                    db.SubmitChanges();
                }
            }
        }
        public static List<Domain.Question> GetDimensionQuestions(int dimensionID, bool getParent)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                var q = (from questions in db.Questions
                        join levelQuestions in db.LevelQuestions on questions.QuestionID equals levelQuestions.QuestionID
                        join level in db.Levels on levelQuestions.LevelID equals level.LevelID
                        join dimensions in db.Dimensions on level.DimensionID equals dimensions.DimensionID
                        where dimensions.DimensionID == dimensionID
                        select questions).Distinct();

                return q.Select(i => i.GetDomainObject(getParent)).ToList();
            }
        }
        // We do not allow new questions to be created.  Only existing questions may be updated
        public static void UpdateQuestionText(int questionID, string questionText)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                Question question = db.Questions.Where(i => i.QuestionID == questionID).SingleOrDefault();

                if (question != null)
                {
                    question.Description = questionText;
                    question.DateModified = DateTime.Now;
                    db.SubmitChanges();
                }
            }
        }
        public static void AddUserLoginHistory(Domain.UserLoginHistory dto)
        {
            using (EvaluationDBDataContext db = new EvaluationDBDataContext())
            {
                UserLoginHistory history = new UserLoginHistory
                {
                    UserID = dto.UserID,
                    LoginDate = dto.LoginDate
                };

                db.UserLoginHistories.InsertOnSubmit(history);
                db.SubmitChanges();
            }
        }