public ViewResult Create(TestModel model) { TestsRepository repository = new TestsRepository(); if (repository.GetTestByShortcut(model.Shortcut) == null) { var result = new TestsRepository().Add(model); if (result == true) ViewBag.Message = Constants.TEST_CREATING_SUCCESS; else ViewBag.Message = Constants.TEST_CREATING_ERROR; return View(model); } ViewBag.Message = Constants.TEST_CREATING_ERROR; return View(model); }
public bool Add(TestModel model) { model.Creator = new UserProfile() { UserId = WebMatrix.WebData.WebSecurity.CurrentUserId, UserName = WebMatrix.WebData.WebSecurity.CurrentUserName }; using (var DB = new MainContext()) { var context = new ValidationContext(model.Infos.First(), serviceProvider: null, items: null); var validationResults = new List<ValidationResult>(); bool isValid = Validator.TryValidateObject(model.Infos.First(), context, validationResults, true); if (isValid) { DB.Infos.Add(model.Infos.First()); DB.SaveChanges(); } else model.Infos.Remove(model.Infos.First()); DB.Tests.Add(model); DB.Entry(model.Space).State = System.Data.Entity.EntityState.Unchanged; DB.Entry(model.Subject).State = System.Data.Entity.EntityState.Unchanged; DB.Entry(model.Creator).State = System.Data.Entity.EntityState.Unchanged; if (model.Infos != null) { foreach (var infoModel in model.Infos) { DB.Entry(infoModel).State = System.Data.Entity.EntityState.Unchanged; } } DB.SaveChanges(); return true; } return false; }
public static CheckResultModel CheckTest(TestModel result) { CheckResultModel check = new CheckResultModel() { QuestionsCount = result.Questions.Count, TestId = result.Id }; foreach (var question in result.Questions) { if (CheckQuestion(question)) check.RightAnswers++; } StatModel stat = new StatModel() { Count = check.RightAnswers, Date = DateTime.Now, Username = WebMatrix.WebData.WebSecurity.CurrentUserName, TestModel = result }; new StatsRepository().Add(stat); return check; }
public ViewResult Pass(TestModel model) { return View("TestResult", TestCheckingAlgorithms.CheckTest(model)); }