PdfController.class.php 3.5 KB
<?php
/**
 * 【后台】导出单个学员成绩单
 * PdfController.class.php
 * @author:daijun
 * @date:2018-04-09
 */

namespace Apicp\Controller\Export;

use Common\Common\ExamHelper;
use Common\Common\ExportDownload;
use Common\Common\User;
use Common\Service\AnswerService;
use Common\Service\AnswerDetailService;
use Common\Service\PaperService;

class PdfController extends AbstractController
{

    /** @var  PaperService  实例化试卷对象 */
    protected $paper_service;
    /** @var  AnswerService  实例化答卷表对象 */
    protected $answer_service;
    /** @var  AnswerDetailService  实例化答卷详情表对象 */
    protected $answer_detail_service;


    public function before_action($action = '')
    {
        if (!parent::before_action($action)) {
            return false;
        }

        $this->paper_service = new PaperService();
        $this->answer_service = new AnswerService();
        $this->answer_detail_service = new AnswerDetailService();

        return true;
    }

    public function Index()
    {
        $params = I('post.');
        $ea_id = rintval($params['ea_id']);

        // 答卷ID不能为空
        if (empty($ea_id)) {

            E('_EMPTY_EA_ID');
        }

        // 获取答卷信息
        $answer = $this->answer_service->get($ea_id);
        // 答卷不存在
        if (empty($answer)) {

            E('_ERR_ANSWER_NOT_FOUND');
        }

        // 根据UID获取用户信息
        $user_service = &User::instance();

        // 获取答卷用户信息
        $user = $user_service->getByUid($answer['uid']);

        // 获取阅卷人信息
        if (empty($answer['marking_name'])) {

            $marking_user = $user_service->getByUid($answer['marking_uid']);
            $answer['marking_name'] = $marking_user['memUsername'];
        }

        // 获取答卷详情
        $answer_detail = $this->answer_detail_service->list_by_conds(['ea_id' => $answer['ea_id']]);

        // 获取试卷信息
        $paper = $this->paper_service->get($answer['ep_id']);

        // 格式化答卷详情
        $answer_list = $this->answer_detail_service->question_list_format(
            $answer_detail,
            $paper['marking_type'],
            AnswerService::ANSWER_PC_ANALYSIS
        );

        // 组装返回数据
        $data = array(
            'ep_name' => $paper['ep_name'],
            'my_score' => $answer['my_score'],
            'username' => $user['memUsername'],
            'marking_name' => $answer['marking_name'] ? $answer['marking_name'] : '系统阅卷',
            'answer_status' => intval($answer['answer_status']),
            'marking_time' => $answer['marking_time'],
            'marking_type' => intval($paper['marking_type']),
            'list' => $answer_list
        );

        // 成绩单名称
        $title = $paper['ep_name'] . '_' . $user['memUsername'] . '_成绩单';

        $params_down = [
            'title' => $title,
            'ea_id' => $this->_login->user['eaId'],
            'username' => $this->_login->user['eaRealname'],
            'type' => ExportDownload::PDF_TYPE
        ];

        // 新增开始生成状态
        $id = ExportDownload::set_down_load($params_down);
        // 生成PDF
        $url = ExamHelper::instance()->create_pdf($title, $data, $this->_login->user['eaId'] . microtime(true));

        // 更新状态
        $update_data = [
            'url' => $url,
            'id' => $id
        ];

        ExportDownload::update_down_load($update_data);

        return true;
    }


}