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.Educational; namespace EMISOnline.CommonLogic.ExamServices { public class ExamSettingServices : IExamSettingServices { public ExamSettingDAL ExamSettingDAL { get; set; } public TestPaperDAL PaperDAL { get; set; } public UserDAL UserDAL { get; set; } public EducationMissionClassDAL EducationMissionClassDAL { get; set; } public IGridResultSet GetOnlinetestList(string testname, int pageIndex, int pageSize) { Expression> exp = (x => true); if (!string.IsNullOrEmpty(testname)) exp = exp.And(q => q.test_name.Contains(testname)); return ExamSettingDAL.GetOnlinetestList(exp).Select(q => new OnlineTestView() { test_name = q.test_name, onlinetest_id = q.onlinetest_id, test_method_set = q.test_method_set_id == 0 ? "在线考试" : "现场考试", test_begin_date = q.test_begin_date, limited_minutes = q.limited_minutes, status = q.status_id == (int)OnlineTestStatus.公开成绩 ? "成绩已发布" : "成绩未发布" }).OrderBy(q => q.onlinetest_id).ToGridResultSet(pageIndex, pageSize); } public IGridResultSet GetStudentOnlinetestList(string testname, DateTime? beginTime, DateTime? endTime, string UserID, int pageIndex, int pageSize) { Expression> exp = (x => true); if (!string.IsNullOrEmpty(testname)) exp = exp.And(q => q.test_name.Contains(testname)); if (beginTime.HasValue) exp = exp.And(q => q.test_begin_date >= beginTime); if (endTime.HasValue) exp = exp.And(q => q.test_end_date <= endTime); //根据UserID,查询用户对应的年级专业 var Executable = EducationMissionClassDAL.GetEducationMissionClassByUserID(Guid.Parse(UserID)).Select(q => q.EducationMissionClassID).ToArray(); exp = exp.And(q => Executable.Contains(q.EducationMissionClassID.Value)); return ExamSettingDAL.GetStudentOnlinetestList(exp, UserID).Select(q => new OnlineTestView() { test_name = q.test_name, onlinetest_id = q.onlinetest_id, test_method_set = q.test_method_set_id == 0 ? "在线考试" : "现场考试", test_begin_date = q.test_begin_date, limited_minutes = q.limited_minutes, status = q.status_id == (int)OnlineTestStatus.公开成绩 ? "成绩已发布" : "成绩未发布" }).OrderBy(q => q.onlinetest_id).ToGridResultSet(pageIndex, pageSize); } public OnlineTestView GetOnlineTestByID(decimal test_id) { OnlineTestView test = new OnlineTestView(); var view = ExamSettingDAL.OnlinetestRepository.GetSingle(q => q.onlinetest_id == test_id); test.onlinetest_id = view.onlinetest_id; test.breaking_id = view.breaking_id; test.breaking_id = view.breaking_id; test.display_type = view.display_type; test.ExamerState = view.ExamerState; test.ExamID = view.ExamID; test.ISautoBegin = view.ISautoBegin; test.isControllable = view.isControllable; test.isOpenAnswer = view.isOpenAnswer; test.isPublishScore = view.isPublishScore; test.isReadPaper = view.isReadPaper; test.limited_minutes = view.limited_minutes; test.ModityContent = view.ModityContent; test.notice_datetime = view.notice_datetime; test.notice_method_id = view.notice_method_id; test.PassRate = view.PassRate; test.rand_test_count = view.rand_test_count; test.rand_test_point_setid = view.rand_test_point_setid; test.rand_test_point_type = view.rand_test_point_type; test.rand_Test_time = view.rand_Test_time; test.relogin_count = view.relogin_count; test.status_id = view.status_id; test.SubCode = view.SubCode; test.subjectiveQAutoRead = view.subjectiveQAutoRead; test.test_address = view.test_address; test.test_begin_date = view.test_begin_date; test.test_code = view.test_code; test.test_end_date = view.test_end_date; test.test_method_set_id = view.test_method_set_id; test.test_name = view.test_name; test.test_paper_id = view.test_paper_id; test.time_of_filling = view.time_of_filling; test.version = view.version; if (view.EducationMissionClassID.HasValue) { var executable = EducationMissionClassDAL.EducationMissionClassRepository.GetSingle(x => view.EducationMissionClassID == x.EducationMissionClassID); test.EducationMissionClassID = view.EducationMissionClassID.Value; test.ExecutablePlanData.Add(test.EducationMissionClassID.ToString(), executable.Name); } return test; } public bool ExamSettingSave(OnlineTestView view, string LoginID) { test_onlinetest test = new test_onlinetest(); if (view.onlinetest_id > 0) { test = ExamSettingDAL.OnlinetestRepository.GetSingle(q => q.onlinetest_id == view.onlinetest_id); test.breaking_id = view.breaking_id; test.breaking_id = view.breaking_id; test.display_type = view.display_type; test.ExamerState = view.ExamerState; test.ExamID = view.ExamID; test.ISautoBegin = view.ISautoBegin; test.isControllable = view.isControllable; test.isOpenAnswer = view.isOpenAnswer; test.isPublishScore = view.isPublishScore; test.isReadPaper = view.isReadPaper; test.limited_minutes = view.limited_minutes; test.ModityContent = view.ModityContent; test.notice_datetime = view.notice_datetime; test.notice_method_id = view.notice_method_id; test.PassRate = view.PassRate; test.rand_test_count = view.rand_test_count; test.rand_test_point_setid = view.rand_test_point_setid; test.rand_test_point_type = view.rand_test_point_type; test.rand_Test_time = view.rand_Test_time; test.relogin_count = view.relogin_count; test.status_id = view.status_id; test.SubCode = view.SubCode; test.subjectiveQAutoRead = view.subjectiveQAutoRead; test.test_address = view.test_address; test.test_begin_date = view.test_begin_date; test.test_code = view.test_code; test.test_end_date = view.test_end_date; test.test_method_set_id = view.test_method_set_id; test.test_name = view.test_name; test.test_paper_id = view.test_paper_id; test.time_of_filling = view.time_of_filling; test.version = view.version; test.EducationMissionClassID = view.EducationMissionClassID; } else { test.breaking_id = view.breaking_id; test.created_by = LoginID; test.created_date = DateTime.Now; test.display_type = view.display_type; test.ExamerState = view.ExamerState; test.ExamID = Guid.NewGuid(); test.ISautoBegin = view.ISautoBegin; test.isControllable = view.isControllable; test.isOpenAnswer = view.isOpenAnswer; test.isPublishScore = view.isPublishScore; test.isReadPaper = view.isReadPaper; test.limited_minutes = view.limited_minutes; test.ModityContent = view.ModityContent; test.notice_datetime = view.notice_datetime; test.notice_method_id = view.notice_method_id; test.PassRate = view.PassRate; test.rand_test_count = view.rand_test_count; test.rand_test_point_setid = view.rand_test_point_setid; test.rand_test_point_type = view.rand_test_point_type; test.rand_Test_time = view.rand_Test_time; test.relogin_count = view.relogin_count; test.status_id = view.status_id; test.SubCode = view.SubCode; test.subjectiveQAutoRead = view.subjectiveQAutoRead; test.test_address = view.test_address; test.test_begin_date = view.test_begin_date; test.test_code = view.test_code; test.test_end_date = view.test_end_date; test.test_method_set_id = view.test_method_set_id; test.test_name = view.test_name; test.test_paper_id = view.test_paper_id; test.time_of_filling = view.time_of_filling; test.version = view.version; test.EducationMissionClassID = view.EducationMissionClassID; ExamSettingDAL.OnlinetestRepository.UnitOfWork.Add(test); } ExamSettingDAL.OnlinetestRepository.UnitOfWork.Commit(); //WinUpdateExam(test); return true; } public object GetPaperList(string papername, int pageIndex, int pageSize) { Expression> exp = (x => true); if (!string.IsNullOrEmpty(papername)) exp = exp.And(q => q.PaperName.Contains(papername)); return ExamSettingDAL.GetPaperList(exp).ToList().Select(paper => new { paper.PaperName, paper.paper_score, Creator = paper.created_by, is_dynamic = paper.is_dynamic ? "是" : "否", created_date = paper.created_date.Value.ToString("yyyy-MM-dd"), paper.test_paper_id }).OrderBy(q => q.test_paper_id).AsQueryable().ToGridResultSet(pageIndex, pageSize); } /// /// 生成考生信息及试卷信息 /// /// /// public bool WinUpdateExam(test_onlinetest setting) { bool bool_rev = false; decimal testid = setting.onlinetest_id; bool isChangePaper = setting.ExamerState.HasValue ? (setting.ExamerState.Equals(2) ? true : false) : false;//是否修改试卷 InsertExamResult(testid); if (isChangePaper)//如果有更换考卷 { //删除考试结果 ExamSettingDAL.ExamineeAnswerRepository.UnitOfWork.Delete(q => q.onlinetest_id == testid); } setting.ExamerState = 1; ExamSettingDAL.OnlinetestRepository.UnitOfWork.Commit(); bool_rev = true; return bool_rev; } public void InsertExamResult(decimal testid) { var TestMans = ExamSettingDAL.GetExamUsers(testid); var TestOnlie = ExamSettingDAL.OnlinetestRepository.GetSingle(q => q.onlinetest_id == testid); List Mans = new List(); foreach (var Man in TestMans) { ExamResult result = new ExamResult() { user_id = Man.UserID.ToString(), test_name = TestOnlie.test_name, test_paper_id = TestOnlie.test_paper_id.Value, test_begin_date = TestOnlie.test_begin_date, test_end_date = TestOnlie.test_end_date, limited_minutes = TestOnlie.limited_minutes, test_address = TestOnlie.test_address, onlinetest_id = TestOnlie.onlinetest_id, OrgId = Man.ClassmajorID.ToString(), username = Man.StudentCardNo }; Mans.Add(result); } ExamSettingDAL.ExamResultRepository.UnitOfWork.AddRange(Mans); } public ExamResult GetExam(string userid, decimal test_id) { var exam = ExamSettingDAL.GetExam(userid, test_id); //首次进入考试,如果该考生拥有该场考试,则把该考生信息插入到考生信息表 if (exam == null) { Expression> exp = (x => x.onlinetest_id == test_id); //根据UserID,查询用户对应的年级专业 var Executable = EducationMissionClassDAL.GetEducationMissionClassByUserID(Guid.Parse(userid)).Select(q => q.EducationMissionClassID).ToArray(); exp = exp.And(q => Executable.Contains(q.EducationMissionClassID.Value)); var test = ExamSettingDAL.GetStudentOnlinetestList(exp, userid).FirstOrDefault(); if (test != null) { exam = new ExamResult() { is_auto_read = test.isReadPaper, test_name = test.test_name, test_paper_id = test.test_paper_id, test_begin_date = test.test_begin_date, limited_minutes = test.limited_minutes, test_end_date = test.test_end_date, onlinetest_id = test.onlinetest_id, user_id = userid, filled = false, }; ExamSettingDAL.ExamResultRepository.UnitOfWork.Add(exam); ExamSettingDAL.ExamResultRepository.UnitOfWork.Commit(); exam = ExamSettingDAL.GetExam(userid, test_id); } } return exam; } public Examinee_ExamStatus GetExamStartLength(decimal test_id, string userid, out double limit) { limit = 0; var testman = ExamSettingDAL.GetExam(userid, test_id); var now = DateTime.Now; if (testman == null) return Examinee_ExamStatus.不能参加该场考试; if (testman.test_onlinetest.status_id == 0) return Examinee_ExamStatus.考试未开始; if (testman.test_onlinetest.test_method_set_id.Value == 1) { // 现场考试 if (now > testman.test_begin_date.Value.AddMinutes(testman.limited_minutes.Value)) return Examinee_ExamStatus.考试已结束; if (testman.state >= 2) return Examinee_ExamStatus.已交卷; } else { if (testman.test_end_date.HasValue && testman.test_end_date < now && !testman.lastLoginTime.HasValue) return Examinee_ExamStatus.考试已结束; // 在线考试不受考试时间限制 if (testman.test_end_date.HasValue && testman.test_begin_date.HasValue && now > testman.test_end_date.Value.AddMinutes(testman.limited_minutes.Value)) return Examinee_ExamStatus.考试已结束; } //if (testman.reloginCount >= testman.test_onlinetest.relogin_count) // return Examinee_ExamStatus.登录次数已大于规定的次数; limit = (testman.test_begin_date.Value - DateTime.Now).TotalSeconds; return Examinee_ExamStatus.考试中; } /// /// 获取考试对应的试卷 /// /// /// public test_paper getPaper(decimal testid) { var q = ExamSettingDAL.OnlinetestRepository.UnitOfWork.Set().Include("test_paper").Single(qx => qx.onlinetest_id == testid).test_paper; return q; } public bool TakePaper(decimal test_id, string userid, string answer) { ExamSettingDAL.ExamineeAnswerRepository.UnitOfWork.Delete(e => e.onlinetest_id == test_id && e.user_id == userid); string errText = string.Empty; if (!string.IsNullOrEmpty(answer)) { var json = new JavaScriptSerializer(); var answers = json.Deserialize(answer); 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 ExamineeAnswer() { ID = Guid.NewGuid(), onlinetest_id = test_id, user_id = userid, resultXml = XElement.Parse(Encoding.UTF8.GetString(mstream.ToArray())).ToString() }; errText += "2-----\r\n"; errText = examAnswer.resultXml.ToString(); ExamSettingDAL.ExamineeAnswerRepository.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 = PaperDAL.ExamResultRepository.GetSingle(m => m.user_id == userid && m.onlinetest_id == test_id); if (test_man != null) { test_man.state = 2; test_man.test_end_date = DateTime.Now; } PaperDAL.ExamResultRepository.UnitOfWork.Commit(); return true; } } public class answerEntry { public string key { get; set; } public string value { get; set; } } }