public ActionResult StartTest(int id) { using (var db = new UsersContext()) { var test = db.Tests.FirstOrDefault(t => t.TestId == id); if (test == null) {return RedirectToAction("RolePermissions", "Home");} var userId = WebSecurity.GetUserId(User.Identity.Name); var actionInProgress = db.Actions.FirstOrDefault(a => a.User.UserId == userId && a.Status == 1); if (actionInProgress != null || test.Questions.Count == 0) { return RedirectToAction("Tests", "Test"); } var action=new Action { User = db.UserProfiles.FirstOrDefault(u => u.UserId == userId), Test = test, Status = 1, TestsInProgres = new List<TestInProgres>() }; var listQuestion = test.Questions.OrderBy(q => q.Number).ToList(); foreach (var question in listQuestion) { action.TestsInProgres.Add(new TestInProgres { Action = action, Question = question, Submitted = false }); } action.DateStart = DateTime.Now; db.Actions.Add(action); db.SaveChanges(); } return RedirectToAction("TestInAction", "Test", new {number = 1}); }
private void RemoveFromProgres(Action action, UsersContext db) { db.Actions.Attach(action); var testsList= action.TestsInProgres.ToList(); foreach (var testInProgres in testsList) { action.TestsInProgres.Remove(testInProgres); db.TestsInProgres.Remove(testInProgres); } db.SaveChanges(); }
private void CopyToHistory(Action action, UsersContext db) { foreach (var test in action.TestsInProgres) { var testToHistory = new TestHistory { Action = action, Number = test.Question.Number, Question = test.Question, UserAnswer = test.UserAnswer }; db.TestHistory.Add(testToHistory); db.SaveChanges(); } }
private void CalculateResult(int userId, Action action, UsersContext db) { var testResult = new TestResults { User = db.UserProfiles.FirstOrDefault(u => u.UserId == userId), Action = action, Test = action.Test, DatePassing = action.DatePassing }; double pointSum = 0; foreach (var test in action.TestsInProgres) { if (test.UserAnswer != null) { switch (test.Question.TestType) { case DataConst.CheckedType: { pointSum += (CalculateChecked(test)); break; } case DataConst.RadioType: { pointSum += (CalculateRadio(test)); break; } } } } testResult.PercentTaken = Convert.ToInt32((pointSum/action.TestsInProgres.Count)*100); testResult.Passed = testResult.PercentTaken >= DataConst.PercentLimit; db.TestsResults.Add(testResult); db.SaveChanges(); }
private void SubbmitTest(int userId, TestInProgres currentQuestion, Action action, UsersContext db) { currentQuestion.Submitted = true; action.DatePassing = DateTime.Now; action.Status = 2; db.SaveChanges(); CalculateResult(userId, action, db); CopyToHistory(action, db); RemoveFromProgres(action, db); }
private ActionResult ExecuteCommand(ResponseAction response, int userId, Action action, TestInProgres currentQuestion, UsersContext db ) { switch (response.Command) { case DataConst.Subbmit: { SubbmitTest(userId, currentQuestion, action, db); return RedirectToAction("Results", "Result"); } case DataConst.Next: { if (response.CurrentNumber.HasValue) { return RedirectToAction("TestInAction", "Test", new { number = response.CurrentNumber + 1}); } break; } case DataConst.Prev: { if (response.CurrentNumber.HasValue) { return RedirectToAction("TestInAction", "Test", new { number = response.CurrentNumber - 1 }); } break; } case DataConst.GoTo: { if (response.GoTo.HasValue) { return RedirectToAction("TestInAction", "Test", new { number = response.GoTo }); } break; } } ViewBag.WarningMessage = DataConst.WarningErrorMessage; return RedirectToAction("TestInAction", "Test", new { number = 1 }); }
private ActionResult CheckCurrentQuestion(QuestionInProgresWrapper result, ResponseAction response, int userId, Action action, TestInProgres currentQuestion, UsersContext db) { try { if (currentQuestion.Question.TestType == DataConst.CheckedType) { SaveResultOfCheckedQuestion(result, currentQuestion, db); } if (currentQuestion.Question.TestType == DataConst.RadioType) { SaveResultOfRadioQuestion(result, currentQuestion, db); } return ExecuteCommand(response, userId, action, currentQuestion, db); } catch (Exception) { ViewBag.WarningMessage = DataConst.WarningErrorMessage; return RedirectToAction("TestInAction", "Test", new { number = 1 }); } }