今日任务:
1、自动组卷
页面效果:
核心代码:
/// <summary>
/// 自动组卷-视图
/// </summary>
/// <returns></returns>
public ActionResult AddPaper()
{
ViewBag.Course = GetCourseInfoForDDL();
ViewBag.PaperLibrary = GetPaperLibraryForDDL();
return View();
}
/// <summary>
/// 自动组卷-功能实现
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public ActionResult AddPaper(FormCollection frm)
{
int PaperLibrary_Id = Convert.ToInt32(frm["PaperLibrary_Id"]);
using (OnLineExamDB db = new OnLineExamDB())
{
//修改试卷考试时长
using (TransactionScope transaction = new TransactionScope())
{
PaperLibrary pl = db.PaperLibrary.First(a => a.PaperLibrary_Id == PaperLibrary_Id);
pl.Duration = Convert.ToInt32(frm["Duration"]);
db.SaveChanges();
//根据设置的科目数量生成相应的试题
string couse_Ids = frm["couse_Id"].ToString();//获取传递来的课程编号
string[] ids = couse_Ids.Split(',');
foreach (string item in ids)
{
int id = Convert.ToInt32(item);
List<QuestionBank> listBank = db.QuestionBank.Where(a => a.course_Id == id).ToList();
List<QuestionBank> newListBank = new List<QuestionBank>();
//创建随机数
Random r = new Random();
//获取该科目的数量
int course_count = Convert.ToInt32(frm["course_counts " + id]);
int[] result = new int[course_count];//定义一个数组用来保存生成过的数
for (int i = 0; i < course_count; i++)//循环遍历5个数值
{
int m = r.Next(1, listBank.Count);//产生随机值
if (!result.Contains(m))//判断产生的新数是否存在result中,如果不存在则添加,否则重新生成
{
result[i] = m;
//取出listBank中第m道题,并添加到试卷表中
Paper p = new Paper();
p.createTime = DateTime.Now;
p.PaperLibrary_Id = PaperLibrary_Id;
p.question_Id = listBank[m].question_Id;
p.status = 1;
db.Paper.Add(p);
db.SaveChanges();
}
else
{
i--;
}
}
}
transaction.Complete();
}
}
ViewBag.Course = GetCourseInfoForDDL();
ViewBag.PaperLibrary = GetPaperLibraryForDDL();
return View();
}
暂无问题