示例#1
0
 public int GetCategoryIdByQuizUnitId(int quizUnitId)
 {
     return(QuizUnit
            .Include(q => q.Category)
            .Single(q => q.QuizUnitId == quizUnitId)
            .Category
            .CategoryId);
 }
示例#2
0
        internal void UpdateQuizUnit(int id, EditQuizUnitVM newModel)
        {
            var oldQuizUnit = QuizUnit
                              .Single(q => q.QuizUnitId == id);

            oldQuizUnit.InfoTextHeader  = newModel.QuizUnitHeader;
            oldQuizUnit.InfoTextContent = newModel.QuizUnitContent;
            oldQuizUnit.Order           = newModel.Order;
            oldQuizUnit.CategoryId      = newModel.SelectedCategoryId;

            SaveChanges();
        }
示例#3
0
 internal void AddNewQuizUnit(AddQuizUnitVM model)
 {
     // Todo - fixa så att order kollas och ändras
     QuizUnit.Add(new QuizUnit
     {
         Order           = QuizUnit.Max(c => c.Order) + 1,
         InfoTextHeader  = model.QuizUnitHeader,
         InfoTextContent = model.QuizUnitContent,
         CategoryId      = model.SelectedCategoryId,
     });
     SaveChanges();
 }
示例#4
0
        public AddQuestionVM GetNewQuestionVM()
        {
            AddQuestionVM model = new AddQuestionVM();

            model.PossibleQuizUnits = QuizUnit.Select(c => new SelectListItem
            {
                Text  = c.InfoTextHeader,
                Value = c.QuizUnitId.ToString(),
            })
                                      .ToArray();

            model.SelectedQuizUnitId = Convert.ToInt32(model.PossibleQuizUnits[0].Value);

            model.Answers = new string[4];

            return(model);
        }
示例#5
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);
        }
示例#6
0
        internal EditQuestionVM GetEditQuestionVMById(int id)
        {
            var q = Question
                    .Include(c => c.Answer)
                    .SingleOrDefault(c => c.QuestionId == id);

            EditQuestionVM model = new EditQuestionVM
            {
                Answers            = q.Answer.Select(a => a.AnswerText).ToArray(),
                Order              = q.Order,
                QuestionText       = q.QuestionText,
                SelectedQuizUnitId = (int)q.QuizUnitId,
                PossibleQuizUnits  = QuizUnit.Select(c => new SelectListItem
                {
                    Text  = c.InfoTextHeader,
                    Value = c.QuizUnitId.ToString(),
                })
                                     .ToArray(),
            };

            return(model);
        }
示例#7
0
        internal QuizOverviewVM GetQuizOverviewVM()
        {
            List <Category> que = new List <Category>();

            foreach (var category in Category)
            {
                List <QuizUnit> porque = new List <QuizUnit>();
                foreach (var quizUnit in QuizUnit)
                {
                    List <Question> donde = new List <Question>();
                    foreach (var question in Question)
                    {
                        if (question.QuizUnitId == quizUnit.QuizUnitId)
                        {
                            donde.Add(question);
                        }
                    }
                    if (quizUnit.CategoryId == category.CategoryId)
                    {
                        porque.Add(quizUnit);
                    }
                }
                que.Add(category);
            }


            QuizOverviewVM model = new QuizOverviewVM
            {
                Categories         = que.ToArray(),
                NumberOfCategories = Category.Count(),
                NumberOfQuizUnits  = QuizUnit.Count(),
                NumberOfQuestions  = Question.Count(),
            };

            return(model);
        }
示例#8
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));
            }
        }
示例#9
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);
        }