AnalysisListController.class.php 8.14 KB
<?php
/**
 * 【考试中心-后台】试题分析列表接口
 * AnalysisListController.class.php
 * @author : wanghuan
 * @date   : 2017-07-17
 * @version: $Id$
 */

namespace Apicp\Controller\Topic;

use Common\Service\PaperService;
use Common\Service\SnapshotService;
use Common\Service\AnswerDetailService;
use Common\Service\RandomSnapshotService;

class AnalysisListController extends AbstractController
{
    // 试卷顺序
    const SORT_PAPER = 1;
    // 正确率升序
    const SORT_ASC = 2;
    // 正确率降序
    const SORT_DESC = 3;

    /** @var PaperService */
    protected $paper_serv;
    /** @var SnapshotService */
    protected $snapshot_serv;
    /** @var AnswerDetailService */
    protected $answer_detail_serv;
    /** @var RandomSnapshotService */
    protected $random_snapshot_serv;

    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {

            return false;
        }

        // 实例化试题service
        $this->paper_serv = new PaperService();
        // 实例化试题快照service
        $this->snapshot_serv = new SnapshotService();
        // 实例化答卷详情service
        $this->answer_detail_serv = new AnswerDetailService();
        // 实例化随机试题快照service
        $this->random_snapshot_serv = new RandomSnapshotService();

        return true;
    }

    public function Index_post()
    {
        // 接收post参数
        $params = I('post.');
        // 参数为空,返回提示"参数不能为空"
        if (empty($params)) {

            E('_ERR_PARAMS_NOT_NULL');
        }

        // 试卷id
        $ep_id = intval($params['ep_id']);
        // 试卷id为空,返回提示"试卷ID不能为空"
        if (!$ep_id) {

            E('_EMPTY_EP_ID');
        }

        // 根据试卷id获取试卷详情
        $paper = $this->paper_serv->get($ep_id);
        if (empty($paper)) {

            E('_ERR_PAPER_NOT_FOUND');
        }

        // 试卷类型:1=自主,2=规则,3=随机
        $ep_type = $paper['ep_type'];

        // 判断分页参数是否为空,为空赋默认值
        $page = !empty($params['page']) ? intval($params['page']) : PaperService::DEFAULT_PAGE;
        $limit = !empty($params['limit']) ? intval($params['limit']) : PaperService::DEFAULT_LIMIT;
        // 分页
        list($start, $limit) = page_limit($page, $limit);

        // 查询条件:试卷id
        $conds['a.ep_id'] = $ep_id;
        // 分页
        $page_option = array(
            $start,
            $limit
        );
        // 排序默认:试卷顺序
        $sort = self::SORT_PAPER;
        // 排序参数不为空,重新赋值
        if (isset($params['sort']) && !empty($params['sort'])) {

            $sort = $params['sort'];
        }

        $order_option = [];
        // 试卷顺序
        if (self::SORT_PAPER == $sort) {
            // 随机抽题
            if (PaperService::TOPIC_RANDOM == $ep_type) {

                $order_option['order_defualt'] = 'ASC';
            } else { // 自主或规则选题

                $order_option['order_num'] = 'ASC';
            }
        } elseif (self::SORT_ASC == $sort) { // 正确率升序

            $order_option = [
                'ea_sort' => 'ASC',
                'probability' => 'ASC'
            ];
        } elseif (self::SORT_DESC == $sort) { // 正确率降序

            $order_option = [
                'ea_sort' => 'ASC',
                'probability' => 'DESC'
            ];
        }

        // 随机抽题
        if (PaperService::TOPIC_RANDOM == $ep_type) {
            $res = $this->random_snapshot($ep_id, $conds, $page_option, $order_option);
        } else { // 自主或规则选题

            $res = $this->snapshot($conds, $page_option, $order_option);
        }

        $topics = $res['topics'];

        $this->_result = [
            'title' => $paper['ep_name'],
            'count' => intval($paper['join_count']),
            'total' => intval($res['total']),
            'limit' => intval($limit),
            'page' => intval($page),
            'list' => $topics
        ];
    }

    /**
     * 获取随机抽题数据
     *
     * @param $ep_id int 试卷id
     * @param $conds array 查询条件
     * @param $page_option array 分页参数
     * @param $order_option array 排序参数
     *
     * @return array|bool array 试题
     */
    protected function random_snapshot($ep_id, $conds, $page_option, $order_option)
    {
        // 初始化试卷题目列表
        $topics = [];
        // 查询条件
        $detail_conds['a.ep_id'] = $ep_id;
        // 分页
        $detail_page_option = null;
        // 排序
        $detail_order_option['ead_id'] = 'ASC';
        // 返回字段
        $detail_fields = 'a.esr_id';
        // 查询参加考试的题
        $answer_data = $this->answer_detail_serv->count_answer_detail_list($detail_conds, $detail_page_option,
            $detail_order_option, $detail_fields);

        $er_ids = array_unique(array_column($answer_data, 'esr_id'));
        // 如果没有参加开始返回空
        if (empty($er_ids)) {

            return [
                'total' => 0,
                'topics' => $topics
            ];
        }

        $conds['a.er_id'] = $er_ids;
        // 获取记录总数
        $total = $this->random_snapshot_serv->conds_random_snapshot_count($conds);
        if ($total) {

            // 题目返回字段
            $fields = ' a.er_id,a.order_num,a.et_id,a.title,a.et_type';
            // 获取试卷题目列表
            $topics = $this->random_snapshot_serv->conds_random_snapshot_list($conds, $page_option, $order_option,
                $fields);
            foreach ($topics as $k => $topic) {
                // 题目id
                $topics[$k]['esr_id'] = $topic['er_id'];
                // 序号
                $topics[$k]['order_num'] = intval($topic['order_num']);
                // 非主观判断题,计算正确率
                if (!in_array($topic['et_type'],
                    [SnapshotService::TOPIC_TYPE_QUESTION, SnapshotService::TOPIC_TYPE_VOICE])
                ) {

                    $topics[$k]['rate'] = (round($topic['probability'] * 100, 2)) . '%';
                } else { // 非主观判断题,不计算正确率

                    $topics[$k]['rate'] = '';
                }
                unset($topics[$k]['er_id'], $topics[$k]['et_id'], $topics[$k]['et_type']);
            }
        }

        return [
            'total' => $total,
            'topics' => $topics
        ];
    }

    /**
     * 获取自主/规则抽题数据
     *
     * @param $conds array 查询条件
     * @param $page_option array 分页参数
     * @param $order_option array 排序参数
     *
     * @return array|bool array 试题
     */
    protected function snapshot($conds, $page_option, $order_option)
    {
        // 初始化试卷题目列表
        $topics = [];
        // 获取记录总数
        $total = $this->snapshot_serv->conds_snapshot_count($conds);
        if ($total) {

            // 题目返回字段
            $fields = 'a.es_id,a.order_num,a.et_id,a.title,a.et_type';
            // 获取试卷题目列表
            $topics = $this->snapshot_serv->conds_snapshot_list($conds, $page_option, $order_option, $fields);
            foreach ($topics as $k => $topic) {
                // 题目id
                $topics[$k]['esr_id'] = $topic['es_id'];
                // 序号
                $topics[$k]['order_num'] = intval($topic['order_num']);
                // 非主观判断题,计算正确率
                if (!in_array($topic['et_type'],
                    [SnapshotService::TOPIC_TYPE_QUESTION, SnapshotService::TOPIC_TYPE_VOICE])
                    && $topic['probability']
                ) {

                    $topics[$k]['rate'] = (round($topic['probability'] * 100, 2)) . '%';
                } else { // 非主观判断题,不计算正确率

                    $topics[$k]['rate'] = '';
                }
                unset($topics[$k]['es_id'], $topics[$k]['et_id'], $topics[$k]['et_type']);
            }
        }

        return [
            'total' => $total,
            'topics' => $topics
        ];
    }
}