Пример #1
0
        public ActionResult AddRandomTest(AddRandomAutomaticTestViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var teacher = this.userService.GetTeacherByAppUserId(this.User.Identity.GetUserId());
            var testId  = this.automaticTestService.Add(
                new AutomaticTest
            {
                Rate      = model.Rate,
                TeacherId = teacher.Id,
                Title     = model.Title,
                Time      = model.Time
            });
            List <int> closeQuestionsIds = new List <int>();
            int        index;
            Random     rand = new Random();

            foreach (var item in model.SelectedTopics)
            {
                if (item.Selected == true)
                {
                    closeQuestionsIds = this.closeQuestionService.GetForTopic(item.Id).Select(x => x.Id).ToList <int>();
                    if (item.QuestionCount <= closeQuestionsIds.Count)
                    {
                        for (int i = 0; i < item.QuestionCount; i++)
                        {
                            index = rand.Next(closeQuestionsIds.Count);
                            this.automaticTestService.AddCloseQuestion(closeQuestionsIds[index], testId);
                            closeQuestionsIds.RemoveAt(index);
                        }
                    }
                    else
                    {
                        var size = closeQuestionsIds.Count;
                        for (int i = 0; i < size; i++)
                        {
                            index = rand.Next(closeQuestionsIds.Count);
                            this.automaticTestService.AddCloseQuestion(closeQuestionsIds[index], testId);
                            closeQuestionsIds.RemoveAt(index);
                        }
                    }
                }
            }
            return(Redirect("/AutomaticTest/Tests"));
        }
Пример #2
0
        public ActionResult SelectTopicsRandom(AddRandomAutomaticTestViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("AddRandomTest", model));
            }
            var teacher   = this.userService.GetTeacherByAppUserId(this.User.Identity.GetUserId());
            var topics    = this.topicService.GetForTeacher(teacher.Id);
            var viewModel = topics.ToList().ConvertAll(x =>
                                                       new SelectTopicAutomaticRandViewModel
            {
                Name = x.Name,
                Id   = x.Id
            });

            model.SelectedTopics = viewModel;
            return(View(model));
        }