Esempio n. 1
0
 public string[] GetAllRoles()
 {
     using (var ctx = new AssessmentEntities())
     {
         return ctx.Role.Select(x => x.Name).ToArray();
     }
 }
 public int GetDisabledCampaignsCount()
 {
     using (var ctx = new AssessmentEntities())
     {
         return ctx.Campaign.Where(x => x.Disabled).Count();
     }
 }
        public static List<Models.Resource> GetResources(string tag, string cultureName)
        {
            using (var ctx = new AssessmentEntities())
            {
                var query =
                    from resource in ctx.Resource
                    join rt in ctx.ResourceType on resource.ResourceTypeId equals rt.Id into rt1
                    from resourceType in rt1.DefaultIfEmpty()
                    join rv in ctx.ResourceValue on resource.Id equals rv.ResourceId into rv1
                    from resourceValue in rv1.Where(rv => rv.CultureName == cultureName).DefaultIfEmpty()
                    where resource.Tag == tag && !resource.Deleted
                    select new Models.Resource()
                    {
                        Id = resource.Id,
                        Name = resource.Name,
                        Value = resourceValue != null ? resourceValue.Value : resource.DefaultValue,
                        Type = resource.ResourceType != null ? (Models.ResourceType)Enum.Parse(typeof(Models.ResourceType), resource.ResourceType.Name) : Models.ResourceType.Undefined,
                        TypeName = resource.ResourceType != null ? resource.ResourceType.Name : string.Empty,
                        Tag = resource.Tag,
                        CultureName = resourceValue.CultureName
                    };

                return query.ToList();
            }
        }
Esempio n. 4
0
 public bool RoleExists(string roleName)
 {
     using (var ctx = new AssessmentEntities())
     {
         return ctx.Role.Where(x => x.Name == roleName).Any();
     }
 }
Esempio n. 5
0
        public bool IsUserInRole(string username, string roleName)
        {
            string username_encrypted = _aes.Encrypt(username);

            using (var ctx = new AssessmentEntities())
            {
                return ctx.UserRole.Where(x => x.User.UserName == username_encrypted && x.Role.Name == roleName && !x.Disabled).Any();
            }
        }
Esempio n. 6
0
        public string[] GetRolesForUser(string username)
        {
            string username_encrypted = _aes.Encrypt(username != null ? username.Trim().ToLower() : username);

            using (var ctx = new AssessmentEntities())
            {
                var list = ctx.UserRole.Where(x => x.User.UserName == username_encrypted && !x.Disabled).Select(x => x.Role.Name);
                if (list != null) return list.ToArray();
                else return new string[0];
            }
        }
Esempio n. 7
0
        public string[] GetUsersInRole(string roleName)
        {
            var list = new List<string>();

            using (var ctx = new AssessmentEntities())
            {
                var list_encrypted = ctx.UserRole.Where(x => x.Role.Name == roleName && !x.Disabled).Select(x => x.User);
                if (list_encrypted != null) foreach (var item in list_encrypted) list.Add(_aes.Decrypt(item.UserName));
            }

            return list.ToArray();
        }
Esempio n. 8
0
        public string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var list = new List<string>();
            string username_encrypted = _aes.Encrypt(usernameToMatch);
            var searchTerm = new Regex(username_encrypted);

            using (var ctx = new AssessmentEntities())
            {
                var list_encrypted = ctx.UserRole.Where(x => searchTerm.IsMatch(x.User.UserName) && x.Role.Name == roleName && !x.Disabled).Select(x => x.User);
                if (list_encrypted != null) foreach (var item in list_encrypted) list.Add(_aes.Decrypt(item.UserName));
            }

            return list.ToArray();
        }
        public static string GetDefaultValue(string resourceName)
        {
            string value = null;

            if (string.IsNullOrEmpty(resourceName)) return null;

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

            return value;
        }
Esempio n. 10
0
        public Models.Admin.Campaign GetCampaign(long id, string baseUrl)
        {
            var campaign = new Models.Admin.Campaign();
            string defaultLink = null;

            if (baseUrl != null)
            {
                if (baseUrl.EndsWith("/"))
                {
                    defaultLink = baseUrl.Remove(baseUrl.LastIndexOf("/"));
                }
                else
                {
                    defaultLink = baseUrl;
                    baseUrl += "/";
                }
            }

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

                    if (campaignObj != null)
                    {
                        campaign = new Models.Admin.Campaign()
                        {
                            Id = campaignObj.Id,
                            Description = campaignObj.Description,
                            Guid = campaignObj.Guid,
                            Disabled = campaignObj.Disabled,
                            Status = campaignObj.Disabled ? CampaignStatus.Deleted : CampaignStatus.Active,
                            Link = campaignObj.Guid == Guid.Empty ? defaultLink : baseUrl + campaignObj.Guid.ToString()
                        };
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in GetCampaign: LeadSource not found [id = " + id + "]");
                    }
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevel.Error, "Error in GetCampaign: " + e.Message);
                }
            }

            return campaign;
        }
Esempio n. 11
0
        public List<Models.Admin.Campaign> GetCampaignList(string baseUrl, bool includeDeleted)
        {
            List<Models.Admin.Campaign> list = null;
            string defaultLink = null;

            if (baseUrl != null)
            {
                defaultLink = baseUrl.TrimEnd('/');
                if (!baseUrl.EndsWith("/")) baseUrl += "/";
            }

            using (var ctx = new AssessmentEntities())
            {
                try
                {
                    var query = ctx.Campaign.AsQueryable();
                    if (!includeDeleted) query = query.Where(x => !x.Disabled);

                    var listObj =
                        query.Select(campaign => new Models.Admin.Campaign
                        {
                            Id = campaign.Id,
                            Description = campaign.Description,
                            Guid = campaign.Guid,
                            Disabled = campaign.Disabled,
                            Status = campaign.Disabled ? CampaignStatus.Deleted : CampaignStatus.Active,
                            Link = campaign.Guid == Guid.Empty ? defaultLink : baseUrl + campaign.Guid
                        });

                    if (listObj != null)
                    {
                        list = listObj.ToList();
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in GetCampaignList: query result is null");
                    }
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevel.Error, "Error in GetCampaignList: " + e.Message);
                }
            }

            if (list == null) list = new List<Models.Admin.Campaign>();

            return list;
        }
        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();
            }
        }
        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;
        }
Esempio n. 14
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;
        }
Esempio n. 15
0
        public static string GetResourceValue(string resourceName, string cultureName)
        {
            using (var ctx = new AssessmentEntities())
            {
                var query =
                    from resource in ctx.Resource
                    join rv in ctx.ResourceValue on resource.Id equals rv.ResourceId into rv1
                    from resourceValue in rv1.Where(rv => rv.CultureName == cultureName).DefaultIfEmpty()
                    where resource.Name == resourceName
                    select new
                    {
                        Value = resourceValue.Value ?? resource.DefaultValue
                    };

                var queryResult = query.FirstOrDefault();

                return queryResult != null ? queryResult.Value : string.Empty;
            }
        }
Esempio n. 16
0
        public List<Models.Assessment.Assessment> GetReport(string assessmentName, string reportViewName, string cultureName, DateTime startDate, DateTime endDate)
        {
            using (var ctx = new AssessmentEntities())
            {
                var query =
                    // Report View
                    from aView in ctx.Assessment
                    join aqView in ctx.AssessmentQuestion on aView.Id equals aqView.AssessmentId

                    // Question text
                    join res1 in ctx.Resource on aqView.TextId equals res1.Id into aqr1
                    from aqrText in aqr1.DefaultIfEmpty()
                    join resv1 in ctx.ResourceValue on aqrText.Id equals resv1.ResourceId into aqrv1
                    from aqrvText in aqrv1.Where(x => x.CultureName == cultureName).DefaultIfEmpty()

                    // Question
                    join aq in ctx.AssessmentQuestion on aqView.OrderNo equals aq.OrderNo
                    join a in ctx.Assessment on aq.AssessmentId equals a.Id

                    // Answer choice
                    join aac in ctx.AssessmentAnswerChoice on aq.Id equals aac.AssessmentQuestionId
                    join res5 in ctx.Resource on aac.TextId equals res5.Id into aqr5
                    from aacr in aqr5.DefaultIfEmpty()
                    join resv5 in ctx.ResourceValue on aacr.Id equals resv5.ResourceId into aqrv5
                    from aacrv in aqrv5.Where(x => x.CultureName == cultureName).DefaultIfEmpty()

                    // Answer
                    join answer in ctx.Answer
                    on new { a1 = aac.AssessmentQuestionId, a2 = aac.QuestionItemId, a3 = aac.AnswerChoiceId }
                    equals new { a1 = answer.AssessmentQuestionId, a2 = answer.QuestionItemId, a3 = answer.AnswerChoiceId }

                    // Response
                    join response in ctx.Response on answer.ResponseId equals response.Id

                    // Overall score
                    join scr in ctx.Score on response.Id equals scr.ResponseId into scr1
                    from score in scr1.DefaultIfEmpty()

                    // Campaign
                    join camp in ctx.Campaign on response.CampaignId equals camp.Id into camp1
                    from campaign in camp1.DefaultIfEmpty()

                    where a.Name == assessmentName && aView.Name == reportViewName
                    && response.DateCreated >= startDate && response.DateCreated < endDate && response.Completed
                    && !aqView.Deleted && !aq.Deleted && !aac.Deleted

                    select new QuestionAnswer
                    {
                        // response
                        ResponseId = answer.ResponseId,
                        DateCreated = response.DateCreated,
                        ScoreOverall = score.Overall,

                        // question
                        QuestionNo = aq.OrderNo,
                        AssessmentQuestionId = aq.Id,
                        AnswerType = aq.AnswerTypeId,

                        PageNo = aq.PageNo,
                        QuestionText = aqrvText.Value,

                        QuestionGroup = aq.QuestionGroup,
                        QuestionGroupId = aq.QuestionGroupId,
                        Optional = aq.Optional,

                        // answer choice
                        AnswerNo = aac.OrderNo,
                        QuestionItemId = aac.QuestionItemId,
                        AnswerChoiceId = aac.AnswerChoiceId,
                        ChoiceType = aac.AnswerTypeId,
                        AnswerText = aacrv.Value,
                        Value = answer.Value,
                        DefaultValue = aacr.DefaultValue,
                        Encrypted = aac.Encrypted,
                        AlternativeAnswer = aac.AlternativeAnswer,
                        AltQuestionItemId = aac.AltQuestionItemId,
                        Score = aac.Score,

                        // campaign
                        CampaignId = campaign != null ? campaign.Id : 0,
                        CampaignGuid = campaign != null ? campaign.Guid : Guid.Empty,
                        CampaignName = campaign != null ? campaign.Name : null,
                        CampaignDescription = campaign != null ? campaign.Description : null
                    };

                var list = MapAssessmentList(query.ToList());
                list.ForEach(x => x.Name = assessmentName);
                return list;
            }
        }
Esempio n. 17
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;
        }
Esempio n. 18
0
        public List<LeadCapture.Models.Response> GetResponses(DateTime startDate, DateTime endDate)
        {
            using (var ctx = new AssessmentEntities())
            {
                var query =
                    from response in ctx.Response
                    where !response.Deleted && response.DateCreated >= startDate && response.DateCreated < endDate
                    select new LeadCapture.Models.Response()
                    {
                        Id = response.Id,
                        DateCreated = response.DateCreated,
                        Email = response.Email,
                        CampaignId = response.CampaignId.HasValue ? (long)response.CampaignId : 0,
                        CompletedPageNo = response.CompletedPageNo.HasValue ? (int)response.CompletedPageNo : 0,
                        Completed = response.Completed,
                        ReportSent = response.ReportSent
                    };

                return query.ToList();
            }
        }
        public long GetAnswerChoiceId(string name)
        {
            long id = 0;

            using (var ctx = new AssessmentEntities())
            {
                var acObj = ctx.AnswerChoice.FirstOrDefault(x => x.Name == name);
                if (acObj != null) id = acObj.Id;
            }

            return id;
        }
Esempio n. 20
0
        public Models.Account.User GetUser(string key, string userName)
        {
            var user = new Models.Account.User();

            try
            {
                using (var ctx = new AssessmentEntities())
                {
                    var guid = Guid.Parse(key);
                    var userObj = ctx.User.FirstOrDefault(x => x.Guid == guid && x.UserName == userName);

                    if (userObj != null)
                    {
                        var roles = new List<string>();
                        var roleObj = userObj.UserRole;
                        foreach (var item in roleObj.OrderBy(x => x.RoleId))
                        {
                            if (!item.Disabled)
                            {
                                var role = item.Role;
                                if (!role.Disabled) roles.Add(item.Role.Name);
                            }
                        }

                        user.Roles = roles;
                        user.Id = userObj.Id;
                        user.UserName = userObj.UserName;
                        user.FirstName = userObj.FirstName;
                        user.LastName = userObj.LastName;
                        user.CompanyName = userObj.CompanyName;
                        user.Email = userObj.Email;
                        if (userObj.EndDate.HasValue) user.EndDate = (DateTime)userObj.EndDate;
                        user.LastLoginDate = userObj.LastLoginDate;
                        user.Status = (Models.Account.UserStatus)userObj.StatusId;
                        user.Deleted = userObj.Deleted;
                    }
                    else
                    {
                        Logger.Log(LogLevel.Error, "Error in GetUser [key = " + key + ", user name = " + userName + "]: user not found");
                    }
                }
            }
            catch (Exception e)
            {
                user.Status = Models.Account.UserStatus.Undefined;
                Logger.Log(LogLevel.Error, "Error in GetUser [key = " + key + ", user name = " + userName + "]: " + e.Message);
            }

            return user;
        }
        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();
            }
        }
        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();
            }
        }
        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();
                    }
                }
            }
        }
        public Models.Assessment.Assessment GetQuestions(string assessmentName, string cultureName)
        {
            using (var ctx = new AssessmentEntities())
            {
                var query =
                    from a in ctx.Assessment
                    // Question
                    join aq in ctx.AssessmentQuestion on a.Id equals aq.AssessmentId
                    // Question text
                    join res1 in ctx.Resource on aq.TextId equals res1.Id into aqr1
                    from aqrText in aqr1.DefaultIfEmpty()
                    join resv1 in ctx.ResourceValue on aqrText.Id equals resv1.ResourceId into aqrv1
                    from aqrvText in aqrv1.Where(x => x.CultureName == cultureName).DefaultIfEmpty()
                    // Alt text
                    join res2 in ctx.Resource on aq.AltTextId equals res2.Id into aqr2
                    from aqrAlt in aqr2.DefaultIfEmpty()
                    join resv2 in ctx.ResourceValue on aqrAlt.Id equals resv2.ResourceId into aqrv2
                    from aqrvAlt in aqrv2.Where(x => x.CultureName == cultureName).DefaultIfEmpty()
                    // Validation text
                    join res3 in ctx.Resource on aq.ValidationTextId equals res3.Id into aqr3
                    from aqrVld in aqr3.DefaultIfEmpty()
                    join resv3 in ctx.ResourceValue on aqrVld.Id equals resv3.ResourceId into aqrv3
                    from aqrvVld in aqrv3.Where(x => x.CultureName == cultureName).DefaultIfEmpty()

                    // Section
                    join sectionObj in ctx.Section on aq.SectionId equals sectionObj.Id into sec1
                    from sec in sec1.DefaultIfEmpty()
                    join res4 in ctx.Resource on sec.ResourceId equals res4.Id into aqr4
                    from secr in aqr4.DefaultIfEmpty()
                    join resv4 in ctx.ResourceValue on secr.Id equals resv4.ResourceId into aqrv4
                    from secrv in aqrv4.Where(x => x.CultureName == cultureName).DefaultIfEmpty()

                    // Answer choice
                    join aac in ctx.AssessmentAnswerChoice on aq.Id equals aac.AssessmentQuestionId
                    join res5 in ctx.Resource on aac.TextId equals res5.Id into aqr5
                    from aacr in aqr5.DefaultIfEmpty()
                    join resv5 in ctx.ResourceValue on aacr.Id equals resv5.ResourceId into aqrv5
                    from aacrv in aqrv5.Where(x => x.CultureName == cultureName).DefaultIfEmpty()

                    where a.Name == assessmentName && !aq.Deleted && !aac.Deleted

                    select new QuestionAnswer
                    {
                        // question
                        QuestionNo = aq.OrderNo,
                        AssessmentQuestionId = aq.Id,
                        AnswerType = aq.AnswerTypeId,
                        Section = secrv.Value,
                        PageNo = aq.PageNo,
                        QuestionText = aqrvText.Value,
                        AltText = aqrvAlt.Value,
                        ValidationText = aqrvVld.Value,
                        QuestionGroup = aq.QuestionGroup,
                        QuestionGroupId = aq.QuestionGroupId,
                        Optional = aq.Optional,

                        // answer choice
                        AnswerNo = aac.OrderNo,
                        QuestionItemId = aac.QuestionItemId,
                        AnswerChoiceId = aac.AnswerChoiceId,
                        ChoiceType = aac.AnswerTypeId,
                        AnswerText = aacrv.Value,
                        Value = null,
                        DefaultValue = aacr.DefaultValue,
                        Encrypted = aac.Encrypted,
                        AlternativeAnswer = aac.AlternativeAnswer,
                        AltQuestionItemId = aac.AltQuestionItemId,
                        Score = aac.Score
                    };

                // map to view model
                var assessment = MapAssessment(query.ToList());

                foreach (var question in assessment.Questions)
                    foreach (var answer in question.Answers)
                        answer.Selected = false;

                assessment.Name = assessmentName;
                return assessment;
            }
        }
        public string GetAnswerChoiceName(long id)
        {
            string name = null;

            using (var ctx = new AssessmentEntities())
            {
                var acObj = ctx.AnswerChoice.FirstOrDefault(x => x.Id == id);
                if (acObj != null) name = acObj.Name;
            }

            return name;
        }
Esempio n. 26
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;
        }
Esempio n. 27
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;
        }
        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();
            }
        }
 public void GetScore(Models.Scoring report)
 {
     using (var ctx = new AssessmentEntities())
     {
         var scoreObj = ctx.Score.FirstOrDefault(x => x.ResponseId == report.ResponseId);
         if (scoreObj != null)
         {
             report.ScoreOverall = scoreObj.Overall;
         }
     }
 }
Esempio n. 30
0
        public Dictionary<string, string> GetResourceValues(Models.ResourceType type, string tag, string cultureName)
        {
            var list = new Dictionary<string, string>();
            string typeName = type.ToString();

            using (var ctx = new AssessmentEntities())
            {
                var query =
                    from resource in ctx.Resource
                    join rt in ctx.ResourceType on resource.ResourceTypeId equals rt.Id into rt1
                    from resourceType in rt1.DefaultIfEmpty()
                    join rv in ctx.ResourceValue on resource.Id equals rv.ResourceId into rv1
                    from resourceValue in rv1.Where(rv => rv.CultureName == cultureName).DefaultIfEmpty()
                    where resourceType.Name == typeName && resource.Tag == tag && !resource.Deleted
                    select new
                    {
                        Name = resource.Name,
                        Value = resourceValue != null ? resourceValue.Value : resource.DefaultValue,
                    };

                query.ToList().ForEach(x => { if (!list.ContainsKey(x.Name)) list.Add(x.Name, x.Value); });
            }

            return list;
        }