Everyday.php 6.5 KB
<?php
namespace app\mobile\controller;

use think\Validate;
use think\Db;
use app\common\controller\Api;
use app\mobile\model\Question;
use app\mobile\model\QuestionAnswer;

/**
 * 每日一练接口
 * @ApiWeigh (95)
 */
class Everyday extends Api
{
	protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\mobile\model\Everyday');
    }

    /**
     * @ApiTitle    (首页)
     * @ApiSummary  (首页)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599131811",
        "data": [{
            "id": 1, //每日一练ID
            "title": "测试每日一练", //标题
            "uptime": "今天", //上架时间
            "do_num": 77 //做过人数
        }]
    })
     */
    public function index()
    {
        $list = $this->model->where('uptime','<=',time())
            ->order('uptime desc')
            ->select();
        foreach ($list as &$v) {
            $v['uptime'] = date('Y-m-d',$v['uptime']) == date('Y-m-d') ? '今天' : date('m月d日',$v['uptime']);
            $v->visible(['id','title','uptime','do_num']);
        }
        $this->success('成功',$list);
    }

    /**
     * @ApiTitle    (每日一练-题目详情)
     * @ApiSummary  (每日一练-题目详情)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="everyday_id", type="int", required=true, description="每日一练ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599046220",
        "data": {
            "total": 1, //题目总数
            "list": [{ //题目列表
                "id": 1, //题目ID
                "title": "测定混凝土立方体抗压强度时,标准试件的尺寸是(      )㎜。", //题目
                "option": "[{\"name\":\"A\",\"gender\":\"100\\u00d7100\\u00d7100\"},{\"name\":\"B\",\"gender\":\"150\\u00d7150\\u00d7150\"},{\"name\":\"C\",\"gender\":\"200\\u00d7200\\u00d7200\"},{\"name\":\"D\",\"gender\":\"70.7\\u00d770.7\\u00d770.7\"}]", //题目选项
                "type": "1", //题目类型:1=单选题,2=多选题,3=判断题,4=简答题
                "answer": "A", //答案
                "note": "45678910", //笔记
                "is_collect": "1" //是否收藏:0=否,1=是
            }]
        }
    })
     */
    public function questionList()
    {
        $user_id = $this->auth->id;
        $everyday_id = $this->request->param('everyday_id');
        empty($everyday_id) && $this->error('缺少必要参数');
        $info = $this->model->get($everyday_id);
        empty($info) && $this->error('每日一练信息不存在');
        $list = Question::alias('q')
            ->join('mobile_question_note qn','q.id = qn.question_id and qn.user_id='.$user_id,'left')
            ->join('mobile_question_collect qc','q.id = qc.question_id and qn.user_id='.$user_id,'left')
            ->where('q.target_id',$everyday_id)
            ->where('q.target_type','4')
            ->field('
                q.id,
                q.title,
                q.option,
                q.type,
                q.answer,
                qn.content note,
                if(qc.id > 0,1,0) is_collect
            ')->select();
        $total = count($list);
        // 删除原来的做题记录
        QuestionAnswer::where('question_id','in',array_column($list, 'id'))
            ->where('user_id',$user_id)
            ->delete();
        $this->success('成功',compact('total','list'));
    }

    /**
     * @ApiTitle    (每日一练-答题卡)
     * @ApiSummary  (每日一练-答题卡)
     * @ApiMethod   (POST)
     *
     * @ApiParams (name="everyday_id", type="int", required=true, description="每日一练ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599132508",
        "data": {
            "total": 1, // 题目数
            "right_rate": 0, //正确率
            "right_count": 0, //正确题数
            "wrong_count": 0, //错误题数
            "no_count": 1, //未做题数
            "list": [{
                "id": 1, //题目ID
                "title": "测定混凝土立方体抗压强度时,标准试件的尺寸是(      )㎜。", //题目
                "option": "[{\"name\":\"A\",\"gender\":\"100\\u00d7100\\u00d7100\"},{\"name\":\"B\",\"gender\":\"150\\u00d7150\\u00d7150\"},{\"name\":\"C\",\"gender\":\"200\\u00d7200\\u00d7200\"},{\"name\":\"D\",\"gender\":\"70.7\\u00d770.7\\u00d770.7\"}]", //题目选项
                "type": "1", //题目类型:1=单选题,2=多选题,3=判断题,4=简答题
                "answer": "A", //答案
                "status": 0, //状态:0=未答,1=答对,2=答错
                "get_score": null //得分
            }]
        }
    })
     */
    public function answerSheet()
    {
        $user_id = $this->auth->id;
        $everyday_id = $this->request->param('everyday_id');
        empty($everyday_id) && $this->error('缺少必要参数');
        $info = $this->model->get($everyday_id);
        empty($info) && $this->error('每日一练信息不存在');
        $list = Question::alias('q')
            ->join('mobile_question_answer qa','q.id = qa.question_id and user_id='.$user_id,'left')
            ->where('q.target_id',$everyday_id)
            ->where('q.target_type','4')
            ->field('
                q.id,
                q.title,
                q.option,
                q.type,
                q.answer,
                if(qa.id > 0,if(qa.is_wrong="0",1,2),0) status,
                qa.get_score
            ')->select();
        // 题目数
        $total = count($list);
        // 得分
        $myscore = array_sum(array_column($list, 'get_score'));
        // 正确题数
        $right_count = 0;
        // 错误题数
        $wrong_count = 0;
        // 未做题数
        $no_count = 0;
        foreach ($list as $v) {
            switch ($v['status']) {
                case '0':
                    $no_count++;
                    break;
                case '1':
                    $right_count++;
                    break;
                case '2':
                    $wrong_count++;
                    break;
            }
        }
        // 正确率
        $right_rate = !empty($right_count) ? bcdiv($right_count,$total,4) * 100 : 0;
        $this->success('成功',compact('total','right_rate','right_count','wrong_count','no_count','list'));
    }
}