示例#1
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);
            }
        }