public ActionResult SelectTopicsRandom(AddRandomManualTestViewModel 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 SelectTopicManualRandViewModel { Name = x.Name, Id = x.Id }); model.SelectedTopics = viewModel; return(View(model)); }
public ActionResult AddRandomTest(AddRandomManualTestViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var teacher = this.userService.GetTeacherByAppUserId(this.User.Identity.GetUserId()); var testId = this.manualTestService.Add( new ManualTest { Rate = model.Rate, TeacherId = teacher.Id, Title = model.Title, Time = model.Time }); List <int> closeQuestionsIds = new List <int>(); List <int> openQuestionsIds = new List <int>(); int index; Random rand = new Random(); foreach (var item in model.SelectedTopics) { if (item.SelectedCloseQ == true) { closeQuestionsIds = this.closeQuestionService.GetForTopic(item.Id).Select(x => x.Id).ToList <int>(); if (item.CloseQCount <= closeQuestionsIds.Count) { for (int i = 0; i < item.CloseQCount; i++) { index = rand.Next(closeQuestionsIds.Count); this.manualTestService.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.manualTestService.AddCloseQuestion(closeQuestionsIds[index], testId); closeQuestionsIds.RemoveAt(index); } } } if (item.SelectedOpenQ == true) { openQuestionsIds = this.openQuestionService.GetForTopic(item.Id).Select(x => x.Id).ToList <int>(); if (item.OpenQCount <= openQuestionsIds.Count) { for (int i = 0; i < item.OpenQCount; i++) { index = rand.Next(openQuestionsIds.Count); this.manualTestService.AddOpenQuestion(openQuestionsIds[index], testId); openQuestionsIds.RemoveAt(index); } } else { var size = openQuestionsIds.Count; for (int i = 0; i < size; i++) { index = rand.Next(openQuestionsIds.Count); this.manualTestService.AddOpenQuestion(openQuestionsIds[index], testId); openQuestionsIds.RemoveAt(index); } } } } return(Redirect("/ManualTest/Tests")); }