Beispiel #1
0
        private QuizUnit GetQuizUnit(int categoryId, string memberId)
        {
            // Hämta alla frågor i kategorin som användaren har klarat av
            var doneQuestionsInCategory = Progress
                                          .Where(p => p.MemberId == memberId)
                                          .Where(p => p.Question.QuizUnit.CategoryId == categoryId)
                                          .Select(p => p.Question)
                                          .Include(p => p.QuizUnit)
                                          .ToArray();

            var numberOfQuestionsInCategory = Question
                                              .Where(q => q.QuizUnit.CategoryId == categoryId)
                                              .Count();

            if (doneQuestionsInCategory.Length == 0)
            {
                return(GetFirstQuizUnitInCategory(categoryId));
            }
            else if (doneQuestionsInCategory.Length >= numberOfQuestionsInCategory)
            {
                // Alla frågor i kategorin är avklarade, navigera till nästa
                var possibleComingCategories = Category
                                               .Where(c => c.Order > Category.Single(d => d.CategoryId == categoryId).Order);

                if (possibleComingCategories.Count() == 0)
                {
                    return(null);
                }
                else
                {
                    var nextCategoryId = possibleComingCategories
                                         .OrderBy(c => c.Order)
                                         .First()
                                         .CategoryId;

                    return(GetQuizUnit(nextCategoryId, memberId));
                }
            }
            else
            {
                // Hämta högsta ordern på ett avklarat quizunit i current category
                int topDoneQuizUnitOrder = doneQuestionsInCategory
                                           .Max(q => q.QuizUnit.Order);

                // Hämta alla quizunits med högre order.
                var nextQuizUnits = QuizUnit
                                    .Where(q => q.CategoryId == categoryId)
                                    .Where(q => q.Order > topDoneQuizUnitOrder);

                if (nextQuizUnits.Count() > 0)
                {
                    var nextQuizUnit = nextQuizUnits
                                       .OrderBy(q => q.Order)
                                       .Include(q => q.Category)
                                       .First();

                    return(nextQuizUnit);
                }

                return(GetFirstQuizUnitInCategory(categoryId));
            }
        }
Beispiel #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);
        }