Beispiel #1
0
        protected void btnImportPersonalityTestQuestionsSet_Click(object sender, EventArgs e)
        {
            var questionsSetList = new List <PersonalityTestQuestionsSet>();

            string dataDirectoryPath = MapPath("~/App_Data/PersonalityTest");

            string[] dataFilePaths = Directory.GetFiles(dataDirectoryPath, "QuestionsSet*.xml");
            foreach (string dataFilePath in dataFilePaths)
            {
                XmlDocument xd = new XmlDocument();
                xd.Load(dataFilePath);

                XmlElement xeQuestionsSet   = (XmlElement)xd.SelectSingleNode("QuestionsSet");
                string     questionsSetCode = xeQuestionsSet.GetAttribute("Code");

                PersonalityTestQuestionsSet questionsSet = new PersonalityTestQuestionsSet(questionsSetCode);

                foreach (XmlNode xnQuestion in xeQuestionsSet.SelectNodes("Question"))
                {
                    XmlElement xeQuestion = (XmlElement)xnQuestion;

                    int                questionCode    = int.Parse(xeQuestion.GetAttribute("Code"));
                    string             questionElement = xeQuestion.GetAttribute("Element");
                    PersonalityElement element         = (PersonalityElement)Enum.Parse(typeof(PersonalityElement), questionElement);

                    PersonalityTestQuestion question = new PersonalityTestQuestion(questionCode, element);

                    foreach (XmlNode xnChoiceScore in xeQuestion.SelectNodes("ChoiceScore"))
                    {
                        XmlElement xeChoiceScore = (XmlElement)xnChoiceScore;

                        string choice = xeChoiceScore.GetAttribute("Choice");
                        int    score  = int.Parse(xeChoiceScore.GetAttribute("Score"));

                        PersonalityTestQuestionChoiceScore choiceScore = new PersonalityTestQuestionChoiceScore(choice, score);
                        question.ChoiceScores.Add(choiceScore);
                    }

                    questionsSet.Questions.Add(question);
                }

                questionsSetList.Add(questionsSet);
            }

            if (questionsSetList.Count > 0)
            {
                using (var context = new ComputingServicesContext())
                {
                    foreach (var questionsSet in questionsSetList)
                    {
                        PersonalityTestQuestionsSet oldQuestionsSet = context.PersonalityTestQuestionsSets.SingleOrDefault(item => item.Code == questionsSet.Code);
                        if (oldQuestionsSet != null)
                        {
                            context.PersonalityTestQuestionsSets.Remove(oldQuestionsSet);
                        }
                        context.PersonalityTestQuestionsSets.Add(questionsSet);
                    }
                    context.SaveChanges();
                }

                ltlLog.Text = "成功完成。";
            }
            else
            {
                ltlLog.Text = "没有数据。";
            }
        }
        private PersonalityTestElementStandardResult GetPersonalityTestElementStandardResult(PersonalityTestPaperResult paperResult, PersonalityTestQuestionsSet questionsSet, PersonalityTestElementStandardParametersSet elementStandardParametersSet)
        {
            var dicElementOriginalValue = new Dictionary <Core.Domain.Models.PersonalityTest.PersonalityElement, int>();

            foreach (var questionAnswer in paperResult.QuestionAnswers)
            {
                var question = questionsSet.Questions.SingleOrDefault(item => item.Code == questionAnswer.QuestionCode);
                if (question == null)
                {
                    continue;
                }

                Core.Domain.Models.PersonalityTest.PersonalityElement element;
                if (!Enum.TryParse <Core.Domain.Models.PersonalityTest.PersonalityElement>(question.Element.ToString(), out element))
                {
                    throw new ArgumentException("Wrong Personality Element");
                }
                if (!dicElementOriginalValue.ContainsKey(element))
                {
                    dicElementOriginalValue.Add(element, 0);
                }

                foreach (var choiceScore in question.ChoiceScores)
                {
                    if (choiceScore.Choice == questionAnswer.Answer)
                    {
                        dicElementOriginalValue[element] += choiceScore.Score;
                        break;
                    }
                }
            }

            var elementStandardScoreList = new List <PersonalityTestElementStandardScore>();

            foreach (Core.Domain.Models.PersonalityTest.PersonalityElement element in dicElementOriginalValue.Keys)
            {
                int originalValue = dicElementOriginalValue[element];

                PersonalityTestElementStandardParameter elementStandardParameter = elementStandardParametersSet.Parameters.Single(item => item.Element == element);

                int standardScoreValue = elementStandardParameter.Segments.Single(item => originalValue >= item.OriginalScoreMin && originalValue <= item.OriginalScoreMax).StandardScore;

                PersonalityTestElementStandardScore elementStandardScore = new PersonalityTestElementStandardScore();
                App.Models.PersonalityElement       appElement;
                if (Enum.TryParse <App.Models.PersonalityElement>(element.ToString(), out appElement))
                {
                    elementStandardScore.Element       = appElement;
                    elementStandardScore.Value         = standardScoreValue;
                    elementStandardScore.OriginalValue = originalValue;

                    elementStandardScoreList.Add(elementStandardScore);
                }
                else
                {
                    throw new ArgumentException("Wrong Personality Element");
                }
            }

            PersonalityTestElementStandardResult elementStandardResult = new PersonalityTestElementStandardResult();

            elementStandardResult.Age    = paperResult.Age;
            elementStandardResult.Scores = elementStandardScoreList.ToArray();
            elementStandardResult.RefId  = paperResult.RefId;

            return(elementStandardResult);
        }