StopController.class.php
3.71 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
<?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;
}
}