SubmitController.class.php
7.76 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
<?php
/**
* Created by PhpStorm.
* User: yingcai
* Date: 2018/4/8
* Time: 上午10:39
*/
namespace Apicp\Controller\Check;
use Common\Service\AnswerDetailService;
use Common\Service\AnswerService;
use Common\Service\PaperService;
use Common\Common\TaskCenter;
class SubmitController extends AbstractController
{
public function Index_post()
{
$params = I('post.');
$ea_id = intval($params['ea_id']);
$submit_type = intval($params['submit_type']);
if (!$ea_id) {
// 答卷ID不能为空
E('_EMPTY_EA_ID');
}
if (!$submit_type) {
// 阅卷提交类型不能为空
E('_EMPTY_CHECK_SUBMIT_TYPE');
}
if (!in_array($submit_type, [AnswerService::CHECK_TYPE_SUBMIT, AnswerService::CHECK_TYPE_SUBMIT_NEXT])) {
// 无效的阅卷提交方式
E('_ERR_CHECK_SUBMIT_TYPE_INVALID');
}
if (empty($params['list'])) {
// 批阅试题列表不能为空
E('_EMPTY_CHECK_QUESTION_LIST');
}
// 实例化答卷表
$answer_serv = new AnswerService();
// 答卷信息
$answer = $answer_serv->get($ea_id);
if (empty($answer)) {
E("_EMPTY_MARKING_INFO");
}
// 判断阅卷状态是否是待批阅
if ($answer['answer_status'] != AnswerService::READ_WAITING) {
E("_ERR_MARKING_PAPER");
}
// 实例化答卷详情service
$answer_detail_serv = new AnswerDetailService();
$ead_ids = array_column($params['list'], 'ead_id');
$answer_detail_list = $answer_detail_serv->list_by_pks($ead_ids);
$answer_detail_list = array_combine_by_key($answer_detail_list, 'ead_id');
// 实例化试卷service
$paper_s = new PaperService();
// 试卷详情
$ep_id = $answer['ep_id'];
$paper_info = $paper_s->get($ep_id);
try {
$answer_detail_serv->start_trans();
foreach ($params['list'] as $val) {
if (!intval($val['ead_id'])) {
// 判断ead_id是否存在
E('_EMPTY_MARKING_EAD_ID');
}
$my_score = $val['my_score'];
// 获取试题信息
$data = $answer_detail_list[$val['ead_id']];
// 判断分数不合法
$score = $data['score'];
if ($my_score < 0 || $my_score > $score) {
E("_ERR_MARKING_SCORE_MIN");
}
// 更新答卷试题状态和分数
$answer_detail_serv->update_by_conds(
['ead_id' => $val['ead_id']],
[
'my_score' => $my_score,
'is_pass' => $my_score == $score ? AnswerDetailService::MY_PASS : AnswerDetailService::NO_MY_PASS, // 得满分算正确,否则错误
'marking_status' => 1
]
);
}
// 计算答题总分
$my_answer_detail_list = $answer_detail_serv->list_by_conds(['ea_id' => $ea_id]);
$my_score = array_sum(array_column($my_answer_detail_list, 'my_score'));
// 答对的试题数据
$pass_answers = array_filter($my_answer_detail_list, function ($v) {
return $v['is_pass'] == AnswerService::MY_PASS;
});
// 做对题数
$my_success_num = count($pass_answers);
// 答错的题数(总答题数-作对的题数)
$my_error_num = count($my_answer_detail_list) - $my_success_num;
// 获取最高分的答卷
$max_score_answer = $answer_serv->get_by_conds(
[
'ep_id' => $answer['ep_id'],
'uid' => $answer['uid'],
],
['my_score' => 'DESC']
);
$max_score = $max_score_answer['my_score'];
// 是否是最高分
$is_score_top = $my_score > $max_score ? AnswerService::IS_SCORE_TOP_TRUE : AnswerService::IS_SCORE_TOP_FALSE;
if ($is_score_top) {
// 更新最高分状态
$answer_serv->update(
$max_score_answer['ea_id'],
['is_score_top' => AnswerService::IS_SCORE_TOP_FALSE]
);
}
// 更新答卷表的 阅卷管理员 和 总分
$answer_serv->update(
$ea_id,
[
'marking_uid' => $this->_login->user['eaId'],
'marking_name' => $this->_login->user['eaRealname'],
'my_score' => $my_score,
'marking_user_type' => 1, // 阅卷人是否是管理员(0:否,1:是)
'is_score_top' => $is_score_top,
'my_is_pass' => $my_score >= $paper_info['pass_score'] ? AnswerService::PASS : AnswerService::UNPASS, // 是否通过
'my_error_num' => $my_error_num, // 做错的题目数
'answer_status' => AnswerService::READ_OVER,
'marking_time' => MILLI_TIME,
]
);
$answer_detail_serv->commit();
} catch (\Think\Exception $e) {
$answer_detail_serv->rollback();
E('_ERR_SUBMIT_CHECK_FAIL');
} catch (\Exception $e) {
$answer_detail_serv->rollback();
E('_ERR_SUBMIT_CHECK_FAIL');
}
// 获取下一份待批阅的答卷ID
if (AnswerService::CHECK_TYPE_SUBMIT_NEXT == $submit_type) {
$next_ea_id = $this->get_next_ea_id($answer['ep_id'], $ea_id);
$this->_result = ['next_ea_id' => $next_ea_id];
}
// 实例化试卷service
$paper_serv = new PaperService();
// 试卷详情
$paper_info = $paper_serv->get($answer['ep_id']);
// 常规任务埋点:考试通过
if (PaperService::TASK_TYPE == $paper_info['exam_type'] && AnswerService::PASS == $answer['my_is_pass']) {
$params = [
'uid' => $answer['uid'],
'customtask_id' => $answer['obj_id'],
'app_data_id' => $answer['ep_id'],
'action_key' => 'exam_pass',
'description' => '考试通过'
];
$taskCenter = &TaskCenter::instance();
$taskCenter->triggerCustomtask($params);
}
// 阅卷交卷成功,发送考试消息(阅卷结果出来发送消息)
$answer_serv->send_cms_answer($ea_id, AnswerService::MARKING_RESULT_MSG);
return true;
}
/**
* 获取下一份待批阅的答卷
*
* @param int $ep_id 试卷ID
* @param int $ea_id 答卷ID
*
* @return int
*/
private function get_next_ea_id($ep_id, $ea_id)
{
// 实例化答卷表
$answer_serv = new AnswerService();
$answer_list = $answer_serv->list_by_conds(
[
'ep_id' => $ep_id,
'answer_status' => AnswerService::READ_WAITING,
],
[],
[],
'ea_id'
);
$ea_ids = array_column($answer_list, 'ea_id');
// 待批阅总数
$total = count($ea_ids);
// 如果待批阅答卷总数大于2,随机获取2个next_ea_id
if ($total >= 2) {
$ea_id_key = array_rand($ea_ids, 2);
$next_ea_id = $ea_ids[$ea_id_key[0]];
// 如果获取的next_ea_id 等于当前批阅答卷的ea_id
if ($next_ea_id == $ea_id) {
$next_ea_id = $ea_ids[$ea_id_key[1]];
}
} else {
$next_ea_id = $total ? $ea_ids[0] : 0;
}
return $next_ea_id;
}
}