SubmitAllController.class.php 6.19 KB
<?php
/**
 * 【考试】考试手动交卷接口
 * SubmitController.class.php
 * @author: wanghuan
 * @date: 2017-08-10
 */

namespace Api\Controller\Answer;

use Common\Common\Train;
use Common\Service\AnswerService;
use Common\Service\PaperService;
use Common\Common\TaskCenter;

class SubmitAllController extends AbstractController
{

    public function Index_post()
    {
        // 接收post参数
        $params = I('post.');

        // 答卷id
        $ea_id = intval($params['ea_id']);
        $map_id = intval($params['map_id']);
        $path_id = intval($params['path_id']);
        // 答卷id为空,返回提示"答卷ID不能为空"
        if (!$ea_id) {

            E('_EMPTY_EA_ID');
        }

        // 答题列表
        $qa_list = $params['qa_list'];
        if (empty($qa_list)) {

            E('_EMPTY_ANSWER_LIST');
        }

        // 常规考试
        $data_type = PaperService::NOMAL_TYPE;
        $obj_id = 0;

        // 任务id
        $customtask_id = I('post.customtask_id', 0, 'intval');
        // 任务id不为空
        if (!empty($customtask_id)) {
            // 任务类
            $data_type = PaperService::TASK_TYPE;
            $obj_id = $customtask_id;
        }

        // 培训计划id
        $plan_id = I('post.plan_id', 0, 'intval');
        // 培训计划id不为空
        if (!empty($plan_id)) {
            // 线下培训
            $data_type = PaperService::TRAIN_TYPE;
            $obj_id = $plan_id;
        }

        if(empty($this->uid)){
            // 用户uid不存在
            E('_EMPTY_UID');
        }

        // 实例化答卷service
        $answer_s = new AnswerService();

        $answer_conds = [
            'ea_id' => $ea_id,
            'uid' => $this->uid
        ];

        $answer_info = $answer_s->get_by_conds($answer_conds);

        if(empty($answer_info)){
            // 回答数据不存在
            E('_ERR_ANSWER_NOT_FOUND');
        }

        // 交卷
        $params = [
            'ea_id' => $ea_id,
            'uid' => $this->uid,
            'qa_list' => $qa_list,
            'data_type' => $data_type,
            'obj_id' => $obj_id,
            'map_id' => $map_id ? $map_id : '',
            'path_id' => $path_id ? $path_id : '',
            'user_info' => $this->_login->user
        ];

        $result = $answer_s->submit_all($params, $award,$answer_info);

        // 交卷失败
        if (!$result) {

            return false;
        }

        if (AnswerService::IS_MAKEUP == $answer_info['is_makeup']) {

            // 如果是补考,返回结果
            $this->_result = [
                'list' => [],
                'score_list' => []
            ];

            return true;
        }

        // 格式化返回的积分策略数据
        $integral = [];
        if (!empty($result['score_list'])) {

            foreach ($result['score_list'] as $v) {
                // 执行成功
                if ($v['result'] == 'SUCCESS') {
                    $integral[] = [
                        'content' => '积分',
                        'number' => intval($v['score'])
                    ];
                }
            }
        }

        // 实例化试卷service
        $paper_s = new PaperService();
        // 试卷详情
        $ep_id = $answer_info['ep_id'];
        $paper_info = $paper_s->get($ep_id);

        // 常规任务埋点:考试通过(模拟试卷考试通过才算完成,测评试卷只要参加考试并交卷就算完成)
        if (PaperService::TASK_TYPE == $paper_info['exam_type'] && $customtask_id) {

            $params = [
                'uid' => $this->uid,
                'customtask_id' => $customtask_id,
                'app_data_id' => $ep_id,
                'action_key' => 'exam_pass',
            ];

            // 模拟试卷
            if (PaperService::SIMULATION_PAPER_TYPE == $paper_info['paper_type'] && AnswerService::PASS == $answer_info['my_is_pass']) {

                $params['description'] = '考试通过';
                $taskCenter = &TaskCenter::instance();
                $taskCenter->triggerCustomtask($params);
            }

            // 测评试卷
            if (PaperService::EVALUATION_PAPER_TYPE == $paper_info['paper_type']) {

                $params['description'] = '考试完成';
                $taskCenter = &TaskCenter::instance();
                $taskCenter->triggerCustomtask($params);
            }
        }

        // 更新成功,类型为线下培训,培训id不为空
        if ($plan_id && PaperService::TRAIN_TYPE == $paper_info['exam_type']) {
            // 同步线下培训考试完成状态
            $Train = &Train::instance();
            $Train->syncTrainStatus($plan_id, $ep_id);
        }

        // 试卷使用类型(0:测评试卷,1:模拟试卷)
        $paper_type = $paper_info['paper_type'];
        // 是否更新参与人数(0=不更新,1=更新)
        $is_update_join = 0;
        // 测评试卷
        if (PaperService::EVALUATION_PAPER_TYPE == $paper_type) {
            // 更新
            $is_update_join = 1;
        } elseif (PaperService::SIMULATION_PAPER_TYPE == $paper_type) { // 模拟试卷
            // 交卷次数
            $condition = [
                'ep_id' => $ep_id,
                'uid' => $this->uid,
                'answer_status' => PaperService::READ_OVER, // 已批阅
                'data_type' => PaperService::NOMAL_TYPE, // 常规考试
            ];
            $answer_count = $answer_s->count_by_conds($condition);

            // 模拟试卷第一次交卷
            if (1 == $answer_count) {
                // 更新
                $is_update_join = 1;
            }
        }

        // 交卷成功,更新参与人数
        if ($is_update_join) {
            // 已参与人数加1,未参与人数减1
            $update_data = [
                'join_count' => $paper_info['join_count'] + 1,
                'unjoin_count' => $paper_info['unjoin_count'] - 1
            ];
            $paper_s->update($ep_id, $update_data);
        }

        // 返回结果
        $this->_result = [
            'list' => isset($award) ? $award : [],
            'score_list' => isset($integral) ? $integral : []
        ];

        return true;
    }
}