QuestionListController.class.php 5.26 KB
<?php
/**
 * 【考试中心-手机端】阅卷获取试题接口
 * QuestionListController.class.php
 * @author: 蔡建华
 * @date: 2017-06-19
 */

namespace Api\Controller\Marking;

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

class QuestionListController extends AbstractController
{

    public function Index_post()
    {
        $params = I('post.');

        $ep_id = intval($params['ep_id']);
        if (!$ep_id) {
            // ep_id 不能为空
            E('_EMPTY_EP_ID');
        }

        $paper_serv = new PaperService();
        $paper = $paper_serv->get($ep_id);

        if (empty($paper)) {
            // 试卷信息不存在
            E('_EMPTY_PAPER_DATA');
        }

        // 判断试卷类型
        if ($paper['paper_type'] != PaperService::EVALUATION_PAPER_TYPE) {
            // 不是测评试卷,则抛错
            E('_ERR_EVALUATION_PAPER_TYPE');
        }

        // 判断用户是否有权进行批阅
        $right_serv = new RightService();
        $is_marking_right = $right_serv->count_by_conds([
            'epc_id' => $ep_id,
            'er_type' => RightService::RIGHT_MARKING,
            'uid' => $this->uid
        ]);

        if (!$is_marking_right) {
            // 不是阅卷人
            E('_ERR_MARKING_NO_QUIT');
        }

        $answer_serv = new AnswerService();

        // 获取待批阅的答卷列表
        $answer_list = $answer_serv->list_by_conds(
            [
                'ep_id' => $ep_id,
                'answer_status' => AnswerService::READ_WAITING,
            ],
            [],
            [],
            'ea_id'
        );

        $ea_ids = array_column($answer_list, 'ea_id');

        // 待批阅总数
        $total = count($ea_ids);

        $ea_id = intval($params['ea_id']);

        // 该试卷已经全部阅卷完毕
        if (!$total && !$ea_id) {

            E('_ERR_NOT_WAITING_ANSWER');
        }

        $waiting_total = 0;

        if (!$ea_id) {
            // ---首次批阅---,则需返回本次需要批阅的答卷ea_id和下一个答卷ea_id
            // 如果待批阅答卷总数大于等于2,随机获取ea_id 和 next_ea_id
            if ($total >= 2) {
                $ea_id_key = array_rand($ea_ids, 2);
                $ea_id = $ea_ids[$ea_id_key[0]];
                $next_ea_id = $ea_ids[$ea_id_key[1]];
            } else {
                $ea_id = $ea_ids[0];
                $next_ea_id = 0;
            }

            $waiting_total = $total;

        } else {
            // ---不是首次批阅---,则只需返回下一个答卷的ea_id
            // 如果待批阅答卷总数大于等于2,随机获取2个next_ea_id
            if ($total >= 2) {
                $ea_id_key = array_rand($ea_ids, 2);
                $next_ea_id = $ea_ids[$ea_id_key[0]];
                // 如果获取的next_ea_id 等于当前批阅答卷的ea_id
                if ($next_ea_id == $ea_id) {
                    $next_ea_id = $ea_ids[$ea_id_key[1]];
                }
            } else {
                $next_ea_id = 0;
            }
        }

        $page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
        $limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;

        // 分页
        list($start, $limit) = page_limit($page, $limit);
        $conds = [
            'et_type' => [
                PaperService::TOPIC_TYPE_QUESTION,
                PaperService::TOPIC_TYPE_VOICE
            ],
            'ea_id' => $ea_id,
            'is_status' => PaperService::DO_STATE,
        ];

        // 实例化答卷详情service
        $answer_detail_s = new AnswerDetailService();

        $total = $answer_detail_s->count_by_conds($conds);

        $data = [];
        if ($total) {
            // 获取待批阅题目列表
            $order_option['order_num'] = 'ASC';
            $data = $answer_detail_s->list_by_conds($conds, [$start, $limit], $order_option);
        } else {
            // 没有获取到可阅试题
            E('_EMPTY_TOP_PAPER_DATA');
        }

        // 获取答卷信息
        $waiting_answer = $answer_serv->get($ea_id);

        // 获取考生信息
        $user_serv = User::instance();
        $user = $user_serv->getByUid($waiting_answer['uid']);

        // 格式化题目列表(阅卷解析)
        $list = $answer_detail_s->question_list_format(
            $data,
            $paper['marking_type'],
            AnswerService::ANSWER_MARKING_ANALYSIS
        );

        $result = [
            'username' => $user['memUsername'],
            'my_end_time' => $waiting_answer['my_end_time'],
            'ea_id' => $ea_id,
            'next_ea_id' => $next_ea_id,
            'list' => $list,
        ];

        // 待批阅答卷总数(只有第一次进入批阅时会返回)
        if ($waiting_total) {
            // 待批阅试卷总数
            $result['waiting_total'] = $waiting_total;
        }

        // 是否开启匿名批阅
        if (AnswerService::OPEN_ANONYMOUS_MARKING == $paper['is_open_anonymous_marking']) {

            unset($result['username']);
        }

        // 返回结果
        $this->_result = $result;

        return true;
    }
}