123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- 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<CourseworkAddView> GetCourseworkList(string testname, int pageIndex, int pageSize)
- {
- Expression<Func<Coursework, bool>> 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<Coursework>().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;
- }
- /// <summary>
- /// 获取考试对应的试卷
- /// </summary>
- /// <param name="testid"></param>
- /// <returns></returns>
- public test_paper getPaper(decimal testid)
- {
- var q = CourseworkDAL.CourseworkRepository.UnitOfWork.Set<Coursework>().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<decimal> 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<decimal> ParsePaper(DataTable typeTb, DataTable paperTb, JavaScriptSerializer json)
- {
- var questionQuery = paperTb.AsEnumerable();
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- var questionset = new List<decimal>();
- 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));
- });
- questionset.AddRange(questionList.Select(q => q.question_id).ToList());
- }
- return questionset;
- }
- public bool TakePaper(decimal test_id, string userid, Dictionary<string, string> answers)
- {
- CourseworkDAL.AnswerRepository.UnitOfWork.Delete<CourseworkAnswer>(e => e.workid == test_id && e.user_id == userid);
- string errText = string.Empty;
- if (answers.Count > 0)
- {
- var json = new JavaScriptSerializer();
- List<AnswerXml> xmlValues = new List<AnswerXml>();
- 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<AnswerXml>));
- 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<CourseworkReviewView> GetReviewList(string testname, decimal workid, int pageIndex, int pageSize)
- {
- Expression<Func<CourseworkResult, bool>> 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 });
- }
- }
- }
|