public void Save(T model, ApplicationDbContext db) { SurveyPartData modelData = model as SurveyPartData; int id = StringToValue.ParseInt(modelData.Id); SurveyTemplate surveyTemplate = db.T_SurveyTemplate.Find(id); if (surveyTemplate != null) { SurveyTemplate newSurveyTemplate = new SurveyTemplate() { Name = modelData.Name, SurveyDate = DateTime.Now, PublishDate = new DateTime(1900, 1, 1), SurveyPartTemplates = new List <SurveyPartTemplate>() }; db.Entry(surveyTemplate).Collection(p => p.SurveyPartTemplates).Load(); if (surveyTemplate.SurveyPartTemplates != null) { foreach (SurveyPartTemplate surveyPartTemplate in surveyTemplate.SurveyPartTemplates) { SurveyPartTemplate newSurveyPartTemplate = new SurveyPartTemplate() { Name = surveyPartTemplate.Name, SummaryTitle = surveyPartTemplate.SummaryTitle, SurveyQuestionTemplates = new List <SurveyQuestionTemplate>() }; newSurveyTemplate.SurveyPartTemplates.Add(newSurveyPartTemplate); db.Entry(surveyPartTemplate).Collection(p => p.SurveyQuestionTemplates).Load(); foreach (SurveyQuestionTemplate surveyQuestionTemplate in surveyPartTemplate.SurveyQuestionTemplates) { SurveyQuestionTemplate newSurveyQuestionTemplate = new SurveyQuestionTemplate() { Name = surveyQuestionTemplate.Name, Definition = surveyQuestionTemplate.Definition, QuestionType = surveyQuestionTemplate.QuestionType }; newSurveyPartTemplate.SurveyQuestionTemplates.Add(newSurveyQuestionTemplate); } } } db.T_SurveyTemplate.Add(newSurveyTemplate); db.SaveChanges(); } }
public void Save(T model, ApplicationDbContext db) { SurveyPartData modelData = model as SurveyPartData; int id; int.TryParse(modelData.Id, out id); SurveyPartTemplate surveyPartTemplate = db.T_SurveyPartTemplate.Find(id); if (surveyPartTemplate != null) { surveyPartTemplate.Name = modelData.Name; surveyPartTemplate.SummaryTitle = modelData.Summary; db.Entry(surveyPartTemplate).State = EntityState.Modified; db.SaveChanges(); } }
public JsonResult EditPart(SurveyPartData model) { int id; if (!int.TryParse(model.Id, out id)) { return(Json(new { result = "Error" })); } ISaveModel <SurveyPartData> saveSurveyTemplate = new SaveSurveyTemplatePartEdit <SurveyPartData>(); saveSurveyTemplate.Save(model, db); return(Json(new { result = "OK" })); }
public int Check(T model, out string message, ApplicationDbContext db) { SurveyPartData modelData = model as SurveyPartData; int errorCode = 0; message = ""; int id = StringToValue.ParseInt(modelData.Id); SurveyTemplate surveyTemplate = db.T_SurveyTemplate.Find(id); if (surveyTemplate != null) { db.Entry(surveyTemplate).Collection(p => p.SurveyPartTemplates).Load(); List <SurveyPartTemplate> surveyPartTemplates = surveyTemplate.SurveyPartTemplates; if (surveyPartTemplates == null || surveyPartTemplates.Count() == 0) { errorCode = 2; message = "Brak przynajmniej jednej sekcji ankiety."; return(errorCode); } foreach (SurveyPartTemplate surveyPartTemplate in surveyTemplate.SurveyPartTemplates) { db.Entry(surveyPartTemplate).Collection(p => p.SurveyQuestionTemplates).Load(); if (surveyPartTemplate.SurveyQuestionTemplates == null || surveyPartTemplate.SurveyQuestionTemplates.Count() == 0) { errorCode = 2; message = "Sekcja '" + surveyPartTemplate.Name + "' nie zawiera ani jednego pytania."; return(errorCode); } } } else { errorCode = 1; message = "Nie znaleziono wzorca ankiety."; } return(errorCode); }
public JsonResult PublishSurvey(SurveyPartData model) { string msg; ICheckData <SurveyPartData> checkSurveyTemplate = new CheckSurveyTemplate <SurveyPartData>(); if (checkSurveyTemplate.Check(model, out msg, db) > 0) { return(Json(new { result = "Error", message = msg })); } ISaveModel <SurveyPartData> publishSurvey = new PublishSurvey <SurveyPartData>(); publishSurvey.Save(model, db); return(Json(new { result = "OK" })); }
public void Save(T model, ApplicationDbContext db) { SurveyPartData modelData = model as SurveyPartData; int id = StringToValue.ParseInt(modelData.Id); DateTime deadline = CalculateDate.StringToDate(modelData.Summary, "."); SurveyTemplate surveyTemplate = db.T_SurveyTemplate.Find(id); if (surveyTemplate != null) { surveyTemplate.PublishDate = DateTime.Now; List <Survey> surveys = new List <Survey>(); List <Employee> employees = db.T_Employees.ToList(); db.Entry(surveyTemplate).Collection(p => p.SurveyPartTemplates).Load(); List <SurveyPartTemplate> surveyPartTemplates = surveyTemplate.SurveyPartTemplates; if (surveyPartTemplates == null) { surveyPartTemplates = new List <SurveyPartTemplate>(); } foreach (Employee employee in employees) { Team team = db.T_Teams.Find(employee.TeamId); Survey survey = new Survey() { EmployeeId = employee.Id, ManagerId = team.ManagerId, SurveyTemplateId = id, SurveyStatusId = 1, Name = modelData.Name, SurveyDate = DateTime.Now, SurveyDadline = deadline, EmployeeCompleted = false, ManagerCompleted = false, CompliteEmployeeDate = new DateTime(1901, 1, 1), CompliteManagerDate = new DateTime(1901, 1, 1), SurveyParts = new List <SurveyPart>() }; if (surveyTemplate.SurveyPartTemplates != null) { foreach (SurveyPartTemplate surveyPartTemplate in surveyTemplate.SurveyPartTemplates) { SurveyPart surveyPart = new SurveyPart() { SurveyPartTemplateId = surveyPartTemplate.Id, SurveyQuestions = new List <SurveyQuestion>() }; survey.SurveyParts.Add(surveyPart); db.Entry(surveyPartTemplate).Collection(p => p.SurveyQuestionTemplates).Load(); foreach (SurveyQuestionTemplate surveyQuestionTemplate in surveyPartTemplate.SurveyQuestionTemplates) { SurveyQuestion surveyQuestion = new SurveyQuestion() { SurveyQuestionTemplateId = surveyQuestionTemplate.Id }; surveyPart.SurveyQuestions.Add(surveyQuestion); } } } surveys.Add(survey); } db.T_Survey.AddRange(surveys); db.SaveChanges(); } }