QuestionListController.class.php 2.05 KB
<?php
/**
 * 【考试中心-后台】 获取试卷试题信息(详情用)
 * QuestionListController.class.php
 * User: daijun
 * Date: 2017-05-23
 */

namespace Apicp\Controller\Paper;

use Common\Service\PaperService;
use Common\Service\SnapshotService;

class QuestionListController extends AbstractController
{
    /** @var  PaperService 试卷信息表 */
    protected $paper_service;
    /** @var SnapshotService 试卷题目快照信息表 */
    protected $snapshot_service;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {
            return false;
        }
        // 实例化试卷信息表
        $this->paper_service = new PaperService();

        // 实例化试卷题目快照信息表
        $this->snapshot_service = new SnapshotService();

        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);

        // 查询试题总数
        $total = $this->snapshot_service->count_by_conds(['ep_id' => $ep_id]);

        // 默认值
        $page = !empty($params['page']) ? intval($params['page']) : 1;
        $limit = !empty($params['limit']) ? intval($params['limit']) : $total;

        // 分页
        list($start, $limit) = page_limit($page, $limit);
        // 排序
        $order_option = ['order_num' => 'ASC'];
        // 获取试题列表
        $list = $this->snapshot_service->list_by_conds(['ep_id' => $ep_id], [$start, $limit], $order_option);

        // 组装返回数据
        $this->_result = [
            'total' => intval($total),
            'limit' => intval($limit),
            'page' => intval($page),
            'ep_name' => $data['ep_name'],
            'intro' => $data['intro'],
            'ep_type' => intval($data['ep_type']),
            'list' => $this->snapshot_service->format_admin_list($list)
        ];

        return true;
    }

}