using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMISOnline.DataLogic.ExamSetting; using System.Web.Script.Serialization; using System.Collections; using EMISOnline.ViewModel.ExamView; using Bowin.Web.Controls.Mvc; using System.Data; namespace EMISOnline.CommonLogic.ExamServices { public class QuestionServices : IQuestionServices { public QuestionDAL QuestionDAL { get; set; } public Entities.test_base_question_type[] ListAllQuestionType() { return QuestionDAL.ListAllQuestionType(); } public decimal[] GeneratePractice(string[] libaryids, List typeID, int? num) { return QuestionDAL.GeneratePractice(libaryids, typeID, num); } public string GetQuestion(decimal question_id) { var question = QuestionDAL.GetQuestion(new decimal[] { question_id }); var js = new JavaScriptSerializer(); return js.Serialize(question); } public string ShowPaperQuestion(decimal[] QeustionSet, Dictionary Answers) { var questions = QuestionDAL.GetPaperQuestion(Answers.Keys.Select(q => decimal.Parse(q)).ToArray()).ToList(); var questionTypes = QuestionDAL.ListAllQuestionType(); var result = new ArrayList(); questions.ForEach(q => { q.userAnswer = Answers[q.question_id.ToString()]; ReadQuestion(q, q.userAnswer, (int)q.question_typeid); }); foreach (var type in questionTypes) { if (questions.Any(q => q.question_typeid == type.base_question_type_id)) result.Add(new { id = type.base_question_type_id, name = type.Name, questions = questions }); } var data = new { score = questions.Sum(q => q.score),//试题总分 studentscore = questions.Sum(q => q.userscore), //你的得分 questionnumber = QeustionSet.Count(),//试题总数 rightnumber = questions.Count(q => q.userscore > 0),//答对题数 errornumber = questions.Count(q => q.userscore == 0 && !string.IsNullOrEmpty(q.userAnswer)),//答错题数 unquestion = QeustionSet.Count() - Answers.Count,//未答题数 }; var js = new JavaScriptSerializer(); return js.Serialize(new { data = data, result = result }); } private void ReadQuestion(PaperQuestionView question, string useranser, int questionType) { var isRight = false; switch (questionType) { case 1: isRight = question.rightAnswer == useranser.Trim(); break; case 2: var provid = question.answers.Select(q => true).ToArray(); isRight = string.Join("|", provid) == useranser.Trim(); break; case 3: isRight = question.rightAnswer == OrderAnswer(useranser); break; case 4: isRight = question.rightAnswer == useranser.Trim(); break; default: break; } if (isRight) question.userscore = question.score; } private string OrderAnswer(string answer) { var answerArr = answer.Split('|'); return string.Join(",", answerArr.OrderBy(q => q).ToArray()); } public List ListQuesLibTreeStu() { List rtn = new List(); var TreeDT = QuestionDAL.ListQuesLibTreeStu(); var query = TreeDT.AsEnumerable().Where(e => e.Field("parent_id") == 1); foreach (var node in query.ToList()) { TreeItem item = new TreeItem(); item.id = node.Field("test_question_libary_id").ToString(); item.text = node.Field("name").ToString(); ; item.attributes = ""; item.children = CreateChildTree(TreeDT, node.Field("test_question_libary_id")); rtn.Add(item); } return rtn; } public List CreateChildTree(DataTable TreeDT, decimal parent_id) { List rtn = new List(); var query = TreeDT.AsEnumerable().Where(e => e.Field("parent_id") == parent_id); foreach (var node in query.ToList()) { TreeItem item = new TreeItem(); item.id = node.Field("test_question_libary_id").ToString(); item.text = node.Field("name").ToString(); ; item.attributes = ""; item.children = CreateChildTree(TreeDT, node.Field("test_question_libary_id")); rtn.Add(item); } return rtn; } } }