StartExamController.class.php 17.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
<?php
/**
 * 【考试中心-手机端】开始作答接口
 * StartExamController.class.php
 * @author: 蔡建华
 * @date: 2017-05-23
 */

namespace Api\Controller\Answer;

use Common\Common\TaskCenter;
use Common\Common\Train;
use Common\Service\AnswerDetailService;
use Common\Service\CategoryService;
use Common\Service\PaperService;
use Common\Service\RightService;
use Common\Service\AnswerService;
use VcySDK\Cron;
use VcySDK\Service;
use Common\Common\StudyMap;

class StartExamController extends AbstractController
{

    /** @var PaperService */
    protected $paper_serv;
    /** @var RightService */
    protected $right_serv;
    /** @var AnswerService */
    protected $answer_serv;
    /** @var AnswerDetailService */
    protected $answer_detail_serv;

    public function before_action($action = '')
    {

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

            return false;
        }

        // 实例化试卷service
        $this->paper_serv = new PaperService();
        // 实例化权限service
        $this->right_serv = new RightService();
        // 实例化答卷service
        $this->answer_serv = new AnswerService();
        // 实例化答卷详情Service
        $this->answer_detail_serv = new AnswerDetailService();

        return true;
    }

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

        $ep_id = intval($params['ep_id']);

        // 试卷id为空,返回提示"试卷ID不能为空"
        if (!$ep_id) {

            E('_EMPTY_EP_ID');
        }

        // 获取试卷信息
        $detail = $this->paper_serv->get($ep_id);
        // 答卷状态信息为空,返回false
        if (!$detail) {

            E('_ERR_DATA_EXAM_DEL_EXIST');
        }

        // 判断用户是否有权限访问“任务类”数据
        if (PaperService::TASK_TYPE == $detail['exam_type']) {

            $taskCenter = &TaskCenter::instance();
            $taskCenter->checkCustomtaskRight(intval($params['customtask_id']), $ep_id, $this->_login->user);
        }

        // 任务id不为空:任务类
        if (isset($params['customtask_id']) && $params['customtask_id']) {

            $data_type = PaperService::TASK_TYPE;
            $obj_id = intval($params['customtask_id']);
        } elseif (isset($params['plan_id']) && $params['plan_id']) { // 培训计划id不为空:线下培训
            // 判断用户是否有权限访问“线下培训”数据
            $train = &Train::instance();
            $train->checkCustomTrainRight(intval($params['plan_id']), $ep_id, $this->_login->user);

            $data_type = PaperService::TRAIN_TYPE;
            $obj_id = intval($params['plan_id']);
        } elseif (PaperService::OTHER_TYPE == $detail['exam_type']) { // 其他类型
            // 判断用户是否有权限访问“学习地图”数据
            $data_type = PaperService::OTHER_TYPE;
            $obj_id = intval($params['map_id']);

            // 地图学习权限检查
            $mapServ = new StudyMap($params['map_id']);
            $mapServ->checkRight($params['path_id'], $ep_id, $this->_login->user, $app = '');
            // 应用通知学习地图
            $mapServ->studyContent($params['path_id'], $ep_id, $this->_login->user, $app = '');

        } else { // 参数为空:常规考试

            $data_type = PaperService::NOMAL_TYPE;
            $obj_id = 0;
        }

        // 查询条件:试卷id,试卷数据状态(已发布和提前终止)、是否禁用(开启)
        $paper_conds = [
            'ep_id' => $ep_id,
            'exam_status>?' => PaperService::PAPER_DRAFT,
            'cate_status' => CategoryService::EC_OPEN_STATES
        ];
        // 根据试卷id查询开启的已发布和提前终止状态的试卷数据
        $data = $this->paper_serv->get_by_conds($paper_conds);
        // 试卷数据为空,返回提示"试卷被删除或禁用"
        if (empty($data)) {

            E('_ERR_DATA_EXAM_DEL_EXIST');
        }

        // 试卷状态
        $ep_status = $this->paper_serv->paper_status($data['exam_status'], $data['begin_time'], $data['end_time']);

        // 默认非补考
        $is_makeup = PaperService::IS_MAKEUP_FALSE;

        if (PaperService::EVALUATION_PAPER_TYPE == $data['paper_type'] && PaperService::NOMAL_TYPE == $data['exam_type']) {
            // 如果是测评试卷 && 是常规考试
            $makeup_conds = [
                'ep_id' => $ep_id,
                'uid' => $this->uid,
                'answer_status>?' => AnswerService::DEFAULT_MARK_STATUS, // 批阅状态为待批阅和已批阅的
            ];
            // 查询该用户是否参与过该考试
            $join_num = $this->answer_serv->count_by_conds($makeup_conds);

            if ($join_num > 0) {

                // 是补考
                $is_makeup = PaperService::IS_MAKEUP;

            } else {

                if (in_array($ep_status, [AnswerService::STATUS_END, AnswerService::STATUS_STOP])) {
                    // 如果未参与并且是已终止或者已结束,则是补考
                    $is_makeup = PaperService::IS_MAKEUP;
                }
            }

            if (PaperService::IS_MAKEUP == $is_makeup) {
                // 如果是补考

                if (PaperService::CLOSE_MAKEUP == $data['is_open_makeup'] && PaperService::IS_MAKEUP == $is_makeup) {
                    // 如果是补考,但是试卷本身未开启补考,则抛错
                    E('_ERR_NOT_ALLOW_JOIN');
                }

                $now_time = MILLI_TIME;

                if ($now_time <= $data['makeup_start_time'] || $now_time >= $data['makeup_end_time']) {
                    // 当前时间不在补考时间内
                    E('_ERR_IS_NOT_MAKEUP_TIME');
                }

                if (($join_num - 1) >= $data['makeup_num']) {
                    // 补考次数已用完
                    E('_ERR_MAKEUP_NUM');
                }

                $pass_conds = [
                    'ep_id' => $ep_id,
                    'uid' => $this->uid,
                    'my_is_pass' => 1, // 答题状态为已通过
                ];

                // 查询通过考试次数
                $answer_pass = $this->answer_serv->count_by_conds($pass_conds);

                if ($answer_pass > 0) {
                    // 已经通过的考试不能补考
                    E('_ERR_MAKEUP_IS_PASS');
                }
            }
        }

        // 常规考试 && 不是补考
        if (PaperService::NOMAL_TYPE == $data['exam_type'] && PaperService::IS_MAKEUP_FALSE == $is_makeup) {

            // 试卷状态
            switch ($ep_status) {
                // 未开始
                case PaperService::STATUS_NOT_START:
                    // 返回提示"考试尚未开始"
                    E('_ERR_EXAM_STATUS_NOT_START');
                    break;
                // 已结束
                case PaperService::STATUS_END:
                    // 返回提示"考试已结束"
                    E('_ERR_EXAM_STATUS_END');
                    break;
                // 已终止
                case PaperService::STATUS_STOP:
                    // 返回提示"考试已终止"
                    E('_ERR_EXAM_STATUS_STOP');
                    break;

                default:
            }
        }

        // 试卷权限:非全公司
        if (PaperService::NO_AUTH_ALL == $data['is_all']) {
            // 权限查询条件:试卷id,权限类型:试卷权限
            $right_conds = [
                'epc_id' => $ep_id,
                'er_type' => PaperService::RIGHT_PAPER
            ];
            // 根据试卷id查询该试卷权限
            $right = $this->right_serv->list_by_conds($right_conds);

            // 查询当前用户是否有考试权限
            $user_right = $this->right_serv->check_get_quit($right, $this->_login->user);
            // 用户没有权限
            if (!empty($right) && !$user_right) {
                // 返回提示"对不起,您没有考试权限"
                E('_ERR_EXAM_QUIT');
            }
        }

        // 答卷查询条件:试卷id,用户id(当前用户),批阅状态(初始化)
        $answer_conds = [
            'ep_id' => $ep_id,
            'uid' => $this->uid,
            'answer_status' => AnswerService::DEFAULT_MARK_STATUS, // 批阅状态常量
            'obj_id' => $obj_id
        ];
        // 根据试卷id查询当前用户批阅状态为初始化的答卷数据
        $res_answer = $this->answer_serv->get_by_conds($answer_conds);
        // 如果存在未交卷的答卷记录
        if (!empty($res_answer)) {

            if (PaperService::IS_MAKEUP == $is_makeup) {
                // 如果是补考
                $ea_id = $res_answer['ea_id'];
                // 补考答卷状态检查以及交卷
                $start_info = $this->answer_serv->answer_status_makeup($res_answer['ea_id'], $this->uid, $data);

                if ($start_info) {

                    $result = $this->get_question_list($ea_id, $data_type, $obj_id, $data, $is_makeup);

                    $result['start_type'] = intval($start_info['start_type']);
                    $result['paper_type'] = intval($data['paper_type']);

                    $this->_result = $result;

                    return true;
                }

            } else {

                // 答卷状态检查以及交卷
                $start_info = $this->answer_serv->answer_status($res_answer['ea_id'], $this->uid, $data_type, $obj_id);
                $ea_id = $res_answer['ea_id'];

                // 如果异常且是模拟试卷
                if (AnswerService::START_TYPE_END == $start_info['start_type']
                    && PaperService::SIMULATION_PAPER_TYPE == $data['paper_type']
                ) {
                    // 开始进行考试
                    $res = $this->answer_serv->paper_start_exam($data, $this->uid, $data_type, $obj_id);
                    if (!$res) {

                        return false;
                    }

                    $ea_id = $res['ea_id'];
                }

                // 继续答卷
                $result = $this->get_question_list($ea_id, $data_type, $obj_id, $data, $is_makeup);
                $result['start_type'] = intval($start_info['start_type']);
                $result['paper_type'] = intval($data['paper_type']);

                $this->_result = $result;

                return true;
            }
        }

        // 试卷类型为测评试卷并且不是补考
        if (PaperService::EVALUATION_PAPER_TYPE == $data['paper_type'] && $is_makeup == 0) {

            // 查询条件:试卷id,用户id
            $an_conds = [
                'ep_id' => $ep_id,
                'uid' => $this->uid,
                'obj_id' => $obj_id
            ];
            // 根据试卷id查询当前用户是否已经交过卷
            $info = $this->answer_serv->get_by_conds($an_conds);
            // 如果已经抽过题了
            if (!empty($info)) {
                // 答卷异常状态
                $start_type = AnswerService::START_TYPE_END;
                $result = $this->get_question_list($info['ea_id'], $data_type, $obj_id, $data, $is_makeup);
                $result['start_type'] = intval($start_type);
                $result['paper_type'] = intval($data['paper_type']);

                $this->_result = $result;

                return true;
            }
        }

        // 开始进行考试
        $res = $this->answer_serv->paper_start_exam($data, $this->uid, $data_type, $obj_id, $is_makeup);
        if (!$res) {

            return false;
        }

        // 返回结果
        $result = $this->get_question_list($res['ea_id'], $data_type, $obj_id, $data, $is_makeup);
        $result['start_type'] = AnswerService::START_TYPE_INITIAL;
        $result['paper_type'] = intval($data['paper_type']);

        // 判断是否是常规考试
        if ($detail['exam_type'] == PaperService::NOMAL_TYPE) {

            $data = [];
            // 判断系统自动交卷定时任务是否存在
            if (empty($detail['corn_exam'])) {
                $cron_exam = $this->created_cron_exam($ep_id, $detail);
                $data['corn_exam'] = $cron_exam;
            }

            // 判断考试统计定时任务是否存在
            if (empty($detail['cron_statistics'])) {
                $cron_statistics = $this->created_cron_statistics($ep_id);
                $data['cron_statistics'] = $cron_statistics;
            }

            // 判断考试排名计划任务是否存在
            if (empty($detail['cron_rank_id'])) {
                $cron_rank_id = $this->created_cron_rank_id($ep_id);
                $data['cron_rank_id'] = $cron_rank_id;
            }

            if (!empty($data)) {
                $this->paper_serv->update($ep_id, $data);
            }
        }

        $this->_result = $result;

        return true;
    }

    /**
     * @desc 创建自动交卷定时任务
     * @param $ep_id int 试卷ID
     * @param $paper array 试卷信息
     * @return string
     */
    private function created_cron_exam($ep_id, $paper)
    {
        $cron_serv = new Cron(Service::instance());
        // 创建计划任务
        $json_params = json_encode(array('ep_id' => $ep_id));

        if ($paper['end_time'] > $paper['makeup_end_time']) {
            // 考试结束10分钟后系统自动将未交卷的试卷交卷(白卷)
            $auto_submit_time = $paper['end_time'] + 600 * 1000;

        } else {

            // 补考结束10分钟后系统自动将未交卷的试卷交卷(白卷)
            $auto_submit_time = $paper['makeup_end_time'] + 600 * 1000;
        }

        // 考试结束系统交卷
        $conds_remind = array(
            'crRemark' => 'cron_exam',
            'crType' => 2,
            'crParams' => $json_params,
            'crMethod' => 'POST',
            'crReqUrl' => oaUrl('Frontend/Callback/AnswerBegin'), // 回调地址
            'crTimes' => 1,
            'crCron' => rgmdate((String)$auto_submit_time, 's i G j n ? Y'),
            'crMonitorExecution' => 0,
            'crDescription' => '考试时间到',
        );

        $res_remind = $cron_serv->add($conds_remind); // crId

        return $res_remind['crId'];
    }

    /**
     * @desc 创建试题统计定时任务
     * @param $ep_id int 试卷ID
     * @return string
     */
    private function created_cron_statistics($ep_id)
    {
        $cron_serv = new Cron(Service::instance());
        // 创建计划任务
        $json_params = json_encode(array('ep_id' => $ep_id));

        // 创建考试统计定时任务
        $paper_statistics = array(
            'crRemark' => 'cron_statistics',
            'crType' => 2,
            'crParams' => $json_params,
            'crMethod' => 'POST',
            'crReqUrl' => oaUrl('Frontend/Callback/Statistics'), // 回调地址
            'crTimes' => 0,
            'crCron' => '0 0/10 * * * ?',
            'crMonitorExecution' => 0,
            'crDescription' => '考试统计',
        );
        $res_cron_statistics = $cron_serv->add($paper_statistics); // crId
        return $res_cron_statistics['crId'];
    }

    /**
     * @desc 创建考试排名定时任务
     * @param $ep_id int 试卷ID
     * @return string
     */
    private function created_cron_rank_id($ep_id)
    {
        $cron_serv = new Cron(Service::instance());
        // 创建计划任务
        $json_params = json_encode(array('ep_id' => $ep_id));

        // 排名列表定时任务ID
        $cron_rank = array(
            'crRemark' => 'corn_rank_list_update',
            'crType' => 2,
            'crParams' => $json_params,
            'crMethod' => 'POST',
            'crReqUrl' => oaUrl('Frontend/Callback/RankList'), // 回调地址
            'crTimes' => 0,
            'crCron' => '0 0/5 * * * ?',
            'crMonitorExecution' => 0,
            'crDescription' => '更新考试成绩排名列表',
        );
        $res_rank_data = $cron_serv->add($cron_rank); // crId
        return $res_rank_data['crId'];
    }

    /*
     * 获取详情列表
     * @param int $ea_id 答卷ID
     * @param int $data_type 试卷类型(1:常规考试,2:任务类,3:线下培训)
     * @param int $obj_id 线下培训ID,任务类ID
     * @param array $paper_detail 试卷详情
     * @param int $is_makeup 是否是补考(0;否,1:是)
     * @return array|bool
     */
    private function get_question_list($ea_id = 0, $data_type = 1, $obj_id = 0, $paper_detail = [], $is_makeup = 0)
    {
        if ($is_makeup == 0) {
            // 根据答卷id获取当前用户的答卷状态信息
            $info = $this->answer_serv->answer_status($ea_id, $this->uid, $data_type, $obj_id);

        } else {

            // 根据答卷id获取当前用户的补考答卷状态信息
            $info = $this->answer_serv->answer_status_makeup($ea_id, $this->uid, $paper_detail);

        }
        // 答卷状态信息为空,返回false
        if (!$info) {

            return false;
        }

        // 查询条件:答卷id
        $conditions['ea_id'] = $ea_id;
        // 排序:序号正序
        $order_option['order_num'] = 'ASC';
        // 查询试卷题目列表
        $list = $this->answer_detail_serv->list_by_conds($conditions, null, $order_option);

        // 格式化题目列表
        $list_data = $this->answer_detail_serv->format_question_list($list, $paper_detail['is_upset_option']);

        return [
            'ea_id' => $ea_id,
            'ep_name' => $info['ep_name'],
            'my_begin_time' => $info['my_begin_time'],
            'left_time' => $info['left_time'],
            'paper_time' => $info['paper_time'],
            'end_time' => $info['end_time'],
            'total' => count($list_data),
            'list' => $list_data
        ];
    }
}