Ejemplo n.º 1
0
        private static QuizItemResultsModel PopulateResultsForLawAnswer(string userId, AnonymousUserService auService, ApplicationDbContext context, QuizItem questionItem)
        {
            // Populate general law data
            var item = new QuizItemResultsModel
            {
                LawId               = questionItem.Law.LawID,
                LawTitle            = questionItem.Law.Title,
                QuestionDescription = questionItem.Law.Text,
                ImageRelativePath   = questionItem.Law.ImageRelativePath,
                QuestionType        = QuizItemType.Law
            };

            // This is copied from Law service. Look to make it generic with expression trees
            var lawVotingResults = context.LawVotes
                                   .Where(x => x.LawID == questionItem.Law.LawID)
                                   .GroupBy(x => x.Vote)
                                   .Select(g => new { Key = g.Key, Count = g.Count() })
                                   .ToList();

            var unsafeResult = lawVotingResults.Where(x => x.Key.HasValue && x.Key.Value).FirstOrDefault();

            item.VotesUp = unsafeResult == null ? 0 : unsafeResult.Count;

            unsafeResult   = lawVotingResults.Where(x => x.Key.HasValue && !x.Key.Value).FirstOrDefault();
            item.VotesDown = unsafeResult == null ? 0 : unsafeResult.Count;

            item.VotesDownPercentage = Infrastructure.Math.Percentage(item.VotesDown, item.VotesDown + item.VotesUp);
            item.VotesUpPercentage   = Infrastructure.Math.Percentage(item.VotesUp, item.VotesDown + item.VotesUp);

            if (userId != null)
            {
                var userVote = context.LawVotes
                               .Include(x => x.LawCustomVote)
                               .Where(x => x.ApplicationUserID == userId && x.LawID == questionItem.Law.LawID)
                               .FirstOrDefault();

                if (userVote != null)
                {
                    item.UserVote = userVote.Vote;
                    if (userVote.LawCustomVote != null)
                    {
                        item.CustomUserVoteText = userVote.LawCustomVote.Text;
                    }
                }
            }
            else
            {
                item.UserVote           = auService.GetUserLawVoteBool(questionItem.Law.LawID);
                item.CustomUserVoteText = auService.GetUserLawVoteCustomText(questionItem.Law.LawID);
            }

            return(item);
        }
Ejemplo n.º 2
0
        public QuizAnsweringModel GetQuizAnsweringModel(int id, int?itemOrderId, string userId)
        {
            using (var context = ApplicationDbContext.Create())
            {
                var result = new QuizAnsweringModel
                {
                    QuizId = id
                };

                var quiz = context
                           .Quizes
                           .Include(x => x.Items)
                           .Include("Items.Law")
                           .Include("Items.LawSection.Law")
                           .Where(x => x.QuizId == id)
                           .FirstOrDefault();

                if (quiz == null)
                {
                    return(null);
                }

                var item = quiz.Items.SingleOrDefault(x => x.Order == itemOrderId);
                if (itemOrderId.HasValue && item == null)
                {
                    return(null);
                }

                // populate quiz general data
                result.Title        = quiz.Title;
                result.Description  = quiz.Description;
                result.QuestionType = itemOrderId.HasValue ? item.Type : (QuizItemType?)null;



                // populate next item
                if (quiz.Items.Count > 0)                   // if no items in list quiz is already over.
                {
                    var orderedItems = quiz.Items.OrderBy(x => x.Order).ToList();
                    if (!itemOrderId.HasValue)
                    {
                        result.NextQuestionId = orderedItems.First().Order;
                    }
                    else
                    {
                        int currentItemIndex = orderedItems.FindIndex(x => x.Order == itemOrderId);
                        if (orderedItems.Count > currentItemIndex + 1)
                        {
                            result.NextQuestionId = orderedItems[currentItemIndex + 1].Order;
                        }
                        else
                        {
                            result.NextQuestionId = -1;
                        }
                    }

                    result.ProgressPercentage = JavnaRasprava.WEB.Infrastructure.Math.Percentage(itemOrderId.HasValue ? itemOrderId.Value : 0,
                                                                                                 quiz.Items.Count).ToString();
                }

                result.CurrentItemIndex = itemOrderId;
                result.TotalItems       = quiz.Items.Count;


                if (item == null)
                {
                    result.ImageRelativePath = quiz.ImageRelativePath;
                }
                else
                {
                    AnonymousUserService auService = new AnonymousUserService();


                    // Populate law
                    if (item.Type == QuizItemType.Law)
                    {
                        result.LawId               = item.Law.LawID;
                        result.LawTitle            = item.Law.Title;
                        result.QuestionDescription = item.Law.Text;
                        result.LawVotes            = LawService.GetLawCustomVotesListInternal(item.Law.LawID, context);
                        result.ImageRelativePath   = item.Law.ImageRelativePath;

                        if (userId == null)
                        {
                            if (auService.HasVotedLaw(item.Law.LawID))
                            {
                                result.UserVoteId         = auService.GetUserLawVote(item.Law.LawID);
                                result.CustomUserVoteText = auService.GetUserLawVoteCustomText(item.Law.LawID);
                            }
                        }
                        else
                        {
                            var userLawVote = context.LawVotes
                                              .Include(x => x.LawCustomVote)
                                              .Where(x => x.ApplicationUserID == userId)
                                              .Where(x => x.LawID == item.Law.LawID)
                                              .FirstOrDefault();

                            if (userLawVote != null)
                            {
                                if (userLawVote.LawCustomVote != null)
                                {
                                    result.UserVoteId         = -1;
                                    result.CustomUserVoteText = userLawVote.LawCustomVote.Text;
                                }
                                else
                                {
                                    result.UserVoteId = userLawVote.Vote.Value ? -3 : -2;
                                }
                            }
                        }
                    }
                    else
                    {
                        result.LawId               = item.LawSection.LawID;
                        result.LawTitle            = item.LawSection.Law.Title;
                        result.SectionId           = item.LawSection.LawSectionID;
                        result.SectionTitle        = item.LawSection.Title;
                        result.QuestionDescription = item.LawSection.Text;
                        result.SectionVotes        = LawService.GetLawSectionCustomVotesListInternal(item.LawSection.LawID, item.LawSection.LawSectionID, context);
                        result.ImageRelativePath   = item.LawSection.ImageRelativePath;

                        if (userId == null)
                        {
                            if (auService.HasVotedLawSection(item.LawSection.LawSectionID))
                            {
                                result.UserVoteId         = auService.GetUserLawSectionVote(item.LawSection.LawSectionID);
                                result.CustomUserVoteText = auService.GetUserLawSectionVoteCustomText(item.LawSection.LawSectionID);
                            }
                        }
                        else
                        {
                            var userLawSectionVote = context.LawSectionVotes
                                                     .Include(x => x.LawSectionCustomVote)
                                                     .Where(x => x.LawSectionID == item.LawSection.LawSectionID)
                                                     .Where(x => x.ApplicationUserID == userId)
                                                     .FirstOrDefault();

                            if (userLawSectionVote != null)
                            {
                                if (userLawSectionVote.LawSectionCustomVote != null)
                                {
                                    result.UserVoteId         = -1;
                                    result.CustomUserVoteText = userLawSectionVote.LawSectionCustomVote.Text;
                                }
                                else
                                {
                                    result.UserVoteId = userLawSectionVote.Vote.Value ? -3 : -2;
                                }
                            }
                        }
                    }
                }

                return(result);
            }
        }