public void SaveModeratedPracticeTest(QuestionBank qb) { using (QBankEntities context = this.CreateConnection()) { if (qb.Status == 1) { try { PracticeTest pt = new PracticeTest(); pt.AddedBy = qb.CreatedBy; pt.ExaminationID = GetExaminationID(qb.ExamGrade); pt.MasterCode = qb.MasterCode; pt.Name = qb.Name; pt.Source = qb.Description; pt.AddedDate = DateTime.Now; pt.Status = 2; context.PracticeTests.Add(pt); context.SaveChanges(); qb.Status = 2; } catch (Exception ex) { //Revert back status code qb.Status = 1; } } else { PracticeTest p = context.PracticeTests.Where(x => x.MasterCode == qb.MasterCode && qb.Status == 2).FirstOrDefault(); bool changed = false; p.ModifiedDate = DateTime.Now; if (p.Name != qb.Name) { p.Name = qb.Name; changed = true; } if (p.Source != qb.Description) { p.Source = qb.Description; changed = true; } //p.ExaminationID = pt.ExaminationID; if (changed) { context.SaveChanges(); } } } }
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(); } } } }