/// <summary> /// Gets the test answers. /// </summary> /// <param name="testCopyId">The test copy id.</param> /// <returns></returns> public List <QuestionModel> GetTestAnswers(int testCopyId) { TestCopy testCopy = m_testsService.GetTestCopy(testCopyId); Test test = m_testsService.GetTestById(testCopy.FK_TestId); List <QuestionModel> questions = new List <QuestionModel>(); XDocument multiTestCopyDocument = XDocument.Load(new StringReader(testCopy.TestCopyStructure)); // Question list IEnumerable <XElement> questionElements = multiTestCopyDocument.Descendants("Q"); foreach (TestQuestion testQuestion in test.TestQuestions) { XElement questionElement = questionElements.Where(q => Convert.ToInt64(q.Attribute("qId").Value) == testQuestion.QuestionId).FirstOrDefault(); if (questionElement != null) { QuestionModel question = new QuestionModel(); question.Id = Convert.ToInt32(questionElement.Attribute("qOrder").Value); question.Content = string.Format("{0}. {1}", question.Id, testQuestion.Question.Content); long questionId = Convert.ToInt64(questionElement.Attribute("qId").Value); Answer answer = AllAnswerList.Where(a => a.FK_QuestionId == questionId && a.IsAnswer == true).FirstOrDefault(); int order = Convert.ToInt32(questionElement.Elements("A") .Where(ao => Convert.ToInt64(ao.Attribute("aId").Value) == answer.AnswerId) .FirstOrDefault() .Attribute("aOrder") .Value); string answerOrder = order == 1 ? "A" : (order == 2 ? "B" : (order == 3 ? "C" : "D")); question.Answer1 = answer != null?string.Format("Đáp án: {0}. {1}", answerOrder, answer.Content) : "Đáp án: "; questions.Add(question); } } return(questions.OrderBy(x => x.Id).ToList()); }
static void Main(string[] args) { TestCopy test = new TestCopy(); test.Age = 30; test.IDCode = "12333"; test.param = new Param(); test.param.Name = "Dep1"; TestCopy copy = test.Clone() as TestCopy; Console.WriteLine(copy.Age); Console.WriteLine(copy.IDCode); Console.WriteLine(copy.param.Name); Console.WriteLine("copy change test: "); test.Age = 60; test.IDCode = "23333"; test.param.Name = "Dep2"; Console.WriteLine(copy.Age); Console.WriteLine(copy.IDCode); Console.WriteLine(copy.param.Name); TestCopy testDeep = new TestCopy(); testDeep.Age = 30; testDeep.IDCode = "12333"; testDeep.param = new Param(); testDeep.param.Name = "Dep3"; Console.WriteLine("TestDeepCopy"); TestCopy copyDeep = testDeep.DeepClone(); Console.WriteLine(copyDeep.Age); Console.WriteLine(copyDeep.IDCode); Console.WriteLine(copyDeep.param.Name); Console.WriteLine("deepcopy change test: "); testDeep.Age = 60; testDeep.IDCode = "23333"; testDeep.param.Name = "Dep4"; Console.WriteLine(copyDeep.Age); Console.WriteLine(copyDeep.IDCode); Console.WriteLine(copyDeep.param.Name); }
/// <summary> /// Gets the test details. /// </summary> /// <param name="testCopyId">The test copy id.</param> /// <returns></returns> public List <QuestionModel> GetTestDetails(int testCopyId) { TestCopy testCopy = m_testsService.GetTestCopy(testCopyId); Test test = m_testsService.GetTestById(testCopy.FK_TestId); List <QuestionModel> questions = new List <QuestionModel>(); XDocument testCopyDocument = XDocument.Load(new StringReader(testCopy.TestCopyStructure)); // Question list IEnumerable <XElement> questionElements = testCopyDocument.Descendants("Q"); //Yeu cau moi: //Nhung bai testCopy trong 1 bai test ko duoc trung nhau //Neu trung thi random cau hoi va cau tra loi do. foreach (var questionElement in questionElements) { TestQuestion testQuestion = test.TestQuestions.FirstOrDefault(x => x.QuestionId == Convert.ToInt64(questionElement.Attribute("qId").Value)); QuestionModel question = new QuestionModel(); question.Id = Convert.ToInt32(questionElement.Attribute("qOrder").Value); question.Content = string.Format("{0}. {1}", question.Id, testQuestion.Question.Content); IEnumerable <XElement> answerElements = questionElement.Elements("A"); question.Answer1 = string.Format("A. {0}", AllAnswerList.Where(a => a.AnswerId == Convert.ToInt64(answerElements .ElementAt(0) .Attribute("aId") .Value)) .FirstOrDefault().Content); question.Answer2 = string.Format("B. {0}", AllAnswerList.Where(a => a.AnswerId == Convert.ToInt64(answerElements .ElementAt(1) .Attribute("aId") .Value)) .FirstOrDefault().Content); question.Answer3 = string.Format("C. {0}", AllAnswerList.Where(a => a.AnswerId == Convert.ToInt64(answerElements .ElementAt(2) .Attribute("aId") .Value)) .FirstOrDefault().Content); question.Answer4 = string.Format("D. {0}", AllAnswerList.Where(a => a.AnswerId == Convert.ToInt64(answerElements .ElementAt(3) .Attribute("aId") .Value)) .FirstOrDefault().Content); questions.Add(question); } //foreach (TestQuestion testQuestion in test.TestQuestions) //{ // XElement questionElement = questionElements.Where(q => Convert.ToInt64(q.Attribute("qId").Value) // == testQuestion.QuestionId).FirstOrDefault(); // if (questionElement != null) // { // QuestionModel question = new QuestionModel(); // question.Id = Convert.ToInt32(questionElement.Attribute("qOrder").Value); // question.Content = string.Format("{0}. {1}", question.Id, testQuestion.Question.Content); // IEnumerable<XElement> answerElements = questionElement.Elements("A"); // question.Answer1 = string.Format("A. {0}", AllAnswerList.Where(a => a.AnswerId == // Convert.ToInt64(answerElements // .ElementAt(0) // .Attribute("aId") // .Value)) // .FirstOrDefault().Content); // question.Answer2 = string.Format("B. {0}", AllAnswerList.Where(a => a.AnswerId == // Convert.ToInt64(answerElements // .ElementAt(1) // .Attribute("aId") // .Value)) // .FirstOrDefault().Content); // question.Answer3 = string.Format("C. {0}", AllAnswerList.Where(a => a.AnswerId == // Convert.ToInt64(answerElements // .ElementAt(2) // .Attribute("aId") // .Value)) // .FirstOrDefault().Content); // question.Answer4 = string.Format("D. {0}", AllAnswerList.Where(a => a.AnswerId == // Convert.ToInt64(answerElements // .ElementAt(3) // .Attribute("aId") // .Value)) // .FirstOrDefault().Content); // questions.Add(question); // } //} return(questions.OrderBy(x => x.Id).ToList()); }
/// <summary> /// Creates the test. /// </summary> /// <param name="subjectId">The subject id.</param> /// <param name="numberOfQuestions">The number of questions.</param> public Test CreateTest(Test test, int subjectId, List <Tuple <QuestionLevel, int> > numberOfQuestions, int purpose, int subTest, double moreTime, out string message) { int totalQuestionForPerSubtest = numberOfQuestions.Sum(x => x.Item2); List <Question> questions = new List <Question>(totalQuestionForPerSubtest * subTest); foreach (Tuple <QuestionLevel, int> question in numberOfQuestions) { questions.AddRange(GetRandomQuestionForLevel(subjectId, question.Item1, purpose, question.Item2, subTest)); } test.CreatedBy = UserService.CurrentUser.CurrentUser.Username; test.CreatedDate = DateTime.Now; test.UpdatedBy = test.CreatedBy; test.UpdatedDate = DateTime.Now; // create test copy foreach (Question q in questions) { var tq = new TestQuestion(); tq.Question = q; tq.Test = test; test.TestQuestions.Add(tq); } IList <Question> tempQs = new List <Question>(); for (int count = 0; count < subTest; count++) { XDocument structure = new XDocument(new XElement("TestCopy")); //New Requirement: //1. Cau hoi trong cac de thi ko duoc trung nhau //2. Neu ko du cau hoi thi duoc trung nhung phai dao vi tri cau hoi va cau tra loi o cac de. //questionIdsPerSubtest with Id and Level var questionIdsPerSubtest = new Dictionary <long, int>(); foreach (Tuple <QuestionLevel, int> question in numberOfQuestions) { for (int j = questions.Count - 1; j >= 0; j--) { if (questions[j].Level == (int)question.Item1 && !questionIdsPerSubtest.Keys.ToList().Exists(x => x == questions[j].QuestionId) && questionIdsPerSubtest.Count < totalQuestionForPerSubtest) { questionIdsPerSubtest.Add(questions[j].QuestionId, (int)question.Item1); tempQs.Add(questions[j]); questions.RemoveAt(j); var countLevel = questionIdsPerSubtest.Values.Count(x => x == (int)question.Item1); if (countLevel == question.Item2) { break; } } } } //if (questionIdsPerSubtest.Count < totalQuestionForPerSubtest) //{ // for (int i = questions.Count - 1; i >= 0; i--) // { // for (int j = tempQs.Count - 1; j >= 0; j--) // { // if (questions[i].Level == tempQs[j].Level // && !questionIdsPerSubtest.Keys.ToList().Exists(x => x == tempQs[j].QuestionId) // && questionIdsPerSubtest.Count < totalQuestionForPerSubtest) // { // questionIdsPerSubtest.Add(tempQs[j].QuestionId, tempQs[j].Level); // } // } // } //} for (int i = 0; i < totalQuestionForPerSubtest; i++) { List <Answer> answers = m_questionService.GetAnswerOfQuestion(questionIdsPerSubtest.Keys.ElementAt(i)); List <XElement> answerList = new List <XElement>(); for (int j = 0; j < answers.Count; j++) { XElement answerElement = new XElement("A", new XAttribute("aOrder", j + 1), new XAttribute("aId", answers[j].AnswerId)); answerList.Add(answerElement); } structure.Element("TestCopy") .Add(new XElement("Q", new XAttribute("qOrder", i + 1), new XAttribute("qId", questionIdsPerSubtest.Keys.ElementAt(i)), answerList)); } TestCopy tc = new TestCopy(); tc.Test = test; tc.TestCopyStructure = structure.ToString(); test.TestCopies.Add(tc); } double timeStill = 0; if (questions.Count > 0) { timeStill = questions.Sum(x => x.Time); } double realTestTime = (tempQs.Sum(q => q.Time) + timeStill) / subTest; if (realTestTime > test.Time + moreTime) { message = "Thời gian chính xác để hoàn thành bài thi là: " + realTestTime + "\nThời gian thi do người dùng cài đặt là: " + test.Time + ".\nThời gian chênh lệch: " + Math.Abs(realTestTime - test.Time) + "\nBạn có muốn tiếp tục tạo đề thi không?"; } else { message = "Thời gian chính xác để hoàn thành bài thi là: " + realTestTime + "\nThời gian thi do người dùng cài đặt là: " + test.Time + ".\nThời gian chênh lệch: " + Math.Abs(realTestTime - test.Time) + "\nBạn có muốn tiếp tục tạo đề thi không?"; } test.TestQuestions.Clear(); foreach (Question q in tempQs) { var tq = new TestQuestion(); tq.Question = q; tq.Test = test; tq.QuestionId = q.QuestionId; if (!test.TestQuestions.ToList().Exists(x => x.QuestionId == q.QuestionId)) { test.TestQuestions.Add(tq); } } return(test); }
public Test CreateLimitedTest(Test test, List <Question> questions, int subTest, double moreTime, out string message) { test.CreatedBy = UserService.CurrentUser.CurrentUser.Username; test.CreatedDate = DateTime.Now; test.UpdatedBy = test.CreatedBy; test.UpdatedDate = DateTime.Now; foreach (Question q in questions) { var tq = new TestQuestion(); tq.Question = q; tq.Test = test; test.TestQuestions.Add(tq); } // create test copy List <long> questionIds = questions.Select(q => q.QuestionId).ToList(); Random ran = new Random(); //Toi da 1000 cau int max = 1000; for (int count = 0; count < subTest; count++) { XDocument structure = new XDocument(new XElement("TestCopy")); for (int i = 0; i < max; i++) { int index = ran.Next(questionIds.Count - 1); long item = questionIds[i % questionIds.Count]; questionIds[i % questionIds.Count] = questionIds[index]; questionIds[index] = item; } for (int i = 0; i < questionIds.Count; i++) { List <Answer> answers = m_questionService.GetAnswerOfQuestion(questionIds[i]); answers = RandomAnswerOrder(answers); List <XElement> answerList = new List <XElement>(); for (int j = 0; j < answers.Count; j++) { XElement answerElement = new XElement("A", new XAttribute("aOrder", j + 1), new XAttribute("aId", answers[j].AnswerId)); answerList.Add(answerElement); } structure.Element("TestCopy") .Add(new XElement("Q", new XAttribute("qOrder", i + 1), new XAttribute("qId", questionIds[i]), answerList)); } TestCopy tc = new TestCopy(); tc.Test = test; tc.TestCopyStructure = structure.ToString(); test.TestCopies.Add(tc); } double realTestTime = questions.Sum(q => q.Time); if (realTestTime > test.Time + moreTime) { message = "Thời gian chính xác để hoàn thành bài thi là: " + realTestTime + "\nThời gian thi do người dùng cài đặt + cấu hình (" + moreTime + " phút): " + (test.Time + moreTime) + ".\nThời gian chênh lệch: " + Math.Abs(realTestTime - (test.Time + moreTime)) + "\nBạn có muốn tiếp tục tạo đề thi không?"; } else { message = "Thời gian chính xác để hoàn thành bài thi là: " + realTestTime + "\nThời gian thi do người dùng cài đặt + cấu hình (" + moreTime + " phút): " + (test.Time + moreTime) + ".\nThời gian chênh lệch: " + Math.Abs(realTestTime - (test.Time + moreTime)) + "\nBạn có muốn tiếp tục tạo đề thi không?"; } return(test); }