public ActionResult Take(int? Id) { // Load the question set if (Id == null || Id == 0) return RedirectToAction("Index", "Home"); var qs = this.QuestionSets.GetById((int)Id); if (qs == null) return RedirectToAction("Error", "Home"); // Load the response set var rs = this.ResponseSets.GetByQuestionnaireAndUser((int)Id, (Guid)Membership.GetUser().ProviderUserKey); if (rs != null) { // Response in progress, continue return View("Steps/Continue", qs); } else { // Start a new response rs = new ResponseSet(); rs.QuestionSetId = qs.Id; rs.UserId = (Guid)Membership.GetUser().ProviderUserKey; rs.QuestionCount = qs.Questions.Count; rs.Responded = DateTime.Now; rs = this.ResponseSets.AddOrUpdate(rs); return View("Steps/Welcome", qs); } }
private ResponseSet UpdateEntity(ResponseSet entity) { // Ensure we get a valid QuestionSet to update if (entity.Id == 0) throw new Exception("No ResponseSet specificied to update, should this be a new ResponseSet?"); var rs = this.uow.Context.ResponseSets.FirstOrDefault(x => x.Id == entity.Id && x.ValidTo == null); if (rs == null) throw new Exception("No ResponseSet found to update, should this be a new QuestionSet?"); // Update the new entity rs.Responded = entity.Responded; rs.Completed = entity.Completed; rs.Abandoned = entity.Abandoned; rs.UserId = entity.UserId; rs.QuestionSet = uow.Context.QuestionSets.FirstOrDefault(x => x.Id == entity.QuestionSetId); rs.ValidFrom = DateTime.Now; foreach (var response in entity.Responses) { if (rs.Responses.FirstOrDefault(x => x.Id == response.Id) != null) continue; Dal.Response r = null; if (response.Id > 0) { r = this.uow.Context.Responses.FirstOrDefault(x => x.Id == response.Id); if (r != null) r.ValidTo = DateTime.Now; } r = this.uow.Context.Responses.Create(); r.Question = uow.Context.Questions.FirstOrDefault(x => x.Id == response.QuestionId); r.Answer = uow.Context.Answers.FirstOrDefault(x => x.Id == response.AnswerId); r.Correct = response.Correct; r.PointsGained = response.PointsGained; r.ValidFrom = DateTime.Now; this.uow.Context.Responses.Add(r); rs.Responses.Add(r); } // Submit the changes to the database this.uow.Save(); // We now have a database Id for this entity entity.Id = rs.Id; return entity; }