AnswerBeginController.class.php 5.47 KB
<?php
/**
 * 考试定时任务,考试结束时间到系统自动交卷(预留10min缓冲)
 * User: heyuelong
 * Date: 2017年06月23日16:37:37
 */

namespace Frontend\Controller\Callback;

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

class AnswerBeginController extends AbstractController
{

    /** @var PaperService 试卷信息表 */
    protected $paper_s;
    /** @var AnswerService 回答信息表 */
    protected $answer_s;
    /** @var AnswerDetailService 答卷详情 */
    protected $answer_detail;

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

        // 实例化试卷表
        $this->paper_s = new PaperService();
        // 实例化回答表
        $this->answer_s = new AnswerService();
        // 答卷详情
        $this->answer_detail = new AnswerDetailService();

        return true;
    }

    public function Index()
    {
        set_time_limit(0);

        // 获取需要发送的试卷id
        $back = $this->callBackData;

        // 兼容手动执行
        $ep_id = $back['ep_id'] ? $back['ep_id'] : I('get.ep_id');

        // 非空判断
        if (empty($ep_id)) {
            return true;
        }

        // 获取基本信息
        $info = $this->paper_s->get_by_conds(
            array(
                'ep_id' => $ep_id,
                'corn_exam != ?' => ''
            ));

        // 如果未开始或者是草稿或者已经结束
        if ($info['begin_time'] > MILLI_TIME ||
            $info['exam_status'] != PaperService::PAPER_PUBLISH ||
            empty($info)
        ) {

            return true;
        }

        // 查询开始答卷但是没有交卷的记录
        $answer_conds = array('ep_id' => $ep_id, 'my_time' => 0, 'answer_status' => 0);
        $answer_list = $this->answer_s->list_by_conds($answer_conds);

        // 试卷使用类型(0:测评试卷,1:模拟试卷)
        $paper_type = $info['paper_type'];

        // 交卷数
        $inc_count = 0;
        foreach ($answer_list as $v) {

            // 时间到自动交卷(该用户超过规定考试时长,考试已经结束)
            if (($v['my_begin_time'] + to_milli_time($info['paper_time'] * 60) <= MILLI_TIME) ||
                ($info['end_time'] <= MILLI_TIME)
            ) {

                // 查询条件:答卷id
                $conds = array(
                    'ea_id' => $v['ea_id']
                );

                // 全部题目数量
                $my_error_num = $this->answer_detail->count_by_conds($conds);
                // 试卷时长
                $paper_time = to_milli_time($info['paper_time'] * 60);
                // 结束时间
                $end_time = $v['my_begin_time'] + $paper_time;

                // 交卷分数
                $data = array(
                    'answer_status' => PaperService::READ_OVER,
                    'my_is_pass' => AnswerService::UNPASS,
                    'my_score' => 0,
                    'my_error_num' => $my_error_num,
                    'my_time' => $end_time - $v['my_begin_time'],
                    'my_end_time' => $end_time,
                    'data_type' => PaperService::NOMAL_TYPE, // 数据类型
                    'obj_id' => ''        // 任务、培训ID
                );

                // 交卷
                $update_result = $this->answer_s->update_by_conds($conds, $data);

                // 交卷成功
                if ($update_result) {
                    // 测评试卷
                    if (PaperService::EVALUATION_PAPER_TYPE == $paper_type) {
                        // 交卷数累加1
                        $inc_count ++;
                    } elseif (PaperService::SIMULATION_PAPER_TYPE == $paper_type) { // 模拟试卷
                        // 交卷次数
                        $condition = array(
                            'ep_id' => $ep_id,
                            'uid' => $v['uid'],
                            'answer_status' => PaperService::READ_OVER, // 已批阅
                            'data_type' => PaperService::NOMAL_TYPE, // 常规考试
                        );
                        $answer_count = $this->answer_s->count_by_conds($condition);

                        // 模拟试卷第一次交卷
                        if (1 == $answer_count) {
                            // 交卷数累加1
                            $inc_count ++;
                        }
                    }
                }
            }
        }

        // 交卷数不为0
        if ($inc_count) {
            // 更新参与人数:已参与人数加交卷数,未参与人数减交卷数
            $update_data = array(
                'join_count' => $info['join_count'] + $inc_count,
                'unjoin_count' => $info['unjoin_count'] - $inc_count
            );
            $this->paper_s->update($ep_id, $update_data);
        }

        // 获取试卷试卷详情
        $answer_info = $this->answer_s->count_by_conds(
            array(
                'ep_id' => $ep_id,
                'my_time' => 0
            ));

        // 如果全部交卷
        if (empty($answer_info) && MILLI_TIME >= $info['end_time']) {

            // 一次执行的定时任务已经执行,所以删除paper表中储存的cronId
            $this->paper_s->update($ep_id,
                array(
                    'corn_exam' => ''
                ));

        }

        exit('SUCCESS');
    }
}