<?php /** * 【考试中心-后台】 提前终止试卷 * StopController.class.php * User: daijun * Date: 2017-05-23 */ namespace Apicp\Controller\Paper; use Common\Common\Cache; use Common\Service\AnswerService; use Common\Service\PaperService; use Common\Service\RightService; class StopController extends AbstractController { /** @var PaperService 试卷信息表 */ protected $paper_service; /** @var AnswerService 答卷记录表 */ protected $answer_service; /** @var RightService 权限表 */ protected $right_service; public function before_action($action = '') { if (!parent::before_action($action)) { return false; } // 实例化试卷信息表 $this->paper_service = new PaperService(); // 实例化权限表 $this->right_service = new RightService(); // 实例化试卷信息表 $this->answer_service = new AnswerService(); return true; } public function Index_post() { $ep_id = I('post.ep_id', 0, 'intval'); $reason = I('post.reason'); // 验证参数 if (empty($ep_id)) { E('_EMPTY_PAPER_ID'); } // 获取试卷信息 $data = $this->paper_service->get($ep_id); // 判断试卷是否存在 if (empty($data)) { E('_EMPTY_PAPER_DATA'); } // 获取试卷当前状态 $paper_status = $this->paper_service->paper_status($data['exam_status'], $data['begin_time'], $data['end_time']); // 如果不是进行中的试卷 if ($paper_status != PaperService::STATUS_ING) { E('_ERR_STOP_ACTION'); } // 组装更新数据 $update_data = [ 'exam_status' => PaperService::PAPER_STOP, 'reason' => strval($reason), 'last_time' => MILLI_TIME, 'reason_time' => MILLI_TIME ]; if (!empty($data['begin_corn'])) { $update_data['begin_corn'] = ''; } elseif (!empty($data['end_cron'])) { $update_data['end_cron'] = ''; } elseif (!empty($data['corn_exam'])) { $update_data['corn_exam'] = ''; } elseif (!empty($data['corn_create_exam'])) { $update_data['corn_create_exam'] = ''; } elseif (!empty($data['cron_statistics'])) { $update_data['cron_statistics'] = ''; } elseif (!empty($data['cron_send_msg'])) { $update_data['cron_send_msg'] = ''; } elseif (!empty($data['cron_rank_id'])) { $update_data['cron_rank_id'] = ''; } // 考试结束时间 $update_data['end_time'] = MILLI_TIME; // 执行更新 $this->paper_service->update($ep_id, $update_data); $data['reason'] = $reason; // 查询开始答卷但是没有交卷的记录 $answer_list = $this->answer_service->list_by_conds(['ep_id' => $ep_id, 'my_time' => 0, 'answer_status<?' => PaperService::READ_WAITING]); foreach ($answer_list as $v) { // 执行交卷 if (!$this->answer_service->stop_submit_papers($v['ea_id'], $v['uid'],$data)) { continue; } } // 临时避免定时任务查询或删除失败抛出错误 try { // 推送消息和删除定时任务 $this->paper_service->stop_paper($data); } catch (\Think\Exception $e) { \Think\Log::record($e); } // 缓存 $cache = Cache::instance(); $data['exam_status'] = PaperService::PAPER_STOP; // 如果没有缓存新增缓存 $cache->set('Common.Exam_wx_' . $ep_id, $data, 7200); return true; } }