Ejemplo n.º 1
0
        public void FinalizeReport(Models.Assessment.Assessment assessment)
        {
            long responseId = assessment.ResponseId;
            if (responseId <= 0) return;

            var now = DateTime.Now;

            using (var ctx = new AssessmentEntities())
            {
                var responseObj = ctx.Response.FirstOrDefault(x => x.Id == responseId);
                if (responseObj != null)
                {
                    responseObj.ReportSent = true;
                    responseObj.DateReportSent = now;
                }

                ctx.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        public long CreateResponse(string responseKey)
        {
            long responseId = 0;
            var now = DateTime.Now;

            using (var ctx = new AssessmentEntities())
            {
                var response = new Response();
                response.DateCreated = now;
                response.DateUpdated = now;

                // save Respose Key and Campaign Id associated with this response
                Guid guid;
                if (!string.IsNullOrEmpty(responseKey) && Guid.TryParse(responseKey, out guid))
                {
                    response.ResponseKey = guid;

                    // check if this Response Key is associated with Campaign Id
                    var campaignObj = ctx.Campaign.FirstOrDefault(x => x.Guid.Equals(guid));
                    if (campaignObj != null) response.CampaignId = campaignObj.Id;

                    // check if this Response Key is associated with User Id
                    var userObj = ctx.User.FirstOrDefault(x => x.Guid.Equals(guid));
                    if (userObj != null)
                    {
                        response.UserId = userObj.Id;
                        if (userObj.PartnerId.HasValue) response.PartnerId = userObj.PartnerId;
                    }
                }

                var assessmentObj = ctx.Assessment.FirstOrDefault(x => x.Name == QuestionCache.AssessmentName);
                if (assessmentObj != null) response.AssessmentId = assessmentObj.Id;

                ctx.Response.Add(response);
                ctx.SaveChanges();
                responseId = response.Id;
            }

            return responseId;
        }
Ejemplo n.º 3
0
        public void FinalizeResponse(Models.Assessment.Assessment assessment)
        {
            long responseId = assessment.ResponseId;
            if (responseId <= 0) return;

            var now = DateTime.Now;
            string email = null;

            using (var ctx = new AssessmentEntities())
            {
                var emailAnswerObj = ctx.Answer.FirstOrDefault(x =>
                    x.ResponseId == responseId &&
                    x.AnswerChoiceId == QuestionCache.EmailChoiceId);

                if (emailAnswerObj != null) email = emailAnswerObj.Value;

                var responseObj = ctx.Response.FirstOrDefault(x => x.Id == responseId);
                if (responseObj != null)
                {
                    responseObj.Email = email;
                    responseObj.Completed = true;
                    responseObj.DateCompleted = now;
                }

                ctx.SaveChanges();
            }
        }
Ejemplo n.º 4
0
        public void SetErrorFlag(long responseId)
        {
            using (var ctx = new AssessmentEntities())
            {
                var responseObj = ctx.Response.FirstOrDefault(x => x.Id == responseId);
                if (responseObj != null)
                {
                    responseObj.Error = true;
                    responseObj.DateUpdated = DateTime.Now;
                }

                ctx.SaveChanges();
            }
        }
Ejemplo n.º 5
0
        public void SaveScore(Models.Scoring report)
        {
            long responseId = report.ResponseId;
            if (responseId <= 0) return;

            var now = DateTime.Now;

            using (var ctx = new AssessmentEntities())
            {
                var scoreObj = ctx.Score.FirstOrDefault(x => x.ResponseId == responseId);

                if (scoreObj != null)
                {
                    scoreObj.Overall = report.ScoreOverall;
                    scoreObj.DateCreated = now;
                }
                else
                {
                    scoreObj = new Score()
                    {
                        ResponseId = responseId,
                        Overall = report.ScoreOverall,
                        DateCreated = now
                    };

                    ctx.Score.Add(scoreObj);
                }

                ctx.SaveChanges();
            }
        }
Ejemplo n.º 6
0
        public void SaveAnswers(Models.Assessment.Assessment assessment)
        {
            long responseId = assessment.ResponseId;
            if (responseId <= 0) return;

            var now = DateTime.Now;

            int completedPageNo = assessment.CurrentPageNo;
            int completedQuestionNo = 0;

            // save answers (value and answer choice Id) by question Id, question item Id and response Id
            using (var ctx = new AssessmentEntities())
            {
                foreach(var question in assessment.Questions)
                {
                    long aqId = question.AssessmentQuestionId;

                    foreach(var answer in question.Answers)
                    {
                        long qiId = answer.QuestionItemId;

                        if (question.PageNo.HasValue && question.PageNo > completedPageNo) completedPageNo = (int)question.PageNo;
                        if (question.OrderNo > completedQuestionNo) completedQuestionNo = question.OrderNo;

                        // answer choice/value
                        long aacId = answer.AnswerChoiceId;
                        string value = answer.Encrypted ? _aes.Encrypt(answer.Value) : answer.Value;

                        var answerObj = ctx.Answer.FirstOrDefault(x =>
                            x.AssessmentQuestionId == aqId &&
                            x.QuestionItemId == qiId &&
                            x.ResponseId == responseId);

                        if (answerObj != null)
                        {
                            answerObj.AnswerChoiceId = aacId;
                            answerObj.Value = value;
                        }
                        else
                        {
                            answerObj = new Repository.Answer()
                            {
                                AssessmentQuestionId = aqId,
                                QuestionItemId = qiId,
                                AnswerChoiceId = aacId,
                                Value = value,
                                ResponseId = responseId,
                                DateCreated = now
                            };

                            ctx.Answer.Add(answerObj);
                        }

                        // update assessment progress
                        var responseObj = ctx.Response.FirstOrDefault(x => x.Id == responseId);
                        if (responseObj != null)
                        {
                            if (responseObj.CompletedPageNo.HasValue && responseObj.CompletedPageNo > completedPageNo) completedPageNo = (int)responseObj.CompletedPageNo;
                            if (responseObj.CompletedQuestionNo.HasValue && responseObj.CompletedQuestionNo > completedQuestionNo) completedQuestionNo = (int)responseObj.CompletedQuestionNo;

                            if (completedPageNo > 0) responseObj.CompletedPageNo = completedPageNo;
                            if (completedQuestionNo > 0) responseObj.CompletedQuestionNo = completedQuestionNo;

                            responseObj.DateUpdated = now;
                        }

                        ctx.SaveChanges();
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public static bool SetDefaultValue(string resourceName, string value)
        {
            if (string.IsNullOrEmpty(resourceName)) return false;

            using (var ctx = new AssessmentEntities())
            {
                var query = ctx.Resource.FirstOrDefault(x => x.Name == resourceName);
                if (query != null)
                {
                    query.DefaultValue = value;
                }
                else
                {
                    var resource = new Repository.Resource();
                    resource.Name = resourceName;
                    resource.DefaultValue = value;
                    ctx.Resource.Add(resource);
                }

                ctx.SaveChanges();
            }

            return true;
        }
Ejemplo n.º 8
0
        public bool SuspendUser(Models.Account.User user, bool toggle = false)
        {
            bool outcome = false;

            // toggle between active and suspended
            int status = toggle ? (int)Models.Account.UserStatus.Suspended : (int)Models.Account.UserStatus.Active;
            int newStatus = toggle ? (int)Models.Account.UserStatus.Active : (int)Models.Account.UserStatus.Suspended;

            using (var ctx = new AssessmentEntities())
            {
                try
                {
                    var userObj = ctx.User.FirstOrDefault(x => x.Id == user.Id && x.StatusId == status);

                    if (userObj != null)
                    {
                        userObj.StatusId = newStatus;
                        ctx.SaveChanges();
                        outcome = true;
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in  SuspendUser [user id = " + user.Id + "]: user not found");
                    }
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevel.Error, "Error in SuspendUser" + (user != null ? " [user id = " + user.Id + "]: " : ": ") + e.Message);
                    throw new Exception();
                }
            }

            return outcome;
        }
Ejemplo n.º 9
0
        public bool UpdateUser(Models.Account.User user, bool updateRole = false)
        {
            bool outcome = false;

            if (string.IsNullOrEmpty(user.FirstName) || string.IsNullOrEmpty(user.LastName)) return outcome;

            try
            {
                using (var ctx = new AssessmentEntities())
                {
                    var userObj = ctx.User.Where(x => x.Id == user.Id).SingleOrDefault();

                    if (userObj != null)
                    {
                        userObj.FirstName = user.FirstName;
                        userObj.LastName = user.LastName;
                        userObj.DateUpdated = DateTime.Now;

                        if (updateRole)
                        {
                            var userRoleObj = userObj.UserRole;
                            var roleAdminObj = ctx.Role.FirstOrDefault(x => x.Name.Contains("Admin"));
                            var roleMasterObj = ctx.Role.FirstOrDefault(x => x.Name.Contains("Master"));

                            if (roleAdminObj != null)
                            {
                                if (user.IsAdmin)
                                {
                                    if (!userRoleObj.Any(x => x.RoleId == roleAdminObj.Id))
                                    {
                                        var newUserRoleObj =
                                            new UserRole()
                                            {
                                                UserId = userObj.Id,
                                                RoleId = roleAdminObj.Id
                                            };

                                        ctx.UserRole.Add(newUserRoleObj);
                                    }
                                    else
                                    {
                                        foreach (var role in userRoleObj.Where(x => x.RoleId == roleAdminObj.Id)) role.Disabled = false;
                                    }
                                }
                                else
                                {
                                    foreach (var role in userRoleObj.Where(x => x.RoleId == roleAdminObj.Id)) role.Disabled = true;
                                }
                            }
                            else
                            {
                                foreach (var item in userRoleObj) item.Disabled = true;
                            }

                            if (roleMasterObj != null)
                            {
                                if (user.IsMaster)
                                {
                                    if (!userRoleObj.Any(x => x.RoleId == roleMasterObj.Id))
                                    {
                                        var newUserRoleObj =
                                            new UserRole()
                                            {
                                                UserId = userObj.Id,
                                                RoleId = roleMasterObj.Id
                                            };

                                        ctx.UserRole.Add(newUserRoleObj);
                                    }
                                    else
                                    {
                                        foreach (var role in userRoleObj.Where(x => x.RoleId == roleMasterObj.Id)) role.Disabled = false;
                                    }
                                }
                                else
                                {
                                    foreach (var role in userRoleObj.Where(x => x.RoleId == roleMasterObj.Id)) role.Disabled = true;
                                }
                            }
                            else
                            {
                                foreach (var item in userRoleObj) item.Disabled = true;
                            }
                        } // end updateRole

                        ctx.SaveChanges();
                        outcome = true;
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in  UpdateUser [user id = " + user.Id + "]: user not found");
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Log(LogLevel.Error, "Error in UpdateUser" + (user != null ? " [user id = " + user.Id + "]: " : ": ") + e.Message);
                throw new Exception();
            }

            return outcome;
        }
Ejemplo n.º 10
0
        public bool AddUser(Models.Account.User user)
        {
            bool outcome = false;
            var now = DateTime.Now;

            if (user != null && string.IsNullOrEmpty(user.Email))
            {
                Logger.Log(LogLevel.Error, "Error in AddUser: email not provided");
                return outcome;
            }

            try
            {
                using (var ctx = new AssessmentEntities())
                {
                    if (ctx.User.Any(x => x.Email == user.Email || x.UserName == user.Email))
                    {
                        Logger.Log(LogLevel.Error, "Error in AddUser [email = " + user.Email + "]: user already exists");
                        return outcome;   // duplicate user
                    }

                    string password_hash = ctx.usp_GetHash(user.Password).SingleOrDefault<string>();

                    // add user
                    var userObj = new User()
                    {
                        Guid = user.Guid,
                        UserName = user.Email,
                        Password = password_hash,
                        Email = user.Email,
                        FirstName = user.FirstName,
                        LastName = user.LastName,
                        StatusId = (int)Models.Account.UserStatus.Unconfirmed,
                        StartDate = now,
                        DateCreated = now,
                        DateUpdated = now
                    };

                    ctx.User.Add(userObj);
                    ctx.SaveChanges();

                    // add Guest role
                    var roleGuestObj = ctx.Role.FirstOrDefault(x => x.Name.Contains("Guest"));

                    if (roleGuestObj != null)
                    {
                        var userRoleObj = new UserRole()
                        {
                            UserId = userObj.Id,
                            RoleId = roleGuestObj.Id
                        };

                        ctx.UserRole.Add(userRoleObj);
                    }

                    // add Admin role
                    if (user.IsAdmin)
                    {
                        var roleAdminObj = ctx.Role.FirstOrDefault(x => x.Name.Contains("Admin"));

                        if (roleAdminObj != null)
                        {
                            var userRoleObj = new UserRole()
                            {
                                UserId = userObj.Id,
                                RoleId = roleAdminObj.Id
                            };

                            ctx.UserRole.Add(userRoleObj);
                        }
                    }

                    ctx.SaveChanges();
                    outcome = true;
                }
            }
            catch (Exception e)
            {
                Logger.Log(LogLevel.Error, "Error in AddUser" + (user != null ? " [email = " + user.Email + "]: " : ": ") + e.Message);
                throw new Exception();
            }

            return outcome;
        }
Ejemplo n.º 11
0
        public bool ResetPassword(Models.Account.User user)
        {
            bool outcome = false;

            if (string.IsNullOrEmpty(user.NewPassword)) return outcome;

            try
            {
                using (var ctx = new AssessmentEntities())
                {
                    string password_hash = ctx.usp_GetHash(user.NewPassword).SingleOrDefault<string>();

                    var userObj = ctx.User.Where(x => x.Email == user.Email).SingleOrDefault();
                    if (userObj != null)
                    {
                        // values used in 'reset password email'
                        user.UserName = userObj.UserName;
                        user.Guid = userObj.Guid;
                        user.FirstName = userObj.FirstName;
                        user.LastName = userObj.LastName;

                        // reset password
                        userObj.Password = password_hash;
                        userObj.StatusId = (int)Models.Account.UserStatus.Unconfirmed;
                        userObj.DateUpdated = DateTime.Now;
                        ctx.SaveChanges();
                        outcome = true;
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in ResetPassword [email = " + user.Email + "]: user not found");
                    }
                }
            }
            catch (Exception e)
            {
                outcome = false;
                Logger.Log(LogLevel.Error, "Error in ResetPassword: " + e.Message);
                throw new Exception();
            }

            return outcome;
        }
Ejemplo n.º 12
0
        public bool DeleteUser(Models.Account.User user, bool toggle = false)
        {
            bool outcome = false;

            using (var ctx = new AssessmentEntities())
            {
                try
                {
                    var userObj = ctx.User.FirstOrDefault(x => x.Id == user.Id && x.Deleted == toggle);

                    if (userObj != null)
                    {
                        userObj.Deleted = !toggle;
                        ctx.SaveChanges();
                        outcome = true;
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in  DeleteUser [user id = " + user.Id + "]: user not found");
                    }
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevel.Error, "Error in DeleteUser" + (user != null ? " [user id = " + user.Id + "]: " : ": ") + e.Message);
                    throw new Exception();
                }
            }

            return outcome;
        }
Ejemplo n.º 13
0
        public bool ChangePassword(Models.Account.User user)
        {
            bool outcome = false;

            if (string.IsNullOrEmpty(user.Password) || string.IsNullOrEmpty(user.NewPassword)
                || string.IsNullOrEmpty(user.NewPasswordConfirm)) return outcome;

            if (user.NewPassword != user.NewPasswordConfirm) return outcome;
            if (user.NewPassword == user.Password) return outcome;

            try
            {
                using (var ctx = new AssessmentEntities())
                {
                    string password_hash = ctx.usp_GetHash(user.NewPassword).SingleOrDefault<string>();

                    var userObj = ctx.User.Where(x => x.UserName == user.UserName).SingleOrDefault();
                    if (userObj != null)
                    {
                        userObj.Password = password_hash;
                        userObj.StatusId = (int)Models.Account.UserStatus.Active;
                        userObj.DateUpdated = DateTime.Now;
                        ctx.SaveChanges();
                        outcome = true;
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in ChangePassword [user name = " + user.UserName + "]: user not found");
                    }
                }
            }
            catch (Exception e)
            {
                outcome = false;
                Logger.Log(LogLevel.Error, "Error in ChangePassword: " + e.Message);
                throw new Exception();
            }

            return outcome;
        }
Ejemplo n.º 14
0
        public bool Authenticate(string userName, string password, out Models.Account.UserStatus status)
        {
            status = Models.Account.UserStatus.Undefined;
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) return false;

            bool result = false;
            string userName_encrypted = _aes.Encrypt(userName.Trim().ToLowerInvariant());
            int status_suspended = (int)Models.Account.UserStatus.Suspended;

            using (var ctx = new AssessmentEntities())
            {
                try
                {
                    string password_hash = ctx.usp_GetHash(password).SingleOrDefault<string>();

                    var userObj = ctx.User.Where(x =>
                           x.UserName == userName_encrypted
                           && x.Password == password_hash
                           && x.StatusId != status_suspended
                           && (x.EndDate.HasValue ? (DateTime)x.EndDate : DateTime.MaxValue) > DateTime.Now
                           && !x.Deleted)
                           .SingleOrDefault();

                    if (userObj != null)
                    {
                        userObj.LastLoginDate = DateTime.Now;
                        status = (Models.Account.UserStatus)userObj.UserStatus.Id;
                        ctx.SaveChanges();
                        result = true;
                    }
                    else
                    {
                        Logger.Log(LogLevel.Info, "Login not allowed [user name = " + userName + "]");
                    }
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevel.Error, "Error in Authenticate: " + e.Message);
                }
            }

            return result;
        }
Ejemplo n.º 15
0
        public bool ToggleCampaign(Models.Admin.Campaign campaign)
        {
            bool success = false;
            long id = campaign.Id;

            using (var ctx = new AssessmentEntities())
            {
                try
                {
                    var campaignObj = ctx.Campaign.FirstOrDefault(x => x.Id == id);

                    if (campaignObj != null)
                    {
                        campaignObj.Disabled = campaign.Disabled;
                        ctx.SaveChanges();
                        success = true;
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in ToggleCampaign: LeadSource not found [id = " + id + "]");
                    }
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevel.Error, "Error in ToggleCampaign: " + e.Message);
                }
            }

            return success;
        }
Ejemplo n.º 16
0
        public bool SaveCampaign(Models.Admin.Campaign campaign)
        {
            bool success = false;
            long id = campaign.Id;

            if (string.IsNullOrEmpty(campaign.Description))
            {
                Logger.Log(LogLevel.Error, "Error in SaveCampaign: empty Description field");
                return success;
            }

            using (var ctx = new AssessmentEntities())
            {
                try
                {
                    string msg;
                    int name_maxlen = 128;
                    int desc_maxlen = 256;
                    int len = campaign.Description.Length;
                    string name = len > name_maxlen ? campaign.Description.Substring(0, name_maxlen) : campaign.Description;
                    string desc = len > desc_maxlen ? campaign.Description.Substring(0, desc_maxlen) : campaign.Description;

                    var campaignObj = ctx.Campaign.FirstOrDefault(x => x.Id == id);

                    if (campaignObj != null)
                    {
                        campaignObj.Name = name;
                        campaignObj.Description = desc;
                        msg = "LeadSource updated: [id = " + id + "], [" + desc + "]";
                    }
                    else
                    {
                        campaignObj = new Repository.Campaign();
                        campaignObj.Name = name;
                        campaignObj.Description = desc;
                        if (campaign.Guid != Guid.Empty) campaignObj.Guid = campaign.Guid;
                        ctx.Campaign.Add(campaignObj);
                        msg = "LeadSource created: [" + desc + "]";
                    }

                    ctx.SaveChanges();
                    success = true;
                    Logger.Log(LogLevel.Info, msg);
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevel.Error, "Error in SaveCampaign: " + e.Message);
                }
            }

            return success;
        }