コード例 #1
0
        public IActionResult Create(Assessment assessment, string[] Topics, int TimeLimit, int NumQuestions, int NumDays)
        {
            if (ModelState.IsValid)
            {

                if (TimeLimit <= 0)
                {
                    TimeLimit = 10;
                }
                if (NumQuestions <= 0)
                {
                    NumQuestions = 20;
                }
                if (NumDays <= 0)
                {
                    NumDays = 7;
                }

                // Generate the times and Access Code
                assessment.AccessCode = Guid.NewGuid().ToString();
                assessment.CreatedDate = DateTime.Now;
                assessment.ExpirationDate = DateTime.Now.AddDays(NumDays);
                assessment.User = User.Identity.Name;
                foreach (string TopicIdString in Topics)
                {
                    int TopicIdInt = int.Parse(TopicIdString);
                    _context.Quizes.Add(
                        new Quiz()
                        {
                            Assessment = assessment,

                            Topic = _context.Topics.Single(m => m.TopicId == TopicIdInt),
                            NumberOfQuestions = NumQuestions,
                            NumberCorrect = 0,
                            TimeLimit = TimeLimit,
                            TimeStarted = null,
                            TimeCompleted = null,
                        });

                }
                _context.Assessments.Add(assessment);
                _context.SaveChanges();

                foreach (Quiz q in _context.Quizes.Where(q => q.AssessmentId == assessment.AssessmentId).ToList())
                {
                    var List = _context.TopicQuestions.Where(x => x.TopicId == q.TopicId && x.IsActive == true).ToList();
                    if (List.Count() < q.NumberOfQuestions)
                    {

                        NumQuestions = List.Count();
                    }
                    int NumEasy = (int) Math.Ceiling(NumQuestions * 0.3);
                    int NumHard = (int) Math.Ceiling(NumQuestions * 0.3);

                    var EasyList = List.Where(x => x.DifficultyLevel == DifficultyLevels.Easy).ToList();
                    var MediumList = List.Where(x => x.DifficultyLevel == DifficultyLevels.Medium).ToList();
                    var HardList = List.Where(x => x.DifficultyLevel == DifficultyLevels.Hard).ToList();
                    var QuizList = new List<TopicQuestion>();
                    Random rand = new Random();
                    var QsUsed = new List<int>();
                    while (QsUsed.Count < NumEasy && QsUsed.Count != EasyList.Count)
                    {
                        int r = rand.Next(0, EasyList.Count());

                        if (!QsUsed.Contains(r))
                        {
                            QuizList.Add(EasyList[r]);
                            QsUsed.Add(r);

                        }

                    }
                    QsUsed = new List<int>();
                    while (QsUsed.Count < NumHard && QsUsed.Count != HardList.Count)
                    {
                        int r = rand.Next(0, HardList.Count());

                        if (!QsUsed.Contains(r))
                        {
                            QuizList.Add(HardList[r]);
                            QsUsed.Add(r);

                        }

                    }
                    QsUsed = new List<int>();
                    int total = NumQuestions - QuizList.Count;
                    while (QsUsed.Count < total && QsUsed.Count != MediumList.Count)
                    {
                        int r = rand.Next(0, MediumList.Count());

                        if (!QsUsed.Contains(r))
                        {
                            QuizList.Add(MediumList[r]);
                            QsUsed.Add(r);

                        }

                    }
                    QsUsed = new List<int>();
                    int i = 1;

                    while (QsUsed.Count < NumQuestions && QsUsed.Count != QuizList.Count)
                    {
                        int r = rand.Next(0, QuizList.Count());

                        if (!QsUsed.Contains(r))
                        {

                           _context.QuizQuestions.Add(
                        new QuizQuestion()
                        {
                            Quiz = q,
                            QuestionNumber = i++,
                            NextQuestionId = 0,
                            Question = QuizList[r],
                        });
                            QsUsed.Add(r);
                        }
                    }
                    q.NumberOfQuestions = QuizList.Count;
                    _context.SaveChanges();
                    int nextId = 0;
                    foreach (QuizQuestion qq in q.Questions.OrderByDescending(x => x.QuestionNumber))
                    {
                        qq.NextQuestionId = nextId;
                        nextId = qq.QuizQuestionId;
                    }

                }

                _context.SaveChanges();
                return RedirectToAction("Code", new { id = assessment.AssessmentId });
            }

            // pass TopicList to ViewBag for Create View
            ViewBag.Topics = _context.Topics.Where(x => x.Questions.Where(y => y.IsActive == true).Count() != 0);
            return View(assessment);
        }
コード例 #2
0
        public IActionResult Edit(Assessment assessment)
        {
            if (ModelState.IsValid)
            {
                _context.Update(assessment);

                // TODO: this is throwing an error when trying to update.
                _context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(assessment);
        }