OnlineTrainController.php 3.9 KB
<?php
/**
 * Created by PhpStorm.
 * auther: sgj
 * Date: 2020/10/10
 * Time: 11:37
 */

namespace app\admin\controller;


use api\common\model\ClassModel;
use cmf\controller\AdminBaseController;

class OnlineTrainController extends AdminBaseController
{
    public function index(){
        $param = $this->request->param();
        $map=[];
        $data=db('class')->where($map)->order('weight desc')->paginate(10);
        $data->appends($param);
        $list=$data->items();
        foreach ($list as $k=>$v){

        }
        $this->assign([
            'data'=>$list,
            'page'=>$data->render(),
        ]);

        return $this->fetch();
    }

    /**
     * 编辑页面
     * @return mixed
     */
    public function edit(){
        if ($this->request->isPost()){
            $data=input();
            $id=$data['id'];
            unset($data['id']);
            $result=db('class')->where('id',$id)->update($data);
            if ($result==1){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }

        }else{
            $id=input('id');
            $info=db('class')->where('id',$id)->find();
            $this->assign('data',$info);
            return $this->fetch();
        }
    }

    /**
     * 添加信息
     * @return mixed
     */
    public function add(){
        if ($this->request->isPost()){
            $data=input();
            $result=db('class')->insert($data);
            if (!empty($result)){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }

        }else{
            return $this->fetch();
        }
    }

    /**
     * 问题列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function questionlist(){
        $id=input('id');
        $list=db('class_question')->where('class_id',$id)->select()->toArray();

        foreach ($list as $k=>$v) {
            $list[$k]['info']=json_decode($v['options'],true);
        }
       $this->assign('list',$list);
       return $this->fetch();
    }

    /**
     * 删除对应问题
     */
    public function delete(){
        $id=input('id');
        $result=db('class_question')->where('id',$id)->delete();
        if ($result){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }

    }

    /**
     * 问题详情
     */
    public function questioninfo(){
        $id=input('id');
        $data=db('class_question')->where('id',$id)->find();
        $data['options']=json_decode($data['options'],true);
        $this->assign('data',$data);
        return $this->fetch();
    }


    public function questionadd(){
        $data['options'][]='';
        $data['options'][]='';
        $data['options'][]='';
        $data['options'][]='';
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 添加问题
     */
    public function addquestionpost(){
        $data=input();
        $data['options']=json_encode($data['options']);
        $result=db('class_question')->insert($data);
        if ($result){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }


    /**
     * 编辑问题
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function editQuestion(){
        $data=input();
        $id=$data['id'];
        $update['title']=$data['title'];
        $update['options']=json_encode($data['options']);
        $update['answer']=$data['answer'];
        $info=db('class_question')->where('id',$id)->update($update);
        if(!empty($info)){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }





}