public ActionResult AutoSubmit(string data) { var userid = User.Identity.GetUserId(); var answerSheet = new AnswerSheet(data) { TestTakerId = userid }; var score = 0f; var test = DbContext.Tests.FirstOrDefault(s => s.TestId == answerSheet.TestId && s.Actived); var account = DbContext.Accounts.First(s => s.Id == userid); if (test != null) { if (account.Courses.First(s => s.CourseId == test.CourseId).Tests.All(s => s.TestId != test.TestId) || !(test.TestStart <= DateTime.Now && DateTime.Now <= test.TestEnd)) { return Json(""); } var answerSheets = account.AnswerSheets.Where(s => s.TestId == test.TestId).OrderByDescending(s => s.TestId).ToList(); foreach (var answer in answerSheet.Answers) { var testDetail = test.TestDetails.FirstOrDefault(s => s.QuestionId == answer.QuestionId); if (testDetail != null) score += testDetail.GradeTest(answer); } answerSheet.Score = score; if (answerSheets.Count == 0) { DbContext.AnswerSheets.Add(answerSheet); DbContext.SaveChanges(); } else { if (answerSheets.Count <= test.SubmitNo) { var lastAnswerSheet = answerSheets.Last(); var minute = DateTime.Now.Subtract(lastAnswerSheet.SubmitTime).TotalMinutes; if (lastAnswerSheet.IsDone || minute >= test.TestTime) { if (answerSheets.Count == test.SubmitNo) return RedirectAccessDeniedPage(Url.Action("Index", "Home")); DbContext.AnswerSheets.Add(answerSheet); DbContext.SaveChanges(); } answerSheet.SubmitTime = lastAnswerSheet.SubmitTime; lastAnswerSheet.Update(answerSheet); DbContext.Entry(lastAnswerSheet).State = EntityState.Modified; DbContext.SaveChanges(); } } } return Json(""); }
public void SetAnswer(AnswerSheet answerSheet) { foreach (var answer in answerSheet.Answers) { var question = TestDetails.First(s => s.QuestionId == answer.QuestionId).Question; foreach (var detail in question.QuestionDetails) { var answerDetail = answer.AnswerDetails.Find(s => s.QuestionDetailId == detail.QuestionDetailId); } } }
public void Update(AnswerSheet sheet) { TestId = sheet.TestId; SubmitTime = sheet.SubmitTime; Answer = sheet.Answer; IsDone = sheet.IsDone; Score = sheet.Score; }