Exemplo n.º 1
0
        internal EditQuizUnitVM GetEditQuizUnitVMById(int id)
        {
            var q = QuizUnit
                    .SingleOrDefault(c => c.QuizUnitId == id);

            EditQuizUnitVM model = new EditQuizUnitVM
            {
                QuizUnitHeader     = q.InfoTextHeader,
                QuizUnitContent    = q.InfoTextContent,
                Order              = q.Order,
                SelectedCategoryId = (int)q.CategoryId,
                Categories         = Category.Select(c => new SelectListItem
                {
                    Text  = c.Title,
                    Value = c.CategoryId.ToString(),
                })
                                     .ToArray(),
            };

            return(model);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="QuizUnitId"></param>
        /// <param name="memberId"></param>
        /// <returns>Returns null if there are no more questions in this quis-unit</returns>
        public QuizQuestionVM GetNextQuestion(int QuizUnitId, string memberId)
        {
            // Todo - Se till att den behandlar first, last och middle elements
            var doneQuestionsInQuizUnitForMember = Progress
                                                   .Where(p => p.MemberId == memberId)
                                                   .Where(p => p.Question.QuizUnitId == QuizUnitId);

            Question lastDoneQuestionInQuizUnit;

            if (doneQuestionsInQuizUnitForMember.Count() > 0)
            {
                // Användaren har avklarade frågor sedan innan i det här quizunitet
                lastDoneQuestionInQuizUnit = doneQuestionsInQuizUnitForMember
                                             .Include(p => p.Question)
                                             .OrderBy(p => p.Question.Order)
                                             .Select(p => p.Question)
                                             .Include(q => q.Answer)
                                             .Last();
            }
            else
            {
                // Användaren har inga avklarade frågor i detta quizunit
                lastDoneQuestionInQuizUnit = null;
            }

            QuizQuestionVM quizQuestionVM;

            if (lastDoneQuestionInQuizUnit == null)
            {
                // Ta första frågan i QuizUnitet
                var firstQuestionOfCurrentQuizUnit = Question
                                                     .Where(q => q.QuizUnitId == QuizUnitId)
                                                     .OrderBy(q => q.Order)
                                                     .Include(q => q.Answer)
                                                     .First();

                quizQuestionVM = new QuizQuestionVM
                {
                    QuestionText   = firstQuestionOfCurrentQuizUnit.QuestionText,
                    Answers        = firstQuestionOfCurrentQuizUnit.Answer.ToArray(),
                    QuestionId     = firstQuestionOfCurrentQuizUnit.QuestionId,
                    QuizUnitHeader = QuizUnit.SingleOrDefault(q => q.QuizUnitId == QuizUnitId).InfoTextHeader,
                    CategoryTitle  = QuizUnit.Include(q => q.Category).SingleOrDefault(q => q.QuizUnitId == QuizUnitId).Category.Title,
                };
            }
            else
            {
                // Leta upp de frågorna som har högre order

                var QuestionsOfHigherOrder = Question
                                             .Where(q => q.QuizUnitId == QuizUnitId)
                                             .Where(q => q.Order > lastDoneQuestionInQuizUnit.Order)
                                             .Include(q => q.Answer);

                if (QuestionsOfHigherOrder.Count() == 0)
                {
                    // Det finns inte fler frågor i det här Quiz-unitet
                    return(null);
                }
                else
                {
                    var porque = QuestionsOfHigherOrder
                                 .OrderBy(q => q.Order)
                                 .First();

                    quizQuestionVM = new QuizQuestionVM
                    {
                        QuestionText   = porque.QuestionText,
                        Answers        = porque.Answer.ToArray(),
                        QuestionId     = porque.QuestionId,
                        QuizUnitHeader = QuizUnit.SingleOrDefault(q => q.QuizUnitId == QuizUnitId).InfoTextHeader,
                        CategoryTitle  = QuizUnit.Include(q => q.Category).SingleOrDefault(q => q.QuizUnitId == QuizUnitId).Category.Title,
                    };
                }
            }

            var categoryId = QuizUnit
                             .SingleOrDefault(q => q.QuizUnitId == QuizUnitId)
                             .CategoryId;

            //double numberOfQuestionsInCategory = Question
            //    .Where(q => q.QuizUnit.CategoryId == categoryId)
            //    .Include(q => q.QuizUnit)
            //    .Count();

            //double doneQuestionsInCategory = Progress
            //    //.Where(p => p.Question.QuizUnitId == QuizUnitId)
            //    .Where(p => p.Question.QuizUnit.CategoryId == categoryId)
            //    .Where(p => p.MemberId == memberId)
            //    .Include(p => p.Question)
            //    //.ThenInclude(q => q.QuizUnit)
            //    .Count();

            //quizQuestionVM.CategoryProgress = Math.Round(100.0 * doneQuestionsInCategory / numberOfQuestionsInCategory);

            quizQuestionVM.CategoryProgress = GetCategoryProgress((int)categoryId, memberId);

            return(quizQuestionVM);
        }