美文网首页PHP经验分享
学习网站作业 测试功能 思路与实现

学习网站作业 测试功能 思路与实现

作者: PEIZIJUN | 来源:发表于2017-09-29 12:09 被阅读0次

    最近在做一个关于学习的网站,有一个作业 测试的功能, 想记录一下:
    用的 thinkphp3.2
    这是界面


    QQ截图20170929114111.png

    控制器:

    //测试
    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'] = '<span style="color:green;">正确</span>。'.htmlspecialchars_decode($ti['analysis']);
                        }else{
                            $yesstr .= "0".',';
                            $ti['jie'] = '<span style="color:red;">错误</span>。正确答案:'.$ti['daan'].htmlspecialchars_decode($ti['analysis']);
                        }
                    }else{
                        $yesstr .= "1".',';
                        $ti['jie'] = '<span style="color:green;">正确</span>。'.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 = "<span style='color:green;'>"+res.da+"</span>";
            }else{
                if(Fraction){
                    var f = $("#Fraction").val();
                    if(f != ''){  //页面储存正确与否,打分用
                        $("#Fraction").val(f+','+'0');
                    }else{
                        $("#Fraction").val('0');
                    }
                }
                var html = "<span style='color:red;'>"+res.da+"</span>";
            }
            html += "。"+res.txt;
            $("#daancontent").html(html);
            $("#daancontent").show();
            $(e).hide();
            $(e).next().show()
        })
    }
    

    点击提交后,显示对错,并且显示正确答案与解析。 提交按钮变成下一题


    image.png

    点击下一题

    //作业测试上一题下一题
    // 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<arr.length;i++){
                        if(arr[i] == 1){
                            j++;
                        }
                    }
    //              console.log(j);
                    var zf =  Math.ceil((100/arr.length)*j);
                    layer.open({
                        title:'提示',
                        shadeClose: false, //点击遮罩关闭
                        content: "最终得分"+ zf,
                        end: function(index, layero){
                            $.post(endpath,{id:id},function(){
                                href('/index.php?g=portal&m=MyCourse&a=mycourse');
                            })
                        }
                    });
                }else{
                    layer.open({
                        title:'提示',
                        shadeClose: false, //点击遮罩关闭
                        content: "作业做完啦",
                        end: function(index, layero){
                            href('/index.php?g=portal&m=MyCourse&a=mycourse');
                        }
                    });
                    
                }
            }else{
                var i = $("#aaaa",data);
                $("#aaa").html(i);
            }
        })
    //  console.log(val);/
    }
    

    点击下一题

    image.png image.png image.png image.png

    大概就是个这, 能参考的参考一下吧~~
    可能有点乱,不太容易看,。。。
    反正我也就是记录一下, 万一以后用的着呢。~~~

    相关文章

      网友评论

        本文标题:学习网站作业 测试功能 思路与实现

        本文链接:https://www.haomeiwen.com/subject/viciextx.html