ResolveController.class.php 2.93 KB
<?php
/**
 * 【考试中心-手机端】查看答案解析接口
 * ResolveController.class.php
 * @author: 蔡建华
 * @date: 2017-05-23
 */

namespace Api\Controller\Answer;

use Common\Service\PaperService;
use Common\Service\AnswerService;
use Common\Service\AnswerDetailService;

class ResolveController extends AbstractController
{

    public function Index_post()
    {
        // 接收post参数
        $params = I('post.');

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

            E('_EMPTY_EA_ID');
        }

        // 实例化答卷service
        $answer_s = new AnswerService();

        // 根据答卷id获取答卷数据
        $answer_info = $answer_s->get($ea_id);

        // 答卷数据为空,返回提示"试卷信息不存在"
        if (empty($answer_info)) {

            E('_EMPTY_PAPER_DATA');
        }

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

        // 答题状态
        $type = intval($params['type']);
        // 查询条件:答卷id
        $conditions = [
            'ea_id' => $ea_id,
        ];

        // 答题状态为答题通过
        if (AnswerService::MY_PASS == $type) {

            // 查询条件追加答题状态:未作答、未通过
            $conditions['is_pass'] = [AnswerService::MY_PASS, AnswerService::MY_PASS_INIT];
        }

        // 实例化答卷详情service
        $answer_detail_s = new AnswerDetailService();
        // 根据答卷id,获取未作答和未通过的答卷总数
        $total = $answer_detail_s->count_by_conds($conditions);

        $list = [];
        // 总数不为空:数据存在
        if ($total) {
            // 排序:序号正序
            $order_option['order_num'] = 'ASC';
            // 根据答卷id,查询未作答和未通过的答卷详情数据列表
            $list = $answer_detail_s->list_by_conds($conditions, [$start, $limit], $order_option);
        }

        // 实例化试卷service
        $paper_s = new PaperService();
        // 根据试卷id获取试卷详情
        $paper = $paper_s->get($answer_info['ep_id']);

        // 格式化返回数据
        $list_data = $answer_detail_s->question_list_format($list, $paper['marking_type'],
            AnswerDetailService::ANSWER_FRONTEND_ANALYSIS);

        // 返回结果
        $this->_result = [
            'total' => intval($total),
            'limit' => intval($limit),
            'ep_name' => $paper['ep_name'],
            'ep_id' => $paper['ep_id'],
            'ea_id' => $answer_info['ea_id'],
            'page' => intval($page),
            'list' => $list_data
        ];
    }
}