public bool SaveAssessmentResultAndAnsweredSheet(AssessmentResultEntity assessmentResultEntity) { try { AssessmentResultData assessmentResultData = new AssessmentResultData(); assessmentResultData.UserId = assessmentResultEntity.UserId; assessmentResultData.AssessmentId = assessmentResultEntity.AssessmentId; assessmentResultData.QuestionPaperId = assessmentResultEntity.QuestionPaperId; assessmentResultData.CanInsertAssessmentResult = assessmentResultEntity.CanInsertAssessmentResult; assessmentResultData.IsWriteAssessmentLater = assessmentResultEntity.IsWriteAssessmentLater; int rightAnsweredCount = 0; foreach (var answer in assessmentResultEntity.AnsweredSheet) { if (answer.SelectedOptionId == answer.RightOptionId) { rightAnsweredCount = rightAnsweredCount + 1; } } assessmentResultData.TotalQuestionsCount = assessmentResultEntity.AnsweredSheet.Count; assessmentResultData.RightAnsweredCount = rightAnsweredCount; List <QuestionPaperData> listQuestionPaperData = new List <QuestionPaperData>(); foreach (var questionEntity in assessmentResultEntity.AnsweredSheet) { QuestionPaperData questionPaperData = new QuestionPaperData(); questionPaperData.ID = questionEntity.ID; questionPaperData.Number = questionEntity.Number; questionPaperData.OptionType = questionEntity.OptionType; questionPaperData.Options = new List <OptionsData>(); questionPaperData.SelectedOptionId = questionEntity.SelectedOptionId; questionPaperData.WrittenAnswer = questionEntity.WrittenAnswer; foreach (var optionEntity in questionEntity.Options) { OptionsData optionData = new OptionsData(); optionData.ID = optionEntity.ID; optionData.OptionText = optionEntity.OptionText; questionPaperData.Options.Add(optionData); } questionPaperData.QuestionText = questionEntity.QuestionText; questionPaperData.RightOptionId = questionEntity.RightOptionId; listQuestionPaperData.Add(questionPaperData); } assessmentResultData.AnsweredSheet = listQuestionPaperData; IQuestionPaperRepository questionPaperRepository = DataFactory.DataFactory.CreateQuestionPaperRepositoryInstance(); return(questionPaperRepository.SaveAssessmentResultAndAnsweredSheet(assessmentResultData)); } catch (Exception ex) { return(false); } }
private StringBuilder CreateQuestionPaperXMLFromDataTable(DataTable dt) { StringBuilder strBuilder = new StringBuilder(); try { List <QuestionPaperData> listQuestionPaperData = new List <QuestionPaperData>(); int questionID = 1; foreach (DataRow row in dt.Rows) { if (row.ItemArray[0].ToString() != string.Empty) { QuestionPaperData objQuestionPaperData = new QuestionPaperData(); objQuestionPaperData.ID = questionID; objQuestionPaperData.Number = Convert.ToInt32(row.ItemArray[0]); objQuestionPaperData.QuestionText = Convert.ToString(row.ItemArray[1]); objQuestionPaperData.OptionType = Convert.ToString(row.ItemArray[2]); int optionID = 100;///option id starts from 100 int optionColumnIndex = 3; while (optionColumnIndex <= 6) { OptionsData option = new OptionsData(); option.ID = optionID; option.OptionText = Convert.ToString(row.ItemArray[optionColumnIndex]); objQuestionPaperData.Options.Add(option); optionID++; optionColumnIndex++; } string optionIDText = Convert.ToString(row.ItemArray[7]); if (optionIDText == "Option1") { objQuestionPaperData.RightOptionId = 100; } else if (optionIDText == "Option2") { objQuestionPaperData.RightOptionId = 101; } else if (optionIDText == "Option3") { objQuestionPaperData.RightOptionId = 102; } else if (optionIDText == "Option4") { objQuestionPaperData.RightOptionId = 103; } questionID++; listQuestionPaperData.Add(objQuestionPaperData); } } XmlSerializer serializer = new XmlSerializer(typeof(List <QuestionPaperData>)); using (StringWriter textWriter = new StringWriter()) { serializer.Serialize(textWriter, listQuestionPaperData); strBuilder.Append(textWriter.ToString()); } } catch (Exception) { throw; } return(strBuilder); }