public async Task <IActionResult> Create(ModuleCreationViewModel moduleViewModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var module = new Module { Title = moduleViewModel.Title, Description = moduleViewModel.Description }; await _context.Modules.AddAsync(module); await _context.SaveChangesAsync(); return(RedirectToRoute(new { controller = "Modules", action = "Details", id = module.ModuleId })); }
public async Task <IActionResult> Create(QuestionCreationViewModel questionViewModel) { if (questionViewModel.Answers == null) { questionViewModel.Answers = questionViewModel.BinaryAnswers; } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var question = new Question { ModuleId = questionViewModel.ModuleId, QuestionType = questionViewModel.QuestionType, Text = questionViewModel.Text }; await _context.Questions.AddAsync(question); var answersList = questionViewModel.Answers .Select(answerViewModel => new Answer() { QuestionId = question.QuestionId, Text = answerViewModel.Text, IsValid = answerViewModel.IsValid }) .ToList(); await _context.Answers.AddRangeAsync(answersList); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Attempt(ExamAttemptViewModel viewModel) { if (viewModel == null) { return(BadRequest()); } viewModel.Exam = await _context.Exams.FirstOrDefaultAsync(e => e.ExamId == viewModel.ExamId); if (viewModel.ParticipationDate.AddMinutes(viewModel.Exam.Duration) > DateTime.Now) { return(BadRequest()); } var examAttempt = new ExamAttempt { ExamId = viewModel.ExamId, UserId = viewModel.UserId, ParticipationDate = viewModel.ParticipationDate }; await _context.ExamAttempts.AddAsync(examAttempt); await _context.SaveChangesAsync(); foreach (ExamQuestionViewModel question in viewModel.Questions) { foreach (ExamAnswerViewModel answer in question.Answers) { if (answer.StudentAnswer) { var userAnswer = new UserAnswer { QuestionId = question.QuestionId, ExamAttemptId = examAttempt.ExamAttemptId, AnswerId = answer.AnswerId }; var realAnswer = await _context.Answers.FirstOrDefaultAsync(a => a.AnswerId == answer.AnswerId); userAnswer.IsValid = realAnswer.IsValid; await _context.UserAnswers.AddAsync(userAnswer); await _context.SaveChangesAsync(); } } } var userAnswers = _context.UserAnswers .Where(userAnswer => userAnswer.ExamAttemptId.Equals(examAttempt.ExamAttemptId)) .ToList(); examAttempt.Result = (userAnswers.Select(userAnswer => userAnswer.IsValid).Count() / (userAnswers.Count() != 0 ? userAnswers.Count() : 1)) * 100; var examAttemptData = _context.ExamAttempts.Update(examAttempt); if (examAttemptData == null) { return(NotFound()); } return(RedirectToAction("Index")); }