public void SaveDataEntryQuestion(Qrator.Common.Question Q, string psCode) { using (QBankEntities context = this.CreateConnection()) { PracticeTest ps = context.PracticeTests.Where(p => p.MasterCode == psCode && p.Status == 1).FirstOrDefault(); if (Q.MasterCode == null) { try { ServerData.Entities.Question qtn = new ServerData.Entities.Question(); qtn.Complexity = Q.Complexity; qtn.Explanation = Q.AnswerExplanation; qtn.HeaderText = Q.Instruction; qtn.QuestionText = Q.QuestionText; qtn.TopicID = GetTopicID(ps.ExaminationID, Q.Subject, Q.Topic); qtn.MasterCode = Guid.NewGuid().ToString(); qtn.Status = 1; qtn.ModifiedDate = DateTime.Now; qtn.PracticeTestID = ps.ID; foreach (var x in Q.OptionTexts) { ServerData.Entities.Answer a = new ServerData.Entities.Answer(); a.OptionText = x.OptionText; a.IsCorrect = x.IsCorrect; a.MasterCode = Guid.NewGuid().ToString(); a.ModifiedDate = DateTime.Now; a.Status = 1; qtn.Answers.Add(a); } context.Questions.Add(qtn); context.SaveChanges(); //Sync back Q.MasterCode = qtn.MasterCode; Q.Status = qtn.Status; foreach (var x in Q.OptionTexts) { ServerData.Entities.Answer a = qtn.Answers.Where(y => y.OptionText == x.OptionText).FirstOrDefault(); x.MasterCode = a.MasterCode; x.Status = a.Status; } } catch (DbEntityValidationException ex) { foreach (var validationErrors in ex.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } Q.MasterCode = string.Empty; Q.Status = 0; foreach (var item in Q.OptionTexts) { item.MasterCode = string.Empty; item.Status = 0; } } } else { if (Q.IsDeleted) { Qrator.ServerData.Entities.Question qt = context.Questions.Where(x => x.MasterCode == Q.MasterCode && x.Status == 1).FirstOrDefault(); context.Questions.Remove(qt); context.SaveChanges(); } else { int i = 0; Qrator.ServerData.Entities.Question qt = context.Questions.Where(x => x.MasterCode == Q.MasterCode && x.Status == 1).FirstOrDefault(); if (qt.Complexity != Q.Complexity) { qt.Complexity = Q.Complexity; i++; } if (qt.Explanation != Q.AnswerExplanation) { qt.Explanation = Q.AnswerExplanation; i++; } if (qt.HeaderText != Q.Instruction) { qt.HeaderText = Q.Instruction; i++; } if (qt.QuestionText != Q.QuestionText) { qt.QuestionText = Q.QuestionText; i++; } if (qt.Topic.Name != Q.Topic) { qt.TopicID = GetTopicID(ps.ExaminationID, Q.Subject, Q.Topic); i++; } bool answerChanged = false; //Use this variable to update last modified date of each answer option //Update Answers foreach (var item in Q.OptionTexts) { Qrator.ServerData.Entities.Answer a = qt.Answers.Where(x => x.MasterCode == item.MasterCode && item.Status == 1).FirstOrDefault(); if (a.IsCorrect != item.IsCorrect) { a.IsCorrect = item.IsCorrect; answerChanged = true; i++; } if (a.OptionText != item.OptionText) { answerChanged = true; a.OptionText = item.OptionText; i++; } if (answerChanged) { a.ModifiedDate = DateTime.Now; } answerChanged = false; } qt.ModifiedDate = DateTime.Now; if (i > 0) { context.SaveChanges(); } } } } }
public void SaveModeratedQuestion(Qrator.Common.Question Q, string psCode) { using (QBankEntities context = this.CreateConnection()) { if (Q.IsDeleted) { //Delete only the one which has been moderated - those brought by moderator and deleted before sync (status = 1) will be left in the qbf file. Qrator.ServerData.Entities.Question qt = context.Questions.Where(x => x.MasterCode == Q.MasterCode && x.Status == 2).FirstOrDefault(); context.Questions.Remove(qt); context.SaveChanges(); return; } PracticeTest ps = context.PracticeTests.Where(p => p.MasterCode == psCode && p.Status == 2).FirstOrDefault(); if (Q.Status == 1) { //Save new record try { ServerData.Entities.Question qtn = new ServerData.Entities.Question(); qtn.Complexity = Q.Complexity; qtn.Explanation = Q.AnswerExplanation; qtn.HeaderText = Q.Instruction; qtn.QuestionText = Q.QuestionText; qtn.TopicID = GetTopicID(ps.ExaminationID, Q.Subject, Q.Topic); qtn.Status = 2; qtn.MasterCode = Q.MasterCode; qtn.ModifiedDate = DateTime.Now; qtn.PracticeTestID = ps.ID; foreach (var x in Q.OptionTexts) { ServerData.Entities.Answer a = new ServerData.Entities.Answer(); a.OptionText = x.OptionText; a.IsCorrect = x.IsCorrect; a.MasterCode = x.MasterCode; a.ModifiedDate = DateTime.Now; a.Status = 2; qtn.Answers.Add(a); } context.Questions.Add(qtn); context.SaveChanges(); //Sync back Q.Status = qtn.Status; foreach (var x in Q.OptionTexts) { x.Status = 2; } } catch (DbEntityValidationException ex) { foreach (var validationErrors in ex.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } Q.Status = 1; foreach (var x in Q.OptionTexts) { x.Status = 1; } } } else { //Update record int i = 0; Qrator.ServerData.Entities.Question qt = context.Questions.Where(x => x.MasterCode == Q.MasterCode && x.Status == 2).FirstOrDefault(); if (qt.Complexity != Q.Complexity) { qt.Complexity = Q.Complexity; i++; } if (qt.Explanation != Q.AnswerExplanation) { qt.Explanation = Q.AnswerExplanation; i++; } if (qt.HeaderText != Q.Instruction) { qt.HeaderText = Q.Instruction; i++; } if (qt.QuestionText != Q.QuestionText) { qt.QuestionText = Q.QuestionText; i++; } if (qt.Topic.Name != Q.Topic) { qt.TopicID = GetTopicID(ps.ExaminationID, Q.Subject, Q.Topic); i++; } bool answerChanged = false; //Use this variable to update last modified date of each answer option //Update Answers foreach (var item in Q.OptionTexts) { Qrator.ServerData.Entities.Answer a = qt.Answers.Where(x => x.MasterCode == item.MasterCode && x.Status == 2).FirstOrDefault(); if (a.IsCorrect != item.IsCorrect) { a.IsCorrect = item.IsCorrect; answerChanged = true; i++; } if (a.OptionText != item.OptionText) { answerChanged = true; a.OptionText = item.OptionText; i++; } if (answerChanged) { a.ModifiedDate = DateTime.Now; } answerChanged = false; } qt.ModifiedDate = DateTime.Now; if (i > 0) { context.SaveChanges(); } } } }