AnswerBeginController.class.php
5.47 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
<?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');
}
}