/// <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);
        }
Exemple #2
0
        public MultiTest CreateMultiTest(MultiTest test, object classForMultiTest, Dictionary <int, List <Tuple <QuestionLevel, int> > > questionsNo, int copyNos, int totalQuestion, int purpose, double moreTime, out string message)
        {
            List <Question> questions = new List <Question>();

            foreach (int subjectId in questionsNo.Keys)
            {
                foreach (var questionNo in questionsNo[subjectId])
                {
                    questions.AddRange(RandomQuestion(subjectId, questionNo.Item1, questionNo.Item2, purpose));
                }
            }
            test.CreatedBy   = UserService.CurrentUser.CurrentUser.Username;
            test.CreatedDate = DateTime.Now;
            test.UpdatedBy   = test.CreatedBy;
            test.UpdatedDate = DateTime.Now;
            test.IsMixedTest = false;
            if (classForMultiTest != null)
            {
                test.FK_ClassId = (int)classForMultiTest;
            }

            foreach (Question question in questions)
            {
                MultiTestQuestion mtq = new MultiTestQuestion();
                mtq.Question = question;
                //mtq.MultiTest = test;
                test.MultiTestQuestions.Add(mtq);
            }
            // create test copy
            List <long> questionIds = questions.Select(q => q.QuestionId).ToList();
            Random      ran         = new Random();
            int         max         = 1000;

            for (int count = 0; count < copyNos; count++)
            {
                XDocument structure = new XDocument(new XElement("MultiTestCopy"));
                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]);
                    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("MultiTestCopy")
                    .Add(new XElement("Q", new XAttribute("qOrder", i + 1), new XAttribute("qId", questionIds[i]),
                                      answerList));
                }
                MultiTestCopy tc = new MultiTestCopy();
                //tc.MultiTest = test;
                tc.MultiTestCopyStructure = structure.ToString();
                test.MultiTestCopies.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 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?";
            }
            return(test);
        }