抽题系统核心。
前一阵子开发了一个小项目,做了一个抽题系统,主要功能就是根据题目类型、难易程度、题量和分数来抽取对应的题目,生成一套带有答案的试题,话不多说先看实例:
function super(){
for($i=0;$i<101;$i++)
$sum[]=$i;
$keres=Db::table('ke')->select();
if(isset($_POST['sub'])){
//接收难度系数比,判断是否和为1
$nan=$_POST['nan']/10;
$zhong=$_POST['zhong']/10;
$yi=$_POST['yi']/10;
if($nan+$zhong+$yi!=1)
echo "<script>alert('难度系数比错误,请重新选择!');location.href='super/super'</script>";
//判断是否选择了科目
if(empty($_POST['ke']))
echo "<script>alert('请选择科目!');location.href='super/super'</script>";
//每个题目的数量
$danxuansum=$_POST['danxuan'];
$duoxuansum=$_POST['duoxuan'];
$panduansum=$_POST['panduan'];
$tiankongsum=$_POST['tiankong'];
$datisum=$_POST['dati'];
$shuliang=$danxuansum;
$shuliang.=','.$duoxuansum;
$shuliang.=','.$panduansum;
$shuliang.=','.$tiankongsum;
$shuliang.=','.$datisum;
//判断是否选择了题型
if($danxuansum==0&&$duoxuansum==0&&$panduansum==0&&$tiankongsum==0&&$datisum==0)
echo "<script>alert('请至少选择一种题型!');location.href='super/super'</script>";
//接受所有题目的分数
$danxuanfs=$_POST['danxuanfs'];
$duoxuanfs=$_POST['duoxuanfs'];
$panduanfs=$_POST['panduanfs'];
$tiankongfs=$_POST['tiankongfs'];
$datifs=$_POST['datifs'];
//计算分数是否正确
$zongfen=$_POST['zongfen'];
$zf=$zongfen;
$jianyan=$danxuansum*$danxuanfs+$duoxuansum*$duoxuanfs+$panduansum*$panduanfs+$tiankongsum*$tiankongfs+$datisum*$datifs;
if($zongfen!=$jianyan)
echo "<script>alert('每题分数和总分不符!');location.href='super/super'</script>";
if($_POST['text']=="")
echo "<script>alert('请输入考试题目!');location.href='super/super'</script>";
if($_POST['ktime']=="")
echo "<script>alert('请输入考试时间!');location.href='super/super'</script>";
//把接受的科目id转化成字符串,方便查找
$ke=implode(",",$_POST['ke']);
//计算每个难度对应的分数
$nansum=$nan*$zongfen;
$zhongsum=$zhong*$zongfen;
$yisum=$yi*$zongfen;
//定义存储随机数字的数组
$copy=array();
//计算题库tid的最大值
$aaa=Db::table('test')->field('count(tid) as max')->find();
$max=$aaa['max'];
//给定重点题分数
$key=ceil($zongfen/2);
//定义存储题库tid的数组
$tid=array();
for($abc=1;$abc>0;$abc++){
if($zongfen==0){
$tid=implode(',',$tid);
$defen=$danxuanfs;
$defen.=','.$duoxuanfs;
$defen.=','.$panduanfs;
$defen.=','.$tiankongfs;
$defen.=','.$datifs;
$res=Db::table('guodu')->where('id',1)->update(['zongfen'=>$zf,'text'=>input('text'),'ktime'=>input('ktime'),'tihao'=>$tid,'uid'=>Session::get('uid'),'shuliang'=>$shuliang,'defen'=>$defen,'time'=>date('Y-m-d H:i:s',time())]);
if($res||$res==0){
$this->success('生成中……',url('index/super/xs'));
}else
$this->error('生成失败!');
break;
}
if(count($copy)==$max){
echo "<script>alert('题目数量不足!');location.href='super/super'</script>";
break;
}
//生成随机数字,随机查询题目
$sj=rand(1,$max);
$res=Db::table('test')
->join('jie','jie.jieid=test.jieid')
->join('zhang','zhang.zhangid=jie.zhangid')
->join('ke','ke.keid=zhang.keid')
->where(['tid'=>['=',$sj],'ke.keid'=>['in',$ke]])
->field('keyid,nanid,lid')
->find();
//优先循环重点题目
if($res['keyid']!=1&&$key>0)
continue;
//判断该随机数字是否被抽到过
if(in_array($sj,$copy))
continue;
//储存随机数
$copy[]=$sj;
//判断某个字段是否有值,如果空则无该题
if(empty($res['keyid']))
continue;
//如果难题分数已够,则跳出循环
if($nansum<=0&&$res['nanid']==1)
continue;
//如果中等题分数已够,则跳出循环
if($zhongsum<=0&&$res['nanid']==2)
continue;
//如果简单题分数已够,则跳出循环
if($yisum<=0&&$res['nanid']==3)
continue;
/******************************************单选筛选***********************************/
if($res['lid']==1&&$danxuansum!=0){
if($res['nanid']==1){
$nansum-=$danxuanfs;
$danxuansum--;
$zongfen-=$danxuanfs;
$tid[]=$sj;
}
if($res['nanid']==2){
$zhongsum-=$danxuanfs;
$danxuansum--;
$zongfen-=$danxuanfs;
$tid[]=$sj;
}
if($res['nanid']==3){
$yisum-=$danxuanfs;
$danxuansum--;
$zongfen-=$danxuanfs;
$tid[]=$sj;
}
if($res['keyid']==1)
$key-=$danxuanfs;
}
/******************************************多选筛选***********************************/
if($res['lid']==2&&$duoxuansum!=0){
if($res['nanid']==1){
$nansum-=$duoxuanfs;
$duoxuansum--;
$zongfen-=$duoxuanfs;
$tid[]=$sj;
}
if($res['nanid']==2){
$zhongsum-=$duoxuanfs;
$duoxuansum--;
$zongfen-=$duoxuanfs;
$tid[]=$sj;
}
if($res['nanid']==3){
$yisum-=$duoxuanfs;
$duoxuansum--;
$zongfen-=$duoxuanfs;
$tid[]=$sj;
}
if($res['keyid']==1)
$key-=$duoxuanfs;
}
/******************************************判断筛选***********************************/
if($res['lid']==3&&$panduansum!=0){
if($res['nanid']==1){
$nansum-=$panduanfs;
$panduansum--;
$zongfen-=$panduanfs;
$tid[]=$sj;
}
if($res['nanid']==2){
$zhongsum-=$panduanfs;
$panduansum--;
$zongfen-=$panduanfs;
$tid[]=$sj;
}
if($res['nanid']==3){
$yisum-=$panduanfs;
$panduansum--;
$zongfen-=$panduanfs;
$tid[]=$sj;
}
if($res['keyid']==1)
$key-=$panduanfs;
}
/******************************************填空筛选***********************************/
if($res['lid']==4&&$tiankongsum!=0){
if($res['nanid']==1){
$nansum-=$tiankongfs;
$tiankongsum--;
$zongfen-=$tiankongfs;
$tid[]=$sj;
}
if($res['nanid']==2){
$zhongsum-=$tiankongfs;
$tiankongsum--;
$zongfen-=$tiankongfs;
$tid[]=$sj;
}
if($res['nanid']==3){
$yisum-=$tiankongfs;
$tiankongsum--;
$zongfen-=$tiankongfs;
$tid[]=$sj;
}
if($res['keyid']==1)
$key-=$tiankongfs;
}
/******************************************大题筛选***********************************/
if($res['lid']==5&&$datisum!=0){
if($res['nanid']==1){
$nansum-=$datifs;
$datisum--;
$zongfen-=$datifs;
$tid[]=$sj;
}
if($res['nanid']==2){
$zhongsum-=$datifs;
$datisum--;
$zongfen-=$datifs;
$tid[]=$sj;
}
if($res['nanid']==3){
$yisum-=$datifs;
$datisum--;
$zongfen-=$datifs;
$tid[]=$sj;
}
if($res['keyid']==1)
$key-=$datifs;
}
}
}
return view('super',['keres'=>$keres,'sum'=>$sum]);
}
博客园:https://www.cnblogs.com/huixincode
如果此文章对您有所帮助记得打赏哦,一分也是对我的支持和鼓励,谢谢!
转载文章请务必保留出处和署名,谢谢!
如果此文章对您有所帮助记得打赏哦,一分也是对我的支持和鼓励,谢谢!
转载文章请务必保留出处和署名,谢谢!