RuleDetailController.class.php 2.21 KB
<?php
/**
 * 【考试中心-后台】 获取试卷规则
 * RuleDetailController.class.php
 * User: daijun
 * Date: 2017-05-23
 */

namespace Apicp\Controller\Paper;

use Common\Service\PaperService;
use Common\Service\TopicAttrService;

class RuleDetailController extends AbstractController
{

    /** @var PaperService */
    protected $paper_service;
    /** @var TopicAttrService */
    protected $topic_attr_service;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {

            return false;
        }

        // 实例化试卷信息表
        $this->paper_service = new PaperService();
        // 实例化试题属性关联关系表
        $this->topic_attr_service = new TopicAttrService();

        return true;
    }

    public function Index_post()
    {
        $ep_id = I('post.ep_id', 0, 'intval');

        // 验证参数
        if (empty($ep_id)) {

            E('_EMPTY_PAPER_ID');
        }

        // 获取试卷信息
        $data = $this->paper_service->get($ep_id);
        if (!$data) {

            E('_EMPTY_PAPER_DATA');
        }

        // 组装请求参数
        $bank_ids = explode(',', $data['bank_data']);

        $attr_ids = [];
        if (!empty($data['tag_data'])) {

            $tag_data = unserialize($data['tag_data']);
            foreach ($tag_data as $v) {

                $attr_ids_arr = [];
                if (is_array($v['attr_data'])) {

                    $attr_ids_arr = array_column($v['attr_data'], 'attr_id');
                }

                if (!empty($attr_ids_arr) && is_array($attr_ids_arr)) {

                    $attr_ids = array_merge($attr_ids, $attr_ids_arr);
                }
            }
        }

        // 获取根据题库和标签计算题库对应题目数量
        $bank_data = $this->topic_attr_service->get_bank_data($bank_ids, $attr_ids, $data['search_type']);
        // 非高级标签对应题目数
        $tag_total_list= $this->topic_attr_service->tag_count($bank_ids, $attr_ids, $data['search_type']);

        // 格式化返回数据
        $result = $this->paper_service->format_rule_data($data, $bank_data,$tag_total_list);

        $this->_result = $result;

        return true;
    }
}