最近在做一个关于学习的网站,有一个作业 测试的功能, 想记录一下:
用的 thinkphp3.2
这是界面
控制器:
//测试
public function ztest(){
//课时id
$id = I("hour_id");
$field = "test";
$uid = $_SESSION['user_id'];
$page = 0;
$section = M('course_section')->field($field.',sc_name')->where("id = '$id'")->find();
$arr = explode(',', $section[$field]);
$count = count($arr);
foreach($arr as $k => $v){
$ti = M('exam_shiti')->where("id = '$v'")->find();
$old = M('course_test')->where("uid = '$uid' and tid = '$v' and type = '2'")->find();
if($old){
$ti['old'] = 1;
$ti['old_val'] = $old['val'];
if($ti['typeid'] < 4){
if($ti['daan'] == $old['val']){
$yesstr .= "1".',';
$ti['jie'] = '正确。'.htmlspecialchars_decode($ti['analysis']);
}else{
$yesstr .= "0".',';
$ti['jie'] = '错误。正确答案:'.$ti['daan'].htmlspecialchars_decode($ti['analysis']);
}
}else{
$yesstr .= "1".',';
$ti['jie'] = '正确。'.htmlspecialchars_decode($ti['analysis']);
}
}else{
$ti['old'] = 2;
}
if($k == $count-1){
$ti['ready'] = 1;
}
$tiarr[] = $ti;
}
if(substr($yesstr, -1) == ','){
$yesstr = substr($yesstr, 0,-1);
}
if(isset($_POST['page'])){ // 类似于上一页下一页 ajax下一题上一题处理地点
if($_POST['page'] > count($tiarr)){
echo 'no';die;
}
$page = $_POST['page']-1;
}
if(isset($_POST['val'])){ // 判断正确与否 ajax提交处理地点
$val = $_POST['val'];
$tid = $_POST['id'];
$dati = M('exam_shiti')->field('daan,analysis')->where("id = '$tid'")->find();
if($_POST['xing'] < 4){
if($dati['daan'] == $val){
$res['da'] = '正确';
}else{
$res['da'] = '错误';
}
}else{
$res['da'] = '正确';
}
$res['txt'] = '正确答案:'.$dati['daan'].htmlspecialchars_decode($dati['analysis']);
echo json_encode($res);die;
}
//查询是否已收藏
$ttid = $tiarr[$page]['id'];
$uid = $_SESSION['user_id'];
$coll = M('exam_collect')->where("exam_id = '$ttid' and uid = '$uid' and type = '1'")->find();
$this->assign('yesstr',$yesstr);
$this->assign('coll',$coll);
$this->assign('hour_id',$id);
$this->assign('section',$section);
$this->assign('page',$page+1);
$this->assign('ti',$tiarr[$page]); //以数组下标做分页显示
$this->assign('webname','我的课程');
$this->display();
}
//储存答案
public function subti(){
$val = I('val');
$id = I('id');
$sid = I('sid');
$type = I('type');
$uid = $_SESSION['user_id'];
$data['val'] = $val;
$data['tid'] = $id;
$data['uid'] = $uid;
$data['sid'] = $sid;
$data['type'] = $type;
$add = M('course_test')->add($data);
}
//删除答案
public function delti(){
$id = I('id');
$uid = $_SESSION['user_id'];
$del = M('course_test')->where("uid = '$uid' and sid = '$id' and type = '2'")->delete();
}
首先是提交功能,分辨题型,去数据库比对对错,返回答案,显示:
注,客户要求,问答题和材料题直接返回正确
//作业提交答案
//xing 题型 1选择 2填空 3判断 4 问答 5材料题
// e 点击对象,id 题ID,path ajax地址(判断正确与否),cunpath ajax地址(保存答案),sid 课时ID
//Fraction 有是测试 测试需要返回分数,且可以重复做题 、无是作业, 只能做一次,不需要打分
function subti(e,id,xing,path,cunpath,sid,Fraction){
var val,type;
if(xing == 1){//选择题
val = $("input[name='daan']:checked").val();
}else if(xing == 2){//填空题
val = $("input[name='daan']").val();
}else if(xing == 3){//判断题
val = $("input[name='daan']:checked").val();
}else if(xing == 4 || xing == 5){
val = $("textarea[name='daan']").val();
}
if(Fraction){
type = 2;
}else{
type = 1;
}
$.post(cunpath,{id:id,val:val,type:type,sid:sid},function(data){ //保存答案到数据库
console.log(data);
})
$.post(path,{val:val,id:id,xing:xing},function(data){ //判断返回内容
var res = JSON.parse(data);
if(res.da == '正确'){
if(Fraction){
var f = $("#Fraction").val();
if(f != ''){ //页面储存正确与否,打分用
$("#Fraction").val(f+','+'1');
}else{
$("#Fraction").val('1');
}
}
var html = ""+res.da+"";
}else{
if(Fraction){
var f = $("#Fraction").val();
if(f != ''){ //页面储存正确与否,打分用
$("#Fraction").val(f+','+'0');
}else{
$("#Fraction").val('0');
}
}
var html = ""+res.da+"";
}
html += "。"+res.txt;
$("#daancontent").html(html);
$("#daancontent").show();
$(e).hide();
$(e).next().show()
})
}
点击提交后,显示对错,并且显示正确答案与解析。 提交按钮变成下一题
点击下一题
//作业测试上一题下一题
// e 本对象 , type 作业or测试 ,id 课时ID ,path ajax发送地址(上下题)
//endpath ajax发送地址(测试的话测试完成需要删除答案记录)
// Fraction 有就是测试 无就是作业
function nextti(e,type,page,id,path,endpath,Fraction){
if(type == 'next'){//下一题
page += 1;
}else if(type == 'prev'){
page = page-1;
}
$.post(path,{page:page,hour_id:id},function(data){
if(data == 'no'){ //返回no 没有下一题了
if(Fraction){
var f = $("#Fraction").val();
var arr = f.split(",");
var j = 0;
for(var i=0;i
点击下一题
大概就是个这, 能参考的参考一下吧~~
可能有点乱,不太容易看,。。。
反正我也就是记录一下, 万一以后用的着呢。~~~