using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMISOnline.DataLogic.ExamSetting; using Bowin.Common.Linq.Entity; using EMISOnline.ViewModel.ExamView; using System.Linq.Expressions; using EMISOnline.Entities; using System.Linq.Expressions; using Bowin.Common.Linq; using EMISOnline.ViewModel; using Bowin.Common; using System.Web.Script.Serialization; using System.Xml.Serialization; using System.IO; using System.Xml.Linq; using EMISOnline.DataLogic.SystemDAL; using EMISOnline.DataLogic.CourseworkMgr; using System.Data; using EMISOnline.CommonLogic.ExamServices; using EMISOnline.ViewModel.Coursework; using System.Collections; namespace EMISOnline.CommonLogic.CourseworkServices { public class CourseworkServices : ICourseworkServices { public CourseworkDAL CourseworkDAL { get; set; } public QuestionDAL QuestionDAL { get; set; } public IGridResultSet GetCourseworkList(string testname, int pageIndex, int pageSize) { Expression> exp = (x => true); if (!string.IsNullOrEmpty(testname)) exp = exp.And(q => q.WorkName.Contains(testname)); return CourseworkDAL.GetCourseworkList(exp).Select(q => q).OrderByDescending(q => q.test_begin_date).ToGridResultSet(pageIndex, pageSize); } public bool ExamSettingSave(CourseworkAddView view, string LoginID) { Coursework test = new Coursework(); if (view.id > 0) { test = CourseworkDAL.CourseworkRepository.GetSingle(q => q.ID == view.id); test.created_by = LoginID; test.created_date = DateTime.Now.ToString("yyyy-MM-dd"); test.EndTime = view.test_end_date; test.BeginTime = view.test_begin_date; test.PaperID = view.test_paper_id; test.WorkName = view.WorkName; test.ModityContent = view.ModityContent; test.EducationMissionClassID = view.EducationMissionClassID; } else { test.created_by = LoginID; test.created_date = DateTime.Now.ToString("yyyy-MM-dd"); test.EndTime = view.test_end_date; test.BeginTime = view.test_begin_date; test.PaperID = view.test_paper_id; test.WorkName = view.WorkName; test.ModityContent = view.ModityContent; test.EducationMissionClassID = view.EducationMissionClassID; CourseworkDAL.CourseworkRepository.UnitOfWork.Add(test); } CourseworkDAL.CourseworkRepository.UnitOfWork.Commit(); return true; } public CourseworkAddView getCourseWork(decimal workid) { var q = CourseworkDAL.CourseworkRepository.UnitOfWork.Set().Include(x => x.test_paper).Single(qx => qx.ID == workid); CourseworkAddView view = new CourseworkAddView(); view.id = q.ID; view.test_begin_date = q.BeginTime; view.test_end_date = q.EndTime; view.test_paper_id = q.PaperID; view.WorkName = q.WorkName; view.test_paper_name = q.test_paper.PaperName; view.PaperData.Add(q.PaperID.ToString(), q.test_paper.PaperName); view.ModityContent = q.ModityContent; return view; } /// /// 获取考试对应的试卷 /// /// /// public test_paper getPaper(decimal testid) { var q = CourseworkDAL.CourseworkRepository.UnitOfWork.Set().Include(x => x.test_paper).Single(qx => qx.ID == testid).test_paper; return q; } public bool UpdatePaperID(decimal test_id, string userid, decimal newPaperid) { return CourseworkDAL.UpdatePaperID(test_id, userid, newPaperid); } public List PaperBuilder(decimal workid, decimal paper_id) { DataSet ds = CourseworkDAL.PaperBuilder(workid, paper_id); var json = new JavaScriptSerializer(); var questionTb = ds.Tables[0]; var typeTb = ds.Tables[1]; var result = new StringBuilder(); var questionset = ParsePaper(typeTb, questionTb, json); return questionset; } private List ParsePaper(DataTable typeTb, DataTable paperTb, JavaScriptSerializer json) { var questionQuery = paperTb.AsEnumerable(); JavaScriptSerializer serializer = new JavaScriptSerializer(); var questionset = new List(); foreach (DataRow typeRow in typeTb.Rows) { var typeid = (decimal)typeRow["base_question_type_id"]; var name = typeRow["Name"] as string; var query = from questions in questionQuery where questions.Field("base_question_type_id") == typeid select questions.Field("question_json"); var questionList = new List(); query.ToList().ForEach(q => { questionList.Add(serializer.Deserialize(q)); }); questionset.AddRange(questionList.Select(q => q.question_id).ToList()); } return questionset; } public bool TakePaper(decimal test_id, string userid, Dictionary answers) { CourseworkDAL.AnswerRepository.UnitOfWork.Delete(e => e.workid == test_id && e.user_id == userid); string errText = string.Empty; if (answers.Count > 0) { var json = new JavaScriptSerializer(); List xmlValues = new List(); foreach (var a in answers) { decimal question_id = 0; if (decimal.TryParse(a.Key, out question_id)) { xmlValues.Add(new AnswerXml() { act_score = 0, answers = a.Value, memo = "", src_score = 0, test_question_Id = question_id }); } } try { errText += "0-----\r\n"; XmlSerializer serializer = new XmlSerializer(typeof(List)); MemoryStream mstream = new MemoryStream(); serializer.Serialize(mstream, xmlValues); mstream.Close(); errText += "1-----\r\n"; errText += Encoding.Default.GetString(mstream.ToArray()); var examAnswer = new CourseworkAnswer() { workid = test_id, user_id = userid, resultXml = XElement.Parse(Encoding.UTF8.GetString(mstream.ToArray())).ToString() }; errText += "2-----\r\n"; errText = examAnswer.resultXml.ToString(); CourseworkDAL.AnswerRepository.UnitOfWork.Add(examAnswer); } catch (Exception ex) { //System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.Current.Request.PhysicalApplicationPath + "\\lingx.txt", true, System.Text.Encoding.UTF8); //sw.WriteLine(DateTime.Now.ToString()); //sw.WriteLine(errText); ; //sw.Close(); } } var test_man = CourseworkDAL.ResultRepository.GetSingle(m => m.user_id == userid && m.workid == test_id); if (test_man != null) { test_man.state = 2; test_man.test_end_date = DateTime.Now; } CourseworkDAL.ResultRepository.UnitOfWork.Commit(); return true; } public IGridResultSet GetReviewList(string testname, decimal workid, int pageIndex, int pageSize) { Expression> exp = (x => x.workid == workid); if (!string.IsNullOrEmpty(testname)) exp = exp.And(q => q.Coursework.WorkName.Contains(testname)); return CourseworkDAL.GetReviewList(exp).OrderBy(q => q.ResultID).ToGridResultSet(pageIndex, pageSize); } public CourseworkResult GetCourseWorkResult(decimal ResultID) { return CourseworkDAL.ResultRepository.GetSingle(q => q.ID == ResultID); } public string ShowPaperQuestion(decimal workid, decimal paper_id) { var QeustionSet = PaperBuilder(workid, paper_id); var questions = QuestionDAL.GetPaperQuestion(QeustionSet.ToArray()).ToList(); var questionTypes = QuestionDAL.ListAllQuestionType(); var Answers = CourseworkDAL.ExamineeAnswers(workid).ToDictionary(q => q.test_question_Id, q => q.answers); var result = new ArrayList(); questions.ForEach(q => { q.userAnswer = Answers.ContainsKey(q.question_id) ? Answers[q.question_id] : ""; //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() - QeustionSet.Count,//未答题数 }; var js = new JavaScriptSerializer(); return js.Serialize(new { data = data, result = result }); } } }