123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using EMISOnline.Entities;
- using System.Xml.Linq;
- using EMISOnline.ViewModel.ExamView;
- using System.Web.Script.Serialization;
- using EMISOnline.DataLogic.ExamSetting;
- using EMISOnline.ViewModel;
- using System.Data;
- using System.Collections;
- namespace EMISOnline.CommonLogic.ExamServices
- {
- public class TestPaperServices : ITestPaperServices
- {
- public TestPaperDAL TestPaperDAL { get; set; }
- /// <summary>
- /// 创建动态子卷
- /// </summary>
- /// <param name="o_paperid"></param>
- /// <returns></returns>
- public decimal CreateSinglePaper(decimal testid, test_paper t_paper)
- {
- decimal paperid = 0;
- if (t_paper != null)
- {
- string libs = t_paper.question_libary_set_id;
- decimal diff_min = t_paper.difficulty_degree_minvalue.HasValue ? t_paper.difficulty_degree_minvalue.Value : 0m;
- decimal diff_max = t_paper.difficulty_degree_maxvalue.HasValue ? t_paper.difficulty_degree_maxvalue.Value : 0m;
- decimal wrong_rate = t_paper.error_percent.HasValue ? t_paper.error_percent.Value : 0m;
- int usedcount_min = t_paper.used_count_min.HasValue ? t_paper.used_count_min.Value : 0;
- int usedcount_max = t_paper.used_count_max.HasValue ? t_paper.used_count_max.Value : 0;
- int prority = t_paper.Prority.HasValue ? t_paper.Prority.Value : 0;
- var ques_setting = new JavaScriptSerializer().Deserialize<QuestionSettingJsonObj[]>(t_paper.question_setting);
- //新试卷
- test_paper newpaper = new test_paper();
- newpaper.created_by = t_paper.created_by;
- newpaper.created_date = t_paper.created_date;
- newpaper.difficulty_degree_maxvalue = t_paper.difficulty_degree_maxvalue;
- newpaper.difficulty_degree_minvalue = t_paper.difficulty_degree_minvalue;
- newpaper.distributing_option = t_paper.distributing_option;
- newpaper.error_percent = t_paper.error_percent;
- newpaper.is_autogenerate = true;
- newpaper.is_dynamic = false;
- newpaper.is_vaild = t_paper.is_vaild;
- newpaper.paper_score = t_paper.paper_score;
- newpaper.PaperName = string.Format("{0}", t_paper.PaperName);
- newpaper.Prority = t_paper.Prority;
- newpaper.question_libary_set_id = t_paper.question_libary_set_id;
- newpaper.question_setting = t_paper.question_setting;
- newpaper.question_update_date_max = t_paper.question_update_date_max;
- newpaper.question_update_date_min = t_paper.question_update_date_min;
- newpaper.test_paper_parent_id = t_paper.test_paper_id;
- newpaper.used_count = t_paper.used_count;
- newpaper.used_count_max = t_paper.used_count_max;
- newpaper.used_count_min = t_paper.used_count_min;
- List<XElement> list = new List<XElement>();
- foreach (var t in ques_setting)
- {
- XElement x = new XElement("QType");
- x.SetAttributeValue("QTypeID", t.QType);
- x.SetAttributeValue("PSum", t.PSum);
- x.SetAttributeValue("TSum", t.TSum);
- x.SetAttributeValue("QScore", t.QScore);
- list.Add(x);
- }
- XElement xml = new XElement("paper_set", list);
- newpaper.question_setting_xml = xml.ToString();
- TestPaperDAL.PaperRepository.UnitOfWork.Add(newpaper);
- TestPaperDAL.PaperRepository.UnitOfWork.Commit();
- paperid = newpaper.test_paper_id;
- ////筛选题目并存储关联数据
- TestPaperDAL.SetQuestion(paperid, diff_min, diff_max, usedcount_min, usedcount_max, wrong_rate);
- //context.SubmitChanges();
- }
- return paperid;
- }
- public bool UpdatePaperID(decimal test_id, string userid, decimal newPaperid)
- {
- return TestPaperDAL.UpdatePaperID(test_id, userid, newPaperid);
- }
- public void RootReadPaper(decimal testid, string userid)
- {
- TestPaperDAL.RootReadPaper(testid, userid);
- }
- public bool IsControllable(decimal id, out int test_method)
- {
- test_method = 0;
- var result = TestPaperDAL.onlinetestRepository.Entities.Where(t => t.onlinetest_id == id).Select(t => new { t.isControllable, t.test_method_set_id }).FirstOrDefault();
- if (result == null)
- return false;
- test_method = (int)(result.test_method_set_id.HasValue ? result.test_method_set_id.Value : 0);
- return result.isControllable ?? false;
- }
- public bool UpdateExamState(decimal test_id, string userid)
- {
- if (GetExamStatus(test_id, userid) == Examinee_ExamStatus.考试中)
- {
- var query = from test in TestPaperDAL.ExamResultRepository.Entities
- where test.onlinetest_id == test_id
- where test.user_id == userid
- select test;
- var testman = query.FirstOrDefault();
- var onlinetest = TestPaperDAL.onlinetestRepository.Entities.Where(q => q.onlinetest_id == test_id).FirstOrDefault();
- testman.relogin_count = (testman.relogin_count ?? 0) + 1;
- //if (testman.test_onlinetest.test_method_set_id.Value != 0 && testman.state != 2)
- //{
- testman.state = 1;
- //}
- testman.lastLoginTime = DateTime.Now;
- // 原设计仅在第一次进入是设定开始时间,重新进入则从这个时间计时
- //if (testman.relogin_count == 1)
- if (onlinetest.test_method_set_id == 0)
- {
- testman.test_begin_date = testman.lastLoginTime;
- testman.test_end_date = null;
- }
- TestPaperDAL.manRepository.UnitOfWork.Commit();
- return true;
- }
- return false;
- }
- public Examinee_ExamStatus GetExamStatus(decimal test_id, string userid)
- {
- var query = from man in TestPaperDAL.ExamResultRepository.Entities
- join test in TestPaperDAL.onlinetestRepository.Entities on man.onlinetest_id equals test.onlinetest_id
- where man.onlinetest_id == test_id
- where man.user_id == userid
- select new
- {
- status_id = test.status_id,
- beginTime = test.test_begin_date,
- endTime = test.test_end_date,
- loginCount = test.relogin_count,
- reloginCount = man.relogin_count,
- lastLoginTime = man.lastLoginTime,
- testMethod = test.test_method_set_id,
- limited = man.limited_minutes,
- manBeginTime = man.test_begin_date,
- state = man.state
- };
- var testman = query.FirstOrDefault();
- var now = DateTime.Now;
- if (testman == null)
- return Examinee_ExamStatus.不能参加该场考试;
- if (testman.status_id == 0 || testman.beginTime > now)
- return Examinee_ExamStatus.考试未开始;
- if (testman.testMethod.Value == 1)
- {
- // 现场考试
- if (now > testman.beginTime.Value.AddMinutes(testman.limited.Value))
- return Examinee_ExamStatus.考试已结束;
- // 现场考试需要检查是否已交卷,在线考试无需判断
- if (testman.state >= 2)
- return Examinee_ExamStatus.已交卷;
- }
- else
- {
- if (testman.endTime < now && !testman.lastLoginTime.HasValue)
- return Examinee_ExamStatus.考试已结束;
- //现在不用判断考试时长
- //if (testman.manBeginTime.HasValue && now > testman.manBeginTime.Value.AddMinutes(testman.limited.Value))
- // return Examinee_ExamStatus.考试已结束;
- }
- if (testman.reloginCount >= testman.loginCount)
- return Examinee_ExamStatus.登录次数已大于规定的次数;
- return Examinee_ExamStatus.考试中;
- }
- public void AddExchange(decimal test_id, int type, int receiveType, string receiver, int extendIntData)
- {
- TestPaperDAL.AddExchange(test_id, type, receiveType, receiver, extendIntData);
- }
- public ExamResult GetExam(string userid, decimal test_id)
- {
- return TestPaperDAL.ExamResultRepository.GetSingle(q => q.onlinetest_id == test_id && q.user_id == userid);
- }
- public double GetExamLastLength(decimal test_id, string userid)
- {
- var result = TestPaperDAL.ExamResultRepository.GetSingle(q => q.onlinetest_id == test_id && q.user_id == userid);
- return (result.test_begin_date.Value.AddMinutes(result.limited_minutes.Value) - DateTime.Now).TotalSeconds;
- }
- public object PaperBuilder(decimal test_id, decimal paper_id)
- {
- DataSet ds = TestPaperDAL.PaperBuilder(test_id, paper_id);
- var json = new JavaScriptSerializer();
- var questionTb = ds.Tables[0];
- var typeTb = ds.Tables[1];
- var examTb = ds.Tables[2];
- var waringTb = ds.Tables[3];
- var result = new StringBuilder();
- var examInfo = ParseExamInfo(examTb, questionTb.Rows.Count, json);
- var paper = ParsePaper(typeTb, questionTb, json);
- //result.AppendFormat("var examInfo = {0};", ParseExamInfo(examTb, questionTb.Rows.Count, json));
- //result.AppendFormat("var paper = [{0}];", ParsePaper(typeTb, questionTb, json));
- //result.Append(ParseWaring(waringTb, json));
- return new { examInfo = examInfo, paper = paper, allWarring = getWaring(waringTb.AsEnumerable(), 2, json), personWarring = getWaring(waringTb.AsEnumerable(), 1, json) }; ;
- }
- private object ParseExamInfo(DataTable examTb, int questionCount, JavaScriptSerializer json)
- {
- var examRow = examTb.Rows[0];
- var examInfo = new
- {
- name = examRow["test_name"].ToString(),
- test_id = (decimal)examRow["onlinetest_id"],
- begin = (DateTime)examRow["test_begin_date"],
- end = (DateTime)examRow["test_end_date"],
- limited = (int)examRow["limited_minutes"],
- breaking = (short)examRow["breaking_id"],
- display_type = examRow["display_type"].ToString(),
- //controllable = examRow["isControllable"] == DBNull.Value ? false : (bool)examRow["isControllable"],
- questionCount = questionCount
- };
- return examInfo;
- }
- private object ParsePaper(DataTable typeTb, DataTable paperTb, JavaScriptSerializer json)
- {
- var buff = new StringBuilder();
- var questionQuery = paperTb.AsEnumerable();
- ArrayList list = new ArrayList();
- JavaScriptSerializer serializer =new JavaScriptSerializer();
- 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<decimal>("base_question_type_id") == typeid
- select questions.Field<string>("question_json");
- var questionList = new List<QuestionDetailView>();
- query.ToList().ForEach(q =>
- {
- questionList.Add(serializer.Deserialize<QuestionDetailView>(q));
- });
- if (questionList.Count > 0)
- {
- if (buff.Length > 0)
- buff.Append(",");
- //buff.AppendFormat("{{id: {0},name: {1},questions:[{2}]}}",
- // typeid, json.Serialize(name), string.Join(",", questionList));
- list.Add(new { id = typeid, name = name, questions = questionList });
- }
- }
- return list;
- }
- private object ParseWaring(DataTable waringTb, JavaScriptSerializer json)
- {
- var buff = new StringBuilder();
- var query = waringTb.AsEnumerable();
- //buff.AppendFormat("var allWarring = {0};", getWaring(query, 2, json));
- //buff.AppendFormat("var personWarring = {0};", getWaring(query, 1, json));
- return new { allWarring = getWaring(query, 2, json), personWarring = getWaring(query, 1, json) };
- }
- private object getWaring(EnumerableRowCollection<DataRow> query, int type, JavaScriptSerializer json)
- {
- var result = query.Where(q => q.Field<int>("AlertType") == type).Select(q => new
- {
- key = q.Field<int>("ID"),
- value = q.Field<string>("Message")
- });
- return result.ToArray();
- }
- public test_onlinetest GetOnlineTest(decimal id)
- {
- return TestPaperDAL.onlinetestRepository.Entities.Single(q => q.onlinetest_id == id);
- }
- public Examinee_ExamStatus GetExamStartLength(decimal test_id, string userid, out double limit)
- {
- limit = 0;
- var query = from man in TestPaperDAL.ExamResultRepository.Entities
- join test in TestPaperDAL.onlinetestRepository.Entities on man.onlinetest_id equals test.onlinetest_id
- where man.onlinetest_id == test_id
- where man.user_id == userid
- select new
- {
- status_id = test.status_id,
- beginTime = test.test_begin_date,
- endTime = test.test_end_date,
- loginCount = test.relogin_count,
- reloginCount = man.relogin_count,
- lastLoginTime = man.lastLoginTime,
- testMethod = test.test_method_set_id,
- limited = man.limited_minutes,
- manBeginTime = man.test_begin_date,
- state = man.state
- };
- var testman = query.FirstOrDefault();
- var now = DateTime.Now;
- if (testman == null)
- return Examinee_ExamStatus.不能参加该场考试;
- if (testman.status_id == 0)
- return Examinee_ExamStatus.考试未开始;
- if (testman.testMethod.Value == 1)
- {
- // 现场考试
- if (now > testman.beginTime.Value.AddMinutes(testman.limited.Value))
- return Examinee_ExamStatus.考试已结束;
- if (testman.state >= 2)
- return Examinee_ExamStatus.已交卷;
- }
- else
- {
- if (testman.endTime < now && !testman.lastLoginTime.HasValue)
- return Examinee_ExamStatus.考试已结束;
- //在线考试不受考试时间限制
- //if (testman.manBeginTime.HasValue && now > testman.manBeginTime.Value.AddMinutes(testman.limited.Value))
- // return Examinee_ExamStatus.考试已结束;
- }
- if (testman.reloginCount >= testman.loginCount)
- return Examinee_ExamStatus.登录次数已大于规定的次数;
- limit = (testman.beginTime.Value - DateTime.Now).TotalSeconds;
- return Examinee_ExamStatus.考试中;
- }
- public string ListExchange(decimal test_id, int receiveType, string receiver, DateTime? sendTime)
- {
- var query = TestPaperDAL.ListExchange(test_id, receiveType, receiver, sendTime);
- JavaScriptSerializer serial = new JavaScriptSerializer();
- return serial.Serialize(query);
- }
- }
- }
|